2022-12-29 00:43:22 +01:00
|
|
|
import { useState } from 'react';
|
2021-02-16 12:33:37 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2022-12-27 01:57:59 +01:00
|
|
|
import { Button } from 'react-basics';
|
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 = (
|
2020-10-13 07:53:59 +02:00
|
|
|
<FormattedMessage id="label.copy-to-clipboard" defaultMessage="Copy to clipboard" />
|
2020-09-06 02:27:01 +02:00
|
|
|
);
|
2020-08-08 05:36:57 +02:00
|
|
|
|
2021-02-16 12:33:37 +01:00
|
|
|
function CopyButton({ element, ...props }) {
|
2020-08-08 05:36:57 +02:00
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
2021-02-16 12:33:37 +01:00
|
|
|
|
|
|
|
CopyButton.propTypes = {
|
|
|
|
element: PropTypes.shape({
|
|
|
|
current: PropTypes.shape({
|
|
|
|
select: PropTypes.func.isRequired,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CopyButton;
|