Skip to content

Commit 4d7b11d

Browse files
authored
Merge pull request #19 from maitamdev/feat/focus-manager
feat(a11y): add focus manager
2 parents e651fcd + b920f19 commit 4d7b11d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/utils/focusManager.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Focus management utilities
2+
export function getFocusableElements(container: HTMLElement): HTMLElement[] {
3+
const selectors = 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=""-1""])';
4+
return Array.from(container.querySelectorAll(selectors));
5+
}
6+
export function saveFocus(): () => void {
7+
const activeElement = document.activeElement as HTMLElement | null;
8+
return () => activeElement?.focus();
9+
}
10+
export function moveFocus(direction: 'next' | 'prev'): void {
11+
const focusable = getFocusableElements(document.body);
12+
const currentIndex = focusable.indexOf(document.activeElement as HTMLElement);
13+
if (currentIndex === -1) return;
14+
const nextIndex = direction === 'next' ? (currentIndex + 1) % focusable.length : (currentIndex - 1 + focusable.length) % focusable.length;
15+
focusable[nextIndex]?.focus();
16+
}

0 commit comments

Comments
 (0)