Skip to content

Commit ae87256

Browse files
committed
fixup! feat(aria/grid): create the aria grid
1 parent 5b31ca9 commit ae87256

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

src/aria/ui-patterns/behaviors/grid/grid-navigation.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type ExactlyOneKey<T, K extends keyof T = keyof T> = {
1919
/** Represents a directional change in the grid, either by row or by column. */
2020
type Delta = ExactlyOneKey<{row: -1 | 1; col: -1 | 1}>;
2121

22-
/** */
23-
export const Direction: Record<'Up' | 'Down' | 'Left' | 'Right', Delta> = {
22+
/** Constants for the four cardinal directions. */
23+
export const direction: Record<'Up' | 'Down' | 'Left' | 'Right', Delta> = {
2424
Up: {row: -1},
2525
Down: {row: 1},
2626
Left: {col: -1},
@@ -67,13 +67,17 @@ export class GridNavigation<T extends GridNavigationCell> {
6767
return this.inputs.gridFocus.focusCoordinates(coords);
6868
}
6969

70-
/** */
70+
/**
71+
* Gets the coordinates of the next focusable cell in a given direction, without changing focus.
72+
*/
7173
peek(direction: Delta, fromCoords: RowCol): RowCol | undefined {
7274
const wrap = direction.row !== undefined ? this.inputs.rowWrap() : this.inputs.colWrap();
7375
return this._peekDirectional(direction, fromCoords, wrap);
7476
}
7577

76-
/** */
78+
/**
79+
* Navigates to the next focusable cell in a given direction.
80+
*/
7781
advance(direction: Delta): boolean {
7882
const nextCoords = this.peek(direction, this.inputs.gridFocus.activeCoords());
7983
return !!nextCoords && this.gotoCoords(nextCoords);
@@ -89,8 +93,8 @@ export class GridNavigation<T extends GridNavigationCell> {
8993
col: -1,
9094
};
9195
return row === undefined
92-
? this._peekDirectional(Direction.Right, fromCoords, 'continuous')
93-
: this._peekDirectional(Direction.Right, fromCoords, 'nowrap');
96+
? this._peekDirectional(direction.Right, fromCoords, 'continuous')
97+
: this._peekDirectional(direction.Right, fromCoords, 'nowrap');
9498
}
9599

96100
/**
@@ -112,8 +116,8 @@ export class GridNavigation<T extends GridNavigationCell> {
112116
col: this.inputs.grid.maxColCount(),
113117
};
114118
return row === undefined
115-
? this._peekDirectional(Direction.Left, fromCoords, 'continuous')
116-
: this._peekDirectional(Direction.Left, fromCoords, 'nowrap');
119+
? this._peekDirectional(direction.Left, fromCoords, 'continuous')
120+
: this._peekDirectional(direction.Left, fromCoords, 'nowrap');
117121
}
118122

119123
/**
@@ -138,7 +142,6 @@ export class GridNavigation<T extends GridNavigationCell> {
138142
const maxColCount = this.inputs.grid.maxColCount();
139143
const rowDelta = delta.row ?? 0;
140144
const colDelta = delta.col ?? 0;
141-
const generalDelta = delta.row ?? delta.col;
142145
let nextCoords = {...fromCoords};
143146

144147
for (let step = 0; step < this._maxSteps(); step++) {
@@ -151,6 +154,7 @@ export class GridNavigation<T extends GridNavigationCell> {
151154
if (wrap === 'nowrap' && isWrapping) return;
152155

153156
if (wrap === 'continuous') {
157+
const generalDelta = delta.row ?? delta.col;
154158
const rowStep = isWrapping ? generalDelta : rowDelta;
155159
const colStep = isWrapping ? generalDelta : colDelta;
156160

src/aria/ui-patterns/behaviors/grid/grid.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {SignalLike} from '../signal-like/signal-like';
1111
import {GridData, BaseGridCell, GridDataInputs, RowCol} from './grid-data';
1212
import {GridFocus, GridFocusCell, GridFocusInputs} from './grid-focus';
1313
import {
14-
Direction,
14+
direction,
1515
GridNavigation,
1616
GridNavigationCell,
1717
GridNavigationInputs,
@@ -91,51 +91,51 @@ export class Grid<T extends GridCell> {
9191

9292
/** Navigates to the cell above the currently active cell. */
9393
up(): boolean {
94-
return this.navigationBehavior.advance(Direction.Up);
94+
return this.navigationBehavior.advance(direction.Up);
9595
}
9696

9797
/** Extends the selection to the cell above the selection anchor. */
9898
rangeSelectUp(): void {
99-
const coords = this.navigationBehavior.peek(Direction.Up, this.selectionAnchor());
99+
const coords = this.navigationBehavior.peek(direction.Up, this.selectionAnchor());
100100
if (coords === undefined) return;
101101

102102
this._rangeSelectCoords(coords);
103103
}
104104

105105
/** Navigates to the cell below the currently active cell. */
106106
down(): boolean {
107-
return this.navigationBehavior.advance(Direction.Down);
107+
return this.navigationBehavior.advance(direction.Down);
108108
}
109109

110110
/** Extends the selection to the cell below the selection anchor. */
111111
rangeSelectDown(): void {
112-
const coords = this.navigationBehavior.peek(Direction.Down, this.selectionAnchor());
112+
const coords = this.navigationBehavior.peek(direction.Down, this.selectionAnchor());
113113
if (coords === undefined) return;
114114

115115
this._rangeSelectCoords(coords);
116116
}
117117

118118
/** Navigates to the cell to the left of the currently active cell. */
119119
left(): boolean {
120-
return this.navigationBehavior.advance(Direction.Left);
120+
return this.navigationBehavior.advance(direction.Left);
121121
}
122122

123123
/** Extends the selection to the cell to the left of the selection anchor. */
124124
rangeSelectLeft(): void {
125-
const coords = this.navigationBehavior.peek(Direction.Left, this.selectionAnchor());
125+
const coords = this.navigationBehavior.peek(direction.Left, this.selectionAnchor());
126126
if (coords === undefined) return;
127127

128128
this._rangeSelectCoords(coords);
129129
}
130130

131131
/** Navigates to the cell to the right of the currently active cell. */
132132
right(): boolean {
133-
return this.navigationBehavior.advance(Direction.Right);
133+
return this.navigationBehavior.advance(direction.Right);
134134
}
135135

136136
/** Extends the selection to the cell to the right of the selection anchor. */
137137
rangeSelectRight(): void {
138-
const coords = this.navigationBehavior.peek(Direction.Right, this.selectionAnchor());
138+
const coords = this.navigationBehavior.peek(direction.Right, this.selectionAnchor());
139139
if (coords === undefined) return;
140140

141141
this._rangeSelectCoords(coords);

src/aria/ui-patterns/grid/grid.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,41 @@ import {Grid, GridInputs as GridBehaviorInputs} from '../behaviors/grid';
1313
import type {GridRowPattern} from './row';
1414
import type {GridCellPattern} from './cell';
1515

16-
/** */
16+
/** Represents the required inputs for the grid pattern. */
1717
export interface GridInputs extends Omit<GridBehaviorInputs<GridCellPattern>, 'cells'> {
18-
/** */
18+
/** The rows that make up the grid. */
1919
rows: SignalLike<GridRowPattern[]>;
2020

21-
/** */
21+
/** A function that returns the grid cell associated with a given element. */
2222
getCell: (e: Element) => GridCellPattern | undefined;
2323
}
2424

25-
/** */
25+
/** The UI pattern for a grid, handling keyboard navigation, focus, and selection. */
2626
export class GridPattern {
27-
/** */
27+
/** The underlying grid behavior that this pattern is built on. */
2828
readonly gridBehavior: Grid<GridCellPattern>;
2929

30-
/** */
30+
/** The cells in the grid. */
3131
readonly cells = computed(() => this.gridBehavior.data.cells());
3232

33-
/** */
33+
/** The tab index for the grid. */
3434
readonly tabIndex = computed(() => this.gridBehavior.gridTabIndex());
3535

36-
/** */
36+
/** Whether the grid is disabled. */
3737
readonly disabled = computed(() => this.gridBehavior.gridDisabled());
3838

39-
/** */
39+
/** The ID of the currently active descendant cell. */
4040
readonly activeDescendant = computed(() => this.gridBehavior.activeDescendant());
4141

42-
/** */
42+
/** The currently active cell. */
4343
readonly activeCell = computed(() => this.gridBehavior.focusBehavior.activeCell());
4444

45-
/** */
45+
/** Whether grid navigation is currently paused by a cell (e.g. an input field). */
4646
readonly pauseGridNavigation = computed(() =>
4747
this.gridBehavior.data.flattenCells().some(c => c.pauseGridNavigation()),
4848
);
4949

50-
/** */
50+
/** Whether the user is currently dragging to select a range of cells. */
5151
readonly dragging = signal<boolean>(false);
5252

5353
/** The keydown event manager for the grid. */
@@ -117,7 +117,7 @@ export class GridPattern {
117117
return manager;
118118
});
119119

120-
/** */
120+
/** The pointerup event manager for the grid. */
121121
readonly pointerup = computed(() => {
122122
const manager = new PointerEventManager();
123123

0 commit comments

Comments
 (0)