{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"popup","title":"Popup","description":"A popup that opens around your finger. The current choice stays where you pressed it, so you slide to the next one and let go.","type":"registry:component","author":"Detent","dependencies":["motion"],"registryDependencies":["https://detentui.com/r/theme.json"],"files":[{"path":"components/detent/components/popup/Popup.tsx","target":"components/detent/components/popup/Popup.tsx","type":"registry:component","content":"'use client'\n\nimport { useEffect, useId, useLayoutEffect, useRef, useState } from 'react'\nimport type { KeyboardEvent, PointerEvent } from 'react'\nimport { animate, motion, useMotionValue, useReducedMotion, useTransform } from 'motion/react'\nimport { clamp, feedback, lerp, notched } from '@/components/detent/lib/detent'\nimport { Layer, fill, frameOf, inFrame, useDismiss, useHost, useReposition } from '@/components/detent/lib/layer'\n\nconst ROW = 36\nconst PAD = 6\nconst LEFT = 14\nconst RIGHT = 34\nconst OPEN = { type: 'spring', bounce: 0, duration: 0.3 } as const\nconst SHUT = { type: 'spring', bounce: 0, duration: 0.24 } as const\nconst FOLLOW = { type: 'spring', stiffness: 900, damping: 60 } as const\nconst LOST = 48\nconst NOW = { duration: 0 } as const\nconst EDGE = 'drop-shadow(0 0 .5px var(--edge)) drop-shadow(0 1px 1px rgb(0 0 0 / .05))'\nconst INK = '[--edge:rgb(0_0_0/.3)] [[data-theme=dark]_&]:[--edge:rgb(255_255_255/.3)]'\n\nconst rubber = (d: number) => 18 * (1 - 1 / (1 + d / 60))\n\nfunction Check() {\n  return (\n    <svg viewBox=\"0 0 14 14\" aria-hidden className=\"absolute right-3.5 size-3.5 self-center\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n      <path d=\"M2.5 7.5 5.6 10.5 11.5 3.5\" />\n    </svg>\n  )\n}\n\nfunction Updown() {\n  return (\n    <svg viewBox=\"0 0 10 14\" aria-hidden className=\"h-3.5 w-2.5\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n      <path d=\"M2 5 5 2l3 3M2 9l3 3 3-3\" />\n    </svg>\n  )\n}\n\nexport type PopupProps = {\n  options: string[]\n  value?: string\n  defaultValue?: string\n  onChange?: (value: string) => void\n  label?: string\n  name?: string\n  radius?: number\n  textClass?: string\n  className?: string\n}\n\nexport function Popup({ options, value: controlled, defaultValue, onChange, label, name, radius = 10, textClass = 'text-[14px] font-medium', className = '' }: PopupProps) {\n  const reduce = !!useReducedMotion()\n  const id = useId()\n  const { contained, frame } = useHost()\n  const [inner, setInner] = useState(() => Math.max(0, options.indexOf(defaultValue ?? '')))\n  const value = controlled === undefined ? inner : Math.max(0, options.indexOf(controlled))\n  const start = value\n  const [shown, setShown] = useState(false)\n  const [active, setActive] = useState(start)\n  const [from, setFrom] = useState(start)\n  const [spot, setSpot] = useState({ x: 0, y: 0 })\n  const [width, setWidth] = useState(0)\n\n  const pill = useRef<HTMLButtonElement>(null)\n  const sheet = useRef<HTMLDivElement>(null)\n  const probes = useRef<(HTMLSpanElement | null)[]>([])\n  const nudge = useRef({ x: 0, y: 0 })\n  const live = useRef({ mode: 'shut' as 'shut' | 'press' | 'tap', y0: 0, u0: 0, left: false, lost: false, active: start, value: start, l: 0, r: 0 })\n\n  const open = useMotionValue(0)\n  const anchor = useMotionValue(start)\n  const plate = useMotionValue(start * ROW)\n  const pull = useMotionValue(0)\n  const pillW = useMotionValue(0)\n  const on = useMotionValue(1)\n  const fade = useMotionValue(0)\n  const pillO = useTransform(fade, (f) => 1 - f)\n  const height = PAD * 2 + ROW * options.length\n  const widthRef = useRef(0)\n\n  const y = useTransform([open, anchor, pull], ([o, a, p]: number[]) => -(PAD + ROW * a) + nudge.current.y * o + p)\n  const x = useTransform(open, (o) => nudge.current.x * o)\n  const clip = useTransform([open, anchor, pillW], ([o, a, w]: number[]) => {\n    const top = (PAD + ROW * a) * (1 - o)\n    const bottom = (height - PAD - ROW * (a + 1)) * (1 - o)\n    return `inset(${top}px ${(widthRef.current - w) * (1 - o)}px ${bottom}px 0 round ${lerp(radius, 16, o)}px)`\n  })\n  const window_ = useTransform(plate, (p) => `inset(${PAD + p}px ${PAD}px ${height - PAD - ROW - p}px ${PAD}px round 10px)`)\n  const inverted = useTransform([open, on], ([o, v]: number[]) => clamp((o - 0.3) / 0.5, 0, 1) * v)\n\n  const widths = () => probes.current.map((p) => (p?.offsetWidth ?? 0) + LEFT + RIGHT)\n\n  useLayoutEffect(() => {\n    const read = () => {\n      const all = widths()\n      pillW.set(all[live.current.value])\n      widthRef.current = Math.max(...all)\n      setWidth(widthRef.current)\n    }\n    read()\n    void document.fonts?.ready.then(read)\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [options.join('\\n')])\n\n  useEffect(() => {\n    if (live.current.mode !== 'shut' || live.current.value === value) return\n    live.current.value = value\n    anchor.set(value)\n    pillW.set(widths()[value])\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [value])\n\n  const measure = () => {\n    const f = frameOf(frame)\n    const box = inFrame(pill.current!.getBoundingClientRect(), f)\n    return { f, box }\n  }\n\n  const mark = (i: number) => {\n    if (live.current.active === i) return\n    live.current.active = i\n    setActive(i)\n  }\n\n  const show = (instant: boolean) => {\n    const s = live.current\n    const { f, box } = measure()\n    const top = box.y - PAD - ROW * s.value\n    nudge.current = {\n      x: Math.min(0, f.width - 8 - (box.x + widthRef.current)),\n      y: top < 12 ? 12 - top : Math.min(0, f.height - 12 - (top + height)),\n    }\n    setSpot({ x: box.x, y: box.y })\n    const at = pill.current!.getBoundingClientRect()\n    s.l = at.left + nudge.current.x\n    s.r = s.l + widthRef.current\n    s.active = s.value\n    setActive(s.value)\n    setFrom(s.value)\n    plate.set(s.value * ROW)\n    fade.set(1)\n    on.set(1)\n    if (sheet.current) sheet.current.style.pointerEvents = ''\n    setShown(true)\n    anchor.set(s.value)\n    animate(pull, 0, NOW)\n    animate(open, 1, instant || reduce ? NOW : OPEN)\n  }\n\n  const hide = (instant = false) => {\n    const s = live.current\n    s.mode = 'shut'\n    if (sheet.current) sheet.current.style.pointerEvents = 'none'\n    animate(pull, 0, SHUT)\n    animate(pillW, widths()[s.value], instant || reduce ? NOW : SHUT)\n    animate(fade, 0, instant || reduce ? NOW : { duration: 0.18, ease: [0.2, 0.9, 0.25, 1] })\n    animate(open, 0, instant || reduce ? NOW : SHUT).then(() => {\n      if (live.current.mode === 'shut') setShown(false)\n    })\n  }\n\n  const commit = (i: number) => {\n    const changed = live.current.value !== i\n    live.current.value = i\n    setInner(i)\n    hide()\n    if (changed) onChange?.(options[i])\n  }\n\n  const down = (e: PointerEvent<HTMLButtonElement>) => {\n    if (e.button !== 0) return\n    const s = live.current\n    feedback.wake(true)\n    if (s.mode !== 'shut') return hide()\n    e.currentTarget.setPointerCapture(e.pointerId)\n    s.mode = 'press'\n    s.y0 = e.clientY\n    s.u0 = s.value * ROW\n    s.left = false\n    s.lost = false\n    show(false)\n  }\n\n  const move = (e: PointerEvent<HTMLButtonElement>) => {\n    const s = live.current\n    if (s.mode !== 'press') return\n    const d = e.clientY - s.y0\n    s.lost = e.clientX < s.l - LOST || e.clientX > s.r + LOST\n    on.set(s.lost ? 0 : 1)\n    const hi = ROW * (options.length - 1)\n    const u = s.u0 + d\n    pull.set(u < 0 ? -rubber(-u) : u > hi ? rubber(u - hi) : 0)\n    const near = clamp(u, 0, hi)\n    plate.set(notched(near, options.map((_, i) => i * ROW), ROW / 2, 0.9))\n    const i = Math.round(near / ROW)\n    if (i !== s.active) {\n      s.left = true\n      mark(i)\n      feedback.tick()\n    }\n  }\n\n  const up = () => {\n    const s = live.current\n    if (s.mode !== 'press') return\n    if (s.lost) return hide()\n    if (!s.left) {\n      s.mode = 'tap'\n      pull.set(0)\n      animate(plate, s.active * ROW, FOLLOW)\n      return\n    }\n    commit(s.active)\n  }\n\n  const hover = (i: number) => {\n    if (live.current.mode !== 'tap') return\n    mark(i)\n    animate(plate, i * ROW, reduce ? NOW : FOLLOW)\n  }\n\n  const keys = (e: KeyboardEvent) => {\n    const s = live.current\n    if (s.mode === 'shut') {\n      if (!['ArrowUp', 'ArrowDown', 'Enter', ' '].includes(e.key)) return\n      e.preventDefault()\n      s.mode = 'tap'\n      return show(true)\n    }\n    const last = options.length - 1\n    const go = { ArrowUp: s.active - 1, ArrowDown: s.active + 1, Home: 0, End: last }[e.key]\n    if (go !== undefined) {\n      e.preventDefault()\n      const i = clamp(go, 0, last)\n      mark(i)\n      plate.set(i * ROW)\n    } else if (e.key === 'Enter' || e.key === ' ') {\n      e.preventDefault()\n      commit(s.active)\n    }\n  }\n\n  useDismiss(shown, [pill, sheet], (why) => hide(why === 'escape'))\n  useReposition(shown, () => {\n    const { box } = measure()\n    setSpot({ x: box.x, y: box.y })\n  })\n\n  const rows = (ghost: boolean) =>\n    options.map((o, i) => (\n      <div\n        key={o}\n        id={ghost ? undefined : `${id}-${i}`}\n        role={ghost ? undefined : 'option'}\n        aria-selected={ghost ? undefined : i === value}\n        onPointerMove={ghost ? undefined : () => hover(i)}\n        onClick={ghost ? undefined : () => live.current.mode === 'tap' && commit(i)}\n        className=\"relative flex items-center whitespace-nowrap\"\n        style={{ height: ROW, paddingLeft: LEFT }}\n      >\n        {o}\n        {i === from && <Check />}\n      </div>\n    ))\n\n  return (\n    <div className={`relative inline-block max-w-full align-middle ${INK} ${className}`} style={{ height: ROW }}>\n      {name && <input type=\"hidden\" name={name} value={options[value]} />}\n      <motion.button\n        ref={pill}\n        type=\"button\"\n        aria-haspopup=\"listbox\"\n        aria-expanded={shown}\n        aria-label={label}\n        aria-activedescendant={shown ? `${id}-${active}` : undefined}\n        onPointerDown={down}\n        onPointerMove={move}\n        onPointerUp={up}\n        onPointerCancel={() => hide()}\n        onTouchEnd={(e) => e.preventDefault()}\n        onKeyDown={keys}\n        onBlur={() => live.current.mode === 'tap' && hide(true)}\n        className={`ring-focus relative flex touch-none select-none items-center whitespace-nowrap bg-surface text-ink before:absolute before:-inset-y-1 before:inset-x-0 ${textClass}`}\n        style={{ height: ROW, paddingLeft: LEFT, paddingRight: 10, filter: EDGE, borderRadius: radius, touchAction: 'none', opacity: pillO }}\n      >\n        <span style={{ paddingRight: RIGHT - 10 }}>{options[value]}</span>\n        <span className=\"absolute right-[9px] text-ink-3\">\n          <Updown />\n        </span>\n      </motion.button>\n\n      <div aria-hidden className={`pointer-events-none invisible absolute left-0 top-0 ${textClass}`}>\n        {options.map((o, i) => (\n          <span\n            key={o}\n            ref={(el) => {\n              probes.current[i] = el\n            }}\n            className=\"absolute whitespace-nowrap\"\n          >\n            {o}\n          </span>\n        ))}\n      </div>\n\n      {shown && (\n        <Layer>\n          <motion.div className={`pointer-events-none ${fill(contained).split(' ')[0]} z-50 ${INK}`} style={{ left: spot.x, top: spot.y, x, y, width, height, opacity: fade }}>\n            <motion.div aria-hidden className=\"pointer-events-none absolute inset-0 rounded-[16px] shadow-[0_18px_36px_-6px_rgb(0_0_0/0.22)]\" style={{ opacity: open }} />\n            <div className=\"absolute inset-0\" style={{ filter: EDGE }}>\n              <motion.div\n                ref={sheet}\n                role=\"listbox\"\n                aria-label={label}\n                onPointerDown={(e) => e.preventDefault()}\n                onMouseDown={(e) => e.preventDefault()}\n                className={`pointer-events-auto relative select-none bg-surface text-ink ${textClass}`}\n                style={{ clipPath: clip, width, height, paddingTop: PAD, paddingBottom: PAD }}\n              >\n                {rows(false)}\n                <motion.div aria-hidden className=\"pointer-events-none absolute inset-0 bg-ink text-surface\" style={{ clipPath: window_, opacity: inverted, paddingTop: PAD }}>\n                  {rows(true)}\n                </motion.div>\n              </motion.div>\n            </div>\n          </motion.div>\n        </Layer>\n      )}\n    </div>\n  )\n}\n"},{"path":"components/detent/lib/detent.ts","target":"components/detent/lib/detent.ts","type":"registry:component","content":"import { useLayoutEffect, useRef, useState } from 'react'\nimport type { KeyboardEvent, PointerEvent as ReactPointerEvent } from 'react'\nimport { useReducedMotion } from 'motion/react'\n\nexport const clamp = (n: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, n))\nexport const lerp = (a: number, b: number, t: number) => a + (b - a) * t\nexport const glide = (a: number, b: number, t: number) => (a > 0 && b > 0 ? a * Math.pow(b / a, t) : lerp(a, b, t))\n\nexport const notched = (x: number, centres: number[], reach: number, hold = 0.7) => {\n  for (const c of centres) {\n    const d = (x - c) / reach\n    if (Math.abs(d) < 1) return x - ((hold * reach) / Math.PI) * Math.sin(Math.PI * d) * ((1 + Math.cos(Math.PI * d)) / 2)\n  }\n  return x\n}\n\nexport const at = (list: number[], i: number, mix = lerp) => {\n  const a = clamp(Math.floor(i), 0, list.length - 1)\n  return mix(list[a], list[Math.min(list.length - 1, a + 1)], clamp(i - a, 0, 1))\n}\n\nexport type Spring = { w: number; z: number }\nexport type Body = { x: number; v: number }\n\nexport function springStep(b: Body, target: number, { w, z }: Spring, dt: number) {\n  const a = b.x - target\n  if (z >= 1) {\n    const e = Math.exp(-w * dt)\n    const k = b.v + w * a\n    b.x = target + (a + k * dt) * e\n    b.v = (b.v - k * w * dt) * e\n    return b\n  }\n  const wd = w * Math.sqrt(1 - z * z)\n  const e = Math.exp(-z * w * dt)\n  const c = Math.cos(wd * dt)\n  const s = Math.sin(wd * dt)\n  const B = (b.v + z * w * a) / wd\n  b.x = target + e * (a * c + B * s)\n  b.v = e * ((B * wd - z * w * a) * c - (a * wd + z * w * B) * s)\n  return b\n}\n\nconst still = (b: Body, target: number, eps: number) => Math.abs(b.x - target) < eps && Math.abs(b.v) < eps * 12\n\nconst rubber = (d: number, reach: number, s: number) => reach * (1 - 1 / (1 + (s * d) / reach))\n\nfunction tracker(window = 90) {\n  const samples: [number, number][] = []\n  return {\n    clear: () => (samples.length = 0),\n    add(t: number, x: number) {\n      samples.push([t, x])\n      while (samples.length > 2 && t - samples[0][0] > window) samples.shift()\n    },\n    read(now: number) {\n      const n = samples.length\n      if (n < 2 || now - samples[n - 1][0] > 60) return 0\n      const mt = samples.reduce((s, p) => s + p[0], 0) / n\n      const mx = samples.reduce((s, p) => s + p[1], 0) / n\n      const num = samples.reduce((s, p) => s + (p[0] - mt) * (p[1] - mx), 0)\n      const den = samples.reduce((s, p) => s + (p[0] - mt) ** 2, 0)\n      return den > 0 ? (num / den) * 1000 : 0\n    },\n  }\n}\n\nexport const feedback = (() => {\n  let audio: AudioContext | null = null\n  let last = 0\n  let asked = false\n  let on = true\n  let cancel: (() => void) | null = null\n  let holding = false\n  const open = () => {\n    cancel = null\n    audio ??= new AudioContext()\n    if (asked && audio.state === 'suspended') void audio.resume()\n  }\n  const warm = () => {\n    if (!on || audio || cancel || holding || typeof AudioContext === 'undefined') return\n    if (typeof requestIdleCallback === 'function') {\n      const id = requestIdleCallback(open, { timeout: 300 })\n      cancel = () => cancelIdleCallback(id)\n    } else {\n      const id = setTimeout(open, 60)\n      cancel = () => clearTimeout(id)\n    }\n  }\n  const letGo = () => {\n    holding = false\n    removeEventListener('pointerup', letGo)\n    removeEventListener('pointercancel', letGo)\n    warm()\n  }\n  const play = (heavy: boolean, loud: number) => {\n    if (!audio) return\n    const t = audio.currentTime\n    const tone = audio.createOscillator()\n    const gain = audio.createGain()\n    tone.frequency.setValueAtTime(heavy ? 900 : 1500, t)\n    tone.frequency.exponentialRampToValueAtTime(heavy ? 240 : 420, t + 0.014)\n    gain.gain.setValueAtTime((heavy ? 0.034 : 0.02) * loud, t)\n    gain.gain.exponentialRampToValueAtTime(0.0001, t + (heavy ? 0.03 : 0.016))\n    tone.connect(gain).connect(audio.destination)\n    tone.start(t)\n    tone.stop(t + 0.034)\n  }\n  return {\n    set(next: boolean) {\n      on = next\n      if (on) return\n      cancel?.()\n      cancel = null\n      void audio?.suspend()\n    },\n    warm,\n    wake(grab = false) {\n      if (!on) return\n      asked = true\n      if (audio) {\n        if (audio.state === 'suspended') void audio.resume()\n      } else if (!grab) warm()\n      else if (!holding && typeof AudioContext !== 'undefined') {\n        cancel?.()\n        cancel = null\n        holding = true\n        addEventListener('pointerup', letGo)\n        addEventListener('pointercancel', letGo)\n      }\n    },\n    tick(heavy = false, speed = 1) {\n      if (!on || !asked) return\n      const now = performance.now()\n      if (now - last < 34) return\n      last = now\n      if (typeof navigator !== 'undefined' && 'vibrate' in navigator) navigator.vibrate(heavy ? 10 : 4)\n      if (!audio) return\n      const loud = 0.45 + 0.55 * clamp(speed, 0, 1)\n      if (audio.state === 'running') play(heavy, loud)\n      else void audio.resume().then(() => on && play(heavy, loud))\n    },\n  }\n})()\n\nexport type FrameInfo = {\n  dt: number\n  mode: Mode\n  live: boolean\n}\nexport type Mode = 'rest' | 'held' | 'coast' | 'settle' | 'sweep'\n\nexport type DetentOptions = {\n  value: number\n  stops?: number[]\n  min?: number\n  max?: number\n  magnet?: number\n  reach?: number\n  throw?: number\n  spring?: Spring\n  catch?: Spring\n  muted?: boolean\n  reduce?: boolean\n  onFrame: (x: number, v: number, info: FrameInfo) => boolean | void\n  onStop?: (i: number) => void\n  onRest?: (x: number, i: number) => void\n}\n\nexport type Detent = ReturnType<typeof createDetent>\n\nconst SPRING: Spring = { w: 17, z: 0.92 }\nconst CATCH: Spring = { w: 24, z: 1 }\nconst MOMENTUM = 1\nconst SWIFT = 8\n\nexport function createDetent(read: () => DetentOptions) {\n  const init = read()\n  const body: Body = { x: init.value, v: 0 }\n  const residual: Body = { x: 0, v: 0 }\n  const track = tracker()\n  let mode: Mode = 'rest'\n  let hand = init.value\n  let target = init.value\n  let spring = SPRING\n  let friction = 4\n  let frame = 0\n  let then = 0\n  let feel = false\n  let stop = -1\n  let shown = init.value\n  let sweep = { from: 0, to: 0, t: 0, T: 1 }\n  let keyAt = 0\n\n  const span = () => {\n    const o = read()\n    const s = o.stops\n    return { o, s, lo: o.min ?? s?.[0] ?? -Infinity, hi: o.max ?? s?.[s.length - 1] ?? Infinity, m: o.reduce ? 0 : o.magnet ?? 0, reach: o.reach ?? ((s && s.length > 1 ? s[1] - s[0] : 1) * 0.5) }\n  }\n\n  const out = (u: number) => {\n    const { s, lo, hi, m, reach } = span()\n    if (u < lo) return lo - rubber(lo - u, reach, 1 - m)\n    if (u > hi) return hi + rubber(u - hi, reach, 1 - m)\n    if (!s || s.length < 2 || !m) return u\n    let i = 0\n    while (i < s.length - 2 && u > s[i + 1]) i++\n    const t = (u - s[i]) / (s[i + 1] - s[i])\n    return s[i] + (s[i + 1] - s[i]) * (t - (m * Math.sin(2 * Math.PI * t)) / (2 * Math.PI))\n  }\n  const slope = (u: number) => {\n    const e = 1e-4\n    return (out(u + e) - out(u - e)) / (2 * e)\n  }\n  const nearest = (x: number) => {\n    const { s, lo, hi } = span()\n    if (!s?.length) return { i: -1, value: clamp(x, lo, hi) }\n    let i = 0\n    for (let k = 1; k < s.length; k++) if (Math.abs(s[k] - x) < Math.abs(s[i] - x)) i = k\n    return { i, value: s[i] }\n  }\n\n  const start = () => {\n    if (frame || typeof requestAnimationFrame === 'undefined') return\n    then = performance.now()\n    frame = requestAnimationFrame(loop)\n  }\n\n  const land = () => {\n    mode = 'rest'\n    body.x = target\n    body.v = 0\n    feel = false\n  }\n\n  const emit = (dt: number) => {\n    const { o } = span()\n    const x = out(body.x)\n    const v = slope(body.x) * body.v\n    shown = x\n    const n = nearest(x).i\n    if (n !== stop) {\n      if (stop >= 0) {\n        o.onStop?.(n)\n        if (feel && !o.muted) feedback.tick(false, Math.abs(v) / (gap() * SWIFT))\n      }\n      stop = n\n    }\n    return o.onFrame(x, v, { dt, mode, live: mode === 'held' || mode === 'coast' })\n  }\n\n  function advance(dt: number, now: number) {\n    const { o } = span()\n    if (mode === 'held' || mode === 'sweep') {\n      if (mode === 'sweep') {\n        sweep.t = Math.min(sweep.T, sweep.t + dt)\n        const p = sweep.t / sweep.T\n        const next = lerp(sweep.from, sweep.to, p * p * p * (p * (6 * p - 15) + 10))\n        body.v = dt > 0 ? (next - hand) / dt + residual.v : body.v\n        hand = next\n      }\n      springStep(residual, 0, o.catch ?? CATCH, dt)\n      body.x = hand + residual.x\n      if (mode === 'held') body.v = track.read(now) + residual.v\n      if (mode === 'sweep' && sweep.t >= sweep.T && still(residual, 0, 1e-3)) {\n        target = sweep.to\n        land()\n        return true\n      }\n      return false\n    }\n    if (mode === 'coast') {\n      const e = Math.exp(-friction * dt)\n      body.x += (body.v / friction) * (1 - e)\n      body.v *= e\n      const left = Math.abs(target - body.x)\n      if (left < coastEnd() || Math.abs(body.v) < 0.5 * coastEnd() || Math.sign(target - body.x) !== Math.sign(body.v)) mode = 'settle'\n      return false\n    }\n    if (mode === 'settle') {\n      springStep(body, target, spring, dt)\n      if (still(body, target, settleEps())) {\n        land()\n        return true\n      }\n    }\n    return false\n  }\n  const gap = () => {\n    const { s } = span()\n    return s && s.length > 1 ? (s[s.length - 1] - s[0]) / (s.length - 1) : 1\n  }\n  const landing = (s: Spring, v: number): Spring => (Math.abs(v) > gap() * MOMENTUM ? s : { ...s, z: 1 })\n  const coastEnd = () => gap() * 0.7\n  const settleEps = () => gap() * 0.0015\n\n  function loop(now: number) {\n    frame = 0\n    const dt = clamp((now - then) / 1000, 1 / 240, 0.05)\n    then = now\n    const was = mode\n    const landed = was !== 'rest' && advance(dt, now)\n    const more = emit(dt)\n    if (landed) {\n      const { o } = span()\n      o.onRest?.(out(body.x), nearest(out(body.x)).i)\n    }\n    if (mode !== 'rest' || more) frame = requestAnimationFrame(loop)\n  }\n\n  const launch = () => {\n    const away = target - body.x\n    if (Math.abs(away) > gap() * 1.6 && Math.sign(away) === Math.sign(body.v)) {\n      friction = clamp(Math.abs(body.v / away), 2.5, 9)\n      mode = 'coast'\n    } else mode = 'settle'\n    start()\n  }\n\n  const to = (value: number, opts: { spring?: Spring; instant?: boolean; velocity?: number; feel?: boolean } = {}) => {\n    const { o, lo, hi } = span()\n    target = clamp(value, lo, hi)\n    if (opts.velocity !== undefined) body.v = opts.velocity\n    feel = !!opts.feel\n    if (opts.instant || o.reduce) {\n      land()\n      emit(0)\n      o.onRest?.(target, nearest(target).i)\n      return\n    }\n    if (mode === 'held') {\n      body.x = hand + residual.x\n      residual.x = residual.v = 0\n    }\n    spring = opts.spring ?? landing(o.spring ?? SPRING, opts.velocity ?? 0)\n    mode = 'settle'\n    start()\n  }\n\n  const api = {\n    get: () => shown,\n    place: () => body.x + (mode === 'rest' ? 0 : body.v / (span().o.catch ?? CATCH).w),\n    velocity: () => slope(body.x) * body.v,\n    mode: () => mode,\n    target: () => target,\n    nearest,\n\n    set(value: number) {\n      body.x = target = hand = value\n      body.v = residual.x = residual.v = 0\n      mode = 'rest'\n      feel = false\n      stop = -1\n      emit(0)\n      stop = nearest(shown).i\n    },\n    to,\n\n    grab(place: number, { feel: touching = true }: { feel?: boolean } = {}) {\n      const { o } = span()\n      residual.x = o.reduce ? 0 : body.x - place\n      residual.v = o.reduce ? 0 : body.v\n      hand = place\n      track.clear()\n      track.add(performance.now(), place)\n      feel = touching\n      mode = 'held'\n      start()\n    },\n    drag(place: number) {\n      if (mode !== 'held') return\n      hand = place\n      track.add(performance.now(), place)\n    },\n    release({ flick = true, home }: { flick?: boolean; home?: number } = {}) {\n      if (mode !== 'held') return\n      const { o, lo, hi } = span()\n      const flung = flick ? track.read(performance.now()) : 0\n      body.x = hand + residual.x\n      body.v = flung + residual.v\n      residual.x = residual.v = 0\n      const rest = home ?? nearest(clamp(body.x + flung * (o.throw ?? 0.16), lo, hi)).value\n      target = rest\n      spring = landing(o.spring ?? SPRING, body.v)\n      if (o.reduce) {\n        to(rest)\n        return\n      }\n      launch()\n    },\n    fling(velocity: number) {\n      const { o, lo, hi } = span()\n      const rest = nearest(clamp(body.x + velocity * (o.throw ?? 0.16), lo, hi)).value\n      if (o.reduce) return to(rest)\n      residual.x = residual.v = 0\n      body.v = velocity\n      feel = false\n      target = rest\n      spring = o.spring ?? SPRING\n      launch()\n    },\n    sweep(value: number, ms: number) {\n      const { o } = span()\n      if (o.reduce) return to(value)\n      residual.x = 0\n      residual.v = body.v\n      hand = body.x\n      sweep = { from: body.x, to: value, t: 0, T: Math.max(0.05, ms / 1000) }\n      feel = false\n      mode = 'sweep'\n      start()\n    },\n    step(by: number, repeat = false) {\n      const { o, s, lo, hi } = span()\n      const now = performance.now()\n      const quick = repeat || now - keyAt < 110\n      keyAt = now\n      if (!o.muted) feedback.wake()\n      const from = mode === 'rest' ? body.x : target\n      const next = s ? s[clamp(nearest(from).i + by, 0, s.length - 1)] : clamp(from + by, lo, hi)\n      to(next, { instant: quick, feel: !quick })\n      return next\n    },\n    wake: start,\n    dispose() {\n      cancelAnimationFrame(frame)\n      frame = 0\n    },\n  }\n  return api\n}\n\nexport function useDetent(options: Omit<DetentOptions, 'reduce'>) {\n  const reduce = !!useReducedMotion()\n  const latest = useRef<DetentOptions>({ ...options, reduce })\n  useLayoutEffect(() => {\n    latest.current = { ...options, reduce }\n  })\n  const [d] = useState(() => createDetent(() => latest.current))\n  useLayoutEffect(() => () => d.dispose(), [d])\n  return d\n}\n\nexport type DragConfig = {\n  axis?: 'x' | 'y'\n  box: () => HTMLElement | null\n  units: number\n  absolute?: (fraction: number) => number\n  hover?: boolean\n  follow?: boolean\n  onTap?: (fraction: number) => void\n  onLeave?: () => void\n  onGrab?: () => void\n  release?: () => { flick?: boolean; home?: number }\n  muted?: boolean\n}\n\nconst SLOP = { mouse: 5, touch: 10 }\n\nexport function dragProps(d: Detent, read: () => DragConfig) {\n  let press: { id: number; px: number; py: number; from: number; moved: boolean; held: boolean; caught: boolean } | null = null\n  const place = (e: ReactPointerEvent) => {\n    const c = read()\n    const r = c.box()?.getBoundingClientRect()\n    if (!r) return null\n    const horizontal = (c.axis ?? 'x') === 'x'\n    const size = (horizontal ? r.width : r.height) || 1\n    const px = horizontal ? e.clientX : e.clientY\n    return { fraction: (px - (horizontal ? r.left : r.top)) / size, px, unit: size / c.units, c }\n  }\n  const hand = (e: ReactPointerEvent) => {\n    const p = place(e)\n    if (!p || !press) return null\n    return p.c.absolute && !press.caught ? p.c.absolute(p.fraction) : press.from + (p.px - ((p.c.axis ?? 'x') === 'x' ? press.px : press.py)) / p.unit\n  }\n  const take = (e: ReactPointerEvent) => {\n    if (!press) return\n    const at = hand(e)\n    if (at === null) return\n    press.held = true\n    read().onGrab?.()\n    d.grab(at)\n  }\n  const inFlight = () => d.mode() !== 'rest' && d.mode() !== 'held' && d.mode() !== 'sweep'\n  return {\n    onPointerDown(e: ReactPointerEvent) {\n      if (e.button) return\n      const c = read()\n      if (!c.muted) feedback.wake(true)\n      e.currentTarget.setPointerCapture?.(e.pointerId)\n      press = { id: e.pointerId, px: e.clientX, py: e.clientY, from: d.place(), moved: false, held: false, caught: inFlight() && !c.follow }\n      if (e.pointerType !== 'touch' || press.caught || !c.absolute) take(e)\n    },\n    onPointerMove(e: ReactPointerEvent) {\n      const c = read()\n      if (!c.muted && e.pointerType === 'mouse') feedback.warm()\n      if (press) {\n        if (e.pointerId !== press.id) return\n        if (!press.moved && Math.hypot(e.clientX - press.px, e.clientY - press.py) > (e.pointerType === 'touch' ? SLOP.touch : SLOP.mouse)) press.moved = true\n        if (!press.held && press.moved) take(e)\n        const at = hand(e)\n        if (at !== null && press.held) d.drag(at)\n        return\n      }\n      if (!c.hover || e.pointerType !== 'mouse' || (inFlight() && !c.follow)) return\n      const p = place(e)\n      if (!p || !c.absolute) return\n      const at = c.absolute(p.fraction)\n      if (d.mode() === 'held') d.drag(at)\n      else {\n        c.onGrab?.()\n        d.grab(at, { feel: false })\n      }\n    },\n    onPointerUp(e: ReactPointerEvent) {\n      if (!press || e.pointerId !== press.id) return\n      const { moved, held } = press\n      const p = place(e)\n      const c = read()\n      press = null\n      if (!moved && c.onTap && p) {\n        if (held) d.release({ flick: false, home: d.get() })\n        c.onTap(p.fraction)\n      } else if (held) d.release(c.release?.())\n    },\n    onPointerCancel(e: ReactPointerEvent) {\n      if (!press || e.pointerId !== press.id) return\n      const { held } = press\n      press = null\n      if (held) d.release({ flick: false })\n      read().onLeave?.()\n    },\n    onPointerLeave(e: ReactPointerEvent) {\n      if (press || e.pointerType !== 'mouse') return\n      const c = read()\n      if (!c.hover) return\n      if (d.mode() === 'held') d.release({ flick: false, home: d.get() })\n      c.onLeave?.()\n    },\n  }\n}\n\nexport const stepKey = (d: Detent, e: KeyboardEvent, keys: Record<string, number> = { ArrowLeft: -1, ArrowRight: 1 }) => {\n  const by = keys[e.key]\n  if (!by) return false\n  e.preventDefault()\n  d.step(by, e.repeat)\n  return true\n}\n\nconst COLUMN = 'display:inline-block;height:1.25em;overflow:hidden;vertical-align:top'\nconst STRIP = '0123456789'\n\nexport function createRoller(el: HTMLElement, format: (n: number) => string, pad = 0, built?: (columns: HTMLElement[]) => void) {\n  let shape = ''\n  let wheels: { strip: HTMLElement; col: HTMLElement }[] = []\n  let scale = 1\n\n  const build = (s: string, digits: number) => {\n    el.textContent = ''\n    wheels = []\n    const col = () => {\n      const c = document.createElement('span')\n      c.style.cssText = COLUMN\n      const strip = document.createElement('span')\n      strip.style.cssText = 'display:block;will-change:transform'\n      strip.innerHTML = [...STRIP, '0'].map((n) => `<span style=\"display:block;height:1.25em\">${n}</span>`).join('')\n      c.append(strip)\n      el.append(c)\n      wheels.push({ strip, col: c })\n    }\n    let padded = Math.max(0, pad - digits)\n    let leading = true\n    for (const ch of s) {\n      if (/\\d/.test(ch)) {\n        if (leading) for (; padded > 0; padded--) col()\n        leading = false\n        col()\n      } else {\n        const c = document.createElement('span')\n        c.textContent = ch\n        c.style.whiteSpace = 'pre'\n        el.append(c)\n      }\n    }\n    built?.([...el.children] as HTMLElement[])\n  }\n\n  return (value: number, speed = 0) => {\n    const s = format(value)\n    const digits = s.replace(/\\D/g, '')\n    const next = s.replace(/\\d/g, '0')\n    if (next !== shape) {\n      shape = next\n      build(s, digits.length)\n    }\n    const numeric = s.replace(/[^\\d.]/g, '').replace(/\\.$/, '')\n    const shownNumber = parseFloat(numeric)\n    const decimals = numeric.includes('.') ? numeric.length - numeric.indexOf('.') - 1 : 0\n    if (shownNumber > 0) scale = Math.abs(value) / shownNumber\n    const place = Math.pow(10, -decimals) * (scale || 1)\n    const q = Math.abs(value) / place\n    const free = clamp(Math.abs(speed) / (place * 6), 0, 1)\n    const count = wheels.length\n    const whole = digits.padStart(count, '0')\n    for (let k = 0; k < count; k++) {\n      const { strip, col } = wheels[count - 1 - k]\n      const unit = Math.pow(10, k)\n      const lower = q % unit\n      const carry = k === 0 ? q - Math.floor(q) : clamp(lower - (unit - 1), 0, 1)\n      const running = (Math.floor(q / unit) % 10) + carry\n      const digit = +whole[count - 1 - k]\n      const off = ((digit - running + 15) % 10) - 5\n      const pos = (running + off * (1 - free) + 10) % 10\n      strip.style.transform = `translateY(${(-pos * 100) / 11}%)`\n      if (pad) {\n        const higher = Math.floor(q / unit)\n        const fade = k === 0 ? 1 : higher > 0 ? 1 : clamp(lower - (unit - 1), 0, 1)\n        col.style.opacity = String(fade)\n      }\n    }\n  }\n}\n\nexport const write = (el: HTMLElement | null | undefined, text: string) => {\n  if (el && el.textContent !== text) el.textContent = text\n}\n"},{"path":"components/detent/lib/layer.tsx","target":"components/detent/lib/layer.tsx","type":"registry:component","content":"'use client'\n\nimport { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react'\nimport type { ReactNode, RefObject } from 'react'\nimport { createPortal } from 'react-dom'\nimport { motion, useReducedMotion } from 'motion/react'\nimport { spring } from '@/components/detent/lib/motion'\n\ntype Host = { layers: HTMLElement | null; frame: HTMLElement | null; recede: (on: boolean) => void }\nconst HostContext = createContext<Host | null>(null)\n\nexport function LayerHost({ children, className = '', layerClassName = '' }: { children: ReactNode; className?: string; layerClassName?: string }) {\n  const [layers, setLayers] = useState<HTMLElement | null>(null)\n  const [frame, setFrame] = useState<HTMLElement | null>(null)\n  const [back, setBack] = useState(0)\n  const reduce = useReducedMotion()\n  const recede = useCallback((on: boolean) => setBack((n) => Math.max(0, n + (on ? 1 : -1))), [])\n  const host = useMemo<Host>(() => ({ layers, frame, recede }), [layers, frame, recede])\n  return (\n    <HostContext.Provider value={host}>\n      <div ref={setFrame} className={`relative isolate overflow-hidden ${className}`}>\n        <motion.div className=\"h-full w-full origin-top\" initial={false} animate={reduce ? {} : { scale: back ? 0.97 : 1, opacity: back ? 0.92 : 1 }} transition={spring.soft}>\n          {children}\n        </motion.div>\n        <div ref={setLayers} className={`pointer-events-none absolute inset-0 z-50 ${layerClassName}`} />\n      </div>\n    </HostContext.Provider>\n  )\n}\n\nconst noop = () => () => {}\nexport const useMounted = () =>\n  useSyncExternalStore(\n    noop,\n    () => true,\n    () => false,\n  )\n\nexport function useHost() {\n  const host = useContext(HostContext)\n  return { contained: !!host, layers: host?.layers ?? null, frame: host?.frame ?? null, recede: host?.recede ?? (() => {}) }\n}\n\nexport function Layer({ children }: { children: ReactNode }) {\n  const mounted = useMounted()\n  const { contained, layers } = useHost()\n  if (!mounted) return null\n  if (contained && !layers) return null\n  return createPortal(children, contained ? layers! : document.body)\n}\n\nexport const fill = (contained: boolean) => (contained ? 'absolute inset-0' : 'fixed inset-0')\n\nexport type Box = { x: number; y: number; w: number; h: number }\nexport type Frame = { left: number; top: number; width: number; height: number; scale: number }\n\nexport function frameOf(frame: HTMLElement | null): Frame {\n  if (!frame) return { left: 0, top: 0, width: innerWidth, height: innerHeight, scale: 1 }\n  const r = frame.getBoundingClientRect()\n  return { left: r.left, top: r.top, width: frame.offsetWidth, height: frame.offsetHeight, scale: r.width / (frame.offsetWidth || r.width || 1) }\n}\n\nexport const inFrame = (r: { left: number; top: number; width: number; height: number }, f: Frame): Box => ({ x: (r.left - f.left) / f.scale, y: (r.top - f.top) / f.scale, w: r.width / f.scale, h: r.height / f.scale })\n\nexport type Side = 'top' | 'bottom' | 'left' | 'right'\nexport type Align = 'start' | 'center' | 'end'\nconst OPPOSITE: Record<Side, Side> = { top: 'bottom', bottom: 'top', left: 'right', right: 'left' }\nconst clampTo = (v: number, lo: number, hi: number) => Math.min(Math.max(v, lo), Math.max(lo, hi))\n\nexport function place(anchor: Box, size: { w: number; h: number }, frame: { width: number; height: number }, want: Side, align: Align = 'center', gap = 8, pad = 8) {\n  const room: Record<Side, number> = { top: anchor.y, bottom: frame.height - anchor.y - anchor.h, left: anchor.x, right: frame.width - anchor.x - anchor.w }\n  const across = want === 'top' || want === 'bottom'\n  const need = (across ? size.h : size.w) + gap + pad\n  const side = room[want] < need && room[OPPOSITE[want]] > room[want] ? OPPOSITE[want] : want\n  let x: number\n  let y: number\n  if (side === 'top' || side === 'bottom') {\n    y = side === 'bottom' ? anchor.y + anchor.h + gap : anchor.y - gap - size.h\n    x = align === 'start' ? anchor.x : align === 'end' ? anchor.x + anchor.w - size.w : anchor.x + anchor.w / 2 - size.w / 2\n    x = clampTo(x, pad, frame.width - pad - size.w)\n    y = clampTo(y, pad, frame.height - pad - size.h)\n  } else {\n    x = side === 'right' ? anchor.x + anchor.w + gap : anchor.x - gap - size.w\n    y = align === 'start' ? anchor.y : align === 'end' ? anchor.y + anchor.h - size.h : anchor.y + anchor.h / 2 - size.h / 2\n    y = clampTo(y, pad, frame.height - pad - size.h)\n    x = clampTo(x, pad, frame.width - pad - size.w)\n  }\n  const arrow = side === 'top' || side === 'bottom' ? clampTo(anchor.x + anchor.w / 2 - x, 14, size.w - 14) : clampTo(anchor.y + anchor.h / 2 - y, 14, size.h - 14)\n  return { x, y, side, arrow }\n}\n\nexport function originOf(side: Side, arrow: number) {\n  if (side === 'bottom') return `${arrow}px 0px`\n  if (side === 'top') return `${arrow}px 100%`\n  if (side === 'right') return `0px ${arrow}px`\n  return `100% ${arrow}px`\n}\n\nconst open: object[] = []\n\nexport function useDismiss(active: boolean, inside: RefObject<HTMLElement | null>[], onDismiss: (why: 'escape' | 'outside') => void) {\n  const me = useRef({})\n  const latest = useRef({ inside, onDismiss })\n  useLayoutEffect(() => {\n    latest.current = { inside, onDismiss }\n  })\n  useEffect(() => {\n    if (!active) return\n    const token = me.current\n    open.push(token)\n    const top = () => open[open.length - 1] === token\n    const key = (e: KeyboardEvent) => {\n      if (e.key !== 'Escape' || !top()) return\n      e.stopPropagation()\n      latest.current.onDismiss('escape')\n    }\n    const press = (e: PointerEvent) => {\n      if (!top()) return\n      const t = e.target as Node\n      if (latest.current.inside.some((r) => r.current?.contains(t))) return\n      latest.current.onDismiss('outside')\n    }\n    document.addEventListener('keydown', key, true)\n    document.addEventListener('pointerdown', press, true)\n    return () => {\n      open.splice(open.indexOf(token), 1)\n      document.removeEventListener('keydown', key, true)\n      document.removeEventListener('pointerdown', press, true)\n    }\n  }, [active])\n}\n\nconst FOCUSABLE = 'a[href],button:not([disabled]),input:not([disabled]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex=\"-1\"])'\n\nexport function useFocusTrap(box: RefObject<HTMLElement | null>, active: boolean, quiet: boolean, first?: RefObject<HTMLElement | null>) {\n  useEffect(() => {\n    if (!active || quiet) return\n    const before = document.activeElement as HTMLElement | null\n    const raf = requestAnimationFrame(() => {\n      const el = first?.current ?? box.current?.querySelector<HTMLElement>(FOCUSABLE) ?? box.current\n      el?.focus({ preventScroll: true })\n    })\n    const key = (e: KeyboardEvent) => {\n      if (e.key !== 'Tab' || !box.current) return\n      const all = [...box.current.querySelectorAll<HTMLElement>(FOCUSABLE)].filter((el) => el.offsetParent !== null)\n      if (!all.length) return e.preventDefault()\n      const i = all.indexOf(document.activeElement as HTMLElement)\n      const next = e.shiftKey ? (i <= 0 ? all.length - 1 : i - 1) : i === all.length - 1 ? 0 : i + 1\n      e.preventDefault()\n      all[next].focus({ preventScroll: true })\n    }\n    document.addEventListener('keydown', key)\n    return () => {\n      cancelAnimationFrame(raf)\n      document.removeEventListener('keydown', key)\n      if (before?.isConnected) before.focus({ preventScroll: true })\n    }\n  }, [active, quiet, box, first])\n}\n\nlet locks = 0\nexport function useScrollLock(active: boolean) {\n  useEffect(() => {\n    if (!active) return\n    const html = document.documentElement\n    if (locks++ === 0) {\n      const bar = innerWidth - html.clientWidth\n      html.style.overflow = 'hidden'\n      if (bar) html.style.paddingRight = `${bar}px`\n    }\n    return () => {\n      if (--locks === 0) {\n        html.style.overflow = ''\n        html.style.paddingRight = ''\n      }\n    }\n  }, [active])\n}\n\nexport function useReposition(active: boolean, fn: () => void) {\n  const latest = useRef(fn)\n  useLayoutEffect(() => {\n    latest.current = fn\n  })\n  useEffect(() => {\n    if (!active) return\n    const run = () => latest.current()\n    addEventListener('resize', run)\n    addEventListener('scroll', run, true)\n    return () => {\n      removeEventListener('resize', run)\n      removeEventListener('scroll', run, true)\n    }\n  }, [active])\n}\n"},{"path":"components/detent/lib/motion.ts","target":"components/detent/lib/motion.ts","type":"registry:component","content":"export const ease = {\n  out: [0.2, 0.9, 0.25, 1] as const,\n  inOut: [0.6, 0, 0.3, 1] as const,\n}\n\nexport const dur = {\n  exit: 0.12,\n  hover: 0.15,\n  enter: 0.2,\n  swap: 0.24,\n  panel: 0.34,\n}\n\nexport const spring = {\n  snappy: { type: 'spring', stiffness: 560, damping: 42, mass: 0.6 } as const,\n  smooth: { type: 'spring', bounce: 0, duration: 0.36 } as const,\n  soft: { type: 'spring', bounce: 0.12, duration: 0.5 } as const,\n  release: { type: 'spring', stiffness: 320, damping: 32 } as const,\n  move: { type: 'spring', stiffness: 420, damping: 38 } as const,\n  data: { type: 'spring', stiffness: 170, damping: 26 } as const,\n  pop: { type: 'spring', bounce: 0.3, duration: 0.4 } as const,\n}\n\nexport const appear = {\n  initial: { opacity: 0, scale: 0.96 },\n  animate: { opacity: 1, scale: 1, transition: { duration: dur.swap, ease: ease.out } },\n  exit: { opacity: 0, scale: 0.97, transition: { duration: dur.exit, ease: ease.out } },\n}\n\nexport const slide = {\n  enter: (dir: number) => ({ x: dir * 24, opacity: 0 }),\n  center: { x: 0, opacity: 1 },\n  exit: (dir: number) => ({ x: dir * -24, opacity: 0 }),\n}\n\nexport const press = { scale: 0.97 }\n\nexport const staggerFor = (count: number) => Math.min(0.04, 0.2 / Math.max(1, count))\n"}]}