Skip to content

Edge nav controls drop keyboard focus to the document body when a press disables them #5602

Closed

Description

@cixzhang

Several components disable a previous/next control the moment its direction runs out. The browser drops focus off a control it disables, so a keyboard user who pressed the control that reached the edge lands on <body> and has to Tab from the top of the document to get back. WCAG 2.4.3 Focus Order.

Driven in Chromium against the current main Storybook build, one press per row until the edge:

story control result
core-pagination--default Go to next page focus on <body> after 9 presses, control disabled
core-calendar--min-max-boundary Next month focus on <body> after 2 presses, control disabled
core-lightbox--gallery Next focus on <body> after 1 press, control not disabled, so this one has a second cause worth finding
core-carousel--default Scroll right focus on <body> after 1 press, control disabled (fixed in #5601)

DateInput/TouchDateField.tsx uses the same isDisabled={!canStepBack} / isDisabled={!canStepForward} shape at its stepper and was not driven.

This is filed once rather than per component because no single component's PR closes it. Each site can patch itself, and #5601 did for Carousel, but the shape is the same everywhere and the repair wants to be shared: something that notices the control the user is on is about to be disabled and moves focus somewhere sensible first.

Two things #5601 learned the hard way, so the next fix does not repeat them.

The obvious receiver, the opposite button, does not work. At the moment of the press it is still disabled, because its enabling state lands with the next render, and focus() on a disabled control is a no-op. Carousel hands focus to its scroll container, which is a permanent tab stop inside the same labelled region. Components without such an element need a different answer, which is part of why this is a system question.

focus() scrolls its element into view by default. In a scrolling component that cancels the very scroll the press started, and the content never moves. Pass preventScroll: true.

A shared helper cannot be an effect: firing focus from an Effect that can re-run is its own defect. The prediction has to happen in the handler that caused the state change.

Activity

  1. added
    hardening
    on Aug 27, 2026
  2. HelloOjasMutreja commented on Sep 7, 2026

    @HelloOjasMutreja
    Contributor

    Read through Pagination.tsx to check what a shared fix would actually have to plug into. Its root is

    <nav
      ref={ref}
      {...mergeProps(themeProps('pagination', {variant, size}), ...)}
      aria-label={label}
      ...>

    a labelled landmark with no tabIndex, so it isn't focusable yet, but it is the same shape of thing Carousel's scroll container already is: a stable, always-present, labelled element belonging to the component itself rather than to whichever button happens to be enabled this render. Giving it tabIndex={-1} (a programmatic-only stop, not a new Tab stop) would make it a valid landing target the same way #5601 used Carousel's scroll container.

    Given that, and taking both #5601 lessons at face value, the shared piece looks like it could be small on purpose: a helper that just does the preventScroll: true focus call with a null-check, e.g.

    function focusBeforeDisabling(target: HTMLElement | null) {
      target?.focus({preventScroll: true});
    }

    called synchronously inside each component's own next/previous click handler, before the state update that will disable the control on the next render, passing in that component's own pre-existing labelled root or region, made focusable with tabIndex={-1} where it wasn't already. The per-component work is picking that target (<nav> for Pagination, presumably the grid or dialog surface for Calendar and Lightbox) and wiring the call into the existing click handlers, not a new abstraction each site has to learn.

    Two things I didn't verify yet, so asking rather than assuming: whether Calendar and Lightbox already have an equivalent stable labelled element to reuse the same way, and whether TouchDateField's stepper (flagged as not yet driven) turns out to need the exact same shape or something narrower given it's a compact stepper rather than a labelled region.

    If this direction sounds right, happy to send it as a small shared util plus the four call sites (Pagination, Calendar, Lightbox, TouchDateField), verified against real Chromium keyboard-only runs the same way the table in the issue was produced, rather than just source reading.

  3. HelloOjasMutreja commented on Sep 21, 2026

    @HelloOjasMutreja
    Contributor

    Drove the four remaining sites in Chromium against current main to confirm scope before proposing anything:

    • Pagination.tsx:838 / :857 — isDisabled={isDisabled || !hasPrevious} / !hasNext} on adjacent Previous/Next buttons, plus First/Last at :820/:864 sharing the same two booleans.
    • Calendar.tsx:539 / :560 — isDisabled={!canNavigatePrevious} / !canNavigateNext}, same shape as Carousel's pair.
    • Lightbox.tsx:650 / :718 — isDisabled={!canPrev} / !canNext}, same shape.
    • DateInput/TouchDateField.tsx:1000 / :1016 — isDisabled={!canStepBack} / !canStepForward}, same shape, and not driven in the original report.

    All four reproduce the same document.activeElement === BODY result the issue describes.

    What #5601 actually landed for Carousel

    Read Carousel.tsx:498-562 directly rather than working from the PR description alone. The shape is:

    • focusedNavRef ('start' | 'end' | null), set by each button's own onFocus, cleared by onBlur only when the button isn't the one currently disabled (Carousel.tsx:670-673) — otherwise the commit's own blur would wipe the tracker before the effect reads it.
    • prevCanScrollRef snapshots both sides' enabled state every render, so the effect can tell which side's false is a new transition versus one that was already disabled.
    • The effect (Carousel.tsx:518-562) fires on [canScrollStart, canScrollEnd], and only acts when: the tracked side just went enabled→disabled, activeElement is still that button or <body> (a real navigation elsewhere is left alone), and it clears the tracker before calling focus() so a re-render or StrictMode double-invoke can't re-fire it.
    • Receiver is the opposite button if it isn't itself disabled, else a documented fallback (Carousel's scroll container).
    • focus({preventScroll: true}), and the whole thing is deliberately an Effect — three attempts to predict the landing spot from the press itself were each wrong under some condition, and a maintainer ruled the Effect acceptable here specifically because it watches the state transition rather than the press, moves focus and nothing else, and cannot double-fire.

    What I'd propose extracting

    A shared hook, something like:

    useEdgeFocusHandoff({
      groups: [
        {key: 'start', controls: [firstButtonRef, previousButtonRef]},
        {key: 'end', controls: [nextButtonRef, lastButtonRef]},
      ],
      isGroupDisabled: {start: !hasPrevious, end: !hasNext},
      fallbackRef: undefined, // optional, e.g. Carousel's scroll container
    })

    returning per-control {onFocus, onBlur} props to spread onto each button, with the same transition-tracking/guard logic as Carousel's effect, generalized to a receiver search over a list of controls per side rather than a single button — Calendar/Lightbox/TouchDateField would each pass one control per group (identical to Carousel's shape); Pagination is the one site that actually needs the list, since First and Previous disable together.

    I'm not confident this is the right shape yet, on purpose. It's an internal state-transition-driven Effect touching four components, the single-component version took three rejected attempts, and the receiver-selection question (which control in Pagination's group is preferred as the landing spot, and in what order) is a real product decision I'd rather not guess at solo. Posting this as a proposal rather than a PR: happy to build useEdgeFocusHandoff (or a different shape you'd rather have) and wire up all four sites once the direction's confirmed, including regression coverage matching #5601's (edge press in both directions, RTL, reduced motion, and the case where every control in a group ends up disabled).

  4. cixzhang commented on Nov 7, 2026

    @cixzhang
    ContributorAuthor

    (Posted by Cindy's agent.) Closing: every site in this issue now keeps keyboard focus at the edge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    hardening

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions