A shared hook, useDialogFocus, gives a modal window its focus: on its X
once mounted, Tab and Shift+Tab looped inside by an onKeyDown handler on
the dialog element (never on document, where Escape is listened for),
and given back in the effect's cleanup, so on every way out: to
returnFocusTo when the host passes it and it holds an element, otherwise
to the element active at mount, unless that one is body or no longer in
the document.
- UpsellDialog: refs on its X and on the dialog element, an optional
returnFocusTo, the Tab handler; the Escape listener is unchanged.
- ProfileSwitcher: its trigger gets a ref, passed to UpsellDialog and
ProfileFormModal, type="button" and aria-expanded={open}, without
aria-haspopup since the list is not a role="menu".
- TransactionTable: an effect, placed before the early return of an
empty table, clears showSplitUpsell once the access is open, so an
upsell that went away with the edition never comes back on its own.
- ProfileFormModal: role="dialog", aria-modal, aria-labelledby to its
title (static id), aria-label and type="button" on its X, Escape that
closes like the X except while saving, the same focus hook; entering
the upsell puts the focus on the X, every return to the list (cancel,
save, deletion) on "Create", never at mount.
Tests: useDialogFocus.test.ts and ProfileFormModal.render.test.tsx are
new; UpsellDialog, ProfileSwitcher, TransactionTable and the deletion
of ProfileFormModal get their cases. Fakes reach the refs through
props.ref, never through a preset slot of the harness.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
74 lines
3.2 KiB
TypeScript
74 lines
3.2 KiB
TypeScript
import { useEffect, type KeyboardEvent as ReactKeyboardEvent, type RefObject } from "react";
|
|
|
|
/**
|
|
* What can take the focus inside a dialog, for the Tab loop (#494): a disabled
|
|
* button or field is left out, as the browser leaves it out of the Tab order.
|
|
*/
|
|
export const FOCUSABLE_SELECTOR =
|
|
'button:not([disabled]), [href], input:not([disabled]), select, textarea, [tabindex]:not([tabindex="-1"])';
|
|
|
|
export interface DialogFocusRefs {
|
|
/** The `role="dialog"` element: Tab and Shift+Tab loop through its focusable elements. */
|
|
dialog: RefObject<HTMLElement | null>;
|
|
/** What takes the focus once the dialog is mounted: its close button. */
|
|
initialFocus: RefObject<HTMLElement | null>;
|
|
/**
|
|
* What takes the focus back when the dialog goes away, when the host has it:
|
|
* it wins over the element that was active at mount. ProfileSwitcher passes
|
|
* its trigger, since the menu item that opened the dialog is gone by then.
|
|
*/
|
|
returnFocusTo?: RefObject<HTMLElement | null>;
|
|
}
|
|
|
|
/** The element active at mount, if the focus can go back to it: neither `body` nor out of the document. */
|
|
function returnable(element: Element | null): HTMLElement | null {
|
|
if (!element || element === document.body || !element.isConnected) return null;
|
|
return element as HTMLElement;
|
|
}
|
|
|
|
/**
|
|
* The focus of a modal window (#494), shared by UpsellDialog and
|
|
* ProfileFormModal.
|
|
*
|
|
* - Once mounted, the dialog remembers the active element and puts the focus
|
|
* on `initialFocus`.
|
|
* - When it goes away, the focus goes to `returnFocusTo` when the host passed
|
|
* it and it holds an element, otherwise to the element remembered at mount,
|
|
* unless that one is `body` or no longer in the document. This runs in the
|
|
* effect's cleanup, so it also covers a dialog its host unmounts without
|
|
* calling `onClose` (the split upsell once the edition is Base).
|
|
* - The returned `onKeyDown`, set on the dialog element (never on `document`,
|
|
* where the dialogs listen for Escape), makes Tab on the last focusable
|
|
* element go to the first, and Shift+Tab on the first go to the last.
|
|
*
|
|
* `useEffect` is its only hook: the hook harness of the tests throws on a
|
|
* generated id or a layout effect (src/test/hookHarness.ts). The refs are
|
|
* stable objects, read when the effect runs and when it is cleaned up, hence
|
|
* the empty dependency list: mount and unmount only.
|
|
*/
|
|
export function useDialogFocus({ dialog, initialFocus, returnFocusTo }: DialogFocusRefs) {
|
|
useEffect(() => {
|
|
const opener = document.activeElement;
|
|
initialFocus.current?.focus();
|
|
return () => {
|
|
(returnFocusTo?.current ?? returnable(opener))?.focus();
|
|
};
|
|
}, []);
|
|
|
|
const onKeyDown = (event: ReactKeyboardEvent<HTMLElement>) => {
|
|
if (event.key !== "Tab") return;
|
|
const focusable = dialog.current?.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR);
|
|
if (!focusable || focusable.length === 0) return;
|
|
const first = focusable[0];
|
|
const last = focusable[focusable.length - 1];
|
|
if (event.shiftKey && event.target === first) {
|
|
event.preventDefault();
|
|
last.focus();
|
|
} else if (!event.shiftKey && event.target === last) {
|
|
event.preventDefault();
|
|
first.focus();
|
|
}
|
|
};
|
|
|
|
return { onKeyDown };
|
|
}
|