From 6c7d4fae84af4f72c237c6188c483d49fb537bb8 Mon Sep 17 00:00:00 2001 From: Joshua Amaju Date: Wed, 8 Apr 2026 11:37:29 +0100 Subject: [PATCH 1/2] chore --- packages/components/package.json | 3 + .../components/src/VirtualFocus/define.ts | 2 + .../src/core/virtual-focus/all-axis/index.ts | 118 +++---- .../src/core/virtual-focus/linear.ts | 32 +- packages/components/src/core/widget.ts | 2 +- packages/core/package.json | 4 + packages/core/src/RovingIndex.ts | 73 ++++ .../core/src/core/roving-index/functions.ts | 85 +++++ packages/core/src/core/roving-index/index.ts | 319 ++++++++++++++++++ packages/core/src/core/roving-index/types.ts | 7 + packages/core/src/index.ts | 5 + pnpm-lock.yaml | 259 +------------- 12 files changed, 592 insertions(+), 317 deletions(-) create mode 100644 packages/components/src/VirtualFocus/define.ts create mode 100644 packages/core/src/RovingIndex.ts create mode 100644 packages/core/src/core/roving-index/functions.ts create mode 100644 packages/core/src/core/roving-index/index.ts create mode 100644 packages/core/src/core/roving-index/types.ts diff --git a/packages/components/package.json b/packages/components/package.json index e6a2a95..8e82c60 100644 --- a/packages/components/package.json +++ b/packages/components/package.json @@ -53,5 +53,8 @@ }, "dependencies": { "tabbable": "^6.3.0" + }, + "peerDependencies": { + "valaria": "workspace:*" } } diff --git a/packages/components/src/VirtualFocus/define.ts b/packages/components/src/VirtualFocus/define.ts new file mode 100644 index 0000000..a03b742 --- /dev/null +++ b/packages/components/src/VirtualFocus/define.ts @@ -0,0 +1,2 @@ +import "./LinearAxis/define.js"; +import "./AllAxis/define.js"; diff --git a/packages/components/src/core/virtual-focus/all-axis/index.ts b/packages/components/src/core/virtual-focus/all-axis/index.ts index e797e44..1cd797b 100644 --- a/packages/components/src/core/virtual-focus/all-axis/index.ts +++ b/packages/components/src/core/virtual-focus/all-axis/index.ts @@ -1,76 +1,80 @@ -import { isFocusable } from "tabbable"; +import { isTabbable } from "tabbable"; +import * as RovingIndex from "valaria/RovingIndex"; + import { VirtualFocus } from "../linear.js"; -import { compute, findNextFocusable } from "./utils.js"; +const defaultCoordinates: RovingIndex.Coordinates = { + rowIndex: 0, + columnIndex: 0, +}; export class VirtualFocusAllAxis extends VirtualFocus { - static TAG_NAME = "va-virtual-focus-all-axis"; + static override TAG_NAME = "va-virtual-focus-all-axis"; - protected handleDocumentKeyDown = (event: KeyboardEvent) => { - if ( - [ - "ArrowUp", - "ArrowDown", - "ArrowLeft", - "ArrowRight", - "Home", - "End", - ].includes(event.key) - ) { - const currentOption = this.currentOption; - const grid = compute(this, [...this.getChildren()]); + private coordinates = defaultCoordinates; - const focusables = grid - .map((_) => _.filter((_) => isFocusable(_))) - .filter((_) => _.length > 0); + #grid() { + const elements = [...this.getChildren()] as HTMLElement[]; - let x = null; - let y = null; + const grid: HTMLElement[][] = []; - for (let i = 0; i < focusables.length; i++) { - const page = focusables[i]; + const size = Number(this.getAttribute("size") ?? 4); - if (!page) continue; + let index = 0; - for (let j = 0; j < page.length; j++) { - const item = page[j]; + while (index < elements.length) { + grid.push(elements.slice(index, index + size)); + index += size; + } - // @ts-expect-error - if (item?.isSameNode(currentOption)) { - [x, y] = [j, i]; - break; - } - } + return grid; + } + + protected itemFocus = (e: Event) => { + e.stopPropagation(); + + const elements = this.#grid(); + const target = e.target as HTMLElement; + const coords = RovingIndex.getElementCoordinates(elements, target); + + if (coords) { + this.activeElement = elements[coords?.rowIndex]?.[coords?.columnIndex]; + this.coordinates = coords; + } + }; - if (y) break; - } + protected itemFocusOut = (e: Event) => { + e.stopPropagation(); + this.coordinates = defaultCoordinates; + this.activeElement = undefined; + }; - x ??= -1; - y ??= 0; + protected handleDocumentKeyDown = (event: KeyboardEvent) => { + if ( + ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(event.key) + ) { + const grid = this.#grid(); - let newFocusable = null; const loop = this.hasAttribute("loop"); const wrap = this.hasAttribute("wrap"); - const opts = { x, y, wrap, loop }; - - if (event.key === "ArrowDown") { - newFocusable = findNextFocusable(focusables, "down", opts); - } else if (event.key === "ArrowRight") { - newFocusable = findNextFocusable(focusables, "right", opts); - } else if (event.key === "ArrowUp") { - newFocusable = findNextFocusable(focusables, "up", opts); - } else if (event.key === "ArrowLeft") { - newFocusable = findNextFocusable(focusables, "left", opts); - } else if (event.key == "Home") { - newFocusable = focusables[0]?.[0]; - } else if (event.key == "End") { - const last = focusables[focusables.length - 1]; - newFocusable = last?.[last.length - 1]; - } - - if (!newFocusable) return; - - this.activeElement = newFocusable; + const next = RovingIndex.getNextItem( + grid, + this.coordinates, + event.key as RovingIndex.Key, + { wrap, loop, isFocusable: isTabbable }, + ); + + if (!next) return; + + this.activeElement = next.item; + + this.coordinates.rowIndex = next.rowIndex; + this.coordinates.columnIndex = next.columnIndex; } }; + + public unmount() { + if (!this.hasAttribute("preserve")) this.coordinates = defaultCoordinates; + super.unmount(); + } } diff --git a/packages/components/src/core/virtual-focus/linear.ts b/packages/components/src/core/virtual-focus/linear.ts index 274fb11..3bc085e 100644 --- a/packages/components/src/core/virtual-focus/linear.ts +++ b/packages/components/src/core/virtual-focus/linear.ts @@ -10,6 +10,8 @@ const ACTIVE_ATTRIBUTE = attr("virtual-focus-active"); export class VirtualFocus extends Widget { static TAG_NAME = "va-virtual-focus"; + static observedAttributes = ["mount", "wrap", "loop", "preserve"]; + protected currentOption?: Element | HTMLElement | undefined | null; get activeElement() { @@ -38,7 +40,19 @@ export class VirtualFocus extends Widget { if (this.hasAttribute("mount")) this.mount(); } - mount() { + attributeChangedCallback(name: any, oldValue: any, newValue: any) { + if (name === "mount") { + if (this.hasAttribute("mount")) { + this.mount(); + } else { + this.unmount(); + } + } + } + + public mount() { + console.log("mounted"); + this.activeElement = this.currentOption ?? this.querySelector(`[${attr("virtual-focus-current")}]`); @@ -46,14 +60,16 @@ export class VirtualFocus extends Widget { const children = this.getChildren(); children.forEach((_) => { - _.addEventListener(ITEM_FOCUS_EVENT, this.#itemFocus); - _.addEventListener(ITEM_FOCUSOUT_EVENT, this.#itemFocusOut); + _.addEventListener(ITEM_FOCUS_EVENT, this.itemFocus); + _.addEventListener(ITEM_FOCUSOUT_EVENT, this.itemFocusOut); }); document.addEventListener("keydown", this.handleDocumentKeyDown); } - unmount() { + public unmount() { + console.log("unmounted"); + this.currentOption?.removeAttribute(ACTIVE_ATTRIBUTE); if (!this.hasAttribute("preserve")) this.currentOption = undefined; @@ -61,8 +77,8 @@ export class VirtualFocus extends Widget { const children = this.getChildren(); children.forEach((_) => { - _.removeEventListener(ITEM_FOCUS_EVENT, this.#itemFocus); - _.removeEventListener(ITEM_FOCUSOUT_EVENT, this.#itemFocusOut); + _.removeEventListener(ITEM_FOCUS_EVENT, this.itemFocus); + _.removeEventListener(ITEM_FOCUSOUT_EVENT, this.itemFocusOut); }); document.removeEventListener("keydown", this.handleDocumentKeyDown); @@ -72,12 +88,12 @@ export class VirtualFocus extends Widget { return this.querySelectorAll(`[${attr("virtual-focus")}]`); } - #itemFocus = (e: Event) => { + protected itemFocus = (e: Event) => { e.stopPropagation(); this.activeElement = e.target as HTMLElement; }; - #itemFocusOut = (e: Event) => { + protected itemFocusOut = (e: Event) => { e.stopPropagation(); this.activeElement = undefined; }; diff --git a/packages/components/src/core/widget.ts b/packages/components/src/core/widget.ts index 3475375..5d7810b 100644 --- a/packages/components/src/core/widget.ts +++ b/packages/components/src/core/widget.ts @@ -17,7 +17,7 @@ export class Widget extends HTMLElement { define(tag, element, options); } - emit(type: string, init?: CustomEventInit | undefined) { + protected emit(type: string, init?: CustomEventInit | undefined) { const event = new CustomEvent(type, init); this.dispatchEvent(event); return event; diff --git a/packages/core/package.json b/packages/core/package.json index 04061c7..fb37a91 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -49,6 +49,10 @@ "types": "./dist/Pagination.d.ts", "import": "./dist/Pagination.js" }, + "./RovingIndex": { + "types": "./dist/RovingIndex.d.ts", + "import": "./dist/RovingIndex.js" + }, "./package.json": "./package.json" }, "devDependencies": { diff --git a/packages/core/src/RovingIndex.ts b/packages/core/src/RovingIndex.ts new file mode 100644 index 0000000..c231992 --- /dev/null +++ b/packages/core/src/RovingIndex.ts @@ -0,0 +1,73 @@ +/** + * @since 0.0.2 + */ +import * as core from "./core/roving-index/index.js"; +import * as functions from "./core/roving-index/functions.js"; + +/** + * @since 0.0.2 + * @category types + */ +export type Key = "ArrowRight" | "ArrowLeft" | "ArrowDown" | "ArrowUp"; + +/** + * @since 0.0.2 + * @category types + */ +export type Coordinates = { + rowIndex: number; + columnIndex: number; +}; + +/** + * @since 0.0.2 + * @category types + */ +export type Options = { + wrap?: boolean; + loop?: boolean; + isFocusable?: (item: T) => boolean; +}; + +/** + * @since 0.0.2 + * @category types + */ +export type Grid = T[][]; + +/** + * @since 0.0.2 + * @category types + */ +export type Result = { + item: T; + rowIndex: number; + flatIndex: number; + columnIndex: number; +} | null; + +/** + * @since 0.0.2 + * @category constructors + */ +export const getNextItem = core.getNextItem; + +/** + * @since 0.0.1 + * @category utils + */ +export const getElementCoordinates: ( + grid: T[][], + target: T, +) => Coordinates | null = functions.getElementCoordinates; + +/** + * @since 0.0.1 + * @category utils + */ +export const focusNextElement: ( + grid: T[][], + current: T | Coordinates, + key: Key, + options?: functions.ElementOptions, +) => Result = functions.focusNextRovingElement; diff --git a/packages/core/src/core/roving-index/functions.ts b/packages/core/src/core/roving-index/functions.ts new file mode 100644 index 0000000..bdfe857 --- /dev/null +++ b/packages/core/src/core/roving-index/functions.ts @@ -0,0 +1,85 @@ +import { getNextItem } from "./index.js"; +import type { Coordinates, Key, Options, Result } from "../../RovingIndex.js"; + +export type ElementOptions = Omit< + Options, + "isFocusable" +> & { + isFocusable?: (item: T) => boolean; + preventScroll?: boolean; +}; + +export function getElementCoordinates( + grid: T[][], + target: T, +): Coordinates | null { + for (let rowIndex = 0; rowIndex < grid.length; rowIndex += 1) { + const row = grid[rowIndex] ?? []; + const columnIndex = row.indexOf(target); + + if (columnIndex >= 0) { + return { rowIndex, columnIndex }; + } + } + + return null; +} + +export function focusNextRovingElement( + grid: T[][], + current: T | Coordinates, + key: Key, + options: ElementOptions = {}, +): Result { + const coordinates = isCoordinates(current) + ? current + : getElementCoordinates(grid, current); + + if (!coordinates) { + return null; + } + + const controllerOptions: Options = { + isFocusable: options.isFocusable ?? isFocusableElement, + }; + + if (options.wrap !== undefined) { + controllerOptions.wrap = options.wrap; + } + + if (options.loop !== undefined) { + controllerOptions.loop = options.loop; + } + + const next = getNextItem(grid, coordinates, key, controllerOptions); + + if (next) { + if (options.preventScroll !== undefined) { + next.item.focus({ preventScroll: options.preventScroll }); + } else { + next.item.focus(); + } + } + + return next; +} + +export function isFocusableElement(element: HTMLElement): boolean { + return ( + !isDisabledElement(element) && + !element.hidden && + element.getAttribute("aria-hidden") !== "true" && + element.tabIndex >= 0 + ); +} + +function isCoordinates(value: HTMLElement | Coordinates): value is Coordinates { + return "rowIndex" in value && "columnIndex" in value; +} + +function isDisabledElement(element: HTMLElement): boolean { + return ( + element.hasAttribute("disabled") || + element.getAttribute("aria-disabled") === "true" + ); +} diff --git a/packages/core/src/core/roving-index/index.ts b/packages/core/src/core/roving-index/index.ts new file mode 100644 index 0000000..b65f2d4 --- /dev/null +++ b/packages/core/src/core/roving-index/index.ts @@ -0,0 +1,319 @@ +import type { + Grid, + Coordinates, + Key, + Options, + Result, +} from "../../RovingIndex.js"; + +type NormalizedItem = { + item: T; + rowIndex: number; + flatIndex: number; + focusable: boolean; + columnIndex: number; +}; + +type NormalizedGrid = { + items: NormalizedItem[]; + rows: NormalizedItem[][]; +}; + +const defaultIsFocusable = (_item: T): boolean => true; + +export function getNextItem( + grid: Grid, + current: Coordinates, + key: Key, + options: Options = {}, +): Result { + const normalized = normalizeGrid( + grid, + options.isFocusable ?? defaultIsFocusable, + ); + + const currentItem = normalized.rows[current.rowIndex]?.[current.columnIndex]; + + if (!currentItem) return null; + + const next = getNextNormalizedItem(normalized, currentItem, key, { + wrap: options.wrap ?? false, + loop: options.loop ?? false, + }); + + return toResult(next); +} + +function normalizeGrid( + grid: Grid, + isFocusable: (item: T) => boolean, +): NormalizedGrid { + const rows: NormalizedItem[][] = []; + const items: NormalizedItem[] = []; + let flatIndex = 0; + + for (let rowIndex = 0; rowIndex < grid.length; rowIndex += 1) { + const row = grid[rowIndex] ?? []; + const normalizedRow: NormalizedItem[] = []; + + for (let columnIndex = 0; columnIndex < row.length; columnIndex += 1) { + const item = row[columnIndex] as T; + + const normalizedItem: NormalizedItem = { + item, + rowIndex, + flatIndex, + columnIndex, + focusable: isFocusable(item), + }; + + normalizedRow.push(normalizedItem); + items.push(normalizedItem); + + flatIndex += 1; + } + + rows.push(normalizedRow); + } + + return { rows, items }; +} + +function getNextNormalizedItem( + grid: NormalizedGrid, + current: NormalizedItem, + key: Key, + options: Required, "wrap" | "loop">>, +): NormalizedItem | null { + switch (key) { + case "ArrowRight": + return getNextHorizontalItem(grid, current, "forward", options); + case "ArrowLeft": + return getNextHorizontalItem(grid, current, "backward", options); + case "ArrowDown": + return getNextVerticalItem(grid, current, "forward", options); + case "ArrowUp": + return getNextVerticalItem(grid, current, "backward", options); + } +} + +function getNextHorizontalItem( + grid: NormalizedGrid, + current: NormalizedItem, + direction: "forward" | "backward", + options: Required, "wrap" | "loop">>, +): NormalizedItem | null { + const row = grid.rows[current.rowIndex] ?? []; + const same_row = findFocusableInRow(row, current.columnIndex, direction); + + if (same_row) return same_row; + + if (options.wrap) { + const wrapped = findFocusableByFlatIndex( + grid.items, + current.flatIndex, + direction, + ); + + if (wrapped) return wrapped; + } + + if ( + !options.loop || + hasFocusablePast(grid.items, current.flatIndex, direction) + ) { + return null; + } + + // Looping is independent from wrapping: only loop from the global focusable edge. + return direction === "forward" + ? firstFocusable(grid.items, current) + : lastFocusable(grid.items, current); +} + +function getNextVerticalItem( + grid: NormalizedGrid, + current: NormalizedItem, + direction: "forward" | "backward", + options: Required, "wrap" | "loop">>, +): NormalizedItem | null { + const rowOrder = getVerticalRowOrder( + grid.rows.length, + current.rowIndex, + direction, + options.loop, + ); + + for (const rowIndex of rowOrder) { + const row = grid.rows[rowIndex] ?? []; + + if (rowIndex === current.rowIndex) { + // A vertical loop can revisit the current row in single-row grids; use row-major fallback. + const loopFallback = + direction === "forward" + ? firstFocusable(row, current) + : lastFocusable(row, current); + + if (loopFallback) return loopFallback; + + continue; + } + + const nearest = findNearestFocusableInRow(row, current.columnIndex); + + if (nearest) return nearest; + } + + return null; +} + +function findFocusableInRow( + row: NormalizedItem[], + fromColumnIndex: number, + direction: "forward" | "backward", +): NormalizedItem | null { + if (direction === "forward") { + for (let index = fromColumnIndex + 1; index < row.length; index += 1) { + const item = row[index]; + if (item?.focusable) return item; + } + } else { + for (let index = fromColumnIndex - 1; index >= 0; index -= 1) { + const item = row[index]; + if (item?.focusable) return item; + } + } + + return null; +} + +function findFocusableByFlatIndex( + items: NormalizedItem[], + fromFlatIndex: number, + direction: "forward" | "backward", +): NormalizedItem | null { + if (direction === "forward") { + for (let index = fromFlatIndex + 1; index < items.length; index += 1) { + const item = items[index]; + if (item?.focusable) return item; + } + } else { + for (let index = fromFlatIndex - 1; index >= 0; index -= 1) { + const item = items[index]; + if (item?.focusable) return item; + } + } + + return null; +} + +function hasFocusablePast( + items: NormalizedItem[], + fromFlatIndex: number, + direction: "forward" | "backward", +): boolean { + return findFocusableByFlatIndex(items, fromFlatIndex, direction) !== null; +} + +function firstFocusable( + items: NormalizedItem[], + current: NormalizedItem, +): NormalizedItem | null { + for (const item of items) { + if (item.focusable && item !== current) { + return item; + } + } + + return null; +} + +function lastFocusable( + items: NormalizedItem[], + current: NormalizedItem, +): NormalizedItem | null { + for (let index = items.length - 1; index >= 0; index -= 1) { + const item = items[index]; + + if (item?.focusable && item !== current) { + return item; + } + } + + return null; +} + +function findNearestFocusableInRow( + row: NormalizedItem[], + targetColumnIndex: number, +): NormalizedItem | null { + const exact = row[targetColumnIndex]; + + if (exact?.focusable) return exact; + + for (let offset = 1; offset <= row.length; offset += 1) { + const before = row[targetColumnIndex - offset]; + + if (before?.focusable) return before; + + const after = row[targetColumnIndex + offset]; + + if (after?.focusable) return after; + } + + return null; +} + +function getVerticalRowOrder( + rowCount: number, + currentRowIndex: number, + direction: "forward" | "backward", + loop: boolean, +): number[] { + const row_order: number[] = []; + + if (direction === "forward") { + for ( + let rowIndex = currentRowIndex + 1; + rowIndex < rowCount; + rowIndex += 1 + ) { + row_order.push(rowIndex); + } + + if (loop) { + for (let rowIndex = 0; rowIndex <= currentRowIndex; rowIndex += 1) { + row_order.push(rowIndex); + } + } + } else { + for (let rowIndex = currentRowIndex - 1; rowIndex >= 0; rowIndex -= 1) { + row_order.push(rowIndex); + } + + if (loop) { + for ( + let rowIndex = rowCount - 1; + rowIndex >= currentRowIndex; + rowIndex -= 1 + ) { + row_order.push(rowIndex); + } + } + } + + return row_order; +} + +function toResult(item: NormalizedItem | null): Result { + if (!item) { + return null; + } + + return { + item: item.item, + rowIndex: item.rowIndex, + flatIndex: item.flatIndex, + columnIndex: item.columnIndex, + }; +} diff --git a/packages/core/src/core/roving-index/types.ts b/packages/core/src/core/roving-index/types.ts new file mode 100644 index 0000000..9e5f5f8 --- /dev/null +++ b/packages/core/src/core/roving-index/types.ts @@ -0,0 +1,7 @@ + + + + + + + diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index d55ebe1..c995c59 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -11,3 +11,8 @@ export * as Pager from "./Pager.js"; * @since 0.0.1 */ export * as Pagination from "./Pagination.js"; + +/** + * @since 0.0.2 + */ +export * as RovingIndex from "./RovingIndex.js"; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7708563..890ae18 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ importers: tabbable: specifier: ^6.3.0 version: 6.3.0 + valaria: + specifier: workspace:* + version: link:../core devDependencies: '@changesets/cli': specifier: ^2.29.8 @@ -55,47 +58,6 @@ importers: specifier: ^4.0.15 version: 4.0.15(@types/node@25.0.1)(@vitest/browser-playwright@4.0.15)(jiti@2.6.1)(jsdom@27.3.0(postcss@8.5.6))(lightningcss@1.30.2)(tsx@4.21.0) - playground/basic: - dependencies: - valaria: - specifier: workspace:* - version: link:../../packages/core - devDependencies: - '@tailwindcss/vite': - specifier: ^4.1.18 - version: 4.1.18(vite@7.2.7(@types/node@25.0.1)(jiti@2.6.1)(lightningcss@1.30.2)(tsx@4.21.0)) - tailwindcss: - specifier: ^4.1.18 - version: 4.1.18 - typescript: - specifier: ~5.9.3 - version: 5.9.3 - vite: - specifier: ^7.2.4 - version: 7.2.7(@types/node@25.0.1)(jiti@2.6.1)(lightningcss@1.30.2)(tsx@4.21.0) - - playground/virtual-focus: - dependencies: - '@valaria/components': - specifier: workspace:* - version: link:../../packages/components - valaria: - specifier: workspace:* - version: link:../../packages/core - devDependencies: - '@tailwindcss/vite': - specifier: ^4.1.18 - version: 4.1.18(vite@7.2.7(@types/node@25.0.1)(jiti@2.6.1)(lightningcss@1.30.2)(tsx@4.21.0)) - tailwindcss: - specifier: ^4.1.18 - version: 4.1.18 - typescript: - specifier: ~5.9.3 - version: 5.9.3 - vite: - specifier: ^7.2.4 - version: 7.2.7(@types/node@25.0.1)(jiti@2.6.1)(lightningcss@1.30.2)(tsx@4.21.0) - packages: '@acemir/cssom@0.9.29': @@ -541,22 +503,9 @@ packages: resolution: {integrity: sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==} engines: {node: '>=12'} - '@jridgewell/gen-mapping@0.3.13': - resolution: {integrity: sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==} - - '@jridgewell/remapping@2.3.5': - resolution: {integrity: sha512-LI9u/+laYG4Ds1TDKSJW2YPrIlcVYOwi2fUC6xB43lueCjgxV4lffOCZCtYFiH6TNOX+tQKXx97T4IKHbhyHEQ==} - - '@jridgewell/resolve-uri@3.1.2': - resolution: {integrity: sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==} - engines: {node: '>=6.0.0'} - '@jridgewell/sourcemap-codec@1.5.5': resolution: {integrity: sha512-cYQ9310grqxueWbl+WuIUIaiUaDcj7WOq5fVhEljNVgRfOUhY9fy2zTvfoqWsnebh8Sl70VScFbICvJnLKB0Og==} - '@jridgewell/trace-mapping@0.3.31': - resolution: {integrity: sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==} - '@manypkg/find-root@1.1.0': resolution: {integrity: sha512-mki5uBvhHzO8kYYix/WRy2WX8S3B5wdVSc9D6KcU5lQNglP2yt58/VfLuAK49glRXChosY8ap2oJ1qgma3GUVA==} @@ -700,96 +649,6 @@ packages: '@standard-schema/spec@1.0.0': resolution: {integrity: sha512-m2bOd0f2RT9k8QJx1JN85cZYyH1RqFBdlwtkSlf4tBDYLCiiZnv1fIIwacK6cqwXavOydf0NPToMQgpKq+dVlA==} - '@tailwindcss/node@4.1.18': - resolution: {integrity: sha512-DoR7U1P7iYhw16qJ49fgXUlry1t4CpXeErJHnQ44JgTSKMaZUdf17cfn5mHchfJ4KRBZRFA/Coo+MUF5+gOaCQ==} - - '@tailwindcss/oxide-android-arm64@4.1.18': - resolution: {integrity: sha512-dJHz7+Ugr9U/diKJA0W6N/6/cjI+ZTAoxPf9Iz9BFRF2GzEX8IvXxFIi/dZBloVJX/MZGvRuFA9rqwdiIEZQ0Q==} - engines: {node: '>= 10'} - cpu: [arm64] - os: [android] - - '@tailwindcss/oxide-darwin-arm64@4.1.18': - resolution: {integrity: sha512-Gc2q4Qhs660bhjyBSKgq6BYvwDz4G+BuyJ5H1xfhmDR3D8HnHCmT/BSkvSL0vQLy/nkMLY20PQ2OoYMO15Jd0A==} - engines: {node: '>= 10'} - cpu: [arm64] - os: [darwin] - - '@tailwindcss/oxide-darwin-x64@4.1.18': - resolution: {integrity: sha512-FL5oxr2xQsFrc3X9o1fjHKBYBMD1QZNyc1Xzw/h5Qu4XnEBi3dZn96HcHm41c/euGV+GRiXFfh2hUCyKi/e+yw==} - engines: {node: '>= 10'} - cpu: [x64] - os: [darwin] - - '@tailwindcss/oxide-freebsd-x64@4.1.18': - resolution: {integrity: sha512-Fj+RHgu5bDodmV1dM9yAxlfJwkkWvLiRjbhuO2LEtwtlYlBgiAT4x/j5wQr1tC3SANAgD+0YcmWVrj8R9trVMA==} - engines: {node: '>= 10'} - cpu: [x64] - os: [freebsd] - - '@tailwindcss/oxide-linux-arm-gnueabihf@4.1.18': - resolution: {integrity: sha512-Fp+Wzk/Ws4dZn+LV2Nqx3IilnhH51YZoRaYHQsVq3RQvEl+71VGKFpkfHrLM/Li+kt5c0DJe/bHXK1eHgDmdiA==} - engines: {node: '>= 10'} - cpu: [arm] - os: [linux] - - '@tailwindcss/oxide-linux-arm64-gnu@4.1.18': - resolution: {integrity: sha512-S0n3jboLysNbh55Vrt7pk9wgpyTTPD0fdQeh7wQfMqLPM/Hrxi+dVsLsPrycQjGKEQk85Kgbx+6+QnYNiHalnw==} - engines: {node: '>= 10'} - cpu: [arm64] - os: [linux] - - '@tailwindcss/oxide-linux-arm64-musl@4.1.18': - resolution: {integrity: sha512-1px92582HkPQlaaCkdRcio71p8bc8i/ap5807tPRDK/uw953cauQBT8c5tVGkOwrHMfc2Yh6UuxaH4vtTjGvHg==} - engines: {node: '>= 10'} - cpu: [arm64] - os: [linux] - - '@tailwindcss/oxide-linux-x64-gnu@4.1.18': - resolution: {integrity: sha512-v3gyT0ivkfBLoZGF9LyHmts0Isc8jHZyVcbzio6Wpzifg/+5ZJpDiRiUhDLkcr7f/r38SWNe7ucxmGW3j3Kb/g==} - engines: {node: '>= 10'} - cpu: [x64] - os: [linux] - - '@tailwindcss/oxide-linux-x64-musl@4.1.18': - resolution: {integrity: sha512-bhJ2y2OQNlcRwwgOAGMY0xTFStt4/wyU6pvI6LSuZpRgKQwxTec0/3Scu91O8ir7qCR3AuepQKLU/kX99FouqQ==} - engines: {node: '>= 10'} - cpu: [x64] - os: [linux] - - '@tailwindcss/oxide-wasm32-wasi@4.1.18': - resolution: {integrity: sha512-LffYTvPjODiP6PT16oNeUQJzNVyJl1cjIebq/rWWBF+3eDst5JGEFSc5cWxyRCJ0Mxl+KyIkqRxk1XPEs9x8TA==} - engines: {node: '>=14.0.0'} - cpu: [wasm32] - bundledDependencies: - - '@napi-rs/wasm-runtime' - - '@emnapi/core' - - '@emnapi/runtime' - - '@tybys/wasm-util' - - '@emnapi/wasi-threads' - - tslib - - '@tailwindcss/oxide-win32-arm64-msvc@4.1.18': - resolution: {integrity: sha512-HjSA7mr9HmC8fu6bdsZvZ+dhjyGCLdotjVOgLA2vEqxEBZaQo9YTX4kwgEvPCpRh8o4uWc4J/wEoFzhEmjvPbA==} - engines: {node: '>= 10'} - cpu: [arm64] - os: [win32] - - '@tailwindcss/oxide-win32-x64-msvc@4.1.18': - resolution: {integrity: sha512-bJWbyYpUlqamC8dpR7pfjA0I7vdF6t5VpUGMWRkXVE3AXgIZjYUYAK7II1GNaxR8J1SSrSrppRar8G++JekE3Q==} - engines: {node: '>= 10'} - cpu: [x64] - os: [win32] - - '@tailwindcss/oxide@4.1.18': - resolution: {integrity: sha512-EgCR5tTS5bUSKQgzeMClT6iCY3ToqE1y+ZB0AKldj809QXk1Y+3jB0upOYZrn9aGIzPtUsP7sX4QQ4XtjBB95A==} - engines: {node: '>= 10'} - - '@tailwindcss/vite@4.1.18': - resolution: {integrity: sha512-jVA+/UpKL1vRLg6Hkao5jldawNmRo7mQYrZtNHMIVpLfLhDml5nMRUo/8MwoX2vNXvnaXNNMedrMfMugAVX1nA==} - peerDependencies: - vite: ^5.2.0 || ^6 || ^7 - '@types/chai@5.2.3': resolution: {integrity: sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==} @@ -991,10 +850,6 @@ packages: emoji-regex@9.2.2: resolution: {integrity: sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==} - enhanced-resolve@5.18.4: - resolution: {integrity: sha512-LgQMM4WXU3QI+SYgEc2liRgznaD5ojbmY3sb8LxyguVkIg5FxdpTkvk72te2R38/TGKxH634oLxXRGY6d7AP+Q==} - engines: {node: '>=10.13.0'} - enquirer@2.4.1: resolution: {integrity: sha512-rRqJg/6gd538VHvR3PSrdRBb/1Vy2YfzHqzvbhGIQpDRKIa4FgV/54b5Q1xYSxOOwKvjXweS26E0Q+nAMwp2pQ==} engines: {node: '>=8.6'} @@ -1675,13 +1530,6 @@ packages: tabbable@6.3.0: resolution: {integrity: sha512-EIHvdY5bPLuWForiR/AN2Bxngzpuwn1is4asboytXtpTgsArc+WmSJKVLlhdh71u7jFcryDqB2A8lQvj78MkyQ==} - tailwindcss@4.1.18: - resolution: {integrity: sha512-4+Z+0yiYyEtUVCScyfHCxOYP06L5Ne+JiHhY2IjR2KWMIWhJOYZKLSGZaP5HkZ8+bY0cxfzwDE5uOmzFXyIwxw==} - - tapable@2.3.0: - resolution: {integrity: sha512-g9ljZiwki/LfxmQADO3dEY1CbpmXT5Hm2fJ+QaGKwSXUylMybePR7/67YW7jOrrvjEgL1Fmz5kzyAjWVWLlucg==} - engines: {node: '>=6'} - term-size@2.2.1: resolution: {integrity: sha512-wK0Ri4fOGjv/XPy8SBHZChl8CM7uMc5VML7SqiQ0zG7+J5Vr+RMQDoHa2CNT6KHUnTGIXH34UDMkPzAUyapBZg==} engines: {node: '>=8'} @@ -2277,25 +2125,8 @@ snapshots: wrap-ansi: 8.1.0 wrap-ansi-cjs: wrap-ansi@7.0.0 - '@jridgewell/gen-mapping@0.3.13': - dependencies: - '@jridgewell/sourcemap-codec': 1.5.5 - '@jridgewell/trace-mapping': 0.3.31 - - '@jridgewell/remapping@2.3.5': - dependencies: - '@jridgewell/gen-mapping': 0.3.13 - '@jridgewell/trace-mapping': 0.3.31 - - '@jridgewell/resolve-uri@3.1.2': {} - '@jridgewell/sourcemap-codec@1.5.5': {} - '@jridgewell/trace-mapping@0.3.31': - dependencies: - '@jridgewell/resolve-uri': 3.1.2 - '@jridgewell/sourcemap-codec': 1.5.5 - '@manypkg/find-root@1.1.0': dependencies: '@babel/runtime': 7.28.4 @@ -2401,74 +2232,6 @@ snapshots: '@standard-schema/spec@1.0.0': {} - '@tailwindcss/node@4.1.18': - dependencies: - '@jridgewell/remapping': 2.3.5 - enhanced-resolve: 5.18.4 - jiti: 2.6.1 - lightningcss: 1.30.2 - magic-string: 0.30.21 - source-map-js: 1.2.1 - tailwindcss: 4.1.18 - - '@tailwindcss/oxide-android-arm64@4.1.18': - optional: true - - '@tailwindcss/oxide-darwin-arm64@4.1.18': - optional: true - - '@tailwindcss/oxide-darwin-x64@4.1.18': - optional: true - - '@tailwindcss/oxide-freebsd-x64@4.1.18': - optional: true - - '@tailwindcss/oxide-linux-arm-gnueabihf@4.1.18': - optional: true - - '@tailwindcss/oxide-linux-arm64-gnu@4.1.18': - optional: true - - '@tailwindcss/oxide-linux-arm64-musl@4.1.18': - optional: true - - '@tailwindcss/oxide-linux-x64-gnu@4.1.18': - optional: true - - '@tailwindcss/oxide-linux-x64-musl@4.1.18': - optional: true - - '@tailwindcss/oxide-wasm32-wasi@4.1.18': - optional: true - - '@tailwindcss/oxide-win32-arm64-msvc@4.1.18': - optional: true - - '@tailwindcss/oxide-win32-x64-msvc@4.1.18': - optional: true - - '@tailwindcss/oxide@4.1.18': - optionalDependencies: - '@tailwindcss/oxide-android-arm64': 4.1.18 - '@tailwindcss/oxide-darwin-arm64': 4.1.18 - '@tailwindcss/oxide-darwin-x64': 4.1.18 - '@tailwindcss/oxide-freebsd-x64': 4.1.18 - '@tailwindcss/oxide-linux-arm-gnueabihf': 4.1.18 - '@tailwindcss/oxide-linux-arm64-gnu': 4.1.18 - '@tailwindcss/oxide-linux-arm64-musl': 4.1.18 - '@tailwindcss/oxide-linux-x64-gnu': 4.1.18 - '@tailwindcss/oxide-linux-x64-musl': 4.1.18 - '@tailwindcss/oxide-wasm32-wasi': 4.1.18 - '@tailwindcss/oxide-win32-arm64-msvc': 4.1.18 - '@tailwindcss/oxide-win32-x64-msvc': 4.1.18 - - '@tailwindcss/vite@4.1.18(vite@7.2.7(@types/node@25.0.1)(jiti@2.6.1)(lightningcss@1.30.2)(tsx@4.21.0))': - dependencies: - '@tailwindcss/node': 4.1.18 - '@tailwindcss/oxide': 4.1.18 - tailwindcss: 4.1.18 - vite: 7.2.7(@types/node@25.0.1)(jiti@2.6.1)(lightningcss@1.30.2)(tsx@4.21.0) - '@types/chai@5.2.3': dependencies: '@types/deep-eql': 4.0.2 @@ -2658,7 +2421,8 @@ snapshots: detect-indent@6.1.0: {} - detect-libc@2.1.2: {} + detect-libc@2.1.2: + optional: true diacritics-map@0.1.0: {} @@ -2676,11 +2440,6 @@ snapshots: emoji-regex@9.2.2: {} - enhanced-resolve@5.18.4: - dependencies: - graceful-fs: 4.2.11 - tapable: 2.3.0 - enquirer@2.4.1: dependencies: ansi-colors: 4.1.3 @@ -2951,7 +2710,8 @@ snapshots: optionalDependencies: '@pkgjs/parseargs': 0.11.0 - jiti@2.6.1: {} + jiti@2.6.1: + optional: true js-yaml@3.14.2: dependencies: @@ -3054,6 +2814,7 @@ snapshots: lightningcss-linux-x64-musl: 1.30.2 lightningcss-win32-arm64-msvc: 1.30.2 lightningcss-win32-x64-msvc: 1.30.2 + optional: true list-item@1.1.1: dependencies: @@ -3361,10 +3122,6 @@ snapshots: tabbable@6.3.0: {} - tailwindcss@4.1.18: {} - - tapable@2.3.0: {} - term-size@2.2.1: {} through2@2.0.5: From 917b53e43607599a6e35eacc1d76ef30578a196c Mon Sep 17 00:00:00 2001 From: Joshua Amaju Date: Mon, 13 Apr 2026 15:05:45 +0100 Subject: [PATCH 2/2] refactor: consolidate VirtualFocusAllAxis imports and remove unused index file --- .changeset/orange-stars-sin.md | 6 + .../src/VirtualFocus/AllAxis/define.ts | 2 +- .../src/VirtualFocus/AllAxis/index.ts | 2 +- .../{all-axis/index.ts => all-axis.ts} | 2 +- .../src/core/virtual-focus/all-axis/utils.ts | 360 ------------------ .../src/core/virtual-focus/utils.ts | 83 ---- 6 files changed, 9 insertions(+), 446 deletions(-) create mode 100644 .changeset/orange-stars-sin.md rename packages/components/src/core/virtual-focus/{all-axis/index.ts => all-axis.ts} (97%) delete mode 100644 packages/components/src/core/virtual-focus/all-axis/utils.ts diff --git a/.changeset/orange-stars-sin.md b/.changeset/orange-stars-sin.md new file mode 100644 index 0000000..59b8cb8 --- /dev/null +++ b/.changeset/orange-stars-sin.md @@ -0,0 +1,6 @@ +--- +"@valaria/components": minor +"valaria": minor +--- + +Use roving index core component for Virtual Focus element diff --git a/packages/components/src/VirtualFocus/AllAxis/define.ts b/packages/components/src/VirtualFocus/AllAxis/define.ts index d9f0414..eebf836 100644 --- a/packages/components/src/VirtualFocus/AllAxis/define.ts +++ b/packages/components/src/VirtualFocus/AllAxis/define.ts @@ -1,3 +1,3 @@ -import { VirtualFocusAllAxis } from "../../core/virtual-focus/all-axis/index.js"; +import { VirtualFocusAllAxis } from "../../core/virtual-focus/all-axis.js"; VirtualFocusAllAxis.define(VirtualFocusAllAxis.TAG_NAME); diff --git a/packages/components/src/VirtualFocus/AllAxis/index.ts b/packages/components/src/VirtualFocus/AllAxis/index.ts index d89c58c..934d229 100644 --- a/packages/components/src/VirtualFocus/AllAxis/index.ts +++ b/packages/components/src/VirtualFocus/AllAxis/index.ts @@ -8,4 +8,4 @@ export { * @category constructor */ VirtualFocusAllAxis, -} from "../../core/virtual-focus/all-axis/index.js"; +} from "../../core/virtual-focus/all-axis.js"; diff --git a/packages/components/src/core/virtual-focus/all-axis/index.ts b/packages/components/src/core/virtual-focus/all-axis.ts similarity index 97% rename from packages/components/src/core/virtual-focus/all-axis/index.ts rename to packages/components/src/core/virtual-focus/all-axis.ts index 1cd797b..5603769 100644 --- a/packages/components/src/core/virtual-focus/all-axis/index.ts +++ b/packages/components/src/core/virtual-focus/all-axis.ts @@ -1,7 +1,7 @@ import { isTabbable } from "tabbable"; import * as RovingIndex from "valaria/RovingIndex"; -import { VirtualFocus } from "../linear.js"; +import { VirtualFocus } from "./linear.js"; const defaultCoordinates: RovingIndex.Coordinates = { rowIndex: 0, diff --git a/packages/components/src/core/virtual-focus/all-axis/utils.ts b/packages/components/src/core/virtual-focus/all-axis/utils.ts deleted file mode 100644 index b360456..0000000 --- a/packages/components/src/core/virtual-focus/all-axis/utils.ts +++ /dev/null @@ -1,360 +0,0 @@ -type Direction = "right" | "left" | "up" | "down"; - -export function compute(viewport: Element, elements: Element[]) { - /** - * Uses a sliding window technique. Calculates how many items can - * fit within the viewport, taking into account gap between items. - * - * When no more items can fit the available space, we slide the window and perform - * the calculations again until a page is assigned to all nodes. - */ - let counter = 0; - const items = elements; - let page: Element[] = []; - let accumulated_size = 0; // usually width - let accumulator: Element[][] = []; - - const rect = viewport.getBoundingClientRect(); - - for (let i = 0; i < items.length; i++) { - const current = items[i]; - const previous = items[i - 1]; - - if (!current) continue; - - const current_rect = current.getBoundingClientRect(); - const previous_rect = previous ? previous.getBoundingClientRect() : null; - - // gap (usually left) from the previous element (if not first child) - let gap = - counter == 0 - ? 0 - : current_rect.left - - ((previous_rect?.left ?? 0) + (previous_rect?.width ?? 0)); - - if (gap < 0) gap = 0; - - // take into account spacing (width if horizontal) - const size = gap + current_rect.width; - - accumulated_size = accumulated_size + size; - - counter++; - - let inserted = false; - - // if new item can fit into container size - if (accumulated_size <= rect.width) { - page.push(current); - inserted = true; - } - - // else start a new page - if (accumulated_size >= rect.width) { - accumulated_size = inserted ? 0 : size; - accumulator = [...accumulator, page]; - page = inserted ? [] : [current]; - counter = 0; - } - } - - accumulator = [...accumulator, page]; - - return accumulator.filter((_) => _.length > 0); -} - -// export function findNextFocusable( -// elements: (HTMLElement | Element)[][], -// direction: Direction, -// { -// x, -// y, -// wrap = true, -// loop = false, -// }: Record<"x" | "y", number> & { loop?: boolean; wrap?: boolean }, -// ) { -// let x_ = x; -// let y_ = y; -// let count = 0; - -// const jump_to = (x: number, y: number) => { -// [x_, y_] = [x, y]; -// }; - -// const step_down = () => { -// y_ += 1; -// }; - -// const step_up = () => { -// y_ -= 1; -// }; - -// const step_left = () => { -// x_ -= 1; -// }; - -// const step_right = () => { -// x_ += 1; -// }; - -// const go_right = (wrap?: boolean) => { -// step_right(); - -// let focusable: Element | undefined | null = null; - -// while (true) { -// const row = elements[y_]; -// focusable = row?.[x_]; - -// // we've reached the end, either left or right -// if (!focusable) { -// // reverse the last operation -// if (row && x_ > row.length - 1) step_left(); - -// if (y_ >= elements.length - 1 && !loop) break; - -// if (wrap) { -// jump_to(0, y_ >= elements.length - 1 ? -1 : y_); -// return go_down("right"); -// } - -// break; -// } - -// if (isFocusable(focusable)) break; - -// step_right(); -// } - -// return focusable; -// }; - -// const go_left = (wrap?: boolean) => { -// step_left(); - -// let focusable: Element | undefined | null = null; - -// while (true) { -// focusable = elements[y_]?.[x_]; - -// // we've reached the end, either left or right -// if (!focusable) { -// // reverse the last operation -// if (x_ < 0) step_right(); - -// if (y_ <= 0 && !loop) break; - -// if (wrap) { -// jump_to(elements[y_]?.length ?? -1, y <= 0 ? elements.length : y_); -// return go_up("left"); -// } - -// break; -// } - -// if (isFocusable(focusable)) break; - -// step_left(); -// } - -// return focusable; -// }; - -// const go_down = (last_direction?: Direction): Element | null => { -// step_down(); - -// const last = elements[y]; -// const current = elements[y_]; - -// if (!current) { -// if (loop && count < 1) { -// count++; -// jump_to(0, -1); -// return go_down(); -// } else { -// step_up(); -// } - -// return null; -// } - -// const element = elements[y_]?.[x_]; - -// if (element && isFocusable(element)) { -// return element; -// } - -// let direction: Direction = last_direction ?? "right"; - -// if (last && current && current.length !== last.length) { -// if (x_ < current.length - 1) { -// // direction = "left"; -// } -// } - -// jump_to(direction == "right" ? -1 : current.length, y_); - -// const found = direction == "right" ? go_right(false) : go_left(false); - -// if (!found) return go_down(direction); - -// return found; -// }; - -// const go_up = (last_direction?: Direction): Element | null => { -// step_up(); - -// const last = elements[y]; -// const current = elements[y_]; - -// if (!current) { -// if (loop && count < 1) { -// count++; -// jump_to(0, elements.length); -// return go_up(); -// } else { -// step_down(); -// } - -// return null; -// } - -// const element = elements[y_]?.[x_]; - -// if (element && isFocusable(element)) { -// return element; -// } - -// let direction: Direction = last_direction ?? "right"; - -// if (last && current && current.length !== last.length) { -// if (x_ >= current.length - 1) { -// // direction = "left"; -// } -// } - -// jump_to(direction == "right" ? -1 : current.length, y_); - -// const found = direction == "right" ? go_right(false) : go_left(false); - -// if (!found) return go_up(direction); - -// return found; -// }; - -// switch (direction) { -// case "down": { -// return go_down(); -// } - -// case "up": { -// return go_up(); -// } - -// case "right": { -// return go_right(wrap); -// } - -// case "left": { -// return go_left(wrap); -// } -// } -// } - -export function findNextFocusable( - elements: (HTMLElement | Element)[][], - direction: Direction, - { - x, - y, - wrap = true, - loop = false, - }: Record<"x" | "y", number> & { loop?: boolean; wrap?: boolean }, -) { - const rows = elements.length; - - const go_right = (wrap?: boolean) => { - const row = elements[y]; - - if (row) { - if (x + 1 < row.length) return elements[y]?.[x + 1]; - if (y + 1 < rows) return elements[y + 1]?.[0]; - if (wrap) return elements[0]?.[0]; - return row[x]; - } - }; - - const go_left = (wrap?: boolean) => { - if (x - 1 >= 0) { - return elements[y]?.[x - 1]; - } - - if (y - 1 >= 0) { - const prev_row_length = elements[y - 1]?.length; - - if (prev_row_length) { - return elements[y - 1]?.[prev_row_length - 1]; - } - } - - if (wrap) { - const lastRow = rows - 1; - const row = elements[lastRow]; - if (row) return elements[lastRow]?.[row.length - 1]; - } - - return elements[y]?.[x]; - }; - - const go_down = () => { - if (y + 1 < rows) { - const row = elements[y + 1]; - if (row) return row?.[Math.min(x, row.length - 1)]; - } - - if (loop) { - const head = elements[0]; - - if (head) { - return head[Math.min(x, head.length - 1)]; - } - } - - return elements[y]?.[x]; - }; - - const go_up = () => { - if (y - 1 >= 0) { - const row = elements[y - 1]; - - if (row) { - return row?.[Math.min(x, row.length - 1)]; - } - } - - if (loop) { - const lastRow = rows - 1; - return elements[lastRow]?.[Math.min(x, elements[lastRow].length - 1)]; - } - - return elements[y]?.[x]; - }; - - switch (direction) { - case "down": { - return go_down(); - } - - case "up": { - return go_up(); - } - - case "right": { - return go_right(wrap); - } - - case "left": { - return go_left(wrap); - } - } -} diff --git a/packages/components/src/core/virtual-focus/utils.ts b/packages/components/src/core/virtual-focus/utils.ts index c46b6db..0bd9307 100644 --- a/packages/components/src/core/virtual-focus/utils.ts +++ b/packages/components/src/core/virtual-focus/utils.ts @@ -1,86 +1,3 @@ -// export function findNextFocusable( -// elements: Element[], -// currentIndex: number, -// direction: "forward" | "backward", -// loop = false, -// ) { -// let count = 0; -// const iterator = direction === "forward" ? 1 : -1; -// let next_index = currentIndex + iterator; - -// let focusable = null; -// let last_focusable_index = null; - -// while (true) { -// let element = elements[next_index]; - -// if (focusable) break; - -// if (!element) { -// if (loop && count < 1) { -// if (direction === "forward") { -// next_index = 0; -// } else { -// next_index = elements.length - 1; -// } - -// count++; - -// continue; -// } - -// break; -// } - -// if (isFocusable(element)) { -// last_focusable_index = next_index; -// focusable = element; -// continue; -// } - -// next_index += iterator; -// } - -// return next_index; -// } - -// export function findNextFocusable( -// elements: (HTMLElement | Element)[], -// currentIndex: number, -// direction: "forward" | "backward", -// loop = false, -// ) { -// let nextFocusable = null; - -// const iterator = direction === "forward" ? 1 : -1; -// let nextIndex = currentIndex + iterator; - -// while (currentIndex < elements.length) { -// nextFocusable = elements[nextIndex] || null; - -// if (nextFocusable === null) { -// // This is where wrapping happens. If we're moving forward and get to the end, then we jump to the beginning. If we're moving backward and get to the start, then we jump to the end. -// if (loop) { -// if (direction === "forward") { -// nextFocusable = elements[0]; -// } else { -// nextFocusable = elements[elements.length - 1]; -// } -// } - -// break; -// } - -// if (isFocusable(nextFocusable)) { -// break; -// } - -// nextIndex += iterator; -// } - -// return nextFocusable; -// } - export function findNextFocusable( elements: (HTMLElement | Element)[], currentIndex: number,