mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 16:53:34 +00:00
* Use typescript-strict-plugin to iteratively turn on strict * Add strict testing to pipeline Can be executed locally through either `npm run test:types` for full type checking including spec files, or `npx tsc-strict` for only tsconfig.json included files. * turn on strict for scripts directory * Use plugin for all tsconfigs in monorepo vscode is capable of executing tsc with plugins, but uses the most relevant tsconfig to do so. If the plugin is not a part of that config, it is skipped and developers get no feedback of strict compile time issues. These updates remedy that at the cost of slightly more complex removal of the plugin when the time comes. * remove plugin from configs that extend one that already has it * Update workspace settings to honor strict plugin * Apply strict-plugin to native message test runner * Update vscode workspace to use root tsc version * `./node_modules/.bin/update-strict-comments` 🤖 This is a one-time operation. All future files should adhere to strict type checking. * Add fixme to `ts-strict-ignore` comments * `update-strict-comments` 🤖 repeated for new merge files
148 lines
3.7 KiB
TypeScript
148 lines
3.7 KiB
TypeScript
// FIXME: Update this file to be type safe and remove this and next line
|
|
// @ts-strict-ignore
|
|
import {
|
|
AfterViewInit,
|
|
ContentChildren,
|
|
Directive,
|
|
HostBinding,
|
|
HostListener,
|
|
Input,
|
|
QueryList,
|
|
} from "@angular/core";
|
|
|
|
import type { A11yCellDirective } from "./a11y-cell.directive";
|
|
import { A11yRowDirective } from "./a11y-row.directive";
|
|
|
|
@Directive({
|
|
selector: "bitA11yGrid",
|
|
standalone: true,
|
|
})
|
|
export class A11yGridDirective implements AfterViewInit {
|
|
@HostBinding("attr.role")
|
|
role = "grid";
|
|
|
|
@ContentChildren(A11yRowDirective)
|
|
rows: QueryList<A11yRowDirective>;
|
|
|
|
/** The number of pages to navigate on `PageUp` and `PageDown` */
|
|
@Input() pageSize = 5;
|
|
|
|
private grid: A11yCellDirective[][];
|
|
|
|
/** The row that currently has focus */
|
|
private activeRow = 0;
|
|
|
|
/** The cell that currently has focus */
|
|
private activeCol = 0;
|
|
|
|
@HostListener("keydown", ["$event"])
|
|
onKeyDown(event: KeyboardEvent) {
|
|
switch (event.code) {
|
|
case "ArrowUp":
|
|
this.updateCellFocusByDelta(-1, 0);
|
|
break;
|
|
case "ArrowRight":
|
|
this.updateCellFocusByDelta(0, 1);
|
|
break;
|
|
case "ArrowDown":
|
|
this.updateCellFocusByDelta(1, 0);
|
|
break;
|
|
case "ArrowLeft":
|
|
this.updateCellFocusByDelta(0, -1);
|
|
break;
|
|
case "Home":
|
|
this.updateCellFocusByDelta(-this.activeRow, -this.activeCol);
|
|
break;
|
|
case "End":
|
|
this.updateCellFocusByDelta(this.grid.length, this.grid[this.grid.length - 1].length);
|
|
break;
|
|
case "PageUp":
|
|
this.updateCellFocusByDelta(-this.pageSize, 0);
|
|
break;
|
|
case "PageDown":
|
|
this.updateCellFocusByDelta(this.pageSize, 0);
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
|
|
/** Prevent default scrolling behavior */
|
|
event.preventDefault();
|
|
}
|
|
|
|
ngAfterViewInit(): void {
|
|
this.initializeGrid();
|
|
}
|
|
|
|
private initializeGrid(): void {
|
|
try {
|
|
this.grid = this.rows.map((listItem) => {
|
|
listItem.role = "row";
|
|
return [...listItem.cells];
|
|
});
|
|
this.grid.flat().forEach((cell) => {
|
|
cell.role = "gridcell";
|
|
cell.getFocusTarget().tabIndex = -1;
|
|
});
|
|
|
|
this.getActiveCellContent().tabIndex = 0;
|
|
} catch (error) {
|
|
// eslint-disable-next-line no-console
|
|
console.error("Unable to initialize grid");
|
|
}
|
|
}
|
|
|
|
/** Get the focusable content of the active cell */
|
|
private getActiveCellContent(): HTMLElement {
|
|
return this.grid[this.activeRow][this.activeCol].getFocusTarget();
|
|
}
|
|
|
|
/** Move focus via a delta against the currently active gridcell */
|
|
private updateCellFocusByDelta(rowDelta: number, colDelta: number) {
|
|
const prevActive = this.getActiveCellContent();
|
|
|
|
this.activeCol += colDelta;
|
|
this.activeRow += rowDelta;
|
|
|
|
// Row upper bound
|
|
if (this.activeRow >= this.grid.length) {
|
|
this.activeRow = this.grid.length - 1;
|
|
}
|
|
|
|
// Row lower bound
|
|
if (this.activeRow < 0) {
|
|
this.activeRow = 0;
|
|
}
|
|
|
|
// Column upper bound
|
|
if (this.activeCol >= this.grid[this.activeRow].length) {
|
|
if (this.activeRow < this.grid.length - 1) {
|
|
// Wrap to next row on right arrow
|
|
this.activeCol = 0;
|
|
this.activeRow += 1;
|
|
} else {
|
|
this.activeCol = this.grid[this.activeRow].length - 1;
|
|
}
|
|
}
|
|
|
|
// Column lower bound
|
|
if (this.activeCol < 0) {
|
|
if (this.activeRow > 0) {
|
|
// Wrap to prev row on left arrow
|
|
this.activeRow -= 1;
|
|
this.activeCol = this.grid[this.activeRow].length - 1;
|
|
} else {
|
|
this.activeCol = 0;
|
|
}
|
|
}
|
|
|
|
const nextActive = this.getActiveCellContent();
|
|
nextActive.tabIndex = 0;
|
|
nextActive.focus();
|
|
|
|
if (nextActive !== prevActive) {
|
|
prevActive.tabIndex = -1;
|
|
}
|
|
}
|
|
}
|