-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfocusManager.ts
More file actions
16 lines (16 loc) · 900 Bytes
/
Copy pathfocusManager.ts
File metadata and controls
16 lines (16 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Focus management utilities
export function getFocusableElements(container: HTMLElement): HTMLElement[] {
const selectors = 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=""-1""])';
return Array.from(container.querySelectorAll(selectors));
}
export function saveFocus(): () => void {
const activeElement = document.activeElement as HTMLElement | null;
return () => activeElement?.focus();
}
export function moveFocus(direction: 'next' | 'prev'): void {
const focusable = getFocusableElements(document.body);
const currentIndex = focusable.indexOf(document.activeElement as HTMLElement);
if (currentIndex === -1) return;
const nextIndex = direction === 'next' ? (currentIndex + 1) % focusable.length : (currentIndex - 1 + focusable.length) % focusable.length;
focusable[nextIndex]?.focus();
}