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