{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "product-tour",
  "title": "Product Tour",
  "description": "A guided product tour: a spotlight glides from element to element over a dimmed page while a card beside it explains each one, with progress dots, Back, Next and Skip, arrow keys, and automatic placement on the side that has room.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/product-tour/product-tour.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type TourPlacement = \"auto\" | \"top\" | \"bottom\" | \"left\" | \"right\";\n\nexport interface TourStep {\n  /** CSS selector of the element to highlight, e.g. '[data-tour=\"search\"]'. Leave out for a centred step. */\n  target?: string;\n  title: string;\n  body?: React.ReactNode;\n  placement?: TourPlacement;\n}\n\nexport interface ProductTourProps {\n  steps?: TourStep[];\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  onStepChange?: (index: number) => void;\n  /** Called when the last step's Done button is pressed (not when skipped). */\n  onFinish?: () => void;\n  /** A button that starts the tour. Leave out and control it with open / onOpenChange. */\n  triggerLabel?: string;\n  /** Space between the highlight and its element, in px. */\n  padding?: number;\n  /** Close when the dimmed area is clicked. */\n  closeOnOverlayClick?: boolean;\n  showProgress?: boolean;\n  /** Look for targets inside, and draw over, the nearest positioned ancestor instead of the page. */\n  contained?: boolean;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.6)\", faint: \"rgba(245,244,241,0.2)\", panel: \"#141414\", border: \"rgba(255,255,255,0.12)\", primary: \"#F5F4F1\", primaryText: \"#0A0A0A\", secondary: \"rgba(255,255,255,0.06)\", ring: \"rgba(245,244,241,0.9)\", scrim: \"rgba(0,0,0,0.62)\", focus: \"rgba(245,244,241,0.85)\", shadow: \"0 18px 50px rgba(0,0,0,0.55)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.6)\", faint: \"rgba(10,10,10,0.16)\", panel: \"#FFFFFF\", border: \"rgba(10,10,10,0.12)\", primary: \"#0A0A0A\", primaryText: \"#FFFFFF\", secondary: \"rgba(10,10,10,0.05)\", ring: \"#FFFFFF\", scrim: \"rgba(10,10,10,0.5)\", focus: \"rgba(10,10,10,0.85)\", shadow: \"0 18px 50px rgba(10,10,10,0.2)\" },\n};\n\nconst DEFAULT_STEPS: TourStep[] = [\n  { target: '[data-tour=\"search\"]', title: \"Search everything\", body: \"Find orders, customers and products from one place. Press / to jump here from anywhere.\" },\n  { target: '[data-tour=\"stats\"]', title: \"Today at a glance\", body: \"Revenue, orders and conversion update live. Click a card to see the full report.\" },\n  { target: '[data-tour=\"new\"]', title: \"Create in one click\", body: \"Add a product, a discount or a draft order without leaving the page.\" },\n  { target: '[data-tour=\"help\"]', title: \"Help is always here\", body: \"Replay this tour, read the docs or chat with us whenever you need.\" },\n];\n\nconst POP_W = 300;\nconst GAP = 12;\nconst EDGE = 12;\n\ntype Box = { x: number; y: number; w: number; h: number };\n\nexport function ProductTour({\n  steps = DEFAULT_STEPS,\n  open,\n  defaultOpen = false,\n  onOpenChange,\n  onStepChange,\n  onFinish,\n  triggerLabel,\n  padding = 6,\n  closeOnOverlayClick = false,\n  showProgress = true,\n  contained = false,\n  theme = \"dark\",\n  className,\n}: ProductTourProps) {\n  const p = PALETTES[theme];\n  const uid = React.useId();\n  const reduce = Boolean(useReducedMotion());\n  const anchorRef = React.useRef<HTMLDivElement>(null);\n  const popRef = React.useRef<HTMLDivElement>(null);\n  const returnFocus = React.useRef<HTMLElement | null>(null);\n  const [internalOpen, setInternalOpen] = React.useState(defaultOpen);\n  const isOpen = open ?? internalOpen;\n  const [index, setIndex] = React.useState(0);\n  const [hole, setHole] = React.useState<Box | null>(null);\n  const [area, setArea] = React.useState({ w: 0, h: 0 });\n  const [popH, setPopH] = React.useState(160);\n\n  // Every time it opens, start again from the first step.\n  const [wasOpen, setWasOpen] = React.useState(isOpen);\n  if (isOpen !== wasOpen) {\n    setWasOpen(isOpen);\n    if (isOpen && index !== 0) setIndex(0);\n  }\n\n  const step = steps[Math.min(index, steps.length - 1)];\n  const last = index >= steps.length - 1;\n\n  const setOpen = React.useCallback(\n    (next: boolean) => {\n      if (open === undefined) setInternalOpen(next);\n      onOpenChange?.(next);\n    },\n    [open, onOpenChange],\n  );\n\n  const go = (i: number) => {\n    setIndex(i);\n    onStepChange?.(i);\n  };\n\n  // Track the current target's box (relative to the container, or the viewport) while open.\n  React.useLayoutEffect(() => {\n    if (!isOpen) return;\n    const root = contained ? anchorRef.current?.parentElement ?? null : null;\n    const el = step?.target ? ((root ?? document).querySelector(step.target) as HTMLElement | null) : null;\n    if (el && !contained) el.scrollIntoView({ block: \"nearest\", inline: \"nearest\" });\n    const measure = () => {\n      const base = root ? root.getBoundingClientRect() : { left: 0, top: 0, width: window.innerWidth, height: window.innerHeight };\n      setArea({ w: base.width, h: base.height });\n      if (!el) return setHole(null);\n      const r = el.getBoundingClientRect();\n      setHole({ x: r.left - base.left - padding, y: r.top - base.top - padding, w: r.width + padding * 2, h: r.height + padding * 2 });\n    };\n    measure();\n    const ro = new ResizeObserver(measure);\n    if (root) ro.observe(root);\n    if (el) ro.observe(el);\n    window.addEventListener(\"resize\", measure);\n    window.addEventListener(\"scroll\", measure, true);\n    return () => {\n      ro.disconnect();\n      window.removeEventListener(\"resize\", measure);\n      window.removeEventListener(\"scroll\", measure, true);\n    };\n  }, [isOpen, step?.target, contained, padding]);\n\n  // The card mounts once the area has been measured.\n  const ready = isOpen && area.w > 0;\n\n  React.useLayoutEffect(() => {\n    const el = popRef.current;\n    if (!ready || !el) return;\n    const ro = new ResizeObserver(() => setPopH(el.offsetHeight));\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, [ready]);\n\n  React.useEffect(() => {\n    if (!ready) return;\n    returnFocus.current = document.activeElement as HTMLElement | null;\n    popRef.current?.focus({ preventScroll: true });\n    return () => returnFocus.current?.focus?.({ preventScroll: true });\n  }, [ready]);\n\n  const close = () => setOpen(false);\n  const next = () => {\n    if (last) {\n      onFinish?.();\n      close();\n    } else go(index + 1);\n  };\n  const back = () => index > 0 && go(index - 1);\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    if (e.key === \"Escape\") close();\n    else if (e.key === \"ArrowRight\") next();\n    else if (e.key === \"ArrowLeft\") back();\n  };\n\n  // Where the card goes: the preferred side if it fits, otherwise the side with the most room.\n  const popW = Math.min(POP_W, Math.max(200, area.w - EDGE * 2));\n  let pos = { x: (area.w - popW) / 2, y: (area.h - popH) / 2 };\n  if (hole) {\n    const room = { bottom: area.h - (hole.y + hole.h), top: hole.y, right: area.w - (hole.x + hole.w), left: hole.x };\n    const fits = { bottom: room.bottom >= popH + GAP + EDGE, top: room.top >= popH + GAP + EDGE, right: room.right >= popW + GAP + EDGE, left: room.left >= popW + GAP + EDGE };\n    const wanted = step?.placement ?? \"auto\";\n    const side: Exclude<TourPlacement, \"auto\"> = wanted !== \"auto\" && fits[wanted] ? wanted : ([\"bottom\", \"top\", \"right\", \"left\"] as const).find((s) => fits[s]) ?? \"bottom\";\n    const cx = hole.x + hole.w / 2 - popW / 2;\n    const cy = hole.y + hole.h / 2 - popH / 2;\n    pos = side === \"bottom\" ? { x: cx, y: hole.y + hole.h + GAP } : side === \"top\" ? { x: cx, y: hole.y - popH - GAP } : side === \"right\" ? { x: hole.x + hole.w + GAP, y: cy } : { x: hole.x - popW - GAP, y: cy };\n    pos.x = Math.max(EDGE, Math.min(pos.x, area.w - popW - EDGE));\n    pos.y = Math.max(EDGE, Math.min(pos.y, area.h - popH - EDGE));\n  }\n\n  const spring = reduce ? { duration: 0 } : { type: \"spring\" as const, stiffness: 320, damping: 34 };\n  const fixed = contained ? \"absolute\" : \"fixed\";\n  // With no target, the highlight shrinks to a point in the middle so the whole area dims.\n  const box = hole ?? { x: area.w / 2, y: area.h / 2, w: 0, h: 0 };\n\n  const button = (kind: \"primary\" | \"secondary\" | \"ghost\", label: string, onClick: () => void) => (\n    <button\n      type=\"button\"\n      onClick={onClick}\n      className=\"cursor-pointer font-semibold whitespace-nowrap outline-none focus-visible:ring-2\"\n      style={{\n        height: 32,\n        padding: kind === \"ghost\" ? \"0 2px\" : \"0 12px\",\n        borderRadius: 9,\n        fontSize: 13,\n        background: kind === \"primary\" ? p.primary : kind === \"secondary\" ? p.secondary : \"transparent\",\n        color: kind === \"primary\" ? p.primaryText : kind === \"ghost\" ? p.muted : p.text,\n        border: kind === \"secondary\" ? `1px solid ${p.border}` : \"none\",\n        [\"--tw-ring-color\" as string]: p.focus,\n      }}\n    >\n      {label}\n    </button>\n  );\n\n  return (\n    <>\n      <div ref={anchorRef} className=\"contents\">\n        {triggerLabel && (\n          <button\n            type=\"button\"\n            onClick={() => {\n              go(0);\n              setOpen(true);\n            }}\n            aria-haspopup=\"dialog\"\n            className=\"cursor-pointer rounded-full border-none px-5 py-2.5 text-[14px] font-semibold outline-none focus-visible:ring-2 focus-visible:ring-offset-2\"\n            style={{ background: p.primary, color: p.primaryText, fontFamily: \"Inter, sans-serif\", [\"--tw-ring-color\" as string]: p.focus }}\n          >\n            {triggerLabel}\n          </button>\n        )}\n      </div>\n\n      <AnimatePresence>\n        {isOpen && (\n          <motion.div key=\"tour\" className={cn(fixed, \"inset-0 z-[80]\", className)} initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} transition={{ duration: reduce ? 0 : 0.2 }} style={{ fontFamily: \"Inter, sans-serif\" }}>\n            {/* Catches clicks so the page underneath can't be used mid-tour. */}\n            <div aria-hidden=\"true\" className=\"absolute inset-0\" onClick={() => closeOnOverlayClick && close()} />\n            {ready && (\n            <>\n            <motion.div\n              aria-hidden=\"true\"\n              className=\"pointer-events-none absolute top-0 left-0\"\n              initial={false}\n              animate={{ x: box.x, y: box.y, width: box.w, height: box.h, borderRadius: hole ? 12 : 0 }}\n              transition={spring}\n              style={{ boxShadow: `0 0 0 9999px ${p.scrim}, 0 0 0 ${hole ? 1.5 : 0}px ${p.ring} inset` }}\n            />\n            <motion.div\n              ref={popRef}\n              role=\"dialog\"\n              aria-modal=\"true\"\n              aria-labelledby={`${uid}-title`}\n              aria-describedby={step?.body ? `${uid}-body` : undefined}\n              tabIndex={-1}\n              onKeyDown={onKeyDown}\n              className=\"absolute top-0 left-0 flex flex-col outline-none\"\n              initial={{ x: pos.x, y: pos.y + 8, opacity: 0 }}\n              animate={{ x: pos.x, y: pos.y, opacity: 1 }}\n              transition={spring}\n              style={{ width: popW, padding: 16, gap: 12, borderRadius: 14, background: p.panel, border: `1px solid ${p.border}`, boxShadow: p.shadow }}\n            >\n              <AnimatePresence mode=\"popLayout\" initial={false}>\n                <motion.div key={index} className=\"flex flex-col\" style={{ gap: 6 }} initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} transition={{ duration: reduce ? 0 : 0.16 }}>\n                  <div className=\"flex items-center justify-between\" style={{ gap: 12 }}>\n                    <h2 id={`${uid}-title`} className=\"m-0 font-semibold tracking-[-0.01em]\" style={{ color: p.text, fontSize: 15 }}>\n                      {step?.title}\n                    </h2>\n                    <span className=\"shrink-0 tabular-nums\" style={{ color: p.muted, fontSize: 12 }} aria-live=\"polite\">\n                      {index + 1} / {steps.length}\n                    </span>\n                  </div>\n                  {step?.body && (\n                    <div id={`${uid}-body`} style={{ color: p.muted, fontSize: 13, lineHeight: 1.55 }}>\n                      {step.body}\n                    </div>\n                  )}\n                </motion.div>\n              </AnimatePresence>\n              <div className=\"flex items-center\" style={{ gap: 8 }}>\n                {showProgress && (\n                  <div className=\"flex items-center\" style={{ gap: 4 }} aria-hidden=\"true\">\n                    {steps.map((_, i) => (\n                      <motion.span key={i} className=\"block rounded-full\" initial={false} animate={{ width: i === index ? 16 : 6, background: i <= index ? p.primary : p.faint }} transition={spring} style={{ height: 6 }} />\n                    ))}\n                  </div>\n                )}\n                <span className=\"flex-1\" />\n                {!last && button(\"ghost\", \"Skip\", close)}\n                {index > 0 && button(\"secondary\", \"Back\", back)}\n                {button(\"primary\", last ? \"Done\" : \"Next\", next)}\n              </div>\n            </motion.div>\n            </>\n            )}\n          </motion.div>\n        )}\n      </AnimatePresence>\n    </>\n  );\n}\n\nexport default ProductTour;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}