mirror of
https://github.com/kremalicious/umami.git
synced 2024-12-24 10:16:19 +01:00
24 lines
523 B
JavaScript
24 lines
523 B
JavaScript
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>
|
|
);
|
|
}
|