Open History to see the last commit
1import { useClipboard } from '@ark-ui/solid'
2import { Show, splitProps } from 'solid-js'
3import CheckIcon from 'lucide-solid/icons/check'
4import CopyIcon from 'lucide-solid/icons/copy'
5import { Button } from './button'
6import type { ButtonProps } from './button'
7import { cn } from '@/utils'
8β
9type CopyButtonProps = Omit<ButtonProps, 'value'> & {
10 // The value that should be copied
11 value: unknown
12 copyTimeout?: number
13}
14β
15/**
16 * An icon button that copies a value to the clipboard and shows a copied state.
17 *
18 * @example
19 * ```tsx
20 * <CopyButton value="Hello world" />
21 * ```
22 */
23export function CopyButton(props: CopyButtonProps) {
24 const [local, others] = splitProps(props, [
25 'value',
26 'variant',
27 'size',
28 'class',
29 ])
30β
31 const variant = () => local.variant ?? 'ghost'
32 const size = () => local.size ?? 'icon'
33β
34 const clipboard = useClipboard({
35 defaultValue: '',
36 timeout: props.copyTimeout ?? 1200,
37 })
38β
39 function handleCopy() {
40 const text = toClipboardText(props.value)
41β
42 clipboard().setValue(text)
43 clipboard().copy()
44 }
45β
46 return (
47 <Button
48 {...clipboard().getTriggerProps()}
49 variant={variant()}
50 size={size()}
51 class={cn('text-muted-foreground hover:text-foreground', props.class)}
52 onClick={handleCopy}
53 {...others}
54 >
55 <Show when={clipboard().copied} fallback={<CopyIcon />}>
56 <CheckIcon />
57 </Show>
58 </Button>
59 )
60}
61β
62function toClipboardText(value: unknown): string {
63 if (typeof value === 'string') return value
64 if (typeof value === 'number') return String(value)
65 if (typeof value === 'boolean') return String(value)
66 if (typeof value === 'bigint') return `${value}n`
67 if (typeof value === 'undefined') return 'undefined'
68 if (typeof value === 'function') return value.toString()
69 if (typeof value === 'symbol') return value.toString()
70 if (value === null) return 'null'
71β
72 const seen = new WeakSet<object>()
73β
74 const text = JSON.stringify(
75 value,
76 (_key, current) => {
77 if (typeof current === 'bigint') return `${current}n`
78 if (typeof current === 'undefined') return 'undefined'
79 if (typeof current === 'function') return current.toString()
80 if (typeof current === 'symbol') return current.toString()
81β
82 if (current instanceof Map) {
83 return {
84 type: 'Map',
85 entries: Array.from(current.entries()),
86 }
87 }
88β
89 if (current instanceof Set) {
90 return {
91 type: 'Set',
92 values: Array.from(current.values()),
93 }
94 }
95β
96 if (current instanceof RegExp) {
97 return current.toString()
98 }
99β
100 if (current instanceof Error) {
101 return {
102 name: current.name,
103 message: current.message,
104 stack: current.stack,
105 }
106 }
107β
108 if (current && typeof current === 'object') {
109 if (seen.has(current)) {
110 let name = 'Object'
111β
112 try {
113 // Looks funny, doesn't it? You can try the unsafe version yourself:
114 // Object.create(null)
115 name = current?.constructor?.name ?? 'Object'
116 } catch {}
117β
118 return `[Circular Reference: ${name}]`
119 }
120 seen.add(current)
121 }
122β
123 return current
124 },
125 2,
126 )
127β
128 return String(text)
129}