umami/components/common/CopyButton.js

24 lines
523 B
JavaScript
Raw Normal View History

2020-08-08 05:36:57 +02:00
import React, { useState } from 'react';
import Button from './Button';
const defaultText = 'Copy to clipboard';
export default function CopyButton({ element, ...props }) {
const [text, setText] = useState(defaultText);
function handleClick() {
if (element?.current) {
element.current.select();
document.execCommand('copy');
setText('Copied!');
window.getSelection().removeAllRanges();
}
}
return (
<Button {...props} onClick={handleClick}>
{text}
</Button>
);
}