Files
supabase/apps/studio/components/ui/CopyButton.tsx
T
Ivan Vasilov 436bdb10ae chore: Move the studio app to apps/studio (#18915)
* Move all studio files from /studio to /apps/studio.

* Move studio specific prettier ignores.

* Fix the ui references from studio.

* Fix the css imports.

* Fix all package.json issues.

* Fix the prettier setup for the studio app.

* Add .turbo folder to prettierignore.

* Fix the github workflows.
2023-11-15 12:38:55 +01:00

48 lines
1.1 KiB
TypeScript

import { Button, ButtonProps } from 'ui'
import { copyToClipboard } from 'lib/helpers'
import { IconClipboard } from 'ui'
import { useEffect, useState } from 'react'
export interface CopyButtonProps extends ButtonProps {
text: string
// used for text-less feedback
bounceIconOnCopy?: boolean
}
const CopyButton: React.FC<CopyButtonProps> = ({
text,
children,
onClick,
bounceIconOnCopy,
...props
}) => {
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={
<IconClipboard
size="tiny"
className={[showCopied && bounceIconOnCopy ? 'animate-bounce' : '', 'transition'].join(
' '
)}
/>
}
{...props}
>
{children ?? (showCopied ? 'Copied' : 'Copy')}
</Button>
)
}
export default CopyButton