{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"copy-morph","type":"registry:block","title":"Copy morph","description":"An install command with package-manager tabs and a copy button whose sheets fold away as the check draws in, then swaps back after two seconds.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/copy-morph/copy-morph.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useId, useRef, useState } from \"react\";\nimport {\n  AnimatePresence,\n  MotionConfig,\n  motion,\n  useInView,\n  useReducedMotion,\n} from \"motion/react\";\nimport { Check, Copy, Terminal } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type CopyMorphCommand = {\n  id: string;\n  label: string;\n  command: string;\n};\n\nexport type CopyMorphProps = {\n  className?: string;\n  title?: string;\n  description?: string;\n  commands?: CopyMorphCommand[];\n  defaultCommandId?: string;\n  /** Text shown after a successful copy. Defaults to \"Copied\". */\n  copiedLabel?: string;\n  /** How long the copied state stays on screen, in ms. Defaults to 2000. */\n  copiedDuration?: number;\n};\n\n/* ------------------------------------------------------------------ */\n/* deterministic demo data                                             */\n/* ------------------------------------------------------------------ */\n\nconst DEFAULT_COMMANDS: CopyMorphCommand[] = [\n  {\n    id: \"npm\",\n    label: \"npm\",\n    command: \"npx shadcn@latest add https://prequelui.terravidhal.me/r/copy-morph.json\",\n  },\n  {\n    id: \"pnpm\",\n    label: \"pnpm\",\n    command: \"pnpm dlx shadcn@latest add https://prequelui.terravidhal.me/r/copy-morph.json\",\n  },\n  {\n    id: \"bun\",\n    label: \"bun\",\n    command: \"bunx --bun shadcn@latest add https://prequelui.terravidhal.me/r/copy-morph.json\",\n  },\n];\n\n/* ------------------------------------------------------------------ */\n/* clipboard                                                           */\n/* ------------------------------------------------------------------ */\n\nasync function writeToClipboard(text: string) {\n  if (navigator.clipboard?.writeText) {\n    await navigator.clipboard.writeText(text);\n    return;\n  }\n\n  const field = document.createElement(\"textarea\");\n  field.value = text;\n  field.setAttribute(\"readonly\", \"\");\n  field.style.position = \"fixed\";\n  field.style.top = \"-999px\";\n  document.body.appendChild(field);\n  field.select();\n  const copied = document.execCommand(\"copy\");\n  document.body.removeChild(field);\n  if (!copied) throw new Error(\"Copy failed\");\n}\n\n/* ------------------------------------------------------------------ */\n/* icon: the two sheets fold away while the check draws in             */\n/* ------------------------------------------------------------------ */\n\nfunction MorphIcon({ copied, reduce }: { copied: boolean; reduce: boolean }) {\n  return (\n    <svg\n      aria-hidden=\"true\"\n      focusable=\"false\"\n      viewBox=\"0 0 20 20\"\n      fill=\"none\"\n      className=\"h-[18px] w-[18px]\"\n    >\n      <motion.g\n        initial={false}\n        animate={{\n          opacity: copied ? 0 : 1,\n          scale: copied ? 0.72 : 1,\n          rotate: copied ? -18 : 0,\n        }}\n        transition={\n          reduce ? { duration: 0 } : { duration: 0.28, ease: EASE }\n        }\n        style={{ transformOrigin: \"10px 10px\" }}\n      >\n        <rect\n          x=\"7\"\n          y=\"3.4\"\n          width=\"9.6\"\n          height=\"9.6\"\n          rx=\"2\"\n          stroke=\"currentColor\"\n          strokeWidth=\"1.5\"\n        />\n        <path\n          d=\"M4.9 7.1v6.3a3.2 3.2 0 0 0 3.2 3.2h6.3\"\n          stroke=\"currentColor\"\n          strokeWidth=\"1.5\"\n          strokeLinecap=\"round\"\n        />\n      </motion.g>\n\n      <motion.g\n        initial={false}\n        animate={{\n          opacity: copied ? 1 : 0,\n          scale: copied ? 1 : 0.72,\n          rotate: copied ? 0 : 18,\n        }}\n        transition={\n          reduce ? { duration: 0 } : { duration: 0.28, ease: EASE }\n        }\n        style={{ transformOrigin: \"10px 10px\" }}\n      >\n        <motion.path\n          d=\"M3.8 10.4l4.1 4.1L16.3 5.9\"\n          stroke=\"currentColor\"\n          strokeWidth=\"1.9\"\n          strokeLinecap=\"round\"\n          strokeLinejoin=\"round\"\n          initial={false}\n          animate={{ pathLength: copied ? 1 : 0, opacity: copied ? 1 : 0 }}\n          transition={\n            reduce\n              ? { duration: 0 }\n              : { duration: 0.34, ease: EASE, delay: copied ? 0.06 : 0 }\n          }\n        />\n      </motion.g>\n    </svg>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function CopyMorph({\n  className,\n  title = \"Quick install\",\n  description = \"Add the block to a project that already uses shadcn/ui.\",\n  commands = DEFAULT_COMMANDS,\n  defaultCommandId,\n  copiedLabel = \"Copied\",\n  copiedDuration = 2000,\n}: CopyMorphProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const inView = useInView(ref, { once: true, margin: \"-10% 0px\" });\n  const reduce = Boolean(useReducedMotion());\n  const groupId = useId();\n\n  const safeCommands = commands.length > 0 ? commands : DEFAULT_COMMANDS;\n  const [activeId, setActiveId] = useState(() => {\n    const initial =\n      safeCommands.find((item) => item.id === defaultCommandId) ?? safeCommands[0];\n    return initial?.id ?? \"\";\n  });\n  const [copied, setCopied] = useState(false);\n\n  const active =\n    safeCommands.find((item) => item.id === activeId) ?? safeCommands[0];\n\n  useEffect(() => {\n    if (!copied) return;\n    const timer = window.setTimeout(\n      () => setCopied(false),\n      Math.max(600, copiedDuration)\n    );\n    return () => window.clearTimeout(timer);\n  }, [copied, copiedDuration]);\n\n  const select = (id: string) => {\n    setActiveId(id);\n    setCopied(false);\n  };\n\n  const handleCopy = async () => {\n    if (!active) return;\n    try {\n      await writeToClipboard(active.command);\n      setCopied(true);\n    } catch {\n      setCopied(false);\n    }\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <motion.div\n        ref={ref}\n        initial={reduce ? false : { opacity: 0, y: 16 }}\n        animate={inView || reduce ? { opacity: 1, y: 0 } : { opacity: 0, y: 16 }}\n        transition={{ duration: reduce ? 0 : 0.6, ease: EASE }}\n        className={cn(\n          \"w-full rounded-xl border border-border bg-card p-5 text-card-foreground sm:p-6\",\n          className\n        )}\n      >\n        <div className=\"flex flex-wrap items-start justify-between gap-3\">\n          <div className=\"min-w-0\">\n            <h2 className=\"text-[15px] font-semibold tracking-tight\">{title}</h2>\n            <p className=\"mt-0.5 text-[13px] text-muted-foreground\">{description}</p>\n          </div>\n          <span className=\"inline-flex shrink-0 items-center gap-1.5 rounded-full border border-border px-2.5 py-1 text-[11.5px] font-medium text-muted-foreground\">\n            <span aria-hidden=\"true\" className=\"h-1.5 w-1.5 rounded-full bg-success\" />\n            MIT\n          </span>\n        </div>\n\n        <div\n          role=\"group\"\n          aria-label=\"Package manager\"\n          className=\"mt-5 inline-flex w-fit items-center gap-0.5 rounded-lg border border-border bg-muted/40 p-0.5\"\n        >\n          {safeCommands.map((item) => {\n            const selected = item.id === active?.id;\n            return (\n              <button\n                key={item.id}\n                type=\"button\"\n                onClick={() => select(item.id)}\n                aria-pressed={selected}\n                className={cn(\n                  \"relative rounded-[6px] px-2.5 py-1 text-[11.5px] font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\",\n                  selected\n                    ? \"text-foreground\"\n                    : \"text-muted-foreground hover:text-foreground\"\n                )}\n              >\n                {selected ? (\n                  <motion.span\n                    layoutId={`copy-morph-manager-${groupId}`}\n                    aria-hidden=\"true\"\n                    className=\"absolute inset-0 rounded-[6px] border border-border bg-card\"\n                    transition={{ duration: reduce ? 0 : 0.35, ease: EASE }}\n                  />\n                ) : null}\n                <span className=\"relative\">{item.label}</span>\n              </button>\n            );\n          })}\n        </div>\n\n        <div\n          className={cn(\n            \"mt-3 min-w-0 rounded-lg border bg-background transition-colors motion-reduce:transition-none\",\n            copied ? \"border-success/40\" : \"border-border\"\n          )}\n        >\n          <div className=\"flex min-w-0 items-center gap-2.5 p-2.5 pl-3\">\n            <Terminal\n              aria-hidden=\"true\"\n              className=\"h-3.5 w-3.5 shrink-0 text-muted-foreground\"\n            />\n            <code\n              title={active?.command}\n              className=\"min-w-0 flex-1 select-all truncate font-mono text-[11.5px] leading-6 text-muted-foreground sm:text-[12px]\"\n            >\n              {active?.command}\n            </code>\n            <button\n              type=\"button\"\n              onClick={() => void handleCopy()}\n              aria-label={copied ? copiedLabel : \"Copy install command\"}\n              className={cn(\n                \"inline-flex h-8 shrink-0 items-center gap-1.5 rounded-md border px-2.5 text-[11.5px] font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card motion-reduce:transition-none\",\n                copied\n                  ? \"border-success/40 bg-success/10 text-success\"\n                  : \"border-border bg-card text-foreground hover:bg-muted\"\n              )}\n            >\n              <MorphIcon copied={copied} reduce={reduce} />\n              <span className=\"relative inline-flex h-4 min-w-[48px] items-center overflow-hidden\">\n                <AnimatePresence initial={false} mode=\"wait\">\n                  <motion.span\n                    key={copied ? \"copied\" : \"copy\"}\n                    initial={reduce ? false : { opacity: 0, y: 8 }}\n                    animate={{ opacity: 1, y: 0 }}\n                    exit={reduce ? { opacity: 0 } : { opacity: 0, y: -8 }}\n                    transition={{ duration: reduce ? 0 : 0.2, ease: EASE }}\n                    className=\"absolute inset-0 flex items-center\"\n                  >\n                    {copied ? copiedLabel : \"Copy\"}\n                  </motion.span>\n                </AnimatePresence>\n              </span>\n            </button>\n          </div>\n\n          <div className=\"flex flex-wrap items-center justify-between gap-2 border-t border-border px-3 py-2\">\n            <span aria-live=\"polite\" className=\"text-[11px] text-muted-foreground\">\n              {copied ? (\n                <span className=\"inline-flex items-center gap-1.5 text-success\">\n                  <Check aria-hidden=\"true\" className=\"h-3 w-3\" />\n                  Copied to clipboard — paste it in your terminal.\n                </span>\n              ) : (\n                \"Copies the exact command for this block.\"\n              )}\n            </span>\n            <span className=\"font-mono text-[10px] tabular-nums text-muted-foreground\">\n              registry v1\n            </span>\n          </div>\n        </div>\n\n        <div className=\"mt-4 flex flex-wrap items-center gap-x-2 gap-y-1 text-[11.5px] text-muted-foreground\">\n          <Copy aria-hidden=\"true\" className=\"h-3 w-3\" />\n          <span>Run it at the root of your project.</span>\n          <span aria-hidden=\"true\" className=\"text-border\">\n            /\n          </span>\n          <span className=\"text-foreground\">Requires Node 18.17 or newer</span>\n        </div>\n      </motion.div>\n    </MotionConfig>\n  );\n}\n\nexport default CopyMorph;\n"}]}