-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathCopyButton.tsx
More file actions
52 lines (48 loc) · 1.13 KB
/
CopyButton.tsx
File metadata and controls
52 lines (48 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React from 'react'
import {
Button,
type ButtonProps,
} from '@sqlmesh-common/components/Button/Button'
import { useCopyClipboard } from '@sqlmesh-common/hooks/useCopyClipboard'
export interface CopyButtonProps extends Omit<ButtonProps, 'children'> {
text: string
delay?: number
children: (copied: boolean) => React.ReactNode
}
export const CopyButton = React.forwardRef<HTMLButtonElement, CopyButtonProps>(
(
{
text,
title = 'Copy to clipboard',
variant = 'secondary',
size = 'xs',
delay = 2000,
disabled = false,
children,
onClick,
...props
},
ref,
) => {
const [copyToClipboard, isCopied] = useCopyClipboard(delay)
return (
<Button
ref={ref}
data-component="CopyButton"
title={title}
size={size}
variant={variant}
onClick={e => {
e.stopPropagation()
copyToClipboard(text)
onClick?.(e)
}}
disabled={disabled || !!isCopied}
{...props}
>
{children(isCopied != null)}
</Button>
)
},
)
CopyButton.displayName = 'CopyButton'