{"slug":"bottom-sheet","name":"Bottom Sheet","description":"Vaul-inspired draggable bottom sheet with snap points, inertia and glass surface.","category":"motion","source_url":"https://beui.dev/r/bottom-sheet/raw","detail_url":"https://beui.dev/r/bottom-sheet","raw_url":"https://beui.dev/r/bottom-sheet/raw","page_url":"https://beui.dev/components/motion/bottom-sheet","markdown_url":"https://beui.dev/components/motion/bottom-sheet.md","published_at":"2026-05-17","updated_at":"2026-11-24","dependencies":["clsx","motion","react","react-dom","tailwind-merge"],"internal":["@/components/motion/bottom-sheet","@/lib/ease","@/lib/focusable","@/lib/hooks/use-focus-trap","@/lib/hooks/use-on-open","@/lib/i18n","@/lib/layers","@/lib/presence-gate","@/lib/scroll-lock","@/lib/touch","@/lib/utils"],"install_slugs":["bottom-sheet"],"requires_theme":true,"props":[{"displayName":"BottomSheet","props":[{"name":"open","type":"boolean","required":true,"defaultValue":null,"description":"Whether the sheet is open."},{"name":"onOpenChange","type":"(open: boolean) => void","required":true,"defaultValue":null,"description":"Called with false when the sheet is dismissed by drag, backdrop or Escape."},{"name":"snapPoints","type":"(number | \"auto\")[]","required":false,"defaultValue":"[0.5, 0.92]","description":"Heights (0-1 = fraction of viewport, or \"auto\"). First entry is default."},{"name":"defaultSnap","type":"number","required":false,"defaultValue":"0","description":"Index into `snapPoints` the sheet opens at. Default: 0"},{"name":"title","type":"string","required":false,"defaultValue":null,"description":"Heading at the top of the sheet, also its accessible name."},{"name":"description","type":"string","required":false,"defaultValue":null,"description":"Text under the title, also its accessible description."},{"name":"className","type":"string","required":false,"defaultValue":null,"description":"Classes merged onto the sheet panel."},{"name":"dismissThreshold","type":"number","required":false,"defaultValue":"120","description":"Min drag distance (px) past current snap to dismiss."},{"name":"labels","type":"Partial<{ label: string; close: string; }>","required":false,"defaultValue":null,"description":"Override any built-in string."}]}],"types":[],"files":[{"path":"components/motion/bottom-sheet.tsx","type":"component","content":"\"use client\";\n// beui.dev/components/motion/bottom-sheet\n\nimport {\n  AnimatePresence,\n  motion,\n  type PanInfo,\n  useDragControls,\n  useReducedMotion,\n} from \"motion/react\";\nimport { type ReactNode, useEffect, useId, useRef, useState } from \"react\";\nimport { createPortal } from \"react-dom\";\nimport { EASE_DRAWER } from \"@/lib/ease\";\nimport { useFocusTrap } from \"@/lib/hooks/use-focus-trap\";\nimport { useOnOpen } from \"@/lib/hooks/use-on-open\";\nimport { useMessages } from \"@/lib/i18n\";\nimport { PresenceGate } from \"@/lib/presence-gate\";\nimport { useScrollLock } from \"@/lib/scroll-lock\";\nimport { TOUCH_GESTURE_CONTENT_CLASS } from \"@/lib/touch\";\nimport { cn } from \"@/lib/utils\";\n\n// Vaul-style glide: a long, fully-damped tween reads smoother than a spring on\n// open - no settle/overshoot, just one clean decel. Same curve drives the\n// backdrop fade so the surface and scrim move as one.\nconst DRAWER = { duration: 0.5, ease: EASE_DRAWER } as const;\n\n/** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */\nexport const bottomSheetMessages = {\n  label: \"Bottom sheet\",\n  close: \"Close bottom sheet\",\n};\n\nexport type BottomSheetMessages = typeof bottomSheetMessages;\n\nexport interface BottomSheetProps {\n  /** Whether the sheet is open. */\n  open: boolean;\n  /** Called with false when the sheet is dismissed by drag, backdrop or Escape. */\n  onOpenChange: (open: boolean) => void;\n  /** Heights (0-1 = fraction of viewport, or \"auto\"). First entry is default. */\n  snapPoints?: (number | \"auto\")[];\n  /** Index into `snapPoints` the sheet opens at. Default: 0 */\n  defaultSnap?: number;\n  /** Heading at the top of the sheet, also its accessible name. */\n  title?: string;\n  /** Text under the title, also its accessible description. */\n  description?: string;\n  children?: ReactNode;\n  /** Classes merged onto the sheet panel. */\n  className?: string;\n  /** Min drag distance (px) past current snap to dismiss. */\n  dismissThreshold?: number;\n  /** Override any built-in string. */\n  labels?: Partial<BottomSheetMessages>;\n}\n\nexport function BottomSheet({\n  open,\n  onOpenChange,\n  snapPoints = [0.5, 0.92],\n  defaultSnap = 0,\n  title,\n  description,\n  children,\n  className,\n  dismissThreshold = 120,\n  labels,\n}: BottomSheetProps) {\n  const t = useMessages(\"bottomSheet\", bottomSheetMessages, labels);\n  const [snap, setSnap] = useState(defaultSnap);\n  const [mounted, setMounted] = useState(false);\n  const dragControls = useDragControls();\n  const sheetRef = useRef<HTMLDivElement>(null);\n  const reduce = useReducedMotion();\n  const uid = useId();\n  const titleId = `${uid}-title`;\n  const descriptionId = `${uid}-description`;\n\n  useEffect(() => {\n    setMounted(true);\n  }, []);\n\n  useOnOpen(open, () => setSnap(defaultSnap));\n\n  // Focus moves into the sheet on open, Tab stays inside it, and focus returns\n  // to the opener on close. `mounted` gates it because the sheet only exists\n  // once the portal can render.\n  useFocusTrap(open && mounted, sheetRef, {\n    onEscape: () => onOpenChange(false),\n  });\n\n  // Lock background scroll while open. overflow:hidden alone is ignored by\n  // iOS Safari - boundary scrolls inside the sheet chain to the page, which\n  // scrolls underneath and ends up somewhere else on close. position:fixed\n  // is the lock that actually holds; it restores the scroll position after.\n  useScrollLock(open, { fixed: true });\n\n  const onDragEnd = (_: unknown, info: PanInfo) => {\n    const velocity = info.velocity.y;\n    const offset = info.offset.y;\n\n    // Strong downward fling or large drag → dismiss.\n    if (velocity > 600 || offset > dismissThreshold) {\n      const smaller = snapPoints.map((_, i) => i).filter((i) => i < snap);\n      if (smaller.length && velocity < 800 && offset < dismissThreshold * 1.6) {\n        setSnap(smaller[smaller.length - 1]);\n      } else {\n        onOpenChange(false);\n      }\n      return;\n    }\n\n    // Strong upward fling → next snap.\n    if (velocity < -500) {\n      setSnap((current) => Math.min(snapPoints.length - 1, current + 1));\n      return;\n    }\n\n    // Otherwise snap to nearest by current offset.\n    setSnap((current) => {\n      if (offset > 80 && current > 0) return current - 1;\n      if (offset < -80 && current < snapPoints.length - 1) return current + 1;\n      return current;\n    });\n  };\n\n  const snapValue = snapPoints[snap];\n  const heightStyle =\n    snapValue === \"auto\"\n      ? { maxHeight: \"92vh\" }\n      : { height: `${snapValue * 100}vh` };\n\n  // Portal to <body>: an ancestor with backdrop-filter or transform becomes\n  // the containing block for fixed descendants, which would position the\n  // sheet against that ancestor instead of the viewport.\n  if (!mounted) return null;\n\n  // Two fixed siblings, no wrapper: the scrim spans the viewport edges but\n  // carries a colour, and the sheet is pinned to the bottom, stops short of the\n  // top edge at every snap point the component ships, and paints an opaque\n  // surface either way. Both hang off `PresenceGate`, so interaction releases in\n  // the same commit that starts the exit rather than when it ends.\n  return createPortal(\n    <AnimatePresence>\n      {open ? (\n        <PresenceGate key=\"backdrop\">\n          {({ gate }) => (\n            <motion.button\n              type=\"button\"\n              aria-label={t(\"close\")}\n              // Outside the sheet, so the focus trap never lands on it; it is\n              // a pointer target, and Escape closes from the keyboard.\n              tabIndex={-1}\n              initial={{ opacity: 0 }}\n              animate={{ opacity: 1 }}\n              exit={{ opacity: 0 }}\n              transition={DRAWER}\n              {...gate}\n              onClick={() => onOpenChange(false)}\n              // A dim scrim with a light blur. backdrop-blur is GPU-expensive and\n              // re-rasterizes every frame the sheet drags over it; a small radius\n              // plus more opacity keeps the glass look without the jank.\n              className=\"pointer-events-auto fixed inset-0 z-50 bg-background/40 backdrop-blur-sm\"\n            />\n          )}\n        </PresenceGate>\n      ) : null}\n      {open ? (\n        <PresenceGate key=\"sheet\">\n          {({ gate }) => (\n            <motion.div\n              ref={sheetRef}\n              drag=\"y\"\n              dragControls={dragControls}\n              dragListener={false}\n              dragConstraints={{ top: 0, bottom: 0 }}\n              dragElastic={{ top: 0.02, bottom: 0.4 }}\n              dragMomentum={false}\n              onDragEnd={onDragEnd}\n              initial={reduce ? { y: 0, opacity: 0 } : { y: \"100%\" }}\n              animate={reduce ? { y: 0, opacity: 1 } : { y: 0 }}\n              exit={reduce ? { y: 0, opacity: 0 } : { y: \"100%\" }}\n              transition={reduce ? { duration: 0.18, ease: EASE_DRAWER } : DRAWER}\n              {...gate}\n              style={{ ...heightStyle, ...gate.style }}\n              className={cn(\n                \"pointer-events-auto fixed bottom-0 left-0 right-0 z-50 mx-auto flex max-w-2xl flex-col overflow-hidden rounded-t-2xl outline-none will-change-transform\",\n                \"border border-border bg-background shadow-xl\",\n                className,\n              )}\n              role=\"dialog\"\n              aria-modal=\"true\"\n              tabIndex={-1}\n              aria-labelledby={title ? titleId : undefined}\n              aria-describedby={description ? descriptionId : undefined}\n              aria-label={title ? undefined : t(\"label\")}\n            >\n              <div className=\"flex flex-col items-center px-4 pb-2 pt-3\">\n                {/* Drag only the pill so the title and description stay selectable. */}\n                <div\n                  onPointerDown={(event) => dragControls.start(event)}\n                  // A slow pull must not hand the gesture to iOS's callout,\n                  // which would leave the sheet frozen mid-drag.\n                  className={cn(\n                    \"flex cursor-grab touch-none items-center justify-center py-1 active:cursor-grabbing\",\n                    TOUCH_GESTURE_CONTENT_CLASS,\n                  )}\n                >\n                  <div className=\"h-1.5 w-10 rounded-full bg-muted-foreground/40\" />\n                </div>\n                {title || description ? (\n                  <div className=\"mt-2 w-full\">\n                    {title ? (\n                      <h2\n                        id={titleId}\n                        className=\"text-base font-medium text-foreground\"\n                      >\n                        {title}\n                      </h2>\n                    ) : null}\n                    {description ? (\n                      <p\n                        id={descriptionId}\n                        className=\"mt-0.5 text-sm text-muted-foreground\"\n                      >\n                        {description}\n                      </p>\n                    ) : null}\n                  </div>\n                ) : null}\n              </div>\n              {/* overscroll-contain stops boundary scrolls from chaining to the page. */}\n              <div className=\"flex-1 overflow-y-auto overscroll-contain px-4 pb-6\">{children}</div>\n            </motion.div>\n          )}\n        </PresenceGate>\n      ) : null}\n    </AnimatePresence>,\n    document.body,\n  );\n}\n"},{"path":"lib/ease.ts","type":"util","content":"// Shared motion tokens. Easing curves mirror the CSS custom properties in\n// globals.css; springs are the canonical physics used across components.\n// Strong custom variants - defaults like `ease-in`/`ease-out` feel weak.\n\nexport const EASE_OUT = [0.16, 1, 0.3, 1] as const;\n/** Accelerating curve for things that fall (a dropped ball, a toppled block). Not for UI entrances. */\nexport const EASE_IN = [0.55, 0, 1, 0.45] as const;\nexport const EASE_IN_OUT = [0.77, 0, 0.175, 1] as const;\nexport const EASE_DRAWER = [0.32, 0.72, 0, 1] as const;\n\n/** CSS string form of EASE_OUT for inline style transitions. */\nexport const EASE_OUT_CSS = \"cubic-bezier(0.16, 1, 0.3, 1)\";\n\n/** Press feedback on buttons and other tappable surfaces. */\nexport const SPRING_PRESS = {\n  type: \"spring\",\n  stiffness: 500,\n  damping: 30,\n  mass: 0.6,\n} as const;\n\n/** Content swaps - label/icon slots trading places inside a control. */\nexport const SPRING_SWAP = {\n  type: \"spring\",\n  stiffness: 460,\n  damping: 30,\n  mass: 0.55,\n} as const;\n\n/** Overlay panel entrances - modals and sheets summoned by pointer. */\nexport const SPRING_PANEL = {\n  type: \"spring\",\n  stiffness: 420,\n  damping: 40,\n  mass: 0.5,\n} as const;\n\n/** Shared-layout glides - pills, indicators and panels morphing between positions. */\nexport const SPRING_LAYOUT = {\n  type: \"spring\",\n  stiffness: 360,\n  damping: 32,\n  mass: 0.6,\n} as const;\n\n/** Cursor-follow physics for decorative mouse tracking (magnetic, tilt, dock). */\nexport const SPRING_MOUSE = {\n  stiffness: 200,\n  damping: 15,\n  mass: 0.3,\n} as const;\n\n/** Dragged handles and fills (sliders) - critically damped `useSpring` config,\n * so the value follows the pointer butterily and never rebounds off an end. */\nexport const SPRING_GLIDE = {\n  stiffness: 700,\n  damping: 50,\n  mass: 0.5,\n} as const;\n\n/** Shared-surface morphs: a trigger growing into its panel and back on one\n * weighted spring, so opening and closing travel the same path. */\nexport const SPRING_MORPH = {\n  type: \"spring\",\n  duration: 0.5,\n  bounce: 0.22,\n} as const;\n"},{"path":"lib/hooks/use-focus-trap.ts","type":"util","content":"\"use client\";\n\nimport { type RefObject, useEffect, useLayoutEffect, useRef } from \"react\";\nimport { cycleTab, focusFirstIn } from \"@/lib/focusable\";\nimport { inLayerAbove, type Layer, openLayer, ownsEscape, trapAbove } from \"@/lib/layers\";\n\nexport interface FocusTrapOptions {\n  /** Called on Escape while this is the innermost open layer. */\n  onEscape?: () => void;\n}\n\n/**\n * Moves focus into `panel` when `open` turns true, keeps Tab cycling inside it,\n * pulls focus back if it lands outside, and returns focus to whatever held it\n * before opening once `open` turns false.\n *\n * Layers opened on top (a menu portaled out of a drawer, a sheet opened from\n * it) keep focus and Escape until they close: see `lib/layers.ts`.\n *\n * For overlays rendered in place, where `useModalScope` cannot inert the rest\n * of the page. Pass `open` rather than presence: an exiting panel is already\n * inert through `PresenceGate`, so the trap releases with the same commit.\n * Give the panel `tabIndex={-1}` so it can take focus when it holds nothing\n * focusable.\n */\nexport function useFocusTrap(\n  open: boolean,\n  panel: RefObject<HTMLElement | null>,\n  { onEscape }: FocusTrapOptions = {},\n) {\n  const escapeRef = useRef(onEscape);\n  useLayoutEffect(() => {\n    escapeRef.current = onEscape;\n  });\n\n  useEffect(() => {\n    if (!open || !panel.current) return;\n    const layer: Layer = {\n      contains: (target) => Boolean(panel.current?.contains(target)),\n      traps: true,\n      get escape() {\n        return Boolean(escapeRef.current);\n      },\n    };\n    const closeLayer = openLayer(layer);\n    const returnFocus =\n      document.activeElement instanceof HTMLElement\n        ? document.activeElement\n        : null;\n    if (!panel.current.contains(document.activeElement)) focusFirstIn(panel.current);\n\n    const onFocus = (event: FocusEvent) => {\n      const target = event.target;\n      if (!(target instanceof Node) || !panel.current) return;\n      if (panel.current.closest(\"[inert]\") || panel.current.contains(target)) return;\n      if (inLayerAbove(layer, target) || trapAbove(layer)) return;\n      focusFirstIn(panel.current);\n    };\n    const onKey = (event: KeyboardEvent) => {\n      if (event.key === \"Escape\") {\n        if (!escapeRef.current || !ownsEscape(layer, event)) return;\n        event.preventDefault();\n        escapeRef.current();\n        return;\n      }\n      const active = document.activeElement;\n      if (event.defaultPrevented || (active && inLayerAbove(layer, active))) return;\n      cycleTab(event, panel.current);\n    };\n    document.addEventListener(\"focusin\", onFocus);\n    document.addEventListener(\"keydown\", onKey);\n    return () => {\n      document.removeEventListener(\"focusin\", onFocus);\n      document.removeEventListener(\"keydown\", onKey);\n      closeLayer();\n      if (returnFocus?.isConnected && !returnFocus.closest(\"[inert]\"))\n        returnFocus.focus({ preventScroll: true });\n    };\n  }, [open, panel]);\n}\n"},{"path":"lib/hooks/use-on-open.ts","type":"util","content":"\"use client\";\n\nimport { useState } from \"react\";\n\n/**\n * Runs `start` during the render in which `open` becomes true.\n *\n * Opening a list starts a fresh session - an empty query, the highlight back at\n * the top - and that is a resolution, not a side effect: a passive effect runs\n * after the commit, so the new session would carry the last one's state for a\n * window in which a key press can land.\n *\n * `start` may only set state belonging to the calling component. Anything that\n * reaches outside it - a consumer's callback, a DOM write, moving focus - is a\n * side effect, and React rejects a callback that sets another component's state\n * during this one's render. Put those in an effect keyed to `open`.\n */\nexport function useOnOpen(open: boolean, start: () => void) {\n  const [wasOpen, setWasOpen] = useState(open);\n  if (open !== wasOpen) {\n    setWasOpen(open);\n    if (open) start();\n  }\n}\n"},{"path":"lib/i18n.tsx","type":"util","content":"\"use client\";\n\nimport {\n  createContext,\n  type ReactNode,\n  useCallback,\n  useContext,\n  useMemo,\n} from \"react\";\n\n/**\n * A component's messages. Plain strings with `{name}` placeholders, so a\n * dictionary stays JSON-serializable and can be passed from a server layout.\n * There are no plural forms: write messages that read correctly for any count.\n */\nexport type Messages = Record<string, string>;\n\n/** A language file: component namespace → any subset of that component's messages. */\nexport type LocaleMessages = Record<string, Partial<Messages>>;\n\ntype LocaleContextValue = {\n  locale?: string;\n  messages: LocaleMessages;\n};\n\nconst NO_MESSAGES: LocaleMessages = {};\n\nconst LocaleContext = createContext<LocaleContextValue>({ messages: NO_MESSAGES });\n\n/** Without a locale, digits stay plain so server and client render the same text. */\nexport function formatNumber(value: number, locale?: string) {\n  return locale ? new Intl.NumberFormat(locale).format(value) : String(value);\n}\n\nexport interface LocaleProviderProps {\n  /** BCP 47 tag, e.g. `de`, `pt-BR`. Drives `Intl` number formatting. */\n  locale: string;\n  /**\n   * Component namespace → translated strings. Missing keys fall back to English.\n   * Pass a stable object (a module constant, imported JSON or a memoized value):\n   * a new object each render gives every translated component a new `t`.\n   */\n  messages?: LocaleMessages;\n  children: ReactNode;\n}\n\nexport function LocaleProvider({ locale, messages = NO_MESSAGES, children }: LocaleProviderProps) {\n  const value = useMemo(() => ({ locale, messages }), [locale, messages]);\n  return <LocaleContext.Provider value={value}>{children}</LocaleContext.Provider>;\n}\n\nexport function useLocale(): { locale?: string } {\n  return { locale: useContext(LocaleContext).locale };\n}\n\n/**\n * Resolves a component's strings: `labels` prop, then the provider's\n * messages for `namespace`, then the component's English `defaults`.\n * Numbers passed as values are formatted for the provider's locale.\n */\nexport function useMessages<T extends Messages>(\n  namespace: string,\n  defaults: T,\n  labels?: Partial<T>,\n) {\n  const { locale, messages } = useContext(LocaleContext);\n  const provided = messages[namespace];\n  // Inline `labels={{ … }}` is a new object every render. Keying it by content\n  // keeps `t` one identity until a label changes, so effects and memos that\n  // depend on `t` don't re-run per render, and render-time reads never lag.\n  const labelsKey = labels ? JSON.stringify(labels) : \"\";\n  const stableLabels = useMemo(\n    () => (labelsKey ? (JSON.parse(labelsKey) as Partial<T>) : undefined),\n    [labelsKey],\n  );\n\n  return useCallback(\n    (key: keyof T & string, values?: Record<string, string | number>) => {\n      const template = stableLabels?.[key] ?? provided?.[key] ?? defaults[key];\n      if (!values) return template;\n      return template.replace(/\\{(\\w+)\\}/g, (match, name: string) => {\n        const value = values[name];\n        if (value === undefined) return match;\n        return typeof value === \"number\" ? formatNumber(value, locale) : value;\n      });\n    },\n    [defaults, stableLabels, locale, provided],\n  );\n}\n"},{"path":"lib/presence-gate.tsx","type":"util","content":"\"use client\";\n\nimport { useIsPresent } from \"motion/react\";\nimport type { ReactNode } from \"react\";\n\nexport interface PresenceGateRenderProps {\n  /**\n   * False from the render that starts the exit animation onward. An overlay\n   * kept in the tree by `AnimatePresence` is still the topmost thing on the\n   * page, so anything it decides from `open` alone stays true for the whole\n   * exit - this is the boolean that already knows the overlay is leaving.\n   */\n  isPresent: boolean;\n  /**\n   * Spread onto every layer that takes pointer events while the overlay is\n   * open. Interaction releases in the same commit that starts the exit while\n   * the visual exit keeps playing: pointer events stop landing, and `inert`\n   * drops the subtree from focus order, from tab order and from the\n   * accessibility tree - an exiting dialog is not a dialog you can still type\n   * into. A layer that never takes pointer events (a wrapper that only centres\n   * the panel) takes `inert={!isPresent}` alone, so its own\n   * `pointer-events-none` is not overwritten.\n   */\n  gate: {\n    inert: boolean;\n    style: { pointerEvents: \"auto\" | \"none\" };\n  };\n}\n\nexport interface PresenceGateProps {\n  children: (props: PresenceGateRenderProps) => ReactNode;\n}\n\n/**\n * Reads the presence of the subtree it renders and hands it down.\n *\n * `useIsPresent` only answers inside the `AnimatePresence` subtree, and the\n * components that own an overlay render the `AnimatePresence` themselves, so\n * the boolean has to be read one component further down: this is that\n * component, and the render prop is how it reaches the layers.\n */\nexport function PresenceGate({ children }: PresenceGateProps) {\n  const isPresent = useIsPresent();\n\n  return children({\n    isPresent,\n    gate: {\n      inert: !isPresent,\n      style: { pointerEvents: isPresent ? \"auto\" : \"none\" },\n    },\n  });\n}\n"},{"path":"lib/scroll-lock.ts","type":"util","content":"\"use client\";\n\nimport { useEffect } from \"react\";\n\nexport interface ScrollLockOptions {\n  /**\n   * Pin the body with `position: fixed` and restore the scroll offset on\n   * release. iOS Safari still scrolls an `overflow: hidden` body under a\n   * touch drag, so full-screen sheets that take touch gestures need this.\n   */\n  fixed?: boolean;\n}\n\n// Reference-counted, so overlays that close out of order never unlock the\n// page under one that is still open. The first lock picks the technique.\nlet owners = 0;\nlet restore: (() => void) | null = null;\n\nfunction apply({ fixed = false }: ScrollLockOptions) {\n  const root = document.documentElement;\n  const body = document.body;\n  const scrollY = window.scrollY;\n  const previous = {\n    rootOverflow: root.style.overflow,\n    overflow: body.style.overflow,\n    position: body.style.position,\n    top: body.style.top,\n    left: body.style.left,\n    right: body.style.right,\n  };\n  root.style.overflow = \"hidden\";\n  body.style.overflow = \"hidden\";\n  if (fixed) {\n    body.style.position = \"fixed\";\n    body.style.top = `-${scrollY}px`;\n    body.style.left = \"0\";\n    body.style.right = \"0\";\n  }\n  return () => {\n    root.style.overflow = previous.rootOverflow;\n    body.style.overflow = previous.overflow;\n    if (!fixed) return;\n    body.style.position = previous.position;\n    body.style.top = previous.top;\n    body.style.left = previous.left;\n    body.style.right = previous.right;\n    window.scrollTo(0, scrollY);\n  };\n}\n\n/** Locks page scroll. Call the returned function to release this lock. */\nexport function lockScroll(options: ScrollLockOptions = {}) {\n  if (owners++ === 0) restore = apply(options);\n  let released = false;\n  return () => {\n    if (released) return;\n    released = true;\n    if (--owners === 0) {\n      restore?.();\n      restore = null;\n    }\n  };\n}\n\n/** Locks page scroll while `locked` is true. */\nexport function useScrollLock(locked: boolean, { fixed = false }: ScrollLockOptions = {}) {\n  useEffect(() => {\n    if (!locked) return;\n    return lockScroll({ fixed });\n  }, [locked, fixed]);\n}\n"},{"path":"lib/touch.ts","type":"util","content":"// Shared touch primitives. iOS and iPadOS run their own gestures on top of the\n// page - the long-press selection callout and the selection it drags in with\n// it - and they win: once the platform claims a touch it cancels ours\n// mid-gesture, so a press-and-hold or a drag simply dies. Surfaces that own\n// their gesture have to opt out.\n//\n// What the two classes below cover, precisely:\n// - `-webkit-touch-callout: none` stops iOS's long-press callout. WebKit-only:\n//   it is not a property other engines have, so it is inert everywhere else.\n// - `user-select: none` stops the long-press selection on every engine,\n//   Android included, and stops a drag from painting a selection under the\n//   cursor. It is inherited, so it reaches every descendant - which is why the\n//   two classes differ only in whether they apply it unconditionally.\n// What neither covers:\n// - Chrome for Android's long-press menu on a link or an image. No CSS\n//   suppresses it; a gesture surface that wraps one needs its own\n//   `onContextMenu` with `preventDefault()`.\n// - The native drag of an `<img>` or `<a>` descendant. `-webkit-user-drag` is\n//   not inherited and plain divs and buttons are not drag sources, so setting\n//   it on the surface does nothing - the child itself needs `draggable={false}`.\n\n/**\n * Classes for a surface that *is* the control: a thumb, a drum, a stage, a\n * handle, a hold button. Selection is suppressed on every input, because a\n * drag that highlights the control's own label is wrong on a mouse too.\n * Compose with `touch-none` when the surface also owns the scroll axis - leave\n * it off when the page must still scroll from there.\n */\nexport const TOUCH_GESTURE_CLASS = \"select-none [-webkit-touch-callout:none]\";\n\n/**\n * The same opt-out for a gesture surface that wraps content the consumer owns:\n * a scroller, a context-menu trigger, a sheet header, a list row. Selection is\n * suppressed only where the platform runs its own press gestures - a coarse\n * pointer - so a mouse user can still select and copy that content. If the\n * gesture itself would paint a selection under the cursor, add `select-none`\n * for the duration of the gesture rather than reaching for\n * `TOUCH_GESTURE_CLASS`.\n *\n * `pointer: coarse` describes the *primary* pointer and nothing else, so a\n * hybrid machine reads it wrong in both directions: a tablet with a mouse\n * plugged in keeps touch as primary and loses mouse selection, and a laptop\n * with a touchscreen keeps the mouse as primary and leaves selection live\n * under a finger. No media query can answer per interaction - the query is\n * about the device, and the question is about the gesture in progress. The\n * default stays here because it is right on the machines that are one thing or\n * the other, and losing a selection is a nuisance; where the miss costs a\n * *gesture* instead, the surface pairs it with `holdSelection` on the press.\n */\nexport const TOUCH_GESTURE_CONTENT_CLASS =\n  \"[-webkit-touch-callout:none] pointer-coarse:select-none\";\n\n/**\n * Suppress selection on `element` for as long as a gesture is running on it,\n * whatever the primary pointer of the machine happens to be. Returns the\n * release. Inline, so it wins over the class above and is gone again the\n * moment the gesture ends.\n *\n * For the press gestures a native selection would otherwise steal - a\n * long-press that opens a menu. Elsewhere prefer the classes: a surface that\n * takes selection away for the whole session is a surface whose text nobody\n * can copy.\n */\nexport function holdSelection(element: HTMLElement) {\n  element.style.setProperty(\"user-select\", \"none\");\n  element.style.setProperty(\"-webkit-user-select\", \"none\");\n  return () => {\n    element.style.removeProperty(\"user-select\");\n    element.style.removeProperty(\"-webkit-user-select\");\n  };\n}\n\n/**\n * Pointer capture, best effort. WebKit throws `NotFoundError` when the pointer\n * is already gone by the time the handler runs - routine on iOS, where the\n * system can claim the touch first - and an uncaught throw takes the rest of\n * the handler, the gesture included, down with it. Touch pointers carry\n * implicit capture anyway, so losing it is never fatal.\n */\nexport function capturePointer(element: Element, pointerId: number) {\n  try {\n    element.setPointerCapture(pointerId);\n  } catch {\n    // Pointer is no longer active - implicit capture still applies on touch.\n  }\n}\n\n/** Release a capture taken with `capturePointer`, ignoring a stale pointer. */\nexport function releasePointer(element: Element, pointerId: number) {\n  try {\n    if (element.hasPointerCapture(pointerId)) {\n      element.releasePointerCapture(pointerId);\n    }\n  } catch {\n    // Capture was already dropped by the browser.\n  }\n}\n\n/**\n * Whether this event came from a pointer that is *hovering*: not a touch, and\n * not currently pressed. Which input the user is holding right now is not\n * something a device capability can answer - a touchscreen laptop hovers and\n * taps, and iPadOS reports a fine hovering pointer for a finger - so both\n * paths stay live and each handler branches on the event it was given.\n *\n * A pen resting on the glass is making contact, not hovering: `buttons` is the\n * tell, and it sends a pen tap down the same route a finger takes.\n *\n * This answers what an *enter* asks. A leave is the other half of a pair and\n * has to be read against the enter that started it - `useHoverGesture` in\n * `lib/hooks/use-hover-gesture` does that, and hover surfaces should use it\n * rather than asking this question twice.\n */\nexport const isHoveringPointer = (event: {\n  pointerType: string;\n  buttons: number;\n}) => event.pointerType !== \"touch\" && event.buttons === 0;\n"},{"path":"lib/utils.ts","type":"util","content":"import { clsx, type ClassValue } from \"clsx\"\nimport { twMerge } from \"tailwind-merge\"\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs))\n}\n"},{"path":"lib/focusable.ts","type":"util","content":"/**\n * Elements that can usually take keyboard focus. The selector cannot see\n * everything focus() checks (an element under `display: none`, say), so a\n * caller that needs certainty tries focus() and checks where it landed.\n */\nexport const FOCUSABLE_SELECTOR = [\n  \"a[href]\",\n  \"button:not(:disabled)\",\n  \"input:not(:disabled):not([type='hidden'])\",\n  \"select:not(:disabled)\",\n  \"textarea:not(:disabled)\",\n  \"[tabindex]:not([tabindex='-1'])\",\n].join(\",\");\n\n/** Elements inside `container` that Tab can reach, in DOM order. */\nexport function focusablesIn(container: Element | null | undefined) {\n  return Array.from(\n    container?.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR) ?? [],\n  ).filter((element) => element.tabIndex >= 0 && !element.closest(\"[inert]\"));\n}\n\n/** Focuses the first tabbable element in `panel`, or the panel itself. */\nexport function focusFirstIn(panel: HTMLElement | null | undefined) {\n  (focusablesIn(panel)[0] ?? panel)?.focus({ preventScroll: true });\n}\n\n/**\n * Keeps a Tab keypress inside `panel`, wrapping from the last tabbable element\n * to the first and back. Give the panel `tabIndex={-1}` so it can hold focus\n * when it has nothing tabbable.\n */\nexport function cycleTab(event: KeyboardEvent, panel: HTMLElement | null) {\n  if (event.key !== \"Tab\" || !panel) return;\n  const elements = focusablesIn(panel);\n  const first = elements[0];\n  const last = elements[elements.length - 1];\n  const active = document.activeElement;\n  if (!first || !last) {\n    event.preventDefault();\n    panel.focus({ preventScroll: true });\n  } else if (event.shiftKey && (active === first || active === panel)) {\n    event.preventDefault();\n    last.focus();\n  } else if (!event.shiftKey && active === last) {\n    event.preventDefault();\n    first.focus();\n  }\n}\n"},{"path":"lib/layers.ts","type":"util","content":"/**\n * Every open overlay on the page, innermost last: focus traps, modal scopes\n * and dismissable menus all register here. Overlays have no shared z-order to\n * consult, and a menu inside a drawer portals out of it, so this stack is how\n * they agree on who answers Escape and where focus may go.\n */\n\nexport interface Layer {\n  /** Whether `target` belongs to this layer (its panel, portals, trigger). */\n  contains: (target: Node) => boolean;\n  /** True for focus traps and modal scopes, which pull stray focus back. */\n  traps?: boolean;\n  /** False for a layer that ignores Escape, so the key reaches the one below. */\n  escape?: boolean;\n}\n\nconst stack: Layer[] = [];\n// The innermost layer when each Escape began, read before any handler runs,\n// so a layer that closes itself mid-event can't hand the key to the one below.\nconst escapeOwners = new WeakMap<Event, Layer | undefined>();\nlet listening = false;\n\nfunction innermostEscapeLayer() {\n  for (let index = stack.length - 1; index >= 0; index--) {\n    if (stack[index].escape !== false) return stack[index];\n  }\n  return undefined;\n}\n\nfunction recordEscapeOwner(event: KeyboardEvent) {\n  if (event.key === \"Escape\") escapeOwners.set(event, innermostEscapeLayer());\n}\n\n/** Registers an open layer. Call the returned function when it closes. */\nexport function openLayer(layer: Layer) {\n  if (!listening) {\n    document.addEventListener(\"keydown\", recordEscapeOwner, true);\n    listening = true;\n  }\n  stack.push(layer);\n  return () => {\n    const index = stack.indexOf(layer);\n    if (index !== -1) stack.splice(index, 1);\n  };\n}\n\n/** Whether this Escape is `layer`'s to answer: it was innermost when the key went down. */\nexport function ownsEscape(layer: Layer, event: KeyboardEvent) {\n  if (event.defaultPrevented) return false;\n  const owner = escapeOwners.has(event)\n    ? escapeOwners.get(event)\n    : innermostEscapeLayer();\n  return owner === layer;\n}\n\n/** Whether `target` sits in a layer opened on top of `layer`. */\nexport function inLayerAbove(layer: Layer, target: Node) {\n  const index = stack.indexOf(layer);\n  return stack.slice(index + 1).some((above) => above.contains(target));\n}\n\n/** Whether a focus trap is open on top of `layer`; stray focus is that one's to pull back. */\nexport function trapAbove(layer: Layer) {\n  const index = stack.indexOf(layer);\n  return stack.slice(index + 1).some((above) => above.traps);\n}\n\n/**\n * Whether `target` belongs to any open layer other than `layer`. Pass\n * `traps: false` to count only dismissable overlays, not the traps under them.\n */\nexport function inOtherLayer(layer: Layer, target: Node, { traps = true } = {}) {\n  return stack.some(\n    (other) => other !== layer && (traps || !other.traps) && other.contains(target),\n  );\n}\n"},{"path":"components/previews/motion/bottom-sheet.preview.tsx","type":"preview","content":"\"use client\";\n\nimport { useState } from \"react\";\nimport { BottomSheet } from \"@/components/motion/bottom-sheet\";\n\nexport function BottomSheetPreview() {\n  const [open, setOpen] = useState(false);\n  return (\n    <>\n      <button\n        type=\"button\"\n        onClick={() => setOpen(true)}\n        className=\"inline-flex h-10 items-center rounded-full border border-border bg-card px-5 text-sm font-medium text-foreground press hover:border-(--color-border-strong)\"\n      >\n        Open bottom sheet\n      </button>\n      <BottomSheet\n        open={open}\n        onOpenChange={setOpen}\n        snapPoints={[0.4, 0.85]}\n        title=\"Quick actions\"\n        description=\"Drag the handle, fling, or swipe down to dismiss.\"\n      >\n        <ul className=\"divide-y divide-border\">\n          {[\"Share\", \"Duplicate\", \"Move to folder\", \"Rename\", \"Archive\", \"Delete\"].map((item) => (\n            <li key={item} className=\"py-3 text-sm text-foreground\">{item}</li>\n          ))}\n        </ul>\n        <div className=\"py-12 text-center text-xs text-muted-foreground\">\n          Fling up to expand, fling down to dismiss.\n        </div>\n      </BottomSheet>\n    </>\n  );\n}\n"}]}