{
  "schemaVersion": 1,
  "target": "react",
  "name": "icon-swap",
  "version": "0.1.0",
  "category": "core",
  "title": "Icon Swap",
  "description": "Cross-fades icons that share one box with a small scale and blur, plus a Copy Button whose two pages slide together into a check that draws itself.",
  "tags": [
    "icon",
    "swap",
    "toggle",
    "copy",
    "clipboard",
    "button"
  ],
  "requires": {
    "projectTypes": [
      "next",
      "vite-react"
    ],
    "styling": "tailwind-v4"
  },
  "npmDependencies": {},
  "registryDependencies": [],
  "docsPath": "/components/icon-swap",
  "props": [
    {
      "name": "value",
      "type": "string",
      "description": "IconSwap: key of the icon to show."
    },
    {
      "name": "icons",
      "type": "Record<string, ReactNode>",
      "description": "IconSwap: every icon the box can show, by key. All stay mounted so swaps are interruptible."
    },
    {
      "name": "CopyButton value",
      "type": "string | () => string | Promise<string>",
      "description": "CopyButton: the text to copy, or a function that returns it."
    },
    {
      "name": "label / copiedLabel",
      "type": "string",
      "default": "\"Copy\" / \"Copied\"",
      "description": "CopyButton: the button's name, and the text announced and shown after copying."
    },
    {
      "name": "showLabel",
      "type": "boolean",
      "default": "false",
      "description": "CopyButton: shows the label next to the icon; both labels share one width so the button never resizes."
    },
    {
      "name": "variant",
      "type": "\"ghost\" | \"secondary\"",
      "default": "\"ghost\"",
      "description": "CopyButton: button style."
    },
    {
      "name": "size",
      "type": "\"sm\" | \"md\"",
      "default": "\"md\"",
      "description": "CopyButton: 28px or 32px tall."
    },
    {
      "name": "timeout",
      "type": "number",
      "default": "1600",
      "description": "CopyButton: milliseconds the check stays before the copy icon returns."
    },
    {
      "name": "onCopy / onCopyError",
      "type": "(text) => void / (error) => void",
      "description": "CopyButton: called after a successful copy, or when the clipboard refuses."
    }
  ],
  "accessibility": [
    "IconSwap is aria-hidden; name the button that holds it, and pair state changes with aria-pressed, aria-expanded, or a changing aria-label.",
    "CopyButton is a native button. Icon-only, it is named by label; with showLabel, the inactive label is aria-hidden so the name is always the visible text.",
    "After copying, the copied label is announced once through a polite live region outside the button.",
    "Falls back to a hidden textarea and the copy command on insecure origins.",
    "With reduced motion preferred, icons and labels swap instantly and the check appears already drawn."
  ],
  "states": [
    "idle",
    "copied",
    "swapping"
  ],
  "files": [
    {
      "path": "ui/icon-swap.tsx",
      "content": "\"use client\";\n\nimport { type ComponentProps, type ReactNode, useEffect, useRef, useState } from \"react\";\n\nfunction cx(...classes: (string | false | null | undefined)[]) {\n  return classes.filter(Boolean).join(\" \");\n}\n\nexport interface IconSwapProps extends Omit<ComponentProps<\"span\">, \"children\"> {\n  /** Key of the icon to show. */\n  value: string;\n  /** Every icon the box can show, by key. All stay mounted, so swaps are interruptible. */\n  icons: Record<string, ReactNode>;\n}\n\n/**\n * Cross-fades icons that share one box: the outgoing icon shrinks, blurs and fades while the\n * incoming one grows into focus, both at once. Size the box with `className` (16px by default).\n * Each icon's wrapper carries `data-state=\"active\"` or `\"inactive\"` on a `group/icon-swap`, so an\n * icon can style its own parts during the swap (see the check in CopyButton).\n * Decorative: label the button that holds it.\n */\nexport function IconSwap({ value, icons, className, ...props }: IconSwapProps) {\n  return (\n    <span aria-hidden=\"true\" className={cx(\"relative inline-grid size-4 shrink-0 place-items-center\", className)} {...props}>\n      {Object.entries(icons).map(([key, icon]) => {\n        const active = key === value;\n        return (\n          <span\n            key={key}\n            data-state={active ? \"active\" : \"inactive\"}\n            className={cx(\n              \"group/icon-swap flex size-full items-center justify-center [grid-area:1/1] [&>svg]:size-full\",\n              \"transition-[opacity,scale,filter] motion-reduce:transition-none\",\n              // In: 220ms quint ease-out. Out: 140ms, so the two never read as a muddle.\n              active\n                ? \"scale-100 opacity-100 blur-none duration-220 ease-[cubic-bezier(0.23,1,0.32,1)]\"\n                : \"pointer-events-none scale-[0.55] opacity-0 blur-[2px] duration-140 ease-out\",\n            )}\n          >\n            {icon}\n          </span>\n        );\n      })}\n    </span>\n  );\n}\n\n/** Two stacked pages. While the swap leaves it, the back page slides onto the front one. */\nfunction CopyGlyph() {\n  return (\n    <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n      <path\n        d=\"M10.5 3.25V3a1.5 1.5 0 0 0-1.5-1.5H3.25A1.75 1.75 0 0 0 1.5 3.25V9a1.5 1.5 0 0 0 1.5 1.5h.25\"\n        className=\"transition-[translate,opacity] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] group-data-[state=inactive]/icon-swap:translate-x-[3px] group-data-[state=inactive]/icon-swap:translate-y-[3px] group-data-[state=inactive]/icon-swap:opacity-0 motion-reduce:transition-none\"\n      />\n      <rect x=\"5.5\" y=\"5.5\" width=\"9\" height=\"9\" rx=\"1.75\" />\n    </svg>\n  );\n}\n\n/** A check that draws itself each time it becomes the active icon, and snaps back once hidden. */\nfunction CheckGlyph() {\n  return (\n    <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.75\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n      <path\n        d=\"M3 8.5 6.5 12 13 4.5\"\n        pathLength={1}\n        className={cx(\n          \"[stroke-dasharray:1] [stroke-dashoffset:1] transition-[stroke-dashoffset] delay-150 duration-0\",\n          \"group-data-[state=active]/icon-swap:[stroke-dashoffset:0] group-data-[state=active]/icon-swap:delay-60 group-data-[state=active]/icon-swap:duration-320 group-data-[state=active]/icon-swap:ease-[cubic-bezier(0.65,0,0.35,1)]\",\n          \"motion-reduce:transition-none\",\n        )}\n      />\n    </svg>\n  );\n}\n\nconst copyVariants = {\n  ghost: cx(\n    \"text-zinc-500 hover:bg-zinc-950/[0.04] hover:text-zinc-950\",\n    \"dark:text-zinc-400 dark:hover:bg-white/[0.06] dark:hover:text-white\",\n  ),\n  secondary: cx(\n    \"border border-zinc-950/12 bg-white text-zinc-700 shadow-[0_1px_2px_rgb(0_0_0/0.04)] hover:border-zinc-950/20 hover:bg-zinc-950/[0.03] hover:text-zinc-950\",\n    \"dark:border-white/15 dark:bg-black dark:text-zinc-300 dark:shadow-none dark:hover:border-white/25 dark:hover:bg-white/[0.06] dark:hover:text-white\",\n  ),\n} as const;\n\nexport type CopyButtonVariant = keyof typeof copyVariants;\n\nexport interface CopyButtonProps extends Omit<ComponentProps<\"button\">, \"value\" | \"children\" | \"onCopy\"> {\n  /** The text to copy, or a function that returns it (it may be async). */\n  value: string | (() => string | Promise<string>);\n  /** Names the button, and is the visible text with `showLabel`. */\n  label?: string;\n  /** Replaces the label while the check shows, and is announced to screen readers. */\n  copiedLabel?: string;\n  /** Shows the label next to the icon. Both labels share one width, so the button never resizes. */\n  showLabel?: boolean;\n  variant?: CopyButtonVariant;\n  /** `sm` is 28px tall, `md` 32px. */\n  size?: \"sm\" | \"md\";\n  /** Milliseconds the check stays before the copy icon returns. */\n  timeout?: number;\n  /** Called with the copied text. */\n  onCopy?: (text: string) => void;\n  /** Called when the clipboard refuses the write. */\n  onCopyError?: (error: unknown) => void;\n}\n\nasync function writeClipboard(text: string) {\n  if (navigator.clipboard && window.isSecureContext) {\n    await navigator.clipboard.writeText(text);\n    return;\n  }\n  // Fallback for insecure origins: a hidden textarea and the legacy copy command.\n  const area = document.createElement(\"textarea\");\n  area.value = text;\n  area.setAttribute(\"readonly\", \"\");\n  area.style.position = \"fixed\";\n  area.style.opacity = \"0\";\n  document.body.append(area);\n  area.select();\n  const ok = document.execCommand(\"copy\");\n  area.remove();\n  if (!ok) throw new Error(\"Copy command was rejected\");\n}\n\n/**\n * Copies text to the clipboard. The two pages of the copy icon slide together and give way to a\n * check that draws itself, in the same box, so nothing around the button moves.\n */\nexport function CopyButton({\n  value,\n  label = \"Copy\",\n  copiedLabel = \"Copied\",\n  showLabel = false,\n  variant = \"ghost\",\n  size = \"md\",\n  timeout = 1600,\n  onCopy,\n  onCopyError,\n  onClick,\n  className,\n  type = \"button\",\n  ...props\n}: CopyButtonProps) {\n  const [copied, setCopied] = useState(false);\n  const timer = useRef<number | undefined>(undefined);\n  useEffect(() => () => window.clearTimeout(timer.current), []);\n\n  async function copy() {\n    try {\n      const text = typeof value === \"function\" ? await value() : value;\n      await writeClipboard(text);\n      setCopied(true);\n      window.clearTimeout(timer.current);\n      timer.current = window.setTimeout(() => setCopied(false), timeout);\n      onCopy?.(text);\n    } catch (error) {\n      onCopyError?.(error);\n    }\n  }\n\n  return (\n    <>\n      <button\n        type={type}\n        aria-label={showLabel ? undefined : label}\n        data-copied={copied || undefined}\n        onClick={(event) => {\n          onClick?.(event);\n          if (!event.defaultPrevented) void copy();\n        }}\n        className={cx(\n          \"inline-flex shrink-0 cursor-default items-center justify-center gap-1.5 rounded-lg font-medium whitespace-nowrap select-none\",\n          \"transition-[background-color,border-color,color,scale] duration-150 ease-out active:duration-100 motion-reduce:transition-none motion-safe:active:scale-[0.96]\",\n          \"focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-sky-500\",\n          \"disabled:pointer-events-none disabled:opacity-50\",\n          copyVariants[variant],\n          showLabel ? (size === \"sm\" ? \"h-7 px-2 text-xs\" : \"h-8 px-2.5 text-sm\") : size === \"sm\" ? \"size-7\" : \"size-8\",\n          className,\n        )}\n        {...props}\n      >\n        <IconSwap\n          value={copied ? \"check\" : \"copy\"}\n          icons={{ copy: <CopyGlyph />, check: <CheckGlyph /> }}\n          className={cx(size === \"sm\" ? \"size-3.5\" : \"size-4\", copied && \"text-zinc-950 dark:text-white\")}\n        />\n        {showLabel ? (\n          <span className=\"inline-grid\">\n            {[label, copiedLabel].map((text, index) => {\n              const active = (index === 1) === copied;\n              return (\n                <span\n                  key={index}\n                  aria-hidden={!active || undefined}\n                  className={cx(\n                    \"[grid-area:1/1] transition-[opacity,filter] motion-reduce:transition-none\",\n                    active\n                      ? \"opacity-100 blur-none duration-200 ease-[cubic-bezier(0.23,1,0.32,1)]\"\n                      : \"opacity-0 blur-[2px] duration-120 ease-out\",\n                  )}\n                >\n                  {text}\n                </span>\n              );\n            })}\n          </span>\n        ) : null}\n      </button>\n      {/* Outside the button so the announcement never becomes part of its name. */}\n      <span className=\"sr-only\" aria-live=\"polite\">\n        {copied ? copiedLabel : \"\"}\n      </span>\n    </>\n  );\n}\n"
    }
  ],
  "example": {
    "content": "\"use client\";\n\nimport { useState } from \"react\";\nimport { CopyButton, IconSwap } from \"@/components/shaken/ui/icon-swap\";\n\nconst iconButton =\n  \"inline-flex size-9 items-center justify-center rounded-lg border border-zinc-950/12 bg-white text-zinc-700 transition-[background-color,color,scale] duration-150 ease-out hover:bg-zinc-950/[0.03] hover:text-zinc-950 focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-sky-500 motion-safe:active:scale-[0.96] dark:border-white/15 dark:bg-black dark:text-zinc-300 dark:hover:bg-white/[0.06] dark:hover:text-white\";\n\nconst menu = (\n  <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\">\n    <path d=\"M2.5 5h11M2.5 11h11\" />\n  </svg>\n);\nconst close = (\n  <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\">\n    <path d=\"m4 4 8 8M12 4l-8 8\" />\n  </svg>\n);\nconst play = (\n  <svg viewBox=\"0 0 16 16\" fill=\"currentColor\">\n    <path d=\"M5 3.4v9.2a.6.6 0 0 0 .9.5l7.3-4.6a.6.6 0 0 0 0-1L5.9 2.9a.6.6 0 0 0-.9.5Z\" />\n  </svg>\n);\nconst pause = (\n  <svg viewBox=\"0 0 16 16\" fill=\"currentColor\">\n    <rect x=\"3.5\" y=\"3\" width=\"3\" height=\"10\" rx=\"0.75\" />\n    <rect x=\"9.5\" y=\"3\" width=\"3\" height=\"10\" rx=\"0.75\" />\n  </svg>\n);\n\nexport default function IconSwapExample() {\n  const [open, setOpen] = useState(false);\n  const [playing, setPlaying] = useState(false);\n\n  return (\n    <div className=\"grid w-full max-w-sm gap-5\">\n      <div className=\"flex items-center gap-2\">\n        <button\n          type=\"button\"\n          aria-label=\"Navigation\"\n          aria-expanded={open}\n          onClick={() => setOpen((value) => !value)}\n          className={iconButton}\n        >\n          <IconSwap value={open ? \"close\" : \"menu\"} icons={{ menu, close }} />\n        </button>\n        <button\n          type=\"button\"\n          aria-label={playing ? \"Pause preview\" : \"Play preview\"}\n          onClick={() => setPlaying((value) => !value)}\n          className={iconButton}\n        >\n          <IconSwap value={playing ? \"pause\" : \"play\"} icons={{ play, pause }} />\n        </button>\n        <CopyButton\n          value=\"https://shakenui.com/r/run/8f3a2c\"\n          label=\"Copy link\"\n          copiedLabel=\"Link copied\"\n          showLabel\n          variant=\"secondary\"\n          className=\"ml-auto h-9\"\n        />\n      </div>\n      <div className=\"flex items-center gap-2 rounded-lg border border-zinc-950/[0.07] bg-zinc-950/[0.02] py-1 pr-1 pl-3 dark:border-white/[0.08] dark:bg-transparent\">\n        <code className=\"min-w-0 flex-1 truncate font-mono text-xs text-zinc-700 dark:text-zinc-300\">npx @shakenui/cli add icon-swap</code>\n        <CopyButton value=\"npx @shakenui/cli add icon-swap\" label=\"Copy command\" size=\"sm\" />\n      </div>\n    </div>\n  );\n}\n"
  }
}
