Skip to content

Commit f41a99d

Browse files
more cleanup
1 parent 6eed8de commit f41a99d

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

src/components/NavigationFocus/FocusContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface IGridItem {
4343
}
4444

4545
/**
46-
* In order to accomodate multi-layout grids,
46+
* In order to accomodate variable-layout grids,
4747
* we need to create an intermediate map that keeps track
4848
* of the "pseudo-index" of an item that spans multiple indices
4949
* (consider case where item takes 2 grids, but is a single DOM Node)

src/components/NavigationFocus/get-next-grid-index.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,31 @@ const isLeftMostColumn = (itemsPerRow: number, index: number) => (index % itemsP
3434
const isRightMostColumn = (grid: IGridItem[], itemsPerRow: number, index: number) => index % itemsPerRow === itemsPerRow - 1 || index === grid.length - 1;
3535
const isTopRow = (itemsPerRow: number, index: number) => index <= itemsPerRow - 1;
3636

37+
const isEdgeColumn = (grid: IGridItem[], itemsPerRow: number, index: number, direction: Direction) => {
38+
switch (direction) {
39+
case 'up': return isTopRow(itemsPerRow, index);
40+
case 'down': return isBottomRow(grid, itemsPerRow, index);
41+
case 'left': return isLeftMostColumn(itemsPerRow, index);
42+
case 'right': return isRightMostColumn(grid, itemsPerRow, index);
43+
default: return false;
44+
}
45+
};
46+
3747
/**
38-
*
48+
* Given a direction (left or right), this function iterates through
49+
* a row until it finds a new grid item that is not disabled.
3950
*/
4051
const getAvailableAdjacentIndex = (grid: IGridItem[], itemsPerRow: number, startingIndex: number, direction: 'left' | 'right') => {
4152
let newIndex = startingIndex;
4253
let countIndex = newIndex;
4354
const offset = DIRECTIONAL_OFFSETS[direction];
4455

45-
while (!!grid[countIndex]) {
56+
while (!!grid[countIndex] && !isEdgeColumn(grid, itemsPerRow, countIndex, direction)) {
4657
const offsetIndex = countIndex + offset;
4758

48-
// Bail if it's an edge column
49-
if (direction === 'left' ? isLeftMostColumn(itemsPerRow, countIndex) : isRightMostColumn(grid, itemsPerRow, countIndex)) {
59+
if (!!grid[offsetIndex] && !grid[offsetIndex].isDisabled && grid[offsetIndex].index !== grid[startingIndex].index) {
60+
newIndex = offsetIndex;
5061
break;
51-
} else if (!!grid[offsetIndex]) {
52-
if (!grid[offsetIndex].isDisabled && grid[offsetIndex].index !== grid[startingIndex].index) {
53-
newIndex = offsetIndex;
54-
break;
55-
}
5662
}
5763

5864
countIndex += offset;
@@ -61,14 +67,19 @@ const getAvailableAdjacentIndex = (grid: IGridItem[], itemsPerRow: number, start
6167
return newIndex;
6268
};
6369

70+
/**
71+
* Given a direction (up or down), this function finds an enabled item
72+
* on a row in the given direction.
73+
*/
6474
const getAvailableVerticalIndex = (grid: IGridItem[], itemsPerRow: number, startingIndex: number, direction: 'up' | 'down') => {
6575
let newIndex = startingIndex;
6676
const offset = DIRECTIONAL_OFFSETS[direction];
6777

68-
if (direction === 'up' ? !isTopRow(itemsPerRow, startingIndex) : !isBottomRow(grid, itemsPerRow, startingIndex)) {
78+
if (!isEdgeColumn(grid, itemsPerRow, startingIndex, direction)) {
6979
/**
7080
* If destination grid is disabled, check if it's a left most column.
71-
* If it is, select the tile adjacent right.
81+
* If it is, iterate right until we find an enabled grid item.
82+
* If it's not, iterate left until we find an enabled grid item.
7283
*/
7384
do {
7485
newIndex += (itemsPerRow * offset);

0 commit comments

Comments
 (0)