Skip to content

fix(Drawer): stop the panel repainting into the page after close() - #5549

Merged
cixzhang merged 3 commits into
mainfrom
fix/drawer-close-repaint
Aug 27, 2026
Merged

cixzhang merged 3 commits into
mainfrom
fix/drawer-close-repaint

Conversation

@freddymeta

@freddymeta freddymeta commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

The bug

Dismiss a Drawer and the panel can snap back into the page for the tail of the close, over the content the user just dismissed it from.

Two things combine:

  1. dialog.close() ran on a hardcoded 250 ms timer with no relation to the panel's own transition. --duration-medium is a theme token — 410 ms in the shipped defaults, 250 ms in y2k, 300 ms in the theme Storybook renders with — so the timer is either early (slide cut short) or racing the boundary.
  2. The panel kept rendering after close(), held by the discrete display transition.

Once close() releases the top layer, the panel is an ordinary position: fixed box again — and any ancestor that establishes a containing block for fixed positioning (transform, filter, container-type, contain, backdrop-filter, will-change) becomes its origin. So the panel that had slid off the viewport edge reappears inside that ancestor and paints, shadow and all, until the hold ends.

That is not exotic: Storybook's own docs page has such an ancestor, and so does any card, virtualised list, or view-transition wrapper.

Repro

Lab/Drawer → Showcase, with one line added to the story root:

root.style.transform = 'translateZ(0)';

Open the drawer, close it, and watch the frames.

Before — origin/main @ 58f9542:

t (ms) dialog.open in top layer display panel x
0 true true flex 500
133 true true flex 872
200 true true flex 896
233 true true flex 899
250 false false flex 608

The panel is 291 px back inside the viewport, after the user dismissed it.

After — this branch:

t (ms) dialog.open in top layer display panel x
0 true true flex 500
133 true true flex 872
200 true true flex 896
233 true true flex 899
250 true true flex 900
318 false false none —

The slide now finishes (x reaches 900) and the panel is already hidden by the time it leaves the top layer. The probe that catches the offending frame reports never painted outside the top layer.

Screenshots

The dashed box is the ancestor carrying the transform. Drawer open:

drawer open

Before — the frame after close(), with the drawer already dismissed. The panel is back inside the page:

before: panel snaps back into the page

After — the same moment on this branch. There is no such frame; this is the first frame after close():

after: nothing paints

(Assets live on the assets/pr-5549 branch of this repo — asset-only, deletable with the PR. I have no fork.)

The fix

  • The transition is authoritative. The exit ends on the panel's own transitionend for transform, backstopped by the duration read off the element — so a theme that rewrites --duration-medium is respected rather than assumed. Same contract BottomSheetPanel.waitForTransition already uses, and it replaces the reduced-motion special case for free (the CSS already collapses to 0.01s).
  • The hide lands in the same task as close(), via flushSync. A plain setState is not enough — React's default scheduling can commit after the next paint, and that one frame is the bug. I measured this: with a plain setState the offending frame was still there at x=600.
  • React owns display for both legs, so the discrete display transition (and transition-behavior: allow-discrete) is gone. @starting-style still drives the entry; it applies whenever an element goes from not-rendered to rendered and does not need display in the transition.
  • The lifecycle is named and isolated. useDrawerDialogPresence now owns native open/close, waitForDrawerExit, computed-duration backstop, synchronous hide, focus restoration, and unmount cleanup. waitForDrawerExit follows BottomSheetPanel.waitForTransition: transitionend/transitioncancel are authoritative and the timeout only prevents a lost event from stranding an open dialog.

Start-side exit latch

This PR also fixes the side prop changing during the exit. The documented state-derived shape is:

<Drawer
  side={side ?? 'end'}
  isOpen={side != null}
  onOpenChange={isOpen => !isOpen && setSide(null)}
/>

Closing clears side, so the live prop reverted to end while the panel was still mounted for its exit. A drawer opened from start therefore teleported to the end edge and slid out right; its data-side theme state changed mid-exit too. The side is now latched while open and held until the exit completes — the same retention contract the docs already require for the drawer's children. The regression test asserts data-side="start" after the controlling state has cleared.

Notes

  • No API change; no .doc.mjs change. @astryxdesign/lab is private, so check:changesets rejects a changeset for it — none added.
  • Drive-by in the same test file: the container-padding test passed onClose, which is not a prop, so pnpm -F @astryxdesign/lab typecheck was already failing on main. One word, and the file typechecks.

Test plan

  • pnpm exec vitest run packages/lab/src/Drawer — 37 pass (34 before; closes on transitionend, ignores a transitionend for another property, and holds the opening side through exit; the existing delay test also asserts the drawer is still open at 250 ms).
  • pnpm -F @astryxdesign/lab typecheck — clean (red on main, see above).
  • pnpm lint:strict — 0 errors.
  • pnpm build, pnpm -F @astryxdesign/storybook build — green.
  • The existing CI Chromium guard now covers Drawer under a transformed ancestor. On this branch: close() ran at display:flex; hidden before the next paint. Negative control against origin/main: painted after close() outside the top layer — display:flex, open:false, x:624, width:374. The same guard keeps its existing MobileNav invariant green.

`dialog.close()` ran on a hardcoded 250ms timer that had nothing to do with
the panel's own transition, and the panel kept rendering afterwards on a
discrete `display` transition. For the rest of the hold it therefore painted
OUTSIDE the top layer, where any ancestor that establishes a containing block
for fixed positioning (`transform`, `filter`, `container-type`, `contain`)
becomes the origin of its `position: fixed` — so the panel snapped back into
the layout, over content the user had just dismissed it from.

Measured on `origin/main` with one `transform: translateZ(0)` on the story
root: the panel reaches x=899 at 233ms, then jumps back to x=608 at 250ms and
keeps painting until the hold ends.

The exit is now driven by the panel's real `transitionend` (backstopped by the
duration read off the element, so a theme that rewrites `--duration-medium`
is respected instead of assumed), and the hide is flushed in the same task as
`close()` so no frame is ever painted outside the top layer. React owns
`display` for both legs; the discrete `display` transition is gone.

Drive-by in the same file: the container-padding test passed `onClose`, which
is not a prop, so `pnpm -F @astryxdesign/lab typecheck` was already red on
main.
@vercel

vercel Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Aug 27, 2026 3:11am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 26, 2026
@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Aug 26, 2026
@github-actions

github-actions Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

Drawer (@astryxdesign/lab) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 463 -
Complexity N/A High (30) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/lab N/A 3.5KB 1.5KB

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

github-actions Bot added a commit that referenced this pull request Aug 26, 2026
Open the `Sides` story from `start`, then close it: the panel jumps to the
opposite edge and slides out to the right.

The panel stays mounted for the exit, so the `side` prop is still read during
it — and the idiomatic consumer shape derives `side` from the same state that
drives `isOpen`:

    side={side ?? 'end'}
    isOpen={side != null}

Closing clears that state, so `side` reverts to its default mid-exit and the
anchoring styles (`inset-inline-*`, the exit transform) flip under the panel.
Measured on the story: opened from `start`, the first frame of the close
already reads `left: 500px` and slides to +400px.

`side` is now latched at open and held for the exit, exactly as the children
are. Same rule as the documented "keep the last-selected item rendered on
close": what the panel shows while leaving is what it showed when open.
github-actions Bot added a commit that referenced this pull request Aug 26, 2026
@freddymeta
freddymeta marked this pull request as ready for review August 26, 2026 22:10

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for chasing this down. The matched close frames show the fix:

Before Head
Before: closed panel still paints Head: closed panel paints nothing

Two things before merge. The close now coordinates render state, a transition listener, timer, flushSync, and unmount cleanup, but the tests mock the dialog in jsdom. A future change can restore the same post-close() frame for anyone using a transformed ancestor while all 37 tests stay green. Could you move the close timing into a named hook or utility and add that Chromium regression? BottomSheetPanel.waitForTransition is the closest existing pattern.

Please also add the start-side latch to the PR body: builders using the documented state-derived side shape now get a different exit and theme-state value, but that second change is not described.

[Reviewed by Robohands]

The close fix coordinated native dialog state, React-rendered presence,
transition events, a computed-duration backstop, focus restoration and
unmount cleanup inline in Drawer. Move that lifecycle into
`useDrawerDialogPresence`, with `waitForDrawerExit` following the existing
`BottomSheetPanel.waitForTransition` pattern: transitionend/transitioncancel
are authoritative and a timeout only prevents a lost event from stranding an
open dialog.

Extend the existing real-browser `modal-close-visibility` CI guard with the
Drawer invariant. It gives the Storybook root a transform (the actual failure
condition), opens and closes the modal Drawer, then samples immediately before
the next paint. The test fails if the closed dialog is still rendered outside
the top layer.

Negative control against origin/main:
  Drawer painted after close() at display:flex, open:false, x:624, width:374

Head:
  close() ran at display:flex; hidden before the next paint

This closes the gap jsdom cannot cover: CSS transitions and the browser top
layer do not exist there, so every unit test stays green when the bad frame is
restored.
@github-actions
github-actions Bot requested a review from cixzhang August 27, 2026 03:08
@freddymeta

Copy link
Copy Markdown
Contributor Author

Addressed both asks in 0f5cf50.

  • Extracted the close lifecycle into useDrawerDialogPresence, with waitForDrawerExit following BottomSheetPanel.waitForTransition: transitionend/transitioncancel are authoritative; the timeout is only the computed-duration backstop. The hook now owns native dialog state, rendered presence, the synchronous post-close() hide, focus restoration, and unmount cleanup.
  • Extended the existing CI Chromium modal-close-visibility guard with Drawer under a transformed Storybook root. It samples immediately before the next paint after close():
    • head: close() ran at display: flex; hidden before the next paint
    • negative control against origin/main: painted after close() outside the top layer — [{"display":"flex","open":false,"x":624,"width":374}]
    • MobileNav's existing invariant stays green.
  • Added the start-side latch to the PR body, including the documented state-derived prop shape, the data-side theme-state effect, and its regression test.

Local gates: 37 Drawer tests, lab typecheck + docs typecheck, lint:strict, full build, Storybook build, and the Chromium guard all pass.

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks—both asks from my earlier review are addressed: the lifecycle is isolated with a passing Chromium regression, and the start-side behavior is now documented. Good to merge.

[Reviewed by Robohands]

@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Aug 27, 2026
@cixzhang
cixzhang merged commit 9ebd45f into main Aug 27, 2026
24 checks passed

This branch was successfully deployed

1 active deployment
Preview — 0f5cf509 Deployed Aug 27, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants