Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/orange-stars-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@valaria/components": minor
"valaria": minor
---

Use roving index core component for Virtual Focus element
3 changes: 3 additions & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,8 @@
},
"dependencies": {
"tabbable": "^6.3.0"
},
"peerDependencies": {
"valaria": "workspace:*"
}
}
2 changes: 1 addition & 1 deletion packages/components/src/VirtualFocus/AllAxis/define.ts
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion packages/components/src/VirtualFocus/AllAxis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export {
* @category constructor
*/
VirtualFocusAllAxis,
} from "../../core/virtual-focus/all-axis/index.js";
} from "../../core/virtual-focus/all-axis.js";
2 changes: 2 additions & 0 deletions packages/components/src/VirtualFocus/define.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "./LinearAxis/define.js";
import "./AllAxis/define.js";
80 changes: 80 additions & 0 deletions packages/components/src/core/virtual-focus/all-axis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { isTabbable } from "tabbable";
import * as RovingIndex from "valaria/RovingIndex";

import { VirtualFocus } from "./linear.js";

const defaultCoordinates: RovingIndex.Coordinates = {
rowIndex: 0,
columnIndex: 0,
};
export class VirtualFocusAllAxis extends VirtualFocus {
static override TAG_NAME = "va-virtual-focus-all-axis";

private coordinates = defaultCoordinates;

#grid() {
const elements = [...this.getChildren()] as HTMLElement[];

const grid: HTMLElement[][] = [];

const size = Number(this.getAttribute("size") ?? 4);

let index = 0;

while (index < elements.length) {
grid.push(elements.slice(index, index + size));
index += size;
}

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;
}
};

protected itemFocusOut = (e: Event) => {
e.stopPropagation();
this.coordinates = defaultCoordinates;
this.activeElement = undefined;
};

protected handleDocumentKeyDown = (event: KeyboardEvent) => {
if (
["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(event.key)
) {
const grid = this.#grid();

const loop = this.hasAttribute("loop");
const wrap = this.hasAttribute("wrap");

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();
}
}
76 changes: 0 additions & 76 deletions packages/components/src/core/virtual-focus/all-axis/index.ts

This file was deleted.

Loading
Loading