[Bug Fix] Sheet: render a native <dialog> and close on Escape

TV
3mo ago
+374-117

Problem

  • SheetContent rendered 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, no showCloseButton equivalent (so MobileSidebar hid the corner button with a [&>button]:hidden hack), no default width (every docs example had to pass sm:max-w-sm by hand), and no data-side to target one side from the outside.

Change

  • Native <dialog> rendered in place, opened with showModal() — 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: cancel is intercepted so the exit animates first, then dialog.close(). The controller waits on the exit animations themselves, as Dialog does in #517: close() sets data-state="closed", collects the panel's and the ::backdrop's CSSAnimations (getAnimations({subtree: true}) on the <dialog>, filtered to its own effect.target), waits on their finished promises with a token per run so a superseded run cannot settle a later one, then calls close(). This differs from the animationend block of #506 because getAnimations() alone never lists ::backdrop and the backdrop's events land on the <dialog> under the same keyframe names: that block closed on whichever exit ended first, and a stale animationcancel from 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-300 so it fades out in step with the panel's 300 ms exit (--tw-duration is registered inherits: false, so ::backdrop does not pick the panel's duration up). The controller does not depend on the two matching: a duration-* override on SheetContent closes 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, so backdropClick hit-tests the panel box and ignores it.
  • UA <dialog> resets: a modal dialog is pinned to all four edges by inset: 0, so each side releases the opposite one, plus m-0 max-w-full max-h-full. not-open:hidden keeps a caller's bare flex from overriding the UA display: none when 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: SheetClose wrapper, show_close_button: on SheetContent (replaces the [&>button]:hidden hack), default w-3/4 sm:max-w-sm for left/right, and data-side on the panel. A caller's own classes still win through tailwind_merge.
  • Both controllers stay: ruby-ui--sheet#open on the wrapper, ruby-ui--sheet-content#close on 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's bg-black/50), and header/footer padding stays on the panel — moving it would reflow every existing sheet.
  • mcp/data/registry.json rebuilt (separate commit).

Test

  • cd gem && bundle exec rake — 15 tests in sheet_test.rb.
  • Manual, on the Sheet docs page:
    1. Open → panel slides in over 500 ms, backdrop fades in, page scroll is locked.
    2. Escape / the corner X / the footer's Cancel → exit animation plays, then it closes, focus returns to the trigger, scroll is restored.
    3. Click the backdrop → closes. Click the panel's own padding next to the content → stays open.
    4. 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 animationend at 300 ms on both panel and ::backdrop and close fires 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); a duration-* 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 external close() both leave scroll unlocked; each side lands on its edge, and the default width resolves to min(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 animation finished promises, 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 SheetClose wrapper and a show_close_button: option on SheetContent; MobileSidebar now uses show_close_button: false instead of its [&>button]:hidden hack.
  • Left and right sides get a default w-3/4 sm:max-w-sm width, and the panel exposes data-side for targeting one side from outside.