Skip to content

[collapsible][accordion] Expand hiddenUntilFound panels synchronously on beforematch - #5514

Merged
aarongarciah merged 6 commits into
mui:masterfrom
aarongarciah:fix/hidden-until-found-highlight
Sep 29, 2026
Merged

aarongarciah merged 6 commits into
mui:masterfrom
aarongarciah:fix/hidden-until-found-highlight

Conversation

@aarongarciah

@aarongarciah aarongarciah commented Aug 14, 2026 •

Copy link
Copy Markdown
Member

Closes #5513.

Expands hiddenUntilFound Collapsible and Accordion panels synchronously during beforematch, allowing Chrome and Safari to highlight find-in-page matches when transition CSS keeps the closed panel at zero height.

If a controlled reveal is canceled or ignored, the panel restores its collapsed styles and hidden="until-found" state without consuming the next normal opening animation.

Covered by the Collapsible and Accordion suites in jsdom and Chromium, and manually verified in Chrome 151.

…atch

The browser removes `hidden` and measures the find-in-page match in the same
task that dispatches `beforematch`, while the React state update only lands in
a later one. Panels kept collapsed by persisted starting styles were still
zero-sized at that point, so the match highlight was dropped even though the
panel opened correctly.

Drop those styles synchronously from the event handler so the panel geometry is
correct before the browser measures.

Co-Authored-By: Claude Fable 5 <[email protected]>
@pkg-pr-new

pkg-pr-new Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@5514
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5514
    

commit: 7e401d5

@aarongarciah aarongarciah added component: collapsible Changes related to the collapsible component. component: accordion Changes related to the accordion component. labels Aug 14, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+275B(+0.06%) 🔺+91B(+0.06%)

Details of bundle changes

Performance

Total duration: 1,115.88 ms -18.72 ms(-1.6%) | Renders: 76 (+0) | Paint: 1,862.15 ms -40.04 ms(-2.1%)

Test Duration Renders
Checkbox mount (500 instances) 83.56 ms 🔺+14.27 ms(+20.6%) 1 (+0)

14 tests within noise — details

Metric alarms

Test Metric Change
Checkbox mount (500 instances) bench:paint 🔺 +21.84 ms

Check out the code infra dashboard for more information about this PR.

@aarongarciah aarongarciah changed the title [collapsible] Expand hiddenUntilFound panels synchronously on beforematch [collapsible][accordion] Expand hiddenUntilFound panels synchronously on beforematch Aug 14, 2026
@aarongarciah
aarongarciah marked this pull request as ready for review August 14, 2026 14:28
@netlify

netlify Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 7e401d5
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6abb3b8ce0d2af0007c92e84
😎 Deploy Preview https://deploy-preview-5514--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

aarongarciah and others added 3 commits August 14, 2026 16:50
…ored

Removing `data-starting-style` imperatively desyncs the DOM from React, which
renders the same prop for as long as the panel stays closed and therefore never
rewrites the attribute. A controlled panel whose `onOpenChange` is ignored was
left permanently expanded while still reporting `data-closed`.

Put the attribute back on the next frame when the open state never arrived, once
the browser has already measured the match. Also bind the handler to the same
element the listener is attached to instead of re-reading the ref.

Co-Authored-By: Claude Fable 5 <[email protected]>
`beforematch` marks the next open as motionless before it knows the open will
happen. When a consumer declines that specific open, the mark survived and the
following ordinary open lost its author-defined transition.

Clear it alongside the restored collapsed styles, in the branch that already
knows the open never arrived.

Co-Authored-By: Claude Fable 5 <[email protected]>
The motion-skip reset only ran when the panel had persisted starting styles to
restore, so keyframe panels — which stop carrying `data-starting-style` once
the animation type is known — kept losing the next open's animation after an
ignored reveal. Schedule the revert frame unconditionally and make only the
attribute restoration conditional.

The frame now also puts back `hidden="until-found"`, which the browser removes
as part of the reveal and React never rewrites: without it, a canceled or
ignored reveal left the panel out of the find-in-page index (and, for ignored
transition panels, concealed only by `height: 0`) until the next open/close
cycle. Canceled reveals schedule the same frame so the panel stays hidden and
searchable, matching the cancellation intent.

Co-Authored-By: Claude Fable 5 <[email protected]>
@aarongarciah

Copy link
Copy Markdown
Member Author

PR review

Bugs (1)

1. ℹ️ If React's commit loses the race to the revert frame, a reveal degrades to pre-PR behavior

Location: packages/react/src/collapsible/panel/useCollapsiblePanel.ts:374

revertRevealFrame = AnimationFrame.request(() => {
  if (latestOpenRef.current) {
    return;
  }
  ...

The frame decides "the open never arrived" by reading latestOpenRef, which is written during React's commit. React schedules that commit as a macrotask (scheduler postMessage) in the same task as the event, so it virtually always lands before the next rendering update's rAF callbacks — measured earlier in this PR's investigation as arriving by macrotask turn 2. But that ordering is not guaranteed: under severe main-thread contention the commit can slip past the frame, in which case an honored reveal gets reverted (re-hidden, starting styles back, flag cleared) one beat before the commit re-opens it — net effect: the match highlight is lost and the open animates, i.e. exactly master's current behavior, self-corrected by the commit. This race was born in dc6dde640, not this commit; the new hidden restore doesn't widen it (React rewrites hidden on the open commit because the prop value actually changes). Not realistically reachable in normal usage, no state is left inconsistent, and the failure mode is the status quo — recording it so the invariant ("frame must run after commit") is written down rather than implicit.

Failure scenario: A page janking hard enough that a postMessage task queued during beforematch hasn't run by the next vsync; the user sees that one reveal without a highlight, as today.

Fix: None needed now. If it ever surfaces, the frame can double-defer (schedule the check one frame later) or verify against a commit counter instead of a single ref read.

Verdict

Approve - all prior findings resolved and verified non-vacuously; the one remaining note is an unreachable-in-practice race whose failure mode is current production behavior.


🤖 Review generated with Claude Code

…ming

The revert frame read the committed open state at the next animation frame,
which races React's scheduled commit: a rendering update can preempt the
still-queued commit task and make the frame revert a reveal whose open was on
the way.

Decide in a task instead. `setOpen` has already scheduled React's work by the
time the timeout is queued, so the decision drains after the commit even when
a busy main thread delays both, and when the consumer ignores the open no
commit is ever scheduled, so the decision holds. The DOM revert still runs in
an animation frame so it stays atomic with respect to paint.

Co-Authored-By: Claude Fable 5 <[email protected]>
@aarongarciah

Copy link
Copy Markdown
Member Author

PR review

Head 0d889eccd. This round I attacked the new task-ordered decision stage specifically: double-beforematch restart semantics (Timeout.start replaces the pending callback whole, params are closure-scoped, stale frames stay idempotent and guarded), StrictMode double-registration (per-effect Timeout instances, each cleaned up), jsdom (listener registers but the event can never fire; timeout cleared on unmount), background-tab timer clamping (unreachable — find-in-page implies a foreground tab), test flake (4× consecutive stable runs, and the switch to waitFor removed the frame-count assumption the new hop invalidated), and bundle cost (Timeout already ships via toast).

No findings.

One residual, known and accepted rather than fixable: setTimeout and React's scheduler use different task sources, and the spec technically lets an engine drain them in either order. Engines drain messages first (the safe direction), the rAF's second latestOpenRef check covers the inversion, and the compounded worst case remains the self-healing one-frame degradation to master behavior. There is no event for "the consumer will never respond," so this floor is structural.

Verdict

Approve - the decision race is closed by task ordering, every prior finding is resolved with non-vacuous coverage, and CI is green on the final head.


🤖 Review generated with Claude Code

@aarongarciah
aarongarciah removed the request for review from colmtuite August 17, 2026 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: accordion Changes related to the accordion component. component: collapsible Changes related to the collapsible component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[collapsible][accordion] Find-in-page match is not highlighted on first reveal when a hiddenUntilFound panel uses CSS transitions

2 participants