1
0
mirror of https://github.com/kremalicious/umami.git synced 2024-12-24 10:16:19 +01:00
umami/components/common/CopyButton.js
2020-09-05 17:27:01 -07:00

27 lines
698 B
JavaScript

import React, { useState } from 'react';
import Button from './Button';
import { FormattedMessage } from 'react-intl';
const defaultText = (
<FormattedMessage id="button.copy-to-clipboard" defaultMessage="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(<FormattedMessage id="message.copied" defaultMessage="Copied!" />);
window.getSelection().removeAllRanges();
}
}
return (
<Button {...props} onClick={handleClick}>
{text}
</Button>
);
}