import * as React from 'react' import { cx } from '../../utils/cx' function useCopyLegacy(content: string) { type CopyState = | { state: 'initial' } | { state: 'error' error: unknown } | { state: 'success' } | { state: 'pending' } // This would be simpler with useActionState but we need to support React 18 here. // React 18 also doesn't have async transitions. const [copyState, dispatch] = React.useReducer( ( state: CopyState, action: | { type: 'reset' | 'copied' | 'copying' } | { type: 'error'; error: unknown } ): CopyState => { if (action.type === 'reset') { return { state: 'initial' } } if (action.type === 'copied') { return { state: 'success' } } if (action.type === 'copying') { return { state: 'pending' } } if (action.type === 'error') { return { state: 'error', error: action.error } } return state }, { state: 'initial', } ) function copy() { if (isPending) { return } if (!navigator.clipboard) { dispatch({ type: 'error', error: 'Copy to clipboard is not supported in this browser', }) } else { dispatch({ type: 'copying' }) navigator.clipboard.writeText(content).then( () => { dispatch({ type: 'copied' }) }, (error) => { dispatch({ type: 'error', error }) } ) } } const reset = React.useCallback(() => { dispatch({ type: 'reset' }) }, []) const isPending = copyState.state === 'pending' return [copyState, copy, reset, isPending] as const } function useCopyModern(content: string) { type CopyState = | { state: 'initial' } | { state: 'error' error: unknown } | { state: 'success' } const [copyState, dispatch, isPending] = React.useActionState( ( state: CopyState, action: 'reset' | 'copy' ): CopyState | Promise => { if (action === 'reset') { return { state: 'initial' } } if (action === 'copy') { if (!navigator.clipboard) { return { state: 'error', error: 'Copy to clipboard is not supported in this browser', } } return navigator.clipboard.writeText(content).then( () => { return { state: 'success' } }, (error) => { return { state: 'error', error } } ) } return state }, { state: 'initial', } ) function copy() { React.startTransition(() => { dispatch('copy') }) } const reset = React.useCallback(() => { dispatch('reset') }, [ // TODO: `dispatch` from `useActionState` is not reactive. // Remove from dependencies once https://github.com/facebook/react/pull/29665 is released. dispatch, ]) return [copyState, copy, reset, isPending] as const } const useCopy = typeof React.useActionState === 'function' ? useCopyModern : useCopyLegacy type CopyButtonProps = React.HTMLProps & { actionLabel: string successLabel: string icon?: React.ReactNode } export function CopyButton( props: CopyButtonProps & { content?: string; getContent?: () => string } ) { const { content, getContent, actionLabel, successLabel, icon, disabled, ...rest } = props const getContentString = (): string => { if (content) { return content } if (getContent) { return getContent() } return '' } const contentString = getContentString() const [copyState, copy, reset, isPending] = useCopy(contentString) const error = copyState.state === 'error' ? copyState.error : null React.useEffect(() => { if (error !== null) { // 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(() => { if (copyState.state === 'success') { const timeoutId = setTimeout(() => { reset() }, 2000) return () => { clearTimeout(timeoutId) } } }, [isPending, copyState.state, reset]) const isDisabled = !navigator.clipboard || isPending || disabled || !!error const label = copyState.state === 'success' ? successLabel : actionLabel // Assign default icon const renderedIcon = copyState.state === 'success' ? ( ) : ( icon || ( ) ) return ( ) } function CopyIcon(props: React.SVGProps) { return ( ) } function CopySuccessIcon() { return ( ) } export const COPY_BUTTON_STYLES = ` .nextjs-data-copy-button { color: inherit; svg { width: var(--size-16); height: var(--size-16); } } .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:not(:disabled), .nextjs-data-copy-button--error:hover:not(:disabled) { color: var(--color-ansi-red); } .nextjs-data-copy-button--success:not(:disabled) { color: var(--color-ansi-green); } `