[Bug Fix] Sheet: render a native <dialog> and close on Escape
TV
3mo ago+374+374+012345678901234567890123456789-117-117-012345678901234567890123456789
Problem
SheetContentrendered a<template>that the controller cloned to the end of<body>on open: no Escape, no focus trap/restore, the page behind stayed interactive, and re-cloning the panel threw away anything the user had typed into it.- Gaps against the shadcn Sheet API: no
SheetClose, noshowCloseButtonequivalent (soMobileSidebarhid the corner button with a[&>button]:hiddenhack), no default width (every docs example had to passsm:max-w-smby hand), and nodata-sideto target one side from the outside.
Change
- Native
<dialog>rendered in place, opened withshowModal()— top layer, inert background, focus trap and focus return from the platform. No template/clone. In shadcn all four registry variants build Sheet from the Dialog primitive ("Extends the Dialog component"), which is where these come from there. - Escape closes:
cancelis intercepted so the exit animates first, thendialog.close(). The controller waits on the exit animations themselves, as Dialog does in #517:close()setsdata-state="closed", collects the panel's and the::backdrop'sCSSAnimations (getAnimations({subtree: true})on the<dialog>, filtered to its owneffect.target), waits on theirfinishedpromises with a token per run so a superseded run cannot settle a later one, then callsclose(). This differs from theanimationendblock of #506 becausegetAnimations()alone never lists::backdropand the backdrop's events land on the<dialog>under the same keyframe names: that block closed on whichever exit ended first, and a staleanimationcancelfrom a previous run could close a new one early. A second Escape mid-exit is left to the browser (non-cancelable in Chrome anyway); a close the controller did not start settles the pending run. - The backdrop gets
backdrop:duration-300so it fades out in step with the panel's 300 ms exit (--tw-durationis registeredinherits: false, so::backdropdoes not pick the panel's duration up). The controller does not depend on the two matching: aduration-*override onSheetContentcloses after the longer of the two. - The body scroll lock is released only when no modal
<dialog>is left open (dialog:modal), so a Sheet nested in a Dialog, or one disconnecting behind another, keeps the page locked. - Backdrop click closes (shadcn parity). On a
<dialog>a click on the panel's own padding targets the same element, sobackdropClickhit-tests the panel box and ignores it. - UA
<dialog>resets: a modal dialog is pinned to all four edges byinset: 0, so each side releases the opposite one, plusm-0 max-w-full max-h-full.not-open:hiddenkeeps a caller's bareflexfrom overriding the UAdisplay: nonewhen closed — the docs' own theme sheet passes one. - API parity, closed here rather than in a follow-up since the component was being rewritten anyway:
SheetClosewrapper,show_close_button:onSheetContent(replaces the[&>button]:hiddenhack), defaultw-3/4 sm:max-w-smfor left/right, anddata-sideon the panel. A caller's own classes still win throughtailwind_merge. - Both controllers stay:
ruby-ui--sheet#openon the wrapper,ruby-ui--sheet-content#closeon the dialog, so the public action strings apps already have in their markup keep working. - Docs: the Side example now shows all four sides, plus a new "No close button" section.
- Unchanged on purpose: backdrop stays
bg-background/80 backdrop-blur-sm(consistent with Dialog/AlertDialog, not shadcn'sbg-black/50), and header/footer padding stays on the panel — moving it would reflow every existing sheet. mcp/data/registry.jsonrebuilt (separate commit).
Test
cd gem && bundle exec rake— 15 tests insheet_test.rb.- Manual, on the Sheet docs page:
- Open → panel slides in over 500 ms, backdrop fades in, page scroll is locked.
- Escape / the corner X / the footer's Cancel → exit animation plays, then it closes, focus returns to the trigger, scroll is restored.
- Click the backdrop → closes. Click the panel's own padding next to the content → stays open.
- All four sides, the "No close button" example (Escape and the footer Close still work), and the mobile menu on a narrow viewport.
- Timing and geometry checked in headless Chrome (CDP): exit reaches
animationendat 300 ms on both panel and::backdropandclosefires once; reopen mid-exit comes back; reopen + close again inside one frame still plays the full 300 ms exit (25 ms before the controller change); aduration-*override on the panel or the backdrop closes after the longer one;requestClose()mid-exit closes at once and a second Escape mid-exit (non-cancelable) settles; removal mid-exit and an externalclose()both leave scroll unlocked; each side lands on its edge, and the default width resolves tomin(75vw, 24rem).
🤖 Generated with Claude Code
Summary by cubic
Renders SheetContent as a native <dialog> opened with showModal() instead of cloning a <template> to the end of <body> — the page behind is now inert, focus is trapped and restored, Escape closes the sheet, and reopening no longer throws away typed content. The existing ruby-ui--sheet#open and ruby-ui--sheet-content#close action strings keep working unchanged.
Bug Fixes
- Escape is intercepted so the exit animation plays before the dialog closes; a second Escape mid-exit settles correctly.
- The close now waits on the panel's and
::backdrop's animationfinishedpromises, so a stale exit run from reopening mid-close can't close the dialog early, and a mismatched duration can't cut either exit short. - Backdrop clicks close the sheet; clicks on the panel's own padding do not.
- The body scroll lock is released only when no modal
<dialog>is left open, so one sheet closing behind another no longer unlocks the page.
New Features
- Adds a
SheetClosewrapper and ashow_close_button:option onSheetContent;MobileSidebarnow usesshow_close_button: falseinstead of its[&>button]:hiddenhack. - Left and right sides get a default
w-3/4 sm:max-w-smwidth, and the panel exposesdata-sidefor targeting one side from outside.