-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(aria/grid): create the aria grid #32092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ok7sai
wants to merge
4
commits into
angular:main
Choose a base branch
from
ok7sai:ng-aria-grid
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,139
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
load("//tools:defaults.bzl", "ng_project", "ng_web_test_suite") | ||
|
||
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", | ||
], | ||
) | ||
|
||
ng_project( | ||
name = "unit_test_sources", | ||
testonly = True, | ||
srcs = [ | ||
"grid.spec.ts", | ||
], | ||
deps = [ | ||
":tabs", | ||
"//:node_modules/@angular/core", | ||
"//:node_modules/@angular/platform-browser", | ||
"//src/cdk/testing/private", | ||
], | ||
) | ||
|
||
ng_web_test_suite( | ||
name = "unit_tests", | ||
deps = [":unit_test_sources"], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
/** | ||
* @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, | ||
Signal, | ||
untracked, | ||
} 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)': 'onFocusIn()', | ||
'(focusout)': 'onFocusOut()', | ||
}, | ||
}) | ||
export class Grid { | ||
/** 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), | ||
); | ||
|
||
/** 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 tree. */ | ||
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), | ||
}); | ||
|
||
/** Whether the focus is in the grid. */ | ||
private readonly _isFocused = signal(false); | ||
|
||
constructor() { | ||
afterRenderEffect(() => { | ||
this.pattern.resetState(); | ||
}); | ||
|
||
afterRenderEffect(() => { | ||
const activeCell = this.pattern.activeCell(); | ||
const hasFocus = untracked(() => this._isFocused()); | ||
const isRoving = this.focusMode() === 'roving'; | ||
if (activeCell !== undefined && isRoving && hasFocus) { | ||
activeCell.element().focus(); | ||
} | ||
}); | ||
} | ||
|
||
/** Handles focusin events on the grid. */ | ||
onFocusIn() { | ||
this._isFocused.set(true); | ||
} | ||
|
||
/** Handles focusout events on the grid. */ | ||
onFocusOut() { | ||
this._isFocused.set(false); | ||
} | ||
|
||
/** 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>(); | ||
ok7sai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** 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.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 _widget = contentChild(GridCellWidget); | ||
|
||
/** The UI pattern for the widget in this cell. */ | ||
private readonly _widgetPattern: Signal<GridCellWidgetPattern | undefined> = computed( | ||
() => this._widget()?.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); | ||
ok7sai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** 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>(); | ||
wagnermaciel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** 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 grid navigation should be paused, usually because this widget has focus. */ | ||
readonly pauseGridNavigation = model<boolean>(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like something that shouldn't be exposed for developers to control |
||
|
||
/** The UI pattern for the grid cell widget. */ | ||
readonly pattern = new GridCellWidgetPattern({ | ||
...this, | ||
cell: () => this._cell.pattern, | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
load("//tools:defaults.bzl", "ng_project", "ng_web_test_suite", "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", | ||
], | ||
) | ||
|
||
ng_project( | ||
name = "unit_test_sources", | ||
testonly = True, | ||
srcs = glob(["**/*.spec.ts"]), | ||
deps = [ | ||
":grid", | ||
"//:node_modules/@angular/core", | ||
"//src/cdk/keycodes", | ||
"//src/cdk/testing/private", | ||
], | ||
) | ||
|
||
ng_web_test_suite( | ||
name = "unit_tests", | ||
deps = [":unit_test_sources"], | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.