Files
supabase/apps/studio/components/ui/CopyButton.tsx
T
Ivan VasilovandJoshen Lim 0895081f4f fix: Make the onCopyUrl work on Safari (#27215)
* Add a new hook for copying URLs.

* Refactor the copyToClipboard function.

* Make the onCopyUrl function awaitable.

* Fix the test.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-08-07 13:50:50 +08:00

51 lines
1.1 KiB
TypeScript

import { Check, Clipboard } from 'lucide-react'
import { useEffect, useState } from 'react'
import { copyToClipboard } from 'lib/helpers'
import { Button, ButtonProps } from 'ui'
export interface CopyButtonProps extends ButtonProps {
text: string
iconOnly?: boolean
copyLabel?: string
copiedLabel?: string
}
const CopyButton = ({
text,
iconOnly = false,
children,
onClick,
copyLabel = 'Copy',
copiedLabel = 'Copied',
...props
}: CopyButtonProps) => {
const [showCopied, setShowCopied] = useState(false)
useEffect(() => {
if (!showCopied) return
const timer = setTimeout(() => setShowCopied(false), 2000)
return () => clearTimeout(timer)
}, [showCopied])
return (
<Button
onClick={async (e) => {
setShowCopied(true)
await copyToClipboard(text)
onClick?.(e)
}}
icon={
showCopied ? (
<Check size={14} strokeWidth={2} className="text-brand" />
) : (
<Clipboard size={14} />
)
}
{...props}
>
{!iconOnly && <>{children ?? (showCopied ? copiedLabel : copyLabel)}</>}
</Button>
)
}
export default CopyButton