(modal)
| 442 | } |
| 443 | |
| 444 | function trapFocusInModal(modal) { |
| 445 | if (!modal || modal.__frTrapBound) return; |
| 446 | modal.__frTrapBound = true; |
| 447 | modal.addEventListener('keydown', (e) => { |
| 448 | if (e.key !== 'Tab') return; |
| 449 | if (getTopmostModal() !== modal) return; |
| 450 | const focusables = getFocusableElements(modal); |
| 451 | if (!focusables.length) { |
| 452 | e.preventDefault(); |
| 453 | modal.focus(); |
| 454 | return; |
| 455 | } |
| 456 | const first = focusables[0]; |
| 457 | const last = focusables[focusables.length - 1]; |
| 458 | const active = document.activeElement; |
| 459 | if (e.shiftKey) { |
| 460 | if (active === first || !modal.contains(active)) { |
| 461 | e.preventDefault(); |
| 462 | last.focus(); |
| 463 | } |
| 464 | } else if (active === last || !modal.contains(active)) { |
| 465 | e.preventDefault(); |
| 466 | first.focus(); |
| 467 | } |
| 468 | }, true); |
| 469 | } |
| 470 | |
| 471 | function applyModalA11yAttrs(modal) { |
| 472 | if (!modal) return; |
no test coverage detected