Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
tsx 62 lines 2.12 KB
Raw
1 'use client';
2
3 import { useState } from 'react';
4 import { Check, Copy } from 'lucide-react';
5
6 /**
7 * Copy-to-clipboard icon button for the code header of the @pierre/diffs widgets.
8 * A faint chip that lifts on hover, presses on click, and crossfades the copy
9 * glyph into a green check on success.
10 *
11 * The widget themes itself off the OS `prefers-color-scheme` (its shadow root
12 * pins `color-scheme: light dark`), NOT the site's class-based dark toggle, so
13 * the header can be light even with the docs in dark mode. The chip's colors
14 * live in the `.diff-copy-button` rule in global.css and use `light-dark()` to
15 * track that same signal, staying legible on either header. Icon-only; the
16 * caller positions it (usually absolute top-right).
17 */
18 export function CopyButton({ text, className }: { text: string; className?: string }) {
19 const [copied, setCopied] = useState(false);
20
21 const copy = async () => {
22 try {
23 await navigator.clipboard.writeText(text);
24 setCopied(true);
25 setTimeout(() => setCopied(false), 1600);
26 } catch {
27 // clipboard unavailable (e.g. insecure context); ignore
28 }
29 };
30
31 return (
32 <button
33 type="button"
34 onClick={copy}
35 aria-label={copied ? 'Copied' : 'Copy code'}
36 title={copied ? 'Copied' : 'Copy'}
37 className={
38 'diff-copy-button inline-flex size-7 items-center justify-center rounded-md border shadow-sm backdrop-blur-md ' +
39 'transition-[transform,background-color,border-color,color] duration-150 ease-out ' +
40 'active:scale-90 ' +
41 (className ?? '')
42 }
43 >
44 <span className="relative size-3.5">
45 <Copy
46 aria-hidden="true"
47 className={
48 'absolute inset-0 size-3.5 transition-all duration-200 ease-out ' +
49 (copied ? 'scale-50 opacity-0' : 'scale-100 opacity-100')
50 }
51 />
52 <Check
53 aria-hidden="true"
54 className={
55 'absolute inset-0 size-3.5 text-emerald-400 transition-all duration-200 ease-out ' +
56 (copied ? 'scale-100 opacity-100' : 'scale-50 opacity-0')
57 }
58 />
59 </span>
60 </button>
61 );
62 }