Skip to content

Commit

Permalink
add failed state
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed May 29, 2024
1 parent 987bafd commit 38e9b5d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@ export function CopyButton({
successLabel: string
content: string
}) {
const [copied, setCopied] = useState(false)
const title = copied ? successLabel : label
// copied: 0 = not copied, 1 = copied, 2 = error
const [copied, setCopied] = useState(0)
const isDisabled = copied === 2
const title = isDisabled ? '' : copied ? successLabel : label
return (
<span
{...props}
title={title}
aria-label={title}
aria-disabled={isDisabled}
role="button"
onClick={() => {
if (isDisabled) return
if (!navigator.clipboard) {
window.console.error(
'Next.js dev overlay: Clipboard API not available'
)
setCopied(2)
return
}
navigator.clipboard.writeText(content).then(() => {
if (copied) return
setCopied(true)
setTimeout(() => setCopied(false), 2000)
setCopied(1)
setTimeout(() => setCopied(0), 2000)
})
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ export const styles = css`
[data-nextjs-data-runtime-error-copy-stack] > svg {
vertical-align: middle;
}
[data-nextjs-data-runtime-error-copy-stack][aria-disabled],
[data-nextjs-data-runtime-error-copy-stack][aria-disabled]:hover {
cursor: pointer;
color: var(--color-ansi-red);
opacity: 0.3;
cursor: not-allowed;
}
[data-nextjs-data-runtime-error-copy-stack-success] {
color: var(--color-ansi-green);
}
Expand Down Expand Up @@ -165,7 +172,6 @@ export const styles = css`
height: var(--size-font-small);
margin-left: var(--size-gap);
flex-shrink: 0;
display: none;
}
Expand Down

0 comments on commit 38e9b5d

Please sign in to comment.