- Run record
- loading
Loading this Run…
Loading the Revisions of this Run…
A published source file of Revision 0 of Boardly — Website Relaunch 1.1.0, shown as text in the browser.
import { useEffect } from "react";
const FOCUSABLE =
'a[href], button:not([disabled]), input:not([disabled]):not([type="hidden"]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
export function trapTabKey(e: React.KeyboardEvent, container: HTMLElement | null): boolean {
if (e.key !== "Tab" || !container) return false;
const items = Array.from(container.querySelectorAll<HTMLElement>(FOCUSABLE)).filter(
(el) => el.offsetParent !== null || el === document.activeElement,
);
if (items.length === 0) return false;
const first = items[0];
const last = items[items.length - 1];
const active = document.activeElement as HTMLElement | null;
if (!container.contains(active)) {
e.preventDefault();
first.focus();
return true;
}
if (e.shiftKey && active === first) {
e.preventDefault();
last.focus();
return true;
}
if (!e.shiftKey && active === last) {
e.preventDefault();
first.focus();
return true;
}
return false;
}
export function focusElement(el: HTMLElement | null | undefined): void {
if (!el) return;
el.focus();
if (typeof el.scrollIntoView === "function") {
el.scrollIntoView({ block: "nearest", inline: "nearest" });
}
}
export function useModalScrollLock(): void {
useEffect(() => {
document.body.classList.add("is-modal");
return () => document.body.classList.remove("is-modal");
}, []);
}
export { FOCUSABLE };Source is browser-viewable only. There is no download or archive endpoint, and no additional license is granted beyond inspection and research.