{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "detent-tray",
  "title": "Detent Tray",
  "author": "Visant Labs <visantsupply@gmail.com>",
  "description": "Bandeja com cinco covas e uma esfera de aço: inclinar segura a esfera na cova até passar da borda, e ela cai na seguinte com um clique.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "@visant/use-media-query",
    "@visant/use-idle",
    "@visant/use-tilt"
  ],
  "files": [
    {
      "path": "registry/visant/ui/detent-tray.tsx",
      "content": "\"use client\";\n\n/*\n * DETENT: a steel ball in a tray with five dimples. Tilt the tray and the ball\n * leans against the wall of its dimple; tilt past the lip and it drops into the\n * next one with a click, never resting between two.\n *\n * One number drives it: the tilt θ. The notch n only changes when θ / 12 is more\n * than 0.65 away from it (hysteresis), so the ball holds, then goes. Between\n * clicks it strains up the dimple wall by a few units, which is what makes the\n * snap feel earned. The ball rolls: its mark turns by distance / radius. On a\n * phone the first tap asks for the gyroscope; drag is the fallback, and left\n * alone a slow draft tips it now and then.\n */\nimport * as React from \"react\";\nimport { motion, useMotionValue, useMotionValueEvent, useReducedMotion, useSpring, useTransform } from \"motion/react\";\nimport { useMediaQuery } from \"@/hooks/use-media-query\";\nimport { useIdle } from \"@/hooks/use-idle\";\nimport { useTilt } from \"@/hooks/use-tilt\";\n\nconst MAX = 30; // degrees of tilt\nconst STEP = 12; // degrees per notch\nconst PITCH = 30; // viewBox units between dimples\nconst R = 9; // ball radius\nconst CY = 80; // tray line\nconst NOTCHES = [-2, -1, 0, 1, 2];\nconst clamp = (v: number, a: number, b: number) => Math.max(a, Math.min(b, v));\n\nexport function DetentTray() {\n  const box = React.useRef<HTMLDivElement>(null);\n  const uid = React.useId().replace(/:/g, \"\");\n  const reduce = useReducedMotion();\n  const coarse = useMediaQuery(\"(pointer: coarse)\");\n  const tilt = useMotionValue(7); // born leaning, straining toward the next dimple\n  const [n, setN] = React.useState(0);\n  const nRef = React.useRef(0);\n  const target = useMotionValue(4 * (7 / STEP));\n  const sprung = useSpring(target, { stiffness: 520, damping: 26 });\n  const x = reduce ? target : sprung;\n  const trayTilt = useTransform(tilt, (t) => t * 0.25);\n  const roll = useTransform(x, (v) => ((v / R) * 180) / Math.PI);\n  const shadowX = useTransform(x, (v) => 100 + v);\n  const drag = React.useRef<{ x: number; from: number } | null>(null);\n\n  useMotionValueEvent(tilt, \"change\", (t) => {\n    const s = t / STEP;\n    let k = nRef.current;\n    if (Math.abs(s - k) > 0.65) k = clamp(Math.round(s), -2, 2);\n    if (k !== nRef.current) {\n      nRef.current = k;\n      setN(k);\n    }\n    target.set(PITCH * k + 4 * clamp(s - k, -0.65, 0.65));\n  });\n\n  const poke = useIdle(box, () => tilt.set((Math.random() - 0.5) * 2 * MAX), 2800);\n  const gyro = useTilt((g) => {\n    if (drag.current) return;\n    poke();\n    tilt.set(clamp(g * 0.9, -MAX, MAX));\n  });\n  const release = () => {\n    drag.current = null; // the tray stays where you left it; the ball stays in its dimple\n  };\n\n  return (\n    <div\n      ref={box}\n      role=\"img\"\n      aria-label={`Uma esfera de aço numa bandeja com cinco cavidades, parada na cavidade ${n + 3} de 5`}\n      onPointerDown={(e) => {\n        if (coarse) gyro.enable();\n        poke();\n        e.currentTarget.setPointerCapture(e.pointerId);\n        drag.current = { x: e.clientX, from: tilt.get() };\n      }}\n      onPointerMove={(e) => {\n        if (!drag.current) return;\n        poke();\n        const half = box.current!.clientWidth / 2;\n        tilt.set(clamp(drag.current.from + ((e.clientX - drag.current.x) / half) * MAX, -MAX, MAX));\n      }}\n      onPointerUp={release}\n      onPointerCancel={release}\n      className=\"relative h-full w-full cursor-grab touch-none select-none overflow-hidden rounded-[var(--r-control)] bg-muted active:cursor-grabbing\"\n    >\n      <svg aria-hidden viewBox=\"0 0 200 160\" preserveAspectRatio=\"xMidYMid meet\" className=\"absolute inset-0 h-full w-full\">\n        <defs>\n          <radialGradient id={`steel-${uid}`} cx=\"0.36\" cy=\"0.32\" r=\"0.75\">\n            <stop offset=\"0\" style={{ stopColor: \"var(--background)\" }} />\n            <stop offset=\"0.35\" style={{ stopColor: \"var(--muted-foreground)\" }} />\n            <stop offset=\"0.8\" style={{ stopColor: \"var(--foreground)\" }} />\n            <stop offset=\"1\" style={{ stopColor: \"var(--muted-foreground)\" }} />\n          </radialGradient>\n        </defs>\n        <motion.g style={{ rotate: trayTilt, originX: \"100px\", originY: `${CY}px` }}>\n          <rect x=\"18\" y={CY - 2} width=\"164\" height=\"16\" rx=\"8\" className=\"fill-card stroke-border\" strokeWidth=\"1\" />\n          {NOTCHES.map((k) => (\n            <g key={k}>\n              <ellipse cx={100 + k * PITCH} cy={CY + 4} rx=\"8\" ry=\"3\" className=\"fill-border\" />\n              <line\n                x1={100 + k * PITCH}\n                x2={100 + k * PITCH}\n                y1={CY + 22}\n                y2={CY + (k === n ? 30 : 26)}\n                strokeWidth=\"1\"\n                className={k === n ? \"stroke-foreground\" : \"stroke-muted-foreground\"}\n              />\n            </g>\n          ))}\n          <motion.ellipse cx={shadowX} cy={CY + 5} rx=\"8\" ry=\"2\" className=\"fill-foreground opacity-25\" />\n          <motion.g style={{ x, originX: \"100px\", originY: `${CY - R + 3}px` }}>\n            <circle cx=\"100\" cy={CY - R + 3} r={R} fill={`url(#steel-${uid})`} />\n            <motion.path\n              d={`M 100 ${CY - R * 2 + 5} A ${R - 2} ${R - 2} 0 0 1 ${100 + R - 2} ${CY - R + 3}`}\n              style={{ rotate: roll, originX: \"100px\", originY: `${CY - R + 3}px` }}\n              strokeWidth=\"0.8\"\n              fill=\"none\"\n              className=\"stroke-background opacity-40\"\n            />\n          </motion.g>\n        </motion.g>\n      </svg>\n    </div>\n  );\n}\n",
      "type": "registry:ui"
    }
  ],
  "meta": {
    "uso": {
      "quando": "Escolher entre poucas posições com um gesto de inclinar, sentindo cada parada",
      "evite": "Valor com consequência a mostrar: detent-slider; opções com nome: segmented-control"
    }
  },
  "categories": [
    "controles",
    "paradas",
    "esfera",
    "inclinacao",
    "giroscopio",
    "clique"
  ],
  "type": "registry:ui"
}