{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"copy-button","type":"registry:ui","title":"Copy button","description":"Copy a value with immediate confirmation.","categories":["actions"],"dependencies":["lucide-react","motion"],"registryDependencies":["https://ui.teamofsilicons.com/r/silicon-motion-tokens.json"],"files":[{"path":"registry/components/copy-button/copy-button.tsx","type":"registry:component","target":"@components/silicon-ui/copy-button/copy-button.tsx","content":"\"use client\";\n\nimport { useState } from \"react\";\nimport { AnimatePresence, motion, useReducedMotion, type TargetAndTransition, type Transition } from \"motion/react\";\nimport { CircleAlert, Copy } from \"lucide-react\";\nimport { motionTokens } from \"../lib/motion-tokens\";\nimport { useCopyFeedback } from \"../lib/use-copy-feedback\";\nimport styles from \"./copy-button.module.css\";\n\nexport interface CopyButtonProps {\n  value: string;\n  label?: string;\n  className?: string;\n  iconOnly?: boolean;\n  variant?: \"outline\" | \"plain\";\n  disabled?: boolean;\n  onCopied?: () => void;\n}\n\n/** Copy feedback is deliberately unhurried: a slow, almost critically damped spring and long, soft crossfades read as calm, never busy.\n *  The same motion plays in reverse when the confirmation hands back to idle, so nothing ever snaps. */\nconst settle = { type: \"spring\", visualDuration: motionTokens.spring.slow.visualDuration, bounce: .06 } as const;\nconst enter = { duration: motionTokens.duration.standard, ease: [...motionTokens.ease.enter] } as const;\nconst leave = { duration: motionTokens.duration.exit, ease: [...motionTokens.ease.standard] } as const;\nconst instant = { duration: motionTokens.duration.instant } as const;\nconst soft = `blur(${motionTokens.blur.soft}px)`;\nconst rest: TargetAndTransition = { opacity: 1, y: 0, scale: 1, filter: \"blur(0px)\" };\nconst fadeIn: TargetAndTransition = { ...rest, opacity: 0 };\nconst fadeOut: TargetAndTransition = { opacity: 0, transition: instant };\n/** Icons trade places in one soft breath: the old glyph shrinks into a blur while the new one grows out of it on the slow spring. */\nconst iconIn: TargetAndTransition = { opacity: 0, scale: .6, filter: soft };\nconst iconOut: TargetAndTransition = { opacity: 0, scale: .6, filter: soft, transition: { duration: motionTokens.duration.exit, ease: [...motionTokens.ease.standard] } };\nconst iconEnter: Transition = { scale: settle, opacity: { ...enter, delay: .03 }, filter: { ...enter, delay: .03 } };\n/** Letters rise about .3em out of a soft blur; the outgoing ones lift away a little faster. */\nconst glyphIn: TargetAndTransition = { opacity: 0, y: 4, filter: soft };\nconst glyphOut: TargetAndTransition = { opacity: 0, y: -3, filter: soft, transition: leave };\n\n/** The success tick draws itself from its short stroke, the way a hand would write it: quick to start, then easing into place while the icon settles. */\nfunction DrawnCheck({ reduced }: { reduced: boolean }) {\n  return <svg width={16} height={16} viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth={1.75} strokeLinecap=\"round\" strokeLinejoin=\"round\">\n    <motion.path d=\"M4 12l5 5L20 6\" initial={reduced ? false : { pathLength: 0, opacity: 0 }} animate={{ pathLength: 1, opacity: 1 }} transition={{ pathLength: { duration: .5, ease: [...motionTokens.ease.standard], delay: .05 }, opacity: { duration: .01, delay: .05 } }} />\n  </svg>;\n}\n\ntype Glyph = { id: string; char: string; order: number };\nconst toGlyphs = (chars: string[], seq: number): Glyph[] => chars.map((char, order) => ({ id: `${seq}:${order}`, char, order }));\n\n/** Shared leading and trailing characters keep their identity, so \"Copy\" to \"Copied\" only replaces the changed letters. */\nfunction useGlyphs(text: string) {\n  const [state, setState] = useState(() => ({ text, seq: 0, glyphs: toGlyphs([...text], 0) }));\n  if (state.text === text) return state.glyphs;\n  const prev = [...state.text], next = [...text];\n  let start = 0, end = 0;\n  while (start < prev.length && start < next.length && prev[start] === next[start]) start++;\n  while (end < prev.length - start && end < next.length - start && prev[prev.length - 1 - end] === next[next.length - 1 - end]) end++;\n  if (start < 2) start = 0;\n  if (end < 2) end = 0;\n  const seq = state.seq + 1;\n  const glyphs = [...state.glyphs.slice(0, start), ...toGlyphs(next.slice(start, next.length - end), seq), ...state.glyphs.slice(state.glyphs.length - end)];\n  setState({ text, seq, glyphs });\n  return glyphs;\n}\n\n/**\n * The label cell already reserves its widest state, so the row stays put and only the letters move. It is a layout root:\n * the letters measure their glide inside the label only, so when the button itself moves (a page entering, a panel\n * sliding, a list reflowing) they ride along instead of flying in from where the button used to be.\n */\nfunction MorphText({ text, reduced }: { text: string; reduced: boolean }) {\n  const glyphs = useGlyphs(text);\n  return <motion.span className={styles.glyphs} layoutRoot>\n    <AnimatePresence mode=\"popLayout\" initial={false}>\n      {glyphs.map(glyph => <motion.span key={glyph.id} className={styles.glyph} layout={reduced ? false : \"position\"} layoutDependency={text} initial={reduced ? fadeIn : glyphIn} animate={rest} exit={reduced ? fadeOut : glyphOut} transition={reduced ? instant : { ...enter, delay: Math.min(glyph.order * .02, .12), layout: settle }}>{glyph.char}</motion.span>)}\n    </AnimatePresence>\n  </motion.span>;\n}\n\nexport function CopyButton({ value, label = \"Copy\", className, iconOnly = false, variant = \"outline\", disabled, onCopied }: CopyButtonProps) {\n  const { state, copy } = useCopyFeedback();\n  const reduced = useReducedMotion() ?? false;\n  const text = state === \"copied\" ? \"Copied\" : state === \"error\" ? \"Failed\" : label;\n\n  async function handleCopy() {\n    if (await copy(value)) onCopied?.();\n  }\n\n  return <><button\n    type=\"button\"\n    className={[styles.button, iconOnly && styles.iconOnly, variant === \"plain\" && styles.plain, className].filter(Boolean).join(\" \")}\n    onClick={() => void handleCopy()}\n    aria-label={label}\n    data-copy-state={state}\n    disabled={disabled}\n  >\n    <span className={styles.icon} aria-hidden=\"true\">\n      <AnimatePresence initial={false}>\n        <motion.span key={state} className={styles.iconInner} data-state={state} initial={reduced ? fadeIn : iconIn} animate={rest} exit={reduced ? fadeOut : iconOut} transition={reduced ? instant : iconEnter}>\n          {state === \"copied\" ? <DrawnCheck reduced={reduced} /> : state === \"error\" ? <CircleAlert size={16} strokeWidth={1.75} /> : <Copy size={16} strokeWidth={1.75} />}\n        </motion.span>\n      </AnimatePresence>\n    </span>\n    {!iconOnly && <span className={styles.label} aria-hidden=\"true\">\n      <span className={styles.measure}>{label}</span><span className={styles.measure}>Copied</span><span className={styles.measure}>Failed</span>\n      <MorphText text={text} reduced={reduced} />\n    </span>}\n  </button><span className={styles.srOnly} role=\"status\" aria-live=\"polite\">{state === \"idle\" ? \"\" : state === \"error\" ? `${label}: Could not copy` : `${label}: Copied`}</span></>;\n}\n"},{"path":"registry/components/copy-button/copy-button.module.css","type":"registry:component","target":"@components/silicon-ui/copy-button/copy-button.module.css","content":"/* Width never changes: the label cell reserves its widest state while icon and letters crossfade inside it. Press releases on the spring curve. */\n.button { display: inline-flex; width: max-content; max-width: 100%; min-height: var(--control-height-sm); align-items: center; justify-content: center; gap: var(--space-related); border: 1px solid var(--border); border-radius: var(--radius-control); padding: 0 var(--space-group); background: var(--surface); color: var(--foreground); font: inherit; font-size: var(--text-sm); font-weight: 500; cursor: pointer; -webkit-tap-highlight-color: transparent; transition: background-color var(--duration-fast) var(--ease-standard), border-color var(--duration-fast) var(--ease-standard), color var(--duration-fast) var(--ease-standard), transform var(--duration-spring) var(--ease-spring); }\n.button:active:not(:disabled) { transform: scale(.97); transition-duration: var(--duration-fast), var(--duration-fast), var(--duration-fast), var(--duration-instant); transition-timing-function: var(--ease-standard); }\n.iconOnly:active:not(:disabled) { transform: scale(.96); }\n@media (hover: hover) and (pointer: fine) { .button:hover:not(:disabled) { background: var(--surface-muted); border-color: var(--border-strong); } .plain:hover:not(:disabled) { border-color: transparent; background: var(--surface-muted); color: var(--foreground); } }\n.button:disabled { cursor: not-allowed; opacity: .5; }\n/* Each icon carries its own color, so the outgoing glyph never tints on its way out. */\n.icon { position: relative; display: grid; width: 16px; height: 16px; flex: none; }\n.iconInner { position: absolute; inset: 0; display: grid; place-items: center; }\n.iconInner[data-state=\"copied\"] { color: var(--success); }\n.iconInner[data-state=\"error\"] { color: var(--danger); }\n/* The inline padding gives blurred edge letters room before the horizontal clip; the negative margin keeps the box the same size. */\n.label { display: grid; min-width: 0; margin-inline: calc(var(--space-tight) * -1); padding-inline: var(--space-tight); overflow-x: clip; text-align: left; white-space: nowrap; }\n.measure, .glyphs { grid-area: 1 / 1; }\n.measure { visibility: hidden; }\n.glyphs { position: relative; display: inline-flex; justify-self: start; white-space: pre; }\n.glyph { display: inline-block; }\n.iconOnly { width: var(--control-height-sm); padding: 0; }\n.plain { border-color: transparent; background: transparent; color: var(--text-secondary); }\n.srOnly { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }\n@media (prefers-reduced-motion: reduce) { .button { transition: none; } .button:active:not(:disabled) { transform: none; } }\n"},{"path":"lib/use-copy-feedback.ts","type":"registry:lib","target":"@components/silicon-ui/lib/use-copy-feedback.ts","content":"\"use client\";\n\nimport { useCallback, useEffect, useRef, useState } from \"react\";\n\nexport type CopyFeedbackState = \"idle\" | \"copied\" | \"error\";\n\n/** Shared clipboard state for actions that render their own button or menu. */\nexport function useCopyFeedback(duration = 1900) {\n  const [state, setState] = useState<CopyFeedbackState>(\"idle\");\n  const [activeKey, setActiveKey] = useState<string | null>(null);\n  const timeout = useRef<ReturnType<typeof setTimeout> | null>(null);\n\n  const reset = useCallback(() => {\n    if (timeout.current) clearTimeout(timeout.current);\n    timeout.current = null;\n    setState(\"idle\");\n    setActiveKey(null);\n  }, []);\n\n  useEffect(() => () => {\n    if (timeout.current) clearTimeout(timeout.current);\n  }, []);\n\n  const copy = useCallback(async (value: string, key = \"default\") => {\n    if (timeout.current) clearTimeout(timeout.current);\n    setActiveKey(key);\n    try {\n      await navigator.clipboard.writeText(value);\n      setState(\"copied\");\n      timeout.current = setTimeout(reset, duration);\n      return true;\n    } catch {\n      setState(\"error\");\n      timeout.current = setTimeout(reset, duration);\n      return false;\n    }\n  }, [duration, reset]);\n\n  return { state, activeKey, copy, reset };\n}\n"}],"docs":"Docs and live preview: https://ui.teamofsilicons.com/copy-button","meta":{"tier":"free","kind":"component","docs":"https://ui.teamofsilicons.com/copy-button","markdown":"https://ui.teamofsilicons.com/copy-button.md","tags":["action","utility"],"status":"stable"}}