Skip to content

Use navigator.clipboard API when available #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

architjain
Copy link

No description provided.

Copy link

vercel bot commented Sep 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-json-view ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 2, 2024 7:57am

Comment on lines +29 to +40
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);

}

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);

@SimonSchick
Copy link

I assume this would fix mac-s-g/react-json-view#131 ?

@mbtools
Copy link
Contributor

mbtools commented Sep 24, 2024

I believe so, @SimonSchick

@Kikobeats
Copy link
Member

Happy to merge this if someone update the PR with the suggested code 🙏

@mbtools
Copy link
Contributor

mbtools commented Sep 25, 2024

I would but am not authorized to change the PR. @architjain

@Kikobeats
Copy link
Member

@mbtools just a new PR 🙂

mbtools added a commit to mbtools/react-json-view that referenced this pull request Sep 26, 2024
Fixes issues with `document.execCommand('copy')` which is deprecated.

Uses `navigator.clipboard` instead which has [browser compatibility](https://caniuse.com/mdn-api_clipboard_writetext) of +93%.

Replaces microlinkhq#37
@Kikobeats
Copy link
Member

superseed by #39

@Kikobeats Kikobeats closed this Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants