-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add copy button for runtime error (#65921)
### What Add copy stack trace button to copy the original stacktrace in error overlay. It will be a dimmed red button with disallowed cursor when it's found not able to copy. #### Video https://github.com/vercel/next.js/assets/4800338/c44e0bf8-f340-41ba-8ee8-4b94fdc298e1 ### Why Makes error reporting easier, users can do one-click to copy the original text
- Loading branch information
Showing
3 changed files
with
113 additions
and
7 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...es/next/src/client/components/react-dev-overlay/internal/components/copy-button/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { useState } from 'react' | ||
|
||
enum CopyState { | ||
Initial = 0, | ||
Copied = 1, | ||
Error = 2, | ||
} | ||
|
||
export function CopyButton({ | ||
label, | ||
successLabel, | ||
content, | ||
...props | ||
}: React.HTMLProps<HTMLSpanElement> & { | ||
label: string | ||
successLabel: string | ||
content: string | ||
}) { | ||
const [copied, setCopied] = useState(CopyState.Initial) | ||
const isDisabled = copied === CopyState.Error | ||
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) { | ||
setCopied(CopyState.Error) | ||
return | ||
} | ||
navigator.clipboard.writeText(content).then(() => { | ||
if (copied) return | ||
setCopied(CopyState.Copied) | ||
setTimeout(() => setCopied(CopyState.Initial), 2000) | ||
}) | ||
}} | ||
> | ||
{copied ? <CopySuccessIcon /> : <CopyIcon />} | ||
</span> | ||
) | ||
} | ||
|
||
function CopyIcon() { | ||
return ( | ||
<svg | ||
width="16" | ||
height="16" | ||
viewBox="0 0 24 24" | ||
fill="transparent" | ||
stroke="currentColor" | ||
strokeWidth="1.5" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
> | ||
<rect width="14" height="14" x="8" y="8" rx="2" ry="2" /> | ||
<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" /> | ||
</svg> | ||
) | ||
} | ||
|
||
function CopySuccessIcon() { | ||
return ( | ||
<svg | ||
height="16" | ||
xlinkTitle="copied" | ||
viewBox="0 0 16 16" | ||
width="16" | ||
stroke="currentColor" | ||
fill="currentColor" | ||
data-nextjs-data-runtime-error-copy-stack-success | ||
> | ||
<path d="M13.78 4.22a.75.75 0 0 1 0 1.06l-7.25 7.25a.75.75 0 0 1-1.06 0L2.22 9.28a.751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018L6 10.94l6.72-6.72a.75.75 0 0 1 1.06 0Z" /> | ||
</svg> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters