* Handle keyboard navigation within modal (focus trap)
(e: KeyboardEvent, modal: HTMLElement)
| 489 | * Handle keyboard navigation within modal (focus trap) |
| 490 | */ |
| 491 | function handleModalKeydown(e: KeyboardEvent, modal: HTMLElement): void { |
| 492 | if (e.key === "Tab") { |
| 493 | const focusableElements = getFocusableElements(modal); |
| 494 | if (focusableElements.length === 0) return; |
| 495 | |
| 496 | const firstElement = focusableElements[0]; |
| 497 | const lastElement = focusableElements[focusableElements.length - 1]; |
| 498 | |
| 499 | if (e.shiftKey) { |
| 500 | // Shift+Tab: if on first element, wrap to last |
| 501 | if (document.activeElement === firstElement) { |
| 502 | e.preventDefault(); |
| 503 | lastElement.focus(); |
| 504 | } |
| 505 | } else { |
| 506 | // Tab: if on last element, wrap to first |
| 507 | if (document.activeElement === lastElement) { |
| 508 | e.preventDefault(); |
| 509 | firstElement.focus(); |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Setup modal functionality |
no test coverage detected