Skip to content

[dev-overlay] disable copy button when clipboard is not available #78101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function useCopyLegacy(content: string) {
if (!navigator.clipboard) {
dispatch({
type: 'error',
error: new Error('Copy to clipboard is not supported in this browser'),
error: 'Copy to clipboard is not supported in this browser',
})
} else {
dispatch({ type: 'copying' })
Expand Down Expand Up @@ -94,9 +94,7 @@ function useCopyModern(content: string) {
if (!navigator.clipboard) {
return {
state: 'error',
error: new Error(
'Copy to clipboard is not supported in this browser'
),
error: 'Copy to clipboard is not supported in this browser',
}
}
return navigator.clipboard.writeText(content).then(
Expand Down Expand Up @@ -153,8 +151,9 @@ export function CopyButton({
const error = copyState.state === 'error' ? copyState.error : null
React.useEffect(() => {
if (error !== null) {
// Additional console.error to get the stack.
console.error(error)
// Only log warning in terminal to avoid showing in the error overlay.
// When it's errored, the copy button will be disabled.
console.warn(error)
}
}, [error])
React.useEffect(() => {
Expand All @@ -168,7 +167,7 @@ export function CopyButton({
}
}
}, [isPending, copyState.state, reset])
const isDisabled = isPending || disabled
const isDisabled = !navigator.clipboard || isPending || disabled || !!error
const label = copyState.state === 'success' ? successLabel : actionLabel

// Assign default icon
Expand Down Expand Up @@ -255,14 +254,18 @@ export const COPY_BUTTON_STYLES = `
height: var(--size-16);
}
}
.nextjs-data-copy-button--initial:hover {
.nextjs-data-copy-button:disabled {
background-color: var(--color-gray-100);
cursor: not-allowed;
}
.nextjs-data-copy-button--initial:hover:not(:disabled) {
cursor: pointer;
}
.nextjs-data-copy-button--error,
.nextjs-data-copy-button--error:hover {
.nextjs-data-copy-button--error:not(:disabled),
.nextjs-data-copy-button--error:hover:not(:disabled) {
color: var(--color-ansi-red);
}
.nextjs-data-copy-button--success {
.nextjs-data-copy-button--success:not(:disabled) {
color: var(--color-ansi-green);
}
`
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,6 @@ export const styles = `
.nextjs-toast-hide-button:hover {
opacity: 1;
}
.nextjs__container_errors_inspect_copy_button {
cursor: pointer;
background: none;
border: none;
color: var(--color-ansi-bright-white);
font-size: var(--size-24);
padding: 0;
margin: 0;
margin-left: 8px;
transition: opacity 0.25s ease;
}
.nextjs__container_errors__error_title {
display: flex;
align-items: center;
Expand Down
Loading