mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 10:55:06 +03:00
* 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>
51 lines
1.1 KiB
TypeScript
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
|