{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"greeting","type":"registry:component","title":"Greeting","description":"Greets by time of day, with a sun or moon that rises into place along an arc.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"components/greeting.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useState, useSyncExternalStore } from \"react\";\nimport { AnimatePresence, motion } from \"motion/react\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { cn } from \"@/lib/utils\";\nimport { usePreviewPlay } from \"@/lib/preview-play\";\n\n// Minute-resolution local time. The server snapshot is null: it can't know\n// the reader's hour, so it renders a neutral \"Hello\" and no glyph.\nfunction subscribeMinute(onChange: () => void) {\n  let timer: ReturnType<typeof setTimeout>;\n  const schedule = () => {\n    timer = setTimeout(() => {\n      onChange();\n      schedule();\n    }, 60_000 - (Date.now() % 60_000) + 20);\n  };\n  schedule();\n  return () => clearTimeout(timer);\n}\nconst minuteSnapshot = () => Math.floor(Date.now() / 60_000);\nconst serverSnapshot = () => null;\n\ntype Period = \"morning\" | \"afternoon\" | \"evening\" | \"night\";\n\nfunction periodOf(hour: number): Period {\n  if (hour >= 5 && hour < 12) return \"morning\";\n  if (hour >= 12 && hour < 17) return \"afternoon\";\n  if (hour >= 17 && hour < 22) return \"evening\";\n  return \"night\";\n}\n\n// Days since a known new moon, folded into one 29.53 day cycle:\n// 0 is new, 0.5 is full. Accurate to within a day, plenty for a glyph.\nconst NEW_MOON = Date.UTC(2000, 0, 6, 18, 14);\nconst SYNODIC = 29.530588853 * 86_400_000;\nconst moonPhase = (at: number) => (((at - NEW_MOON) % SYNODIC) + SYNODIC) % SYNODIC / SYNODIC;\n\nconst round = (n: number) => Math.round(n * 100) / 100;\n\nfunction Sun({ hour }: { hour: number }) {\n  // Height of the sun as 0..1 over a 6:00 to 18:00 day: rays are stubs at\n  // dawn and dusk and longest at noon.\n  const height = Math.max(0, Math.sin(((hour - 6) / 12) * Math.PI));\n  const inner = 7.5;\n  const outer = inner + 1.2 + height * 2.6;\n  return (\n    <>\n      <circle cx=\"12\" cy=\"12\" r=\"4.25\" />\n      {Array.from({ length: 8 }, (_, i) => {\n        const a = (i / 8) * Math.PI * 2;\n        return (\n          <line\n            key={i}\n            x1={round(12 + Math.cos(a) * inner)}\n            y1={round(12 + Math.sin(a) * inner)}\n            x2={round(12 + Math.cos(a) * outer)}\n            y2={round(12 + Math.sin(a) * outer)}\n          />\n        );\n      })}\n    </>\n  );\n}\n\nfunction Moon({ phase }: { phase: number }) {\n  const r = 7;\n  // The terminator is a half ellipse whose width follows the phase.\n  const rx = round(Math.abs(Math.cos(phase * Math.PI * 2)) * r);\n  const waxing = phase < 0.5;\n  const crescent = phase < 0.25 || phase > 0.75;\n  // Outer limb on the lit side (right while waxing, northern sky), then back\n  // up the terminator, bulging toward the lit side only for a crescent.\n  const outerSweep = waxing ? 1 : 0;\n  const innerSweep = waxing === crescent ? 0 : 1;\n  const d = `M12 ${12 - r}A${r} ${r} 0 0 ${outerSweep} 12 ${12 + r}A${rx} ${r} 0 0 ${innerSweep} 12 ${12 - r}Z`;\n  return (\n    <>\n      <circle cx=\"12\" cy=\"12\" r={r} opacity={0.3} />\n      <path d={d} fill=\"currentColor\" stroke=\"none\" />\n    </>\n  );\n}\n\nexport function Greeting({ name, className }: { name: string; className?: string }) {\n  const minute = useSyncExternalStore(subscribeMinute, minuteSnapshot, serverSnapshot);\n  const reduceMotion = useReducedMotion();\n\n  let word = \"Hello\";\n  let glyph: React.ReactNode = null;\n  let glyphKey = \"none\";\n  if (minute !== null) {\n    const now = new Date(minute * 60_000);\n    const hour = now.getHours() + now.getMinutes() / 60;\n    const period = periodOf(now.getHours());\n    word = `Good ${period}`;\n    const sunUp = hour >= 6 && hour < 19;\n    glyphKey = sunUp ? \"sun\" : \"moon\";\n    glyph = sunUp ? <Sun hour={hour} /> : <Moon phase={moonPhase(minute * 60_000)} />;\n  }\n\n  return (\n    <p className={cn(\"relative flex items-center gap-2.5 text-lg font-medium text-foreground\", className)}>\n      {/* The slot is reserved from the first render, so the glyph arriving\n          never nudges the text. */}\n      <span aria-hidden className=\"relative size-5 shrink-0\">\n        <AnimatePresence>\n          {glyph && (\n            <motion.svg\n              key={glyphKey}\n              viewBox=\"0 0 24 24\"\n              fill=\"none\"\n              stroke=\"currentColor\"\n              strokeWidth={1.75}\n              strokeLinecap=\"round\"\n              className=\"absolute inset-0 size-5 overflow-visible text-foreground\"\n              // Rises along an arc: x travels evenly while y eases out, so\n              // the body lifts fast and levels off like a sunrise. 700ms is\n              // long for UI but it plays once, on arrival, and never blocks.\n              initial={\n                reduceMotion\n                  ? { opacity: 0 }\n                  : { opacity: 0, x: -10, y: 10, rotate: -30 }\n              }\n              animate={{\n                opacity: 1,\n                x: 0,\n                y: 0,\n                rotate: 0,\n                transition: {\n                  opacity: { duration: 0.3, ease: \"easeOut\" },\n                  x: { duration: 0.7, ease: [0.4, 0, 0.6, 1] },\n                  y: { duration: 0.7, ease: [0.23, 1, 0.32, 1] },\n                  rotate: { duration: 0.7, ease: [0.23, 1, 0.32, 1] },\n                },\n              }}\n              // Sets on the far side of the arc, quicker than it rose.\n              exit={\n                reduceMotion\n                  ? { opacity: 0, transition: { duration: 0.2 } }\n                  : { opacity: 0, x: 8, y: 8, transition: { duration: 0.3, ease: [0.4, 0, 1, 1] } }\n              }\n            >\n              {glyph}\n            </motion.svg>\n          )}\n        </AnimatePresence>\n      </span>\n      <span className=\"relative inline-flex whitespace-nowrap\">\n        <AnimatePresence mode=\"popLayout\" initial={false}>\n          <motion.span\n            key={word}\n            className=\"inline-block\"\n            initial={{ opacity: 0, filter: \"blur(4px)\", y: reduceMotion ? 0 : 6 }}\n            animate={{\n              opacity: 1,\n              filter: \"blur(0px)\",\n              y: 0,\n              transition: { duration: 0.3, ease: [0.23, 1, 0.32, 1] },\n            }}\n            // Softer than the entrance: a short lift and fade.\n            exit={{\n              opacity: 0,\n              filter: \"blur(4px)\",\n              y: reduceMotion ? 0 : -4,\n              transition: { duration: 0.18, ease: \"easeOut\" },\n            }}\n          >\n            {word}\n          </motion.span>\n        </AnimatePresence>\n        {/* Slides over as the word changes width instead of jumping. */}\n        <motion.span layout=\"position\" transition={{ type: \"spring\", duration: 0.35, bounce: 0 }}>\n          , {name}\n        </motion.span>\n      </span>\n    </p>\n  );\n}\n\nexport default function GreetingDemo() {\n  const [run, setRun] = useState(0);\n  const play = usePreviewPlay();\n\n  // Index preview: presses Replay, so the sun or moon rises along its arc\n  // again, and again after a calm pause while the card stays hovered.\n  useEffect(() => {\n    if (!play) return;\n    const replay = () => setRun((r) => r + 1);\n    const first = setTimeout(replay, 150);\n    // The rise takes 700ms; the rest is a pause to look at it.\n    const again = setInterval(replay, 3200);\n    return () => {\n      clearTimeout(first);\n      clearInterval(again);\n    };\n  }, [play]);\n\n  return (\n    <div className=\"flex flex-col items-start gap-5\">\n      <Greeting key={run} name=\"Ada\" />\n      <button\n        type=\"button\"\n        onClick={() => setRun((r) => r + 1)}\n        className=\"-ml-3 h-8 rounded-full px-3 text-sm text-muted-foreground outline-hidden transition-[scale,color,background-color] duration-150 ease-out hover:bg-muted hover:text-foreground focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-foreground active:scale-[0.96]\"\n      >\n        Replay\n      </button>\n    </div>\n  );\n}\n"},{"path":"hooks/use-reduced-motion.ts","type":"registry:hook","target":"hooks/use-reduced-motion.ts","content":"\"use client\";\n\nimport { useSyncExternalStore } from \"react\";\n\n// Motion's useReducedMotion reads the media query on the very first client\n// render, but the server can't know it, so every component that renders\n// differently under reduced motion broke hydration for those users. This\n// reports false during hydration, matching the server HTML, then the real\n// preference straight after, and follows it if it changes.\nconst query = \"(prefers-reduced-motion: reduce)\";\n\nfunction subscribe(onChange: () => void) {\n  const media = window.matchMedia(query);\n  media.addEventListener(\"change\", onChange);\n  return () => media.removeEventListener(\"change\", onChange);\n}\n\nexport function useReducedMotion() {\n  return useSyncExternalStore(\n    subscribe,\n    () => window.matchMedia(query).matches,\n    () => false,\n  );\n}\n"},{"path":"lib/preview-play.ts","type":"registry:lib","target":"lib/preview-play.ts","content":"\"use client\";\n\nimport { createContext, useContext } from \"react\";\n\n// Lets a demo know whether it's showing in an index card, and whether that\n// card is hovered or focused. Demos use it to act themselves out on hover:\n// a star button stars itself, a checkbox scribbles, and so on.\n//\n// null: not in a preview (the component's own page, or real use).\n// false: in a preview, at rest.\n// true: in a preview, being hovered or focused: play the show.\nexport const PreviewPlayContext = createContext<boolean | null>(null);\n\nexport function usePreviewPlay() {\n  return useContext(PreviewPlayContext);\n}\n"}],"categories":["text"],"docs":"From ui lab: https://lab.xevrion.dev/lab/greeting"}