-
Notifications
You must be signed in to change notification settings - Fork 58
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
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); | ||
|
||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
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);
I assume this would fix mac-s-g/react-json-view#131 ? |
I believe so, @SimonSchick |
Happy to merge this if someone update the PR with the suggested code 🙏 |
I would but am not authorized to change the PR. @architjain |
@mbtools just a new PR 🙂 |
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
superseed by #39 |
No description provided.