Files

115 lines
3.5 KiB
TypeScript
Raw Permalink Normal View History

import React, { useEffect, useState } from 'react';
import Portal from '@/components/elements/Portal';
2026-10-05 18:10:18 -04:00
import Button from '@/components/elements/Button';
import { TextArea, TextInput } from '@/components/form/controls';
import copyToClipboard from '@/lib/clipboard';
import { cn } from '@/lib/cn';
2026-10-12 09:04:03 -04:00
import { useSkin } from '@/themes/skins';
2020-10-31 16:16:41 -04:00
2026-10-12 09:04:03 -04:00
export interface CopyOnClickProps {
2022-07-02 18:53:03 -04:00
text: string | number | null | undefined;
showInNotification?: boolean;
children: React.ReactNode;
}
2026-10-05 18:10:18 -04:00
interface ClickableChildProps {
className?: string;
role?: React.AriaRole;
tabIndex?: number;
onClick?: React.MouseEventHandler<HTMLElement>;
onKeyDown?: React.KeyboardEventHandler<HTMLElement>;
}
type CopyStatus = 'copied' | 'failed';
const interactiveTypes = new Set<React.ReactElement['type']>([
'a',
'button',
'input',
'select',
'textarea',
Button,
Button.Text,
Button.Danger,
TextInput,
TextArea,
]);
2026-10-08 16:53:20 -04:00
const copyStatusMessage = (status: CopyStatus, text: CopyOnClickProps['text'], showInNotification: boolean): string => {
if (status === 'failed') {
return 'Unable to copy text to clipboard.';
}
return showInNotification ? `Copied "${String(text)}" to clipboard.` : 'Copied text to clipboard.';
};
2026-10-05 18:10:18 -04:00
/** Copies `text` when its single child is clicked; a non-interactive child becomes a focusable button. */
const CopyOnClick = ({ text, showInNotification = false, children }: CopyOnClickProps) => {
const [status, setStatus] = useState<CopyStatus | null>(null);
2026-10-12 09:04:03 -04:00
const ui = useSkin('CopyOnClick', { status: status ?? undefined });
2020-10-31 16:16:41 -04:00
useEffect(() => {
2026-10-08 16:53:20 -04:00
if (!status) {
return;
}
2020-10-31 16:16:41 -04:00
const timeout = setTimeout(() => {
2026-10-05 18:10:18 -04:00
setStatus(null);
2020-10-31 16:16:41 -04:00
}, 2500);
return () => {
clearTimeout(timeout);
};
2026-10-05 18:10:18 -04:00
}, [status]);
2020-10-31 16:16:41 -04:00
2026-10-05 18:10:18 -04:00
if (!React.isValidElement<ClickableChildProps>(children)) {
throw new Error('Component passed to <CopyOnClick/> must be a valid React element.');
}
2026-10-05 18:10:18 -04:00
const element = React.Children.only(children);
const copy = () => {
void copyToClipboard(String(text)).then((copied) => setStatus(copied ? 'copied' : 'failed'));
};
const interactive = interactiveTypes.has(element.type);
const onKeyDown = (e: React.KeyboardEvent<HTMLElement>) => {
element.props.onKeyDown?.(e);
if (!e.defaultPrevented && e.target === e.currentTarget && (e.key === 'Enter' || e.key === ' ')) {
e.preventDefault();
copy();
}
};
2026-10-08 16:53:20 -04:00
const child = text
? React.cloneElement(element, {
2026-10-05 18:10:18 -04:00
role: interactive ? element.props.role : (element.props.role ?? 'button'),
tabIndex: interactive ? element.props.tabIndex : (element.props.tabIndex ?? 0),
onKeyDown: interactive ? element.props.onKeyDown : onKeyDown,
2026-10-12 09:04:03 -04:00
className: cn(element.props.className, ui.classes('target')),
onClick: (e: React.MouseEvent<HTMLElement>) => {
2026-10-05 18:10:18 -04:00
copy();
element.props.onClick?.(e);
},
2026-10-08 16:53:20 -04:00
})
: element;
2020-10-31 16:16:41 -04:00
return (
<>
2026-10-05 18:10:18 -04:00
{status && (
<Portal>
2026-10-12 09:04:03 -04:00
<div {...ui('root')}>
<div role='status' {...ui('status')}>
<p {...ui('message')}>{copyStatusMessage(status, text, showInNotification)}</p>
</div>
2026-10-05 18:10:18 -04:00
</div>
</Portal>
)}
{child}
2020-10-31 16:16:41 -04:00
</>
);
};
export default CopyOnClick;