mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-14 17:25:02 +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>
|
|
);
|
|
}
|