Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ng-dev/commit-message.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const commitMessage: CommitMessageConfig = {
'multiple', // For when a commit applies to multiple components.
'aria/accordion',
'aria/combobox',
'aria/grid',
'aria/listbox',
'aria/menu',
'aria/radio-group',
Expand Down
1 change: 1 addition & 0 deletions src/aria/config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ARIA_ENTRYPOINTS = [
"accordion",
"combobox",
"deferred-content",
"grid",
"listbox",
"menu",
"radio-group",
Expand Down
18 changes: 18 additions & 0 deletions src/aria/grid/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("//tools:defaults.bzl", "ng_project")

package(default_visibility = ["//visibility:public"])

ng_project(
name = "grid",
srcs = [
"grid.ts",
"index.ts",
],
deps = [
"//:node_modules/@angular/core",
"//src/aria/deferred-content",
"//src/aria/ui-patterns",
"//src/cdk/a11y",
"//src/cdk/bidi",
],
)
259 changes: 259 additions & 0 deletions src/aria/grid/grid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import {_IdGenerator} from '@angular/cdk/a11y';
import {
afterRenderEffect,
booleanAttribute,
computed,
contentChild,
contentChildren,
Directive,
ElementRef,
inject,
input,
model,
Signal,
} from '@angular/core';
import {GridPattern, GridRowPattern, GridCellPattern, GridCellWidgetPattern} from '../ui-patterns';

/** A directive that provides grid-based navigation and selection behavior. */
@Directive({
selector: '[ngGrid]',
exportAs: 'ngGrid',
host: {
'class': 'grid',
'role': 'grid',
'[tabindex]': 'pattern.tabIndex()',
'[attr.aria-disabled]': 'pattern.disabled()',
'[attr.aria-activedescendant]': 'pattern.activeDescendant()',
'(keydown)': 'pattern.onKeydown($event)',
'(pointerdown)': 'pattern.onPointerdown($event)',
'(pointermove)': 'pattern.onPointermove($event)',
'(pointerup)': 'pattern.onPointerup($event)',
'(focusin)': 'pattern.onFocusIn($event)',
'(focusout)': 'pattern.onFocusOut($event)',
},
})
export class Grid {
/** A reference to the host element. */
private readonly _elementRef = inject(ElementRef);

/** The rows that make up the grid. */
private readonly _rows = contentChildren(GridRow);

/** The UI patterns for the rows in the grid. */
private readonly _rowPatterns: Signal<GridRowPattern[]> = computed(() =>
this._rows().map(r => r.pattern),
);

/** The host native element. */
readonly element = computed(() => this._elementRef.nativeElement);

/** Whether selection is enabled for the grid. */
readonly enableSelection = input(false, {transform: booleanAttribute});

/** Whether the grid is disabled. */
readonly disabled = input(false, {transform: booleanAttribute});

/** Whether to skip disabled items during navigation. */
readonly skipDisabled = input(true, {transform: booleanAttribute});

/** The focus strategy used by the grid. */
readonly focusMode = input<'roving' | 'activedescendant'>('roving');

/** The wrapping behavior for keyboard navigation along the row axis. */
readonly rowWrap = input<'continuous' | 'loop' | 'nowrap'>('loop');

/** The wrapping behavior for keyboard navigation along the column axis. */
readonly colWrap = input<'continuous' | 'loop' | 'nowrap'>('loop');

/** The UI pattern for the grid. */
readonly pattern = new GridPattern({
...this,
rows: this._rowPatterns,
getCell: e => this._getCell(e),
});

constructor() {
afterRenderEffect(() => this.pattern.resetStateEffect());
afterRenderEffect(() => this.pattern.focusEffect());
}

/** Gets the cell pattern for a given element. */
private _getCell(element: Element): GridCellPattern | undefined {
const cellElement = element.closest('[ngGridCell]');
if (cellElement === undefined) return;

const widgetElement = element.closest('[ngGridCellWidget]');
for (const row of this._rowPatterns()) {
for (const cell of row.inputs.cells()) {
if (
cell.element() === cellElement ||
(widgetElement !== undefined && cell.element() === widgetElement)
) {
return cell;
}
}
}
return;
}
}

/** A directive that represents a row in a grid. */
@Directive({
selector: '[ngGridRow]',
exportAs: 'ngGridRow',
host: {
'class': 'grid-row',
'[attr.role]': 'role()',
},
})
export class GridRow {
/** A reference to the host element. */
private readonly _elementRef = inject(ElementRef);

/** The cells that make up this row. */
private readonly _cells = contentChildren(GridCell);

/** The UI patterns for the cells in this row. */
private readonly _cellPatterns: Signal<GridCellPattern[]> = computed(() =>
this._cells().map(c => c.pattern),
);

/** The parent grid. */
private readonly _grid = inject(Grid);

/** The parent grid UI pattern. */
readonly grid = computed(() => this._grid.pattern);

/** The host native element. */
readonly element = computed(() => this._elementRef.nativeElement);

/** The ARIA role for the row. */
readonly role = input<'row' | 'rowheader'>('row');

/** The index of this row within the grid. */
readonly rowIndex = input<number>();

/** The UI pattern for the grid row. */
readonly pattern = new GridRowPattern({
...this,
cells: this._cellPatterns,
});
}

/** A directive that represents a cell in a grid. */
@Directive({
selector: '[ngGridCell]',
exportAs: 'ngGridCell',
host: {
'class': 'grid-cell',
'[attr.role]': 'role()',
'[attr.id]': 'pattern.id()',
'[attr.rowspan]': 'pattern.rowSpan()',
'[attr.colspan]': 'pattern.colSpan()',
'[attr.data-active]': 'pattern.active()',
'[attr.aria-disabled]': 'pattern.disabled()',
'[attr.aria-rowspan]': 'pattern.rowSpan()',
'[attr.aria-colspan]': 'pattern.colSpan()',
'[attr.aria-rowindex]': 'pattern.ariaRowIndex()',
'[attr.aria-colindex]': 'pattern.ariaColIndex()',
'[attr.aria-selected]': 'pattern.ariaSelected()',
'[tabindex]': 'pattern.tabIndex()',
},
})
export class GridCell {
/** A reference to the host element. */
private readonly _elementRef = inject(ElementRef);

/** The widget contained within this cell, if any. */
private readonly _widgets = contentChild(GridCellWidget);

/** The UI pattern for the widget in this cell. */
private readonly _widgetPattern: Signal<GridCellWidgetPattern | undefined> = computed(
() => this._widgets()?.pattern,
);

/** The parent row. */
private readonly _row = inject(GridRow);

/** A unique identifier for the cell. */
private readonly _id = inject(_IdGenerator).getId('ng-grid-cell-');

/** The host native element. */
readonly element = computed(() => this._elementRef.nativeElement);

/** The ARIA role for the cell. */
readonly role = input<'gridcell' | 'columnheader'>('gridcell');

/** The number of rows the cell should span. */
readonly rowSpan = input<number>(1);

/** The number of columns the cell should span. */
readonly colSpan = input<number>(1);

/** The index of this cell's row within the grid. */
readonly rowIndex = input<number>();

/** The index of this cell's column within the grid. */
readonly colIndex = input<number>();

/** Whether the cell is disabled. */
readonly disabled = input(false, {transform: booleanAttribute});

/** Whether the cell is selected. */
readonly selected = model<boolean>(false);

/** Whether the cell is selectable. */
readonly selectable = input<boolean>(true);

/** The UI pattern for the grid cell. */
readonly pattern = new GridCellPattern({
...this,
id: () => this._id,
grid: this._row.grid,
row: () => this._row.pattern,
widget: this._widgetPattern,
});
}

/** A directive that represents a widget inside a grid cell. */
@Directive({
selector: '[ngGridCellWidget]',
exportAs: 'ngGridCellWidget',
host: {
'class': 'grid-cell-widget',
'[attr.data-active]': 'pattern.active()',
'[tabindex]': 'pattern.tabIndex()',
},
})
export class GridCellWidget {
/** A reference to the host element. */
private readonly _elementRef = inject(ElementRef);

/** The parent cell. */
private readonly _cell = inject(GridCell);

/** The host native element. */
readonly element = computed(() => this._elementRef.nativeElement);

/** Whether the widget is activated and the grid navigation should be paused. */
readonly activate = model<boolean>(false);

/** The UI pattern for the grid cell widget. */
readonly pattern = new GridCellWidgetPattern({
...this,
cell: () => this._cell.pattern,
});

/** Focuses the widget. */
focus(): void {
this.element().focus();
}
}
9 changes: 9 additions & 0 deletions src/aria/grid/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

export * from './grid';
1 change: 1 addition & 0 deletions src/aria/ui-patterns/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ts_project(
"//src/aria/ui-patterns/accordion",
"//src/aria/ui-patterns/behaviors/signal-like",
"//src/aria/ui-patterns/combobox",
"//src/aria/ui-patterns/grid",
"//src/aria/ui-patterns/listbox",
"//src/aria/ui-patterns/menu",
"//src/aria/ui-patterns/radio-group",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class PointerEventManager<T extends PointerEvent> extends EventManager<T>
};
}

if (typeof args[0] === 'number' && typeof args[1] === 'function') {
if (args.length === 2) {
return {
button: MouseButton.Main,
modifiers: args[0] as ModifierInputs,
Expand Down
15 changes: 15 additions & 0 deletions src/aria/ui-patterns/behaviors/grid/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("//tools:defaults.bzl", "ts_project")

package(default_visibility = ["//visibility:public"])

ts_project(
name = "grid",
srcs = glob(
["**/*.ts"],
exclude = ["**/*.spec.ts"],
),
deps = [
"//:node_modules/@angular/core",
"//src/aria/ui-patterns/behaviors/signal-like",
],
)
Loading
Loading