Files
TREK/client/src/mobile/components/modalFocus.ts
T
Maurice 508e17bc11 fix(assistant): the phone chat holds the focus as a dialog, and its lists close on Escape
The full screen chat is a modal dialog but left the focus on the trip behind
it. It now takes the focus when shown, keeps Tab inside and hands the focus
back when set aside or closed, through the helper MSheet now shares. The
dropdown panels take the focus onto their first entry, close on Escape and
give the focus back to their button; the history and menu buttons say
whether their panel is open, and the open chat is announced as current.
2026-10-24 02:06:24 +00:00

42 lines
1.5 KiB
TypeScript

import { type KeyboardEvent, type RefObject, useEffect } from 'react';
const FOCUSABLE =
'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
/**
* Keeps Tab inside a modal panel: past its last control it goes round to the
* first, and Shift+Tab from the first one (or from the panel itself) to the
* last. A panel with nothing to focus keeps the focus where it is.
*/
export function keepTabInside(e: KeyboardEvent, panel: HTMLElement | null): void {
if (e.key !== 'Tab' || !panel) return;
const focusable = panel.querySelectorAll<HTMLElement>(FOCUSABLE);
if (focusable.length === 0) {
e.preventDefault();
return;
}
const first = focusable[0];
const last = focusable[focusable.length - 1];
if (e.shiftKey && (document.activeElement === first || document.activeElement === panel)) {
e.preventDefault();
last.focus();
} else if (!e.shiftKey && document.activeElement === last) {
e.preventDefault();
first.focus();
}
}
/**
* While `shown`, the focus sits on the modal panel (which takes it with
* `tabIndex={-1}`); once it is no longer shown, the focus goes back to where
* it was before.
*/
export function useModalFocus(panel: RefObject<HTMLElement | null>, shown: boolean): void {
useEffect(() => {
if (!shown) return;
const previous = document.activeElement as HTMLElement | null;
panel.current?.focus();
return () => previous?.focus();
}, [panel, shown]);
}