Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/js/components/CopyToClipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,32 @@ export default class extends React.PureComponent {
}
}

handleCopy = () => {
const container = document.createElement('textarea');
copyText = async (textToCopy) => {
if (navigator.clipboard) {
return await navigator.clipboard.writeText(textToCopy);
} else {
const container = document.createElement('textarea');
container.innerHTML = textToCopy;
document.body.appendChild(container);
container.select();
document.execCommand('copy');
document.body.removeChild(container);

}

Comment on lines +29 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (navigator.clipboard) {
return await navigator.clipboard.writeText(textToCopy);
} else {
const container = document.createElement('textarea');
container.innerHTML = textToCopy;
document.body.appendChild(container);
container.select();
document.execCommand('copy');
document.body.removeChild(container);
}
return await navigator.clipboard.writeText(textToCopy);

I think it's ok to assume navigator.clipboard.writeText is there

compat is +90% https://caniuse.com/mdn-api_clipboard_writetext

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, but then you can just replace await copyText with await navigator.clipboard.writeText(textToCopy);

}

handleCopy = async () => {
const { clickCallback, src, namespace } = this.props;

container.innerHTML = JSON.stringify(
const textToCopy = JSON.stringify(
this.clipboardValue(src),
null,
' '
);

document.body.appendChild(container);
container.select();
document.execCommand('copy');
await copyText(textToCopy);

document.body.removeChild(container);

this.copiedTimer = setTimeout(() => {
this.setState({
Expand Down