MCPcopy Create free account
hub / github.com/error311/FileRise / trapFocusInModal

Function trapFocusInModal

public/js/main.js:444–469  ·  view source on GitHub ↗
(modal)

Source from the content-addressed store, hash-verified

442}
443
444function 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
471function applyModalA11yAttrs(modal) {
472 if (!modal) return;

Callers 1

handleModalOpenFunction · 0.85

Calls 2

getTopmostModalFunction · 0.85
getFocusableElementsFunction · 0.85

Tested by

no test coverage detected