Primitives / DOM

Mounts for focus

Autofocus() focuses the element on insert, then emits Focused: requested, not landed, since a non-focusable element may decline.

FocusScope({ contain?, restore?, initialFocus? }) keeps focus inside an overlay. On insert the container focuses initialFocus, else its first tabbable descendant, else itself. With contain (default), Tab from the last tabbable wraps to the first, Shift+Tab from the first wraps to the last, and focus that lands outside comes straight back; Tab in the middle is the browser's. On unmount, with restore (default), focus goes back to the element that was focused at mount; when that is gone and focus went with the scope (it is on body, or inside), to a place chosen by the rules under FocusReturn below. Focus the user moved elsewhere stays, and a scope opened with focus on nothing gives nothing back. Scopes and HideOutside share one stack of layers, in the order they opened, and the topmost owns containment: a scope with a layer open above it contains nothing, so a second dialog drawn beside the first (as a dialog portaled to the body is) keeps its focus, and the first contains again when it closes. A native <dialog> does all of this itself; this is for a custom overlay, a menu, or a command palette. tabbableWithin(element) is exported: focusable, visible descendants with a non-negative tabindex, in order. Its Behavior form is in interaction.

FollowTabStop moves focus within a container to its roving tab stop when a transition the container did not see moves it or removes the focused row. It never takes focus from outside the container. TreeNavigation uses it. On a container marked data-fk-roving it reads only the stops no nested roving container holds, so a row that holds another roving widget keeps that widget's stop. RovingTabindex, ListNavigation, GridNavigation and TreeNavigation write the mark on their containers; it is no focus owner, so a FocusRecovery around a tree still owns the rows inside it.

FocusRecovery({ fallback }) moves focus to a declared fallback when the marked part that had it is removed, hidden, made inert or disabled, or moved in the tree, in the same task as the change. fallback maps a part slot to its steps (same, next, previous, parent, stop, { part }, owner); the marks it reads (data-fk-focus-owner, -part, -key, -parent) are drawn by its Behavior. It never acts on focus that moved elsewhere. The Behavior form and the steps are in interaction.

FocusReturn() gives an overlay whose own restoring is not ours (a native <dialog> returns to its opener, and to body when that is gone) a way back. It reads what had focus before the overlay took it when the overlay mounts, focuses nothing, and, when the overlay unmounts with focus lost (on body, or inside the overlay), focuses the first of these that can take it:

  1. the opener itself;
  2. an element with the opener's id, for an opener drawn again as a new element;
  3. when the opener was a part of a FocusRecovery owner, that owner's fallback steps for its slot (same, next, previous, parent, stop, { part }, owner), read against the parts as they stand then. Its neighbours are the ones seen when the overlay opened: a row added while the overlay was open is not one; the step logic is lent by the FocusRecovery module, so a page that never imports it carries none of it, and has no owner to ask;
  4. the layer below, the one opened just before this one and still open: its first tabbable element, else itself;
  5. the first tabbable element of the page.

Focus the user moved elsewhere is left alone, and the opener is focused without scrolling the page to it. FocusScope restores by the same rules, with its opener read from activeElement alone. FocusReturn is a layer of the stack while mounted, so the layers opened before it read as covered (a FocusScope below contains nothing meanwhile, and a HideOutside below leaves the overlay shown), and takes the opener from the last elements that had focus, not only activeElement, which a Command that shows a dialog has usually moved already. That trail is one focusin listener on the page's document, installed when the FocusReturn module loads (a frame's document is followed from its first FocusReturn), and holds its elements weakly; a page with only FocusScope or FocusRecovery installs none.

ScrollLock locks the document's scroll while its element is mounted, refcounted so nested overlays release together, with Foldkit's iOS handling; HideOutside marks everything outside its element inert while mounted, keyed by an id the Mount mints so two overlays restore independently, and never a layer opened after its own, wherever that is drawn: a dialog portaled into a container the first had hidden is shown a frame after it opens, and its scope then takes focus. Both are Foldkit's own Dom.lockScroll and Dom.inertOthers as Mounts. A native <dialog> shown modally needs neither.