{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"motion-sheet","type":"registry:component","title":"Sheet (Motion)","description":"Sheet with motion: it slides in on a smooth curve while what's inside follows a moment later, and on a touch screen you drag it back toward its edge to close it.","dependencies":["lucide-react","motion","radix-ui"],"registryDependencies":["https://dashral.com/r/utils.json","https://dashral.com/r/lib-locale.json","https://dashral.com/r/lib-portal.json","https://dashral.com/r/motion-reduced.json","https://dashral.com/r/motion-tokens.json","https://dashral.com/r/sheet.json"],"files":[{"path":"registry/dashral/motion/sheet.tsx","type":"registry:file","target":"components/dashral/motion/sheet.tsx","content":"\"use client\"\n\nimport * as React from \"react\"\nimport { Dialog as SheetPrimitive } from \"radix-ui\"\nimport { XIcon } from \"lucide-react\"\nimport { AnimatePresence, animate, motion, useMotionValue, useTransform } from \"motion/react\"\nimport type { SheetProps as KitSheetProps, SheetSide } from \"../sheet\"\nimport { useLocale } from \"../lib/locale\"\nimport { cn } from \"../lib/utils\"\nimport { usePortalContainer } from \"../lib/portal\"\n\nimport { useReducedMotion } from \"../lib/motion-reduced\"\nimport { duration, ease } from \"../lib/motion-tokens\"\n\nexport type { SheetSide } from \"../sheet\"\nexport { SheetClose } from \"../sheet\"\n\nexport type SheetProps = KitSheetProps & {\n  /** On a touch screen, drag the panel back toward its edge to close it. On by default. */\n  swipeToClose?: boolean\n}\n\n/** The smooth curve every move uses: no spring, no overshoot. */\nconst smooth = [0.4, 0, 0.2, 1] as const\n\n/** A light buzz on phones that have one. */\nconst buzz = (pattern: number) => {\n  if (typeof navigator !== \"undefined\" && \"vibrate\" in navigator) navigator.vibrate(pattern)\n}\n\n/** Where a drag shouldn't start: a field you type in, or something you drag on your own. */\nconst keepsTouch = \"input, textarea, select, [contenteditable], [data-slot=slider], [role=slider]\"\n\n// The kit's sides, without its CSS slide (motion moves the panel here).\nconst sides: Record<SheetSide, string> = {\n  right: \"inset-y-0 right-0 h-full w-3/4 border-l sm:max-w-sm\",\n  left: \"inset-y-0 left-0 h-full w-3/4 border-r sm:max-w-sm\",\n  start: \"inset-y-0 start-0 h-full w-3/4 border-e sm:max-w-sm\",\n  end: \"inset-y-0 end-0 h-full w-3/4 border-s sm:max-w-sm\",\n  top: \"inset-x-0 top-0 h-auto max-h-[calc(100dvh-2rem)] border-b\",\n  bottom: \"inset-x-0 bottom-0 h-auto max-h-[calc(100dvh-2rem)] border-t\",\n}\n\n/** The edge a side sits on, with start and end turned into left or right for the reading direction. */\nfunction edgeOf(side: SheetSide, dir: \"ltr\" | \"rtl\"): \"top\" | \"right\" | \"bottom\" | \"left\" {\n  if (side === \"start\") return dir === \"rtl\" ? \"right\" : \"left\"\n  if (side === \"end\") return dir === \"rtl\" ? \"left\" : \"right\"\n  return side\n}\n\n/**\n * The kit's Sheet with motion: the same trigger, title, body, footer, form and props. It slides in from its edge on a\n * smooth curve while the page behind dims, and what's inside follows a moment later; it slides back out as it closes.\n * On a touch screen, drag it back toward its edge and it follows your finger 1:1, the backdrop clearing as it goes:\n * far or fast enough and it slides away closed (phones buzz), short of it it glides back. The other way it barely\n * gives. With reduced motion it only fades and doesn't drag.\n */\nexport function Sheet({\n  trigger,\n  title,\n  description,\n  children,\n  footer,\n  form,\n  side = \"end\",\n  padding = \"default\",\n  showCloseButton = true,\n  autoFocus = true,\n  swipeToClose = true,\n  className,\n  classNames,\n  open,\n  defaultOpen,\n  onOpenChange,\n  ...props\n}: SheetProps) {\n  const portalContainer = usePortalContainer()\n  const { t, dir } = useLocale()\n  const reduced = useReducedMotion()\n  const padded = padding === \"default\"\n  // Kept here, so the panel can stay on screen while it slides out.\n  const [own, setOwn] = React.useState(defaultOpen ?? false)\n  const isOpen = open ?? own\n  const change = (next: boolean) => {\n    if (open === undefined) setOwn(next)\n    onOpenChange?.(next)\n  }\n\n  const edge = edgeOf(side, dir)\n  const vertical = edge === \"top\" || edge === \"bottom\"\n  // +1 when closing moves toward larger x or y (right, bottom), -1 toward smaller (left, top).\n  const outward = edge === \"right\" || edge === \"bottom\" ? 1 : -1\n  // Off screen, as a share of the panel's own size: where it comes from and goes back to.\n  const away = vertical ? { y: `${outward * 100}%` } : { x: `${outward * 100}%` }\n  const home = vertical ? { y: \"0%\" } : { x: \"0%\" }\n\n  // A drag: how far the panel has come toward its edge, in px. It's applied with CSS translate, apart from the\n  // transform that slides it in and out, so the two add up: a panel let go halfway leaves from where it is.\n  const offset = useMotionValue(0)\n  const size = React.useRef(1)\n  const translate = useTransform(offset, (value) => (vertical ? `0px ${value}px` : `${value}px 0px`))\n  const backdrop = useTransform(offset, (value) => 1 - Math.min(1, Math.abs(value) / size.current) * 0.85)\n  React.useEffect(() => {\n    if (isOpen) offset.jump(0)\n  }, [isOpen, offset])\n  const canSwipe = swipeToClose && !reduced\n\n  const drag = React.useRef<{ id: number; start: number; cross: number; at: number; last: number; time: number; v: number; locked: boolean | null } | null>(null)\n  const onPointerDown = (event: React.PointerEvent<HTMLDivElement>) => {\n    if (!canSwipe || event.pointerType !== \"touch\") return\n    const target = event.target as HTMLElement\n    if (target.closest(keepsTouch)) return\n    // A body that scrolls along the drag's own axis keeps the finger for scrolling (a tall bottom sheet).\n    const body = target.closest<HTMLElement>(\"[data-slot=sheet-body]\")\n    if (vertical && body && body.scrollHeight > body.clientHeight + 1) return\n    size.current = vertical ? event.currentTarget.offsetHeight : event.currentTarget.offsetWidth\n    const along = vertical ? event.clientY : event.clientX\n    drag.current = { id: event.pointerId, start: along, cross: vertical ? event.clientX : event.clientY, at: 0, last: along, time: event.timeStamp, v: 0, locked: null }\n  }\n  const onPointerMove = (event: React.PointerEvent<HTMLDivElement>) => {\n    const d = drag.current\n    if (!d || d.id !== event.pointerId) return\n    const along = vertical ? event.clientY : event.clientX\n    const cross = vertical ? event.clientX : event.clientY\n    // Decide once which way the finger means: along the drag (the panel moves) or across it (the body scrolls).\n    if (d.locked === null) {\n      const a = Math.abs(along - d.start)\n      const c = Math.abs(cross - d.cross)\n      if (a < 6 && c < 6) return\n      d.locked = a > c\n      if (!d.locked) {\n        drag.current = null\n        return\n      }\n      event.currentTarget.setPointerCapture(event.pointerId)\n    }\n    const moved = (along - d.start) * outward\n    // Toward the edge it follows 1:1; the other way it barely gives.\n    offset.set(outward * (moved > 0 ? moved : moved * 0.06))\n    const dt = Math.max(1, event.timeStamp - d.time)\n    d.v = ((along - d.last) * outward) / dt\n    d.last = along\n    d.time = event.timeStamp\n  }\n  const onPointerUp = (event: React.PointerEvent<HTMLDivElement>) => {\n    const d = drag.current\n    drag.current = null\n    if (!d || d.id !== event.pointerId || !d.locked) return\n    const moved = offset.get() * outward\n    // Far enough (a third of the panel) or fast enough (0.5 px/ms) closes it; otherwise it glides home.\n    if (moved > size.current / 3 || (d.v > 0.5 && moved > 24)) {\n      buzz(8)\n      change(false)\n    } else animate(offset, 0, { duration: 0.3, ease: smooth })\n  }\n\n  // What's inside arrives a moment after the panel, rising a little from its edge.\n  const enter = (index: number) =>\n    reduced\n      ? {}\n      : {\n          initial: { opacity: 0, ...(vertical ? { y: outward * 8 } : { x: outward * 12 }) },\n          animate: { opacity: 1, x: 0, y: 0 },\n          transition: { duration: 0.32, ease: smooth, delay: 0.12 + index * 0.04 },\n        }\n\n  const inner = (\n    <>\n      <motion.div data-slot=\"sheet-header\" {...enter(0)} className={cn(\"flex flex-col gap-1.5 p-4\", !padded && \"sr-only\", classNames?.header)}>\n        <SheetPrimitive.Title data-slot=\"sheet-title\" className={cn(\"font-heading font-medium text-foreground\", classNames?.title)}>\n          {title}\n        </SheetPrimitive.Title>\n        {description ? (\n          <SheetPrimitive.Description data-slot=\"sheet-description\" className={cn(\"text-sm text-muted-foreground\", classNames?.description)}>\n            {description}\n          </SheetPrimitive.Description>\n        ) : null}\n      </motion.div>\n      <motion.div data-slot=\"sheet-body\" {...enter(1)} className={cn(\"min-h-0 flex-1 overflow-y-auto overscroll-contain\", padded && \"px-4\", classNames?.body)}>\n        {children}\n      </motion.div>\n      {footer && (\n        <motion.div data-slot=\"sheet-footer\" {...enter(2)} className={cn(\"mt-auto flex flex-col gap-2 p-4\", classNames?.footer)}>\n          {footer}\n        </motion.div>\n      )}\n    </>\n  )\n\n  return (\n    <SheetPrimitive.Root data-slot=\"sheet\" open={isOpen} onOpenChange={change} {...props}>\n      {trigger && <SheetPrimitive.Trigger asChild>{trigger}</SheetPrimitive.Trigger>}\n      <AnimatePresence>\n        {isOpen && (\n          <SheetPrimitive.Portal forceMount container={portalContainer}>\n            <SheetPrimitive.Overlay forceMount asChild>\n              <motion.div\n                data-slot=\"sheet-overlay\"\n                className=\"fixed inset-0 z-50\"\n                initial={{ opacity: 0 }}\n                animate={{ opacity: 1 }}\n                exit={{ opacity: 0, transition: { duration: 0.28, ease: smooth } }}\n                transition={{ duration: 0.4, ease: smooth }}\n              >\n                {/* The blur stays put and only the opacity moves (animating a full-screen blur costs frames); a drag clears it. */}\n                <motion.div className=\"absolute inset-0 bg-black/40 backdrop-blur-[2px]\" style={{ opacity: backdrop }} />\n              </motion.div>\n            </SheetPrimitive.Overlay>\n            <SheetPrimitive.Content\n              forceMount\n              asChild\n              {...(description ? {} : { \"aria-describedby\": undefined })}\n              onOpenAutoFocus={\n                autoFocus\n                  ? undefined\n                  : (event) => {\n                      // The panel takes focus (it's focusable, tabIndex -1), so Tab still starts inside it.\n                      event.preventDefault()\n                      ;(event.currentTarget as HTMLElement).focus()\n                    }\n              }\n            >\n              <motion.div\n                data-slot=\"sheet-content\"\n                style={{ translate, touchAction: canSwipe ? (vertical ? \"none\" : \"pan-y\") : undefined }}\n                onPointerDown={onPointerDown}\n                onPointerMove={onPointerMove}\n                onPointerUp={onPointerUp}\n                onPointerCancel={() => {\n                  drag.current = null\n                  if (offset.get() !== 0) animate(offset, 0, { duration: 0.3, ease: smooth })\n                }}\n                initial={reduced ? { opacity: 0 } : away}\n                animate={reduced ? { opacity: 1 } : home}\n                exit={reduced ? { opacity: 0, transition: { duration: duration.fast } } : { ...away, transition: { duration: 0.3, ease: ease.exit } }}\n                transition={reduced ? { duration: duration.fast } : { duration: 0.45, ease: smooth }}\n                className={cn(\"fixed z-50 flex flex-col bg-background shadow-lg outline-none\", sides[side], className)}\n              >\n                {canSwipe && vertical && (\n                  // A handle on touch screens, saying the panel can be dragged toward its edge.\n                  <span\n                    aria-hidden\n                    className={cn(\"absolute left-1/2 hidden h-1 w-9 -translate-x-1/2 rounded-full bg-muted-foreground/25 pointer-coarse:block\", edge === \"bottom\" ? \"top-2\" : \"bottom-2\")}\n                  />\n                )}\n                {form ? (\n                  <form {...form} className={cn(\"flex min-h-0 flex-1 flex-col\", form.className)}>\n                    {inner}\n                  </form>\n                ) : (\n                  inner\n                )}\n                {showCloseButton && (\n                  <SheetPrimitive.Close className=\"absolute top-4 end-4 rounded-xs opacity-70 transition-opacity hover:opacity-100 focus:ring-2 focus:ring-ring focus:ring-offset-2 focus:outline-hidden disabled:pointer-events-none\">\n                    <XIcon className=\"size-4\" />\n                    <span className=\"sr-only\">{t(\"close\")}</span>\n                  </SheetPrimitive.Close>\n                )}\n              </motion.div>\n            </SheetPrimitive.Content>\n          </SheetPrimitive.Portal>\n        )}\n      </AnimatePresence>\n    </SheetPrimitive.Root>\n  )\n}\n"}],"cssVars":{"theme":{"font-heading":"var(--font-geist, \"Geist\"), var(--font-inter, \"Inter\"), var(--font-arabic, \"IBM Plex Sans Arabic\"), ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", Roboto, sans-serif"},"light":{},"dark":{}}}