import { forwardRef, useLayoutEffect, useRef } from 'react'; import { twMerge } from 'tailwind-merge'; import useMergedRef from '@react-hook/merged-ref'; import { Button } from './Button'; import { Copy6 } from '../icons/Copy6'; import { useTemporaryState } from '../hooks/useTemporaryState'; import { CheckCircle } from '../icons/CheckCircle'; import { useTruncateMiddle } from '../hooks/useTruncateMiddle'; import type { ComponentPropsWithRef, ElementRef } from 'react'; type CopyButtonProps = ComponentPropsWithRef & { children: string; postCopyMessage?: string; }; export const CopyButton = forwardRef, CopyButtonProps>(function CopyButton( { size = 'sm', children, onClick, color = 'grey', roundness = 'lg', className, postCopyMessage = 'Copied', ...other }, forwardedRef, ) { const [clicked, setClicked] = useTemporaryState(false, 2000); const handleClick = (event: React.MouseEvent) => { if (onClick) { onClick(event); } navigator.clipboard.writeText(children); setClicked(true); }; const localButtonRef = useRef(null); const ref = useMergedRef(localButtonRef, forwardedRef); useLayoutEffect(() => { const resizeObserver = new ResizeObserver((entries: ResizeObserverEntry[]) => { const { inlineSize } = entries[0].borderBoxSize[0]; if (localButtonRef.current && !clicked) { localButtonRef.current.style.setProperty(`--copy-button-min-with`, `${inlineSize}px`); } }); if (localButtonRef.current) { resizeObserver.observe(localButtonRef.current, { box: 'border-box' }); } return () => { resizeObserver.disconnect(); }; }, [localButtonRef, clicked]); const text = useTruncateMiddle({ text: children, maxLength: 14 }); return ( ); });