{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "hourglass",
  "title": "Hourglass",
  "author": "VenusUI",
  "description": "An hourglass timer you turn over; turn it mid-way and the sand runs back.",
  "files": [
    {
      "path": "registry/default/ui/hourglass.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\ninterface HourglassProps extends React.ComponentProps<\"div\"> {\n  /** Seconds for the sand to run through. */\n  duration?: number\n  onDone?: () => void\n}\n\nconst format = (seconds: number) => {\n  const total = Math.ceil(seconds)\n  return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, \"0\")}`\n}\n\n/**\n * An hourglass timer. Press it to turn it over: the sand runs from the top\n * bulb to the bottom for `duration` seconds. Turn it mid-way and it runs back\n * with whatever has fallen so far, like a real one.\n */\nfunction Hourglass({ duration = 60, onDone, className, ...props }: HourglassProps) {\n  // Share of the sand in the bulb that starts at the top of the drawing.\n  const [sand, setSand] = React.useState(0)\n  const [turns, setTurns] = React.useState(0)\n  const [running, setRunning] = React.useState(false)\n  const flipped = turns % 2 === 1\n  const top = flipped ? 1 - sand : sand\n  const left = top * duration\n  const clip = `${React.useId().replace(/[^a-zA-Z0-9]/g, \"\")}-glass`\n  const done = React.useRef(onDone)\n  const level = React.useRef(sand)\n  React.useEffect(() => {\n    done.current = onDone\n    level.current = sand\n  })\n\n  React.useEffect(() => {\n    if (!running) return\n    let last = performance.now()\n    const tick = setInterval(() => {\n      const now = performance.now()\n      const step = (now - last) / 1000 / duration\n      last = now\n      const next = Math.min(1, Math.max(0, flipped ? level.current + step : level.current - step))\n      level.current = next\n      setSand(next)\n      if ((flipped && next >= 1) || (!flipped && next <= 0)) {\n        setRunning(false)\n        done.current?.()\n      }\n    }, 100)\n    return () => clearInterval(tick)\n  }, [running, flipped, duration])\n\n  const turn = () => {\n    setTurns((value) => value + 1)\n    setRunning(true)\n  }\n\n  return (\n    <div data-slot=\"hourglass\" className={cn(\"flex w-48 flex-col items-center gap-4\", className)} {...props}>\n      <button\n        type=\"button\"\n        onClick={turn}\n        aria-label={running ? \"Turn the hourglass over\" : \"Turn the hourglass to start\"}\n        className=\"rounded-xl outline-none transition-[rotate] duration-700 ease-in-out focus-visible:ring-3 focus-visible:ring-ring/60 motion-reduce:transition-none\"\n        style={{ rotate: `${turns * 180}deg` }}\n      >\n        <svg viewBox=\"0 0 120 200\" className=\"w-36 drop-shadow-[0_12px_14px_rgb(0_0_0/0.3)]\" aria-hidden=\"true\">\n          <defs>\n            <clipPath id={clip}>\n              <path d=\"M24 22h72c0 34-26 52-30 78 4 26 30 44 30 78H24c0-34 26-52 30-78-4-26-30-44-30-78z\" />\n            </clipPath>\n          </defs>\n          <rect x=\"8\" y=\"6\" width=\"104\" height=\"16\" rx=\"5\" fill=\"#7c4a24\" />\n          <rect x=\"8\" y=\"178\" width=\"104\" height=\"16\" rx=\"5\" fill=\"#7c4a24\" />\n          <rect x=\"14\" y=\"20\" width=\"6\" height=\"160\" rx=\"3\" fill=\"#5c3518\" />\n          <rect x=\"100\" y=\"20\" width=\"6\" height=\"160\" rx=\"3\" fill=\"#5c3518\" />\n          <g clipPath={`url(#${clip})`}>\n            {/* Sand in the top bulb (drawing coordinates), settled at its bottom. */}\n            <rect x=\"0\" y={22 + (1 - sand) * 78} width=\"120\" height={sand * 78} fill=\"#d9a441\" />\n            {/* Sand in the bottom bulb, piled at the base. */}\n            <rect x=\"0\" y={178 - (1 - sand) * 78} width=\"120\" height={(1 - sand) * 78} fill=\"#d9a441\" />\n            {running ? <rect x=\"58.5\" y={flipped ? 22 : 96} width=\"3\" height=\"82\" fill=\"#d9a441\" /> : null}\n          </g>\n          <path d=\"M24 22h72c0 34-26 52-30 78 4 26 30 44 30 78H24c0-34 26-52 30-78-4-26-30-44-30-78z\" fill=\"rgb(255 255 255 / 0.12)\" stroke=\"#94a3b8\" strokeWidth=\"2.5\" />\n          <path d=\"M32 30c2 18 12 30 18 40\" stroke=\"white\" strokeOpacity=\".5\" strokeWidth=\"3\" strokeLinecap=\"round\" fill=\"none\" />\n        </svg>\n      </button>\n      <p role=\"timer\" aria-live=\"off\" className=\"font-mono text-2xl tabular-nums\">\n        {format(turns === 0 ? duration : left)}\n      </p>\n      <p aria-live=\"polite\" className=\"-mt-3 text-xs text-muted-foreground\">\n        {running ? \"Running\" : turns > 0 && left <= 0.05 ? \"Time’s up. Turn it over.\" : \"Press the glass to start\"}\n      </p>\n    </div>\n  )\n}\n\nexport { Hourglass }\n",
      "type": "registry:ui"
    }
  ],
  "meta": {
    "version": "0.1.0"
  },
  "categories": [
    "data-display",
    "interactive"
  ],
  "type": "registry:ui"
}