AddThisFeature

Focus Containment Primitive

Keep keyboard focus inside an open overlay and return it where it started.

involved Components & Primitives

What it adds

A shared containment primitive that moves focus into an overlay, cycles it within, and restores it on close.

What your agent is told to do

5
  1. 1

    Find every overlay that takes over the screen — dialogs, drawers, command palettes, mobile navigation — and give all of them the same containment behaviour rather than each implementing its own.

  2. 2

    Record the element that had focus before the overlay opened, move focus to a sensible starting point inside it, and return focus to the recorded element when it closes.

  3. 3

    Recompute the set of focusable elements at the moment focus moves, not once when the overlay opens. Overlay content changes as fields appear, sections expand, and content loads.

  4. 4

    Track containment as a stack so a confirmation opened from a dialog contains focus to itself and hands containment back to its parent when dismissed.

  5. 5

    Do not hide the rest of the page from assistive technology by removing it from the accessibility tree and then forget to reverse it. A stale hidden state leaves the whole application silent to a screen reader after the overlay closes.

Edge cases it handles

7
  • The focusable elements inside an overlay change while it is open — asynchronously loaded content, conditionally rendered fields, a disabled submit that becomes enabled. Query the set live rather than caching a list captured at open time.
  • Overlays open other overlays. Containment must nest, so the innermost overlay holds focus and closing it restores containment to the one beneath rather than to the page.
  • Screen reader users navigate with virtual cursors and gestures that are not tab presses, and a containment that fights those tools traps them. Keep Escape working, keep the close control reachable, and do not attempt to intercept every mode of navigation.
  • The element that had focus before the overlay opened may be gone by the time it closes — the row was deleted, the list re-rendered, the trigger was itself inside the overlay. Fall back to a stable nearby container so focus never lands on the document body.
  • An overlay with no focusable content at all still needs a target, otherwise focus stays behind it and every keystroke goes to the page underneath.
  • Containment must not swallow browser-level shortcuts or prevent the user reaching the address bar; contain focus within the document, not within the browser.
  • Content inside the overlay rendered elsewhere in the DOM — a menu or popover portalled to the document — is visually inside but structurally outside. Treat it as part of the contained region or focus will escape whenever such a control opens.

Definition of done

9
  • Tab and Shift-Tab cycle only within the open overlay and never reach the page behind it.
  • Focus moves into the overlay on open and returns to the triggering element on close.
  • Focusable content added or removed while the overlay is open is included in the cycle immediately.
  • Nested overlays contain focus independently and hand containment back in order.
  • Escape and the visible close control both work in every overlay using the primitive.
  • A missing or removed trigger results in focus landing on a defined fallback, never on the document body.
  • The rest of the page is hidden from assistive technology only while an overlay is open, and is exposed again afterwards.
  • The feature matches the existing design system.
  • No existing functionality is broken.

Related features

How it works

  1. 1

    Copy the link

    Grab the Markdown instruction URL for this feature.

  2. 2

    Give it to your AI

    Paste it into Claude Code, Cursor, v0, Lovable — whatever you build with.

  3. 3

    It inspects, then implements

    Your agent reads your existing app first, then adds the feature to fit it.

Works with your stack

These instructions are written to adapt. They tell the agent to detect your framework, match your existing design system, and reuse what you already have — rather than assuming a particular stack.

Need it tighter than that? Customize the feature and tell it exactly what you're running.