summaryrefslogtreecommitdiff
path: root/shared/components/src/actions/focus-node.ts
blob: 907f5846f1e1eaac50e665a8a96d41fb8f87183c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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();
            }
        },
    };
}