fix(ui): a late form takes a dialog's focus once, even if it arrived elsewhere
The watch stayed armed when the form arrived while the focus was off the close button, so a later change in the body pulled the focus off a close button the user went back to. It now disarms on arrival and re-arms only when the focus is taken away.
This commit is contained in:
@@ -106,9 +106,12 @@ export default function Modal({
|
||||
// A form that loads after the dialog opens (the issue composer shows a spinner first) has no field
|
||||
// to take the focus at open, so it lands on the ✕. When the first control arrives the focus moves
|
||||
// there — but only while it still rests on the ✕: a user who has moved it keeps it where it is.
|
||||
// The watch re-arms when the focused control is taken away (a failed load's Retry giving way to
|
||||
// the spinner again): the focus fell to <body>, so it waits on the ✕ for the form (E32 review).
|
||||
// Only <body>: focus in a dialog stacked on top is not this one's to take.
|
||||
// The form takes it ONCE: arrived while the focus was elsewhere, it disarms all the same, or a later
|
||||
// mutation would pull the focus off a ✕ the user went back to (E32 review round 2). The watch
|
||||
// re-arms only when the focus is taken away, not moved: the body has no control again, or the
|
||||
// focused one went (a failed load's Retry giving way to the spinner, or straight to the form) and
|
||||
// the focus fell to <body> — it then waits on the ✕ for the form (E32 review). Only <body>: focus
|
||||
// in a dialog stacked on top is not this one's to take.
|
||||
useEffect(() => {
|
||||
const body = bodyRef.current;
|
||||
if (initialFocus !== "field" || !body || focusablesIn(body).length > 0) return;
|
||||
@@ -119,9 +122,13 @@ export default function Modal({
|
||||
armed = true;
|
||||
}
|
||||
const first = focusablesIn(body)[0];
|
||||
if (!armed || !first || document.activeElement !== closeRef.current) return;
|
||||
if (!first) {
|
||||
armed = true;
|
||||
return;
|
||||
}
|
||||
if (!armed) return;
|
||||
armed = false;
|
||||
first.focus();
|
||||
if (document.activeElement === closeRef.current) first.focus();
|
||||
});
|
||||
watch.observe(body, { childList: true, subtree: true });
|
||||
return () => watch.disconnect();
|
||||
|
||||
Reference in New Issue
Block a user