Skip to content

Commit

Permalink
add copied state to ClickToCopy component
Browse files Browse the repository at this point in the history
  • Loading branch information
nuotsu committed Feb 3, 2025
1 parent 684e9b3 commit 4174d73
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sanitypress",
"version": "6.0.4",
"version": "6.0.5",
"description": "An impressive Next.js + Sanity.io starter template",
"author": "nuotsu <[email protected]> (https://nuotsu.dev)",
"license": "ISC",
Expand Down
13 changes: 9 additions & 4 deletions src/ui/ClickToCopy.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
'use client'

import { VscCopy } from 'react-icons/vsc'
import { VscCheck, VscCopy } from 'react-icons/vsc'
import { useState, type ComponentProps } from 'react'
import { cn } from '@/lib/utils'

export default function ClickToCopy({
value,
children,
className,
...props
}: {
value?: string
} & React.ComponentProps<'button'>) {
const [copied, setCopied] = useState(false)
return (
<button
className={cn('cursor-copy', className)}
className={cn('cursor-copy', copied && 'pointer-events-none', className)}
onClick={() => {
if (typeof window === 'undefined' || !value) return

navigator.clipboard.writeText(value)

setCopied(true)
setTimeout(() => setCopied(false), 1000)
}}
title="Click to copy"
{...props}
>
{children || <VscCopy />}
{copied ? <VscCheck /> : <VscCopy />}
</button>
)
}

0 comments on commit 4174d73

Please sign in to comment.