import {forwardRef, useImperativeHandle, useRef} from 'react'; import classNames from 'classnames'; import {selectText} from 'sentry/utils/selectText'; type Props = { children: React.ReactNode; className?: string; style?: React.CSSProperties; }; type AutoSelectHandle = { selectText: () => void; }; const AutoSelectText: React.ForwardRefRenderFunction = ( {children, className, ...props}, forwardedRef ) => { const element = useRef(null); // We need to expose a selectText method to parent components // and need an imperative ref handle. useImperativeHandle(forwardedRef, () => ({ selectText: () => handleClick(), })); function handleClick() { if (!element.current) { return; } selectText(element.current); } // use an inner span here for the selection as otherwise the selectText // function will create a range that includes the entire part of the // div (including the div itself) which causes newlines to be selected // in chrome. return (
{children}
); }; export default forwardRef(AutoSelectText);