FocusWithinController
Reports one enter when focus comes into an element and one leave when it goes outside.
Reach for it for a field or a card that reacts to focus anywhere inside it, however many elements it holds.
Import
import { FocusWithinController, createFocusWithinController } from '@ecopages/radiant-ui/controllers';Try it
Tab into the card, move between its two buttons, then Tab out. The status changes twice, not once per button.
Focus is outside
<div data-example="focus-within">
<div data-box>
<button type="button">First</button>
<button type="button">Second</button>
</div>
<p data-status>Focus is outside</p>
</div>import { FocusWithinController } from '@ecopages/radiant-ui/controllers';
export function mount(root: HTMLElement): () => void {
const card = root.querySelector<HTMLElement>('[data-box]')!;
const status = root.querySelector('[data-status]')!;
const focus = new FocusWithinController({
getRoot: () => card,
onEnter: () => {
status.textContent = 'Focus is inside';
},
onLeave: () => {
status.textContent = 'Focus is outside';
},
});
focus.attach();
return () => focus.destroy();
}mount runs once for each example element on the page, and the function it returns undoes it; see how the examples run.
Config
| Option | Description |
|---|---|
getRoot | The element whose focus is watched. Read on every event, so it may change while attached. |
onEnter | Called once when focus comes in from outside the root, with the focusin that brought it. |
onLeave | Called once when focus goes outside the root, with the focusout (or the focusin elsewhere) that settled it. |
Lifecycle
attach() joins one focusin and focusout listener pair on document that serves every attached controller; destroy() leaves it. A focusin is judged for every controller before any callback runs, and every leave runs before every enter, so with several controllers the one focus left reports its leave first, whatever order they attached in. attach() takes focus the root already holds, such as an autofocus element, as inside without calling onEnter; isFocused reads it. With a host, createFocusWithinController(host, config) does the same on connect and disconnect; see bindController.
What counts as inside
- Any descendant of the root, so moving focus between them reports nothing.
- A node a PortalController took out of the root, or out of a portaled node that is itself inside it. A select, combobox or date picker whose popup moved to
document.bodyis still part of its field, with no wiring of its own.DismissControllercounts a portaled popup as inside its surface by the same rule. - A press that starts inside the root holds back the leave of focus that goes to nothing. Pressing a label or other part that cannot take focus moves focus to nothing before the label's activation focuses its control again, so the leave waits until the click has finished, or the next press, key press, focus or window blur (releasing the pointer does not end the press, and focus coming back inside the root from outside it or from nothing clears it, while a move between its own parts keeps it): if focus is inside the root by then, the visit continues with no leave and no enter, otherwise the leave runs. No timer settles it, so a press that gets no click keeps the visit open until then. A press that starts outside the root, and focus that moves to another element, leave at once.
- A focus event is judged when it bubbles to the document, after the handlers of its ancestors, which may move focus on. A handler above the root that stops the event does not hide it: it is judged in the next task and dropped then if its target no longer holds focus.
- A
focusinwhose target is no longer the focused element is stale, because a handler moved focus on first; the move has its own event, which is the one that counts. - A
focusoutwith norelatedTargetwhiledocument.activeElementis still the element that blurred is the window or tab losing focus, not a leave. Focus that moves to nothing, as a press on the page background does, is a leave.