Skip to content

Commit

Permalink
use enum
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed May 29, 2024
1 parent 38e9b5d commit 790855d
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { useState } from 'react'

enum CopyState {
Initial = 0,
Copied = 1,
Error = 2,
}

export function CopyButton({
label,
successLabel,
Expand All @@ -10,9 +16,8 @@ export function CopyButton({
successLabel: string
content: string
}) {
// copied: 0 = not copied, 1 = copied, 2 = error
const [copied, setCopied] = useState(0)
const isDisabled = copied === 2
const [copied, setCopied] = useState(CopyState.Initial)
const isDisabled = copied === CopyState.Error
const title = isDisabled ? '' : copied ? successLabel : label
return (
<span
Expand All @@ -24,13 +29,13 @@ export function CopyButton({
onClick={() => {
if (isDisabled) return
if (!navigator.clipboard) {
setCopied(2)
setCopied(CopyState.Error)
return
}
navigator.clipboard.writeText(content).then(() => {
if (copied) return
setCopied(1)
setTimeout(() => setCopied(0), 2000)
setCopied(CopyState.Copied)
setTimeout(() => setCopied(CopyState.Initial), 2000)
})
}}
>
Expand Down

0 comments on commit 790855d

Please sign in to comment.