This is my copy to clipboard code, I am using textarea but when i am pasting what i copied on code, everything is just on one line:
const CopyButton = ({ text }: { text: string }) => {
const init_val = 'Copy';
const [btn_val, set_value] = useState(init_val);
const copyToClipboard = () => {
const textField = document.createElement('textarea');
textField.innerText = text;
document.body.appendChild(textField);
textField.select();
document.execCommand('copy');
textField.remove();
set_value('Copied!');
setTimeout(() => {
set_value(init_val);
}, 1500);
};
return (
<button className='copy-to-clipboard' onClick={copyToClipboard}>
{btn_val}
</button>
);
};
I am thinking of solving this with regexp but don't know how and can't seem to find a solution around.