示例工程 21-keyboard-focus-a11y

21-keyboard-focus-a11y/app/.tsc/src/lib/focus.js

javascript · 0.7 KB

export function focusableIds(items) {
    return items.filter((item) => !item.disabled).map((item) => item.id);
}
export function nextFocusIndex(items, current, delta) {
    const enabled = items
        .map((item, index) => ({ item, index }))
        .filter((entry) => !entry.item.disabled)
        .map((entry) => entry.index);
    if (enabled.length === 0)
        return -1;
    const position = enabled.indexOf(current);
    const base = position === -1 ? (delta === 1 ? -1 : 0) : position;
    const next = (base + delta + enabled.length) % enabled.length;
    return enabled[next] ?? enabled[0] ?? -1;
}
export function trapTabIndex(items, current, shift) {
    return nextFocusIndex(items, current, shift ? -1 : 1);
}
//# sourceMappingURL=focus.js.map