Morph a trigger into a sheet
trigger.style.viewTransitionName = 'morph';document.startViewTransition(() => { flushSync(() => setOpen(true)); trigger.style.viewTransitionName = '';});/* on the panel */view-transition-name: morph;--scrollsheet-travel: none;Give the trigger and panel the same view-transition-name. document.startViewTransition
captures the element before and after your state change, then animates between their positions
and sizes. Set --scrollsheet-travel: none on the panel to disable the sheet’s own slide or
zoom-and-fade animation during both opening and closing.
Only one rendered element can use a given view-transition-name at a time. The panel can keep
its name in CSS because a closed <dialog> is display: none and does not participate. The
trigger stays visible, so assign its name just before the transition and clear it in the state
update callback. Otherwise, the visible trigger and panel would share a name.
A button that grows into a modal
Section titled “A button that grows into a modal”This example animates a button into a side="center" dialog.
import * as React from 'react';import { flushSync } from 'react-dom';import { Sheet } from 'scrollsheet';import 'scrollsheet/styles.css';
type VTDocument = Document & { startViewTransition?: (update: () => void) => { finished: Promise<unknown> };};
function reducedMotion() { return window.matchMedia('(prefers-reduced-motion: reduce)').matches;}
// A trigger that's scrolled off-screen or already unmounted has nowhere for// the reverse morph to land, so the guard checks it's still in the viewport,// not just still in the DOM.function onscreen(el: HTMLElement) { const r = el.getBoundingClientRect(); return r.bottom > 0 && r.right > 0 && r.top < window.innerHeight && r.left < window.innerWidth;}
/** Wires one trigger element to morph into its sheet by a shared `name`. */function useTriggerMorph(name: string, setOpen: (open: boolean) => void) { const triggerRef = React.useRef<HTMLButtonElement>(null);
const openMorph = () => { const doc = document as VTDocument; const trigger = triggerRef.current; if (!doc.startViewTransition || reducedMotion() || !trigger) { setOpen(true); return; } trigger.style.viewTransitionName = name; doc.startViewTransition(() => { flushSync(() => setOpen(true)); trigger.style.viewTransitionName = ''; }); };
const closeMorph = () => { const doc = document as VTDocument; const trigger = triggerRef.current; if (!doc.startViewTransition || reducedMotion() || !trigger || !onscreen(trigger)) { setOpen(false); return; } const transition = doc.startViewTransition(() => { flushSync(() => setOpen(false)); trigger.style.viewTransitionName = name; }); transition.finished.finally(() => { trigger.style.viewTransitionName = ''; }); };
return { triggerRef, openMorph, closeMorph };}
export function NewProjectButton() { const [open, setOpen] = React.useState(false); const { triggerRef, openMorph, closeMorph } = useTriggerMorph('new-project', setOpen);
return ( <> <button ref={triggerRef} type="button" onClick={openMorph}> New project </button> <Sheet.Root side="center" open={open} onOpenChange={(next) => !next && closeMorph()}> <Sheet.Content className="morph-panel" style={{ width: 360, viewTransitionName: 'new-project' }}> <Sheet.Title>New project</Sheet.Title> <Sheet.Description>Give it a name to get started.</Sheet.Description> <input autoFocus placeholder="Project name" /> <button type="button" onClick={closeMorph}>Create</button> </Sheet.Content> </Sheet.Root> </> );}.morph-panel { --scrollsheet-travel: none;}Route every dismissal through closeMorph. On a controlled sheet, backdrop clicks and Escape
call onOpenChange(false) without changing open. This lets the morph start before the state
changes.
A bottom sheet, same hook
Section titled “A bottom sheet, same hook”The same useTriggerMorph hook works for bottom sheets. --scrollsheet-travel: none disables
the slide animation, just as it disables a centered dialog’s zoom and fade:
interface Order { id: string; title: string; detail: string;}
function OrderRow({ order }: { order: Order }) { const [open, setOpen] = React.useState(false); // Unique per row: many rows are mounted at once, and each needs its own name. const name = `order-${order.id}`; const { triggerRef, openMorph, closeMorph } = useTriggerMorph(name, setOpen);
return ( <> <button ref={triggerRef} type="button" className="order-row" onClick={openMorph}> {order.title} </button> <Sheet.Root open={open} onOpenChange={(next) => !next && closeMorph()} detents={['content', 'full']}> <Sheet.Content className="morph-panel" style={{ viewTransitionName: name }}> <Sheet.Handle /> <Sheet.Title>{order.title}</Sheet.Title> <p>{order.detail}</p> <Sheet.Close>Done</Sheet.Close> </Sheet.Content> </Sheet.Root> </> );}What this costs you
Section titled “What this costs you”Integration boundary
Section titled “Integration boundary”This recipe uses the browser’s View Transitions API around controlled open changes. It does
not require a scrollsheet prop or another animation dependency. Snapshot cost and animation
quality depend on the page and device; check both opening and closing in your app.