{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"wake-time-wheel","type":"registry:component","title":"Wake Time Wheel","description":"Set the alarm on a pair of wheels: hours and minutes are snap-scrolling columns with momentum whose rows turn away in perspective around a lens, each a keyboard spinbutton, with the time the sunrise starts beneath.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/wake-time-wheel.tsx","content":"\"use client\";\n\nimport { useEffect, useId, useRef, useState, useSyncExternalStore } from \"react\";\nimport type { CSSProperties, FormEvent, KeyboardEvent as ReactKeyboardEvent, ReactNode } from \"react\";\n\n/**\n * WakeTimeWheel — set the alarm on a pair of wheels.\n *\n * Hours and minutes are two snap-scrolling columns (native scrolling, with\n * momentum) whose rows turn away in perspective around a lens; each is a\n * spinbutton for the arrow keys, Page Up and Down, Home and End. Beneath,\n * the time the sunrise will start. Submits the time (and posts it with\n * `name`); without a handler it confirms in a status line.\n *\n * Part of the Modern Minimal kit: a paper-white room (#f5f5f2, surfaces\n * #ffffff, ink #141412) and one light, dawn #ffa64d, only ever as light.\n * Two-layer shadows from one light above; scroll binds values directly,\n * with no easing. Inter Tight through var(--font-inter-tight) for display\n * over Geist. Respects prefers-reduced-motion. No dependencies beyond React.\n * Paste it as its own file: it repeats the kit's small helpers, which would\n * clash in one module. Load Inter Tight with next/font (variable:\n * \"--font-inter-tight\") on a parent, or from Google Fonts or @fontsource.\n */\n\nconst SANS = 'var(--font-sans, \"Geist\", \"Inter\", ui-sans-serif, system-ui, sans-serif)';\n\nconst MONO = 'var(--font-mono, \"Geist Mono\", ui-monospace, \"SFMono-Regular\", Menlo, Consolas, monospace)';\n\nconst DISPLAY = 'var(--font-inter-tight, \"Inter Tight\", var(--font-sans, \"Geist\"), ui-sans-serif, system-ui, sans-serif)';\n\nconst EASE = \"cubic-bezier(0.16, 1, 0.3, 1)\";\n\n/** A paper-white room and one light. */\nconst PAPER = {\n  ground: \"#f5f5f2\",\n  surface: \"#ffffff\",\n  well: \"#ecebe6\",\n  solid: \"#141412\",\n  ink: \"rgba(20,20,18,0.92)\",\n  ink2: \"rgba(20,20,18,0.6)\",\n  ink3: \"rgba(20,20,18,0.38)\",\n  line: \"rgba(20,20,18,0.08)\",\n  line2: \"rgba(20,20,18,0.14)\",\n  dawn: \"#ffa64d\",\n  night: \"#0e0f11\",\n} as const;\n\n/** Light from above: a 1px contact shadow and a soft ambient one, both falling the same way. */\nconst SHADOW = \"0 1px 2px rgba(20,20,18,0.06), 0 24px 48px -12px rgba(20,20,18,0.1)\";\n\nfunction clamp(v: number, lo: number, hi: number): number {\n  return v < lo ? lo : v > hi ? hi : v;\n}\n\n/** Links: strip tabs and newlines, refuse backslashes, allow http(s), mailto, tel and same-site paths. */\nfunction safeHref(raw: string): string {\n  const v = raw.replace(/[\\t\\n\\r]/g, \"\").trim();\n  if (!v || v.includes(\"\\\\\")) return \"#\";\n  if (/^(https?:|mailto:|tel:)/i.test(v)) return v;\n  if (/^[/#?]/.test(v) && !/^\\/\\//.test(v)) return v;\n  return \"#\";\n}\n\nfunction subscribeMotion(cb: () => void) {\n  const m = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n  m.addEventListener(\"change\", cb);\n  return () => m.removeEventListener(\"change\", cb);\n}\n\n/** True when the visitor asked for reduced motion. False on the server. */\nfunction useReducedMotion(): boolean {\n  return useSyncExternalStore(\n    subscribeMotion,\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\n// Each part carries only the CSS it needs, so any one of them works on its own.\n// revert-layer keeps an element's own rounded corners when a host page's focus rule flattens them.\nconst FOCUS_CSS = \".mm-scope :focus-visible,.mm-scope:focus-visible{outline:2px solid var(--mm-ring,#141412)!important;outline-offset:3px;border-radius:revert-layer}\";\n\nconst MM_BUTTON_INK = {\n  solid: { bg: PAPER.solid, fg: \"#f5f5f2\", border: \"transparent\", shadow: true },\n  light: { bg: PAPER.surface, fg: PAPER.solid, border: \"rgba(20,20,18,0.08)\", shadow: true },\n  outline: { bg: \"transparent\", fg: PAPER.solid, border: \"rgba(20,20,18,0.22)\", shadow: false },\n} as const;\n\nexport type MmButtonVariant = keyof typeof MM_BUTTON_INK;\n\n/**\n * WeightPressButton — a button whose press you can see in its type.\n *\n * A pill in Inter Tight. Hover and the label thickens a step and the button\n * lifts a pixel as its ambient shadow widens; press and the label goes bold\n * under your finger while the button sinks, the ambient shadow collapsing to\n * the contact one, then springs back past rest on release. Both shadows fall\n * from one light above. Solid, light and outline; a link when given an href.\n * CSS transitions on a variable weight, no animation library.\n */\nexport function WeightPressButton({\n  children,\n  href,\n  onClick,\n  type = \"button\",\n  variant = \"solid\",\n  size = \"lg\",\n  disabled = false,\n  ariaLabel,\n  ariaPressed,\n  className = \"\",\n}: {\n  children: ReactNode;\n  href?: string;\n  onClick?: () => void;\n  type?: \"button\" | \"submit\";\n  variant?: MmButtonVariant;\n  size?: \"lg\" | \"md\" | \"sm\";\n  disabled?: boolean;\n  ariaLabel?: string;\n  ariaPressed?: boolean;\n  className?: string;\n}) {\n  const ink = MM_BUTTON_INK[variant] ?? MM_BUTTON_INK.solid;\n  const [state, setState] = useState<\"rest\" | \"hover\" | \"down\">(\"rest\");\n  const reduced = useReducedMotion();\n  const weight = state === \"down\" ? 680 : state === \"hover\" ? 560 : 500;\n  const shadow = !ink.shadow\n    ? \"none\"\n    : state === \"down\"\n      ? \"0 1px 1px rgba(20,20,18,0.1), 0 6px 12px -8px rgba(20,20,18,0.14)\"\n      : state === \"hover\"\n        ? \"0 1px 2px rgba(20,20,18,0.06), 0 30px 56px -14px rgba(20,20,18,0.16)\"\n        : SHADOW;\n  const pad = size === \"sm\" ? \"h-9 px-4 text-[13px]\" : size === \"md\" ? \"h-11 px-5 text-[14px]\" : \"h-[52px] px-7 text-[15px]\";\n  const common = {\n    \"aria-label\": ariaLabel,\n    className: \"mm-scope relative inline-flex select-none items-center justify-center rounded-full no-underline outline-none \" + pad + (disabled ? \" pointer-events-none opacity-40 \" : \" \") + className,\n    style: {\n      background: ink.bg,\n      color: ink.fg,\n      border: `1px solid ${ink.border}`,\n      boxShadow: shadow,\n      fontFamily: DISPLAY,\n      fontWeight: weight,\n      letterSpacing: \"-0.01em\",\n      transform: state === \"down\" ? \"translateY(1px) scale(0.985)\" : state === \"hover\" ? \"translateY(-1px)\" : \"none\",\n      transition: reduced\n        ? \"none\"\n        : state === \"rest\"\n          ? `transform 300ms cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 260ms ${EASE}, font-weight 220ms ${EASE}`\n          : `transform 160ms ${EASE}, box-shadow 160ms ${EASE}, font-weight 160ms ${EASE}`,\n    } as CSSProperties,\n    onPointerEnter: () => setState(\"hover\"),\n    onPointerLeave: () => setState(\"rest\"),\n    onPointerDown: () => setState(\"down\"),\n    onPointerUp: () => setState(\"hover\"),\n    onPointerCancel: () => setState(\"rest\"),\n    onFocus: () => setState((s) => (s === \"rest\" ? \"hover\" : s)),\n    onBlur: () => setState(\"rest\"),\n    onKeyDown: (e: ReactKeyboardEvent) => {\n      if (e.key === \"Enter\" || (e.key === \" \" && !href)) setState(\"down\");\n    },\n    onKeyUp: () => setState(\"hover\"),\n  };\n  // The label is laid out at the boldest weight too, invisibly, so the button never changes width.\n  const label = (\n    <span className=\"relative inline-grid\">\n      <span aria-hidden className=\"invisible col-start-1 row-start-1 whitespace-nowrap\" style={{ fontWeight: 680 }}>\n        {children}\n      </span>\n      <span className=\"col-start-1 row-start-1 whitespace-nowrap text-center\">{children}</span>\n    </span>\n  );\n  if (href) {\n    return (\n      <>\n        <style>{FOCUS_CSS}</style>\n        <a href={safeHref(href)} onClick={onClick} {...common}>\n          {label}\n        </a>\n      </>\n    );\n  }\n  return (\n    <>\n      <style>{FOCUS_CSS}</style>\n      <button type={type} onClick={onClick} disabled={disabled} aria-pressed={ariaPressed} {...common}>\n        {label}\n      </button>\n    </>\n  );\n}\n\nconst MM_ROW = 40;\n\n/** One wheel: a snap-scrolling column whose rows turn away in perspective, and a spinbutton for keys. */\nfunction MmWheel({ label, values, value, onChange, format }: { label: string; values: number[]; value: number; onChange: (v: number) => void; format: (v: number) => string }) {\n  const ref = useRef<HTMLDivElement>(null);\n  const rows = useRef<(HTMLDivElement | null)[]>([]);\n  const reduced = useReducedMotion();\n  const idx = Math.max(0, values.indexOf(value));\n  const settle = useRef(0);\n  const seed = useRef(idx);\n  const onChangeRef = useRef(onChange);\n  useEffect(() => {\n    onChangeRef.current = onChange;\n  });\n\n  // The rows turn by their distance from the middle; drawn on scroll, not on a timer.\n  useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    let raf = 0;\n    const draw = () => {\n      raf = 0;\n      const c = el.scrollTop / MM_ROW;\n      rows.current.forEach((r, i) => {\n        if (!r) return;\n        const d = clamp(i - c, -3, 3);\n        r.style.transform = `perspective(360px) rotateX(${(-d * 22).toFixed(2)}deg) translateZ(${(-Math.abs(d) * 6).toFixed(2)}px)`;\n        r.style.opacity = (1 - Math.min(0.85, Math.abs(d) * 0.3)).toFixed(3);\n      });\n    };\n    const onScroll = () => {\n      if (!raf) raf = requestAnimationFrame(draw);\n      clearTimeout(settle.current);\n      settle.current = window.setTimeout(() => {\n        const i = clamp(Math.round(el.scrollTop / MM_ROW), 0, values.length - 1);\n        if (values[i] !== undefined) onChangeRef.current(values[i]);\n      }, 140);\n    };\n    el.scrollTop = seed.current * MM_ROW;\n    draw();\n    el.addEventListener(\"scroll\", onScroll, { passive: true });\n    return () => {\n      el.removeEventListener(\"scroll\", onScroll);\n      cancelAnimationFrame(raf);\n      clearTimeout(settle.current);\n    };\n  }, [values]);\n\n  const go = (i: number) => {\n    const el = ref.current;\n    const j = clamp(i, 0, values.length - 1);\n    if (el) el.scrollTo({ top: j * MM_ROW, behavior: reduced ? \"auto\" : \"smooth\" });\n    onChange(values[j]);\n  };\n  const onKey = (e: ReactKeyboardEvent<HTMLDivElement>) => {\n    // Step from where the wheel is now, even mid-fling, not from the last settled value.\n    const el = ref.current;\n    const here = el ? clamp(Math.round(el.scrollTop / MM_ROW), 0, values.length - 1) : idx;\n    const step = e.key === \"ArrowDown\" ? 1 : e.key === \"ArrowUp\" ? -1 : e.key === \"PageDown\" ? 3 : e.key === \"PageUp\" ? -3 : 0;\n    if (e.key === \"Home\" || e.key === \"End\") {\n      e.preventDefault();\n      go(e.key === \"Home\" ? 0 : values.length - 1);\n      return;\n    }\n    if (!step) return;\n    e.preventDefault();\n    clearTimeout(settle.current);\n    go(here + step);\n  };\n  return (\n    <div\n      ref={ref}\n      role=\"spinbutton\"\n      tabIndex={0}\n      aria-label={label}\n      aria-valuemin={values[0]}\n      aria-valuemax={values[values.length - 1]}\n      aria-valuenow={value}\n      aria-valuetext={format(value)}\n      onKeyDown={onKey}\n      className=\"relative h-[200px] w-[76px] snap-y snap-mandatory overflow-y-scroll outline-none [scrollbar-width:none] [&::-webkit-scrollbar]:hidden\"\n      style={{ paddingBlock: MM_ROW * 2, maskImage: \"linear-gradient(transparent, #000 30%, #000 70%, transparent)\", WebkitMaskImage: \"linear-gradient(transparent, #000 30%, #000 70%, transparent)\" }}\n    >\n      {values.map((v, i) => (\n        <div\n          key={v}\n          ref={(el) => void (rows.current[i] = el)}\n          aria-hidden\n          onClick={() => go(i)}\n          className=\"flex snap-center items-center justify-center tabular-nums\"\n          style={{ height: MM_ROW, fontFamily: DISPLAY, fontSize: 26, fontWeight: 560, letterSpacing: \"-0.02em\", color: PAPER.solid, cursor: \"pointer\" }}\n        >\n          {format(v)}\n        </div>\n      ))}\n    </div>\n  );\n}\n\nexport type MmAlarm = { time: string; hours: number; minutes: number; sunriseFrom: string };\n\n/**\n * WakeTimeWheel — set the alarm on a pair of wheels.\n *\n * Hours and minutes are two snap-scrolling columns (native scrolling, so\n * they carry momentum and settle on a value) whose rows turn away in\n * perspective above and below a lens; each column is a spinbutton you can\n * drive with the arrow keys, Page Up and Down, Home and End. Beneath, the\n * time the sunrise will start. Submitting sends the time (and posts it with\n * `name` in a form); without a handler it confirms in a status line.\n */\nexport function WakeTimeWheel({\n  label = \"Wake at\",\n  defaultValue = \"06:30\",\n  step = 5,\n  sunrise = 30,\n  name,\n  submitLabel = \"Set alarm\",\n  onSubmit,\n  className = \"\",\n}: {\n  label?: string;\n  /** \"HH:MM\", 24-hour. */\n  defaultValue?: string;\n  /** Minutes between choices (1-30). */\n  step?: number;\n  /** How many minutes the sunrise runs before the alarm. */\n  sunrise?: number;\n  name?: string;\n  submitLabel?: string;\n  onSubmit?: (value: MmAlarm) => void;\n  className?: string;\n}) {\n  const st = clamp(Math.round(step), 1, 30);\n  // A malformed default (\"6\", \"\", \"6:3O\") falls back to zero rather than reaching NaN.\n  const parts = defaultValue.split(\":\");\n  const h0 = Number.isFinite(Number(parts[0])) ? Number(parts[0]) : 0;\n  const m0 = Number.isFinite(Number(parts[1])) ? Number(parts[1]) : 0;\n  const [hours, setHours] = useState(clamp(h0, 0, 23));\n  const [minutes, setMinutes] = useState(clamp(Math.round(m0 / st) * st, 0, 60 - st));\n  const [done, setDone] = useState(\"\");\n  const labelId = useId();\n  const [hourValues] = useState(() => Array.from({ length: 24 }, (_, i) => i));\n  const [minuteValues] = useState(() => Array.from({ length: Math.floor(60 / st) }, (_, i) => i * st));\n  const pad = (n: number) => String(n).padStart(2, \"0\");\n  const time = `${pad(hours)}:${pad(minutes)}`;\n  const from = hours * 60 + minutes - clamp(sunrise, 0, 180);\n  const fromText = `${pad(Math.floor(((from % 1440) + 1440) % 1440 / 60))}:${pad((((from % 1440) + 1440) % 1440) % 60)}`;\n\n  const submit = (e: FormEvent<HTMLFormElement>) => {\n    e.preventDefault();\n    const value: MmAlarm = { time, hours, minutes, sunriseFrom: fromText };\n    onSubmit?.(value);\n    // Without a handler nothing is saved, so the status says what was chosen, not that it was set.\n    setDone(onSubmit ? `Set for ${time}. The sunrise starts at ${fromText}.` : `Chosen: ${time}, sunrise from ${fromText}. Connect onSubmit to save it.`);\n  };\n\n  return (\n    <form onSubmit={submit} className={\"mm-scope flex w-full flex-col items-center gap-6 \" + className} style={{ fontFamily: SANS, color: PAPER.ink }}>\n      <style>{FOCUS_CSS}</style>\n      <div className=\"flex w-full max-w-[360px] flex-col items-center gap-4 rounded-[28px] px-6 py-6\" style={{ background: PAPER.surface, boxShadow: SHADOW, border: `1px solid ${PAPER.line}` }}>\n        <span id={labelId} className=\"text-[11px] uppercase\" style={{ fontFamily: MONO, letterSpacing: \"0.16em\", color: PAPER.ink3 }}>\n          {label}\n        </span>\n        <div role=\"group\" aria-labelledby={labelId} className=\"relative flex items-center gap-1\">\n          <span aria-hidden className=\"pointer-events-none absolute inset-x-[-10px] top-1/2 h-[40px] -translate-y-1/2 rounded-[12px]\" style={{ background: PAPER.well }} />\n          <MmWheel label=\"Hours\" values={hourValues} value={hours} onChange={setHours} format={pad} />\n          <span aria-hidden className=\"relative text-[26px] font-semibold\" style={{ fontFamily: DISPLAY, color: PAPER.ink3 }}>\n            :\n          </span>\n          <MmWheel label=\"Minutes\" values={minuteValues} value={minutes} onChange={setMinutes} format={pad} />\n        </div>\n        <p className=\"m-0 text-[13px] tabular-nums\" style={{ color: PAPER.ink2 }}>\n          Sunrise from <span style={{ color: PAPER.solid }}>{fromText}</span> · alarm at <span style={{ color: PAPER.solid }}>{time}</span>\n        </p>\n        {name ? <input type=\"hidden\" name={name} value={time} /> : null}\n        <WeightPressButton type=\"submit\" size=\"md\">\n          {submitLabel}\n        </WeightPressButton>\n      </div>\n      <p role=\"status\" className=\"m-0 min-h-[20px] text-[13px]\" style={{ color: PAPER.ink2 }}>\n        {done}\n      </p>\n    </form>\n  );\n}\n","type":"registry:component","target":"components/viberdy/wake-time-wheel.tsx"}],"meta":{"tier":"free","tags":["form","time picker","wheel","scroll snap","input","alarm"],"href":"https://viberdy.dev/library/wake-time-wheel","aiPrompt":"Build a React + Tailwind form, `WakeTimeWheel({ label, defaultValue, step, sunrise, name, submitLabel, onSubmit, className })`.\n\nEach column is a 200px-tall overflow-y scroller with scroll-snap-type y mandatory, 80px padding at each end and 40px rows that snap to centre, faded at the ends with a mask. On scroll (a requestAnimationFrame per frame of scrolling), each row gets perspective(360px) rotateX(-d x 22deg) translateZ(-|d| x 6px) and opacity 1 - min(0.85, 0.3 |d|), d being its distance from the centre in rows; 140ms after the scroll stops, the value is the nearest row. Keys: arrows +-1, Page Up/Down +-3, Home/End, each scrolling to the row. A #ecebe6 lens behind the centre row. Beneath: the sunrise start (the time minus `sunrise` minutes) and the alarm; a hidden input with name; the kit's WeightPressButton to submit; a status line.\n\nModern Minimal grammar: a paper-white room, ground #f5f5f2 (surfaces #ffffff that lift, a recessed well #ecebe6), ink #141412 at 92/60/38% (never grey-400 body copy), hairlines at 8% and 14%; one light, dawn #ffa64d, only ever as light (the lamp's glow, a lit state), never as a fill. Shadows come from one light above in two layers: a contact 0 1px 2px rgba(20,20,18,0.06) and an ambient 0 24px 48px -12px rgba(20,20,18,0.1), both falling the same way; a pressed thing loses the ambient, a lifted one widens it. Type: Inter Tight through var(--font-inter-tight, \"Inter Tight\", ...) for display at 600 with -0.035em tracking and 0.98 leading (tracking tightens only at display sizes), Geist for reading at 16-17px/1.55, Geist Mono uppercase at 11-12px for readouts. Radii 12px controls (pills for buttons), 20px cards, 28px media. Motion: scroll binds values directly with no easing (a value moves exactly with the scroll, like scrubbing film); discrete hover and press states take 150-260ms with a spring back past rest; ambient loops 40s or more. Respect prefers-reduced-motion (ambient scenes hold still; scroll-bound values still follow the scroll, which the visitor drives). No dependencies beyond React.","license":"https://viberdy.dev/license"}}