mirror of
https://github.com/supabase/supabase.git
synced 2026-10-07 18:35:07 +03:00
* SQL preview on hover snippet * Fix class name * Fix some styling * Fix some styling * Remove console log * Fix
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import { Button, ButtonProps, IconCheck, IconClipboard } from 'ui'
|
|
import { copyToClipboard } from 'lib/helpers'
|
|
import { useEffect, useState } from 'react'
|
|
|
|
export interface CopyButtonProps extends ButtonProps {
|
|
text: string
|
|
iconOnly?: boolean
|
|
}
|
|
const CopyButton = ({ text, iconOnly = false, children, onClick, ...props }: CopyButtonProps) => {
|
|
const [showCopied, setShowCopied] = useState(false)
|
|
|
|
useEffect(() => {
|
|
if (!showCopied) return
|
|
const timer = setTimeout(() => setShowCopied(false), 2000)
|
|
return () => clearTimeout(timer)
|
|
}, [showCopied])
|
|
|
|
return (
|
|
<Button
|
|
onClick={(e) => {
|
|
setShowCopied(true)
|
|
copyToClipboard(text)
|
|
onClick?.(e)
|
|
}}
|
|
icon={
|
|
showCopied ? (
|
|
<IconCheck size="tiny" strokeWidth={2} className="text-brand" />
|
|
) : (
|
|
<IconClipboard size="tiny" />
|
|
)
|
|
}
|
|
{...props}
|
|
>
|
|
{!iconOnly && <>{children ?? (showCopied ? 'Copied' : 'Copy')}</>}
|
|
</Button>
|
|
)
|
|
}
|
|
export default CopyButton
|