summaryrefslogtreecommitdiff
path: root/shared/components/src/actions/focus-node.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/components/src/actions/focus-node.ts')
-rw-r--r--shared/components/src/actions/focus-node.ts19
1 files changed, 19 insertions, 0 deletions
diff --git a/shared/components/src/actions/focus-node.ts b/shared/components/src/actions/focus-node.ts
new file mode 100644
index 0000000..907f584
--- /dev/null
+++ b/shared/components/src/actions/focus-node.ts
@@ -0,0 +1,19 @@
+export default function focusNode(
+ node: HTMLElement,
+ focusedIndex: number | null,
+) {
+ const nodeIndex = Number(node.getAttribute('data-index'));
+
+ // Handle the initial focus when applicable
+ if (nodeIndex === focusedIndex) {
+ node.focus();
+ }
+
+ return {
+ update(newFocusedIndex) {
+ if (nodeIndex === newFocusedIndex) {
+ node.focus();
+ }
+ },
+ };
+}