Skip to content
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

Use navigator.clipboard.writeText when available #87

Open
silverwind opened this issue Nov 20, 2019 · 5 comments
Open

Use navigator.clipboard.writeText when available #87

silverwind opened this issue Nov 20, 2019 · 5 comments

Comments

@silverwind
Copy link

silverwind commented Nov 20, 2019

This is a new promise-based API available available in some browsers which does not require DOM interaction to copy a string:

https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText
https://jsfiddle.net/p73yv6re/

@mlsad3
Copy link

mlsad3 commented Aug 21, 2020

I think a downside of this is it looks like you can only copy text, and can't specify the format. Am I incorrect here?

@silverwind
Copy link
Author

writeText is only for text. For other formats, there is write.

@martinbroos
Copy link

martinbroos commented Nov 13, 2020

To use the new api but still support older browsers i'm using this implementation. But might be good to add this to copy-to-clipboard.

async onClick() {
        try {
            await navigator.clipboard.writeText(shareUrl);
        } catch {
            // Otherwise we use copy-to-clipboard library that tries to use execCommand which is deprecated in modern browsers
            // when browser does not allow clipboard copy it will show a modal with the link in it
            copy(shareUrl);
        }
}

@silverwind
Copy link
Author

silverwind commented May 25, 2021

I guess with this API available, this module is pretty much obsolete, so one option could be to not support it, letting this module exist only as a "legacy solution".

@Reverier-Xu
Copy link

I guess with this API available, this module is pretty much obsolete, so one option could be to not support it, letting this module exist only as a "legacy solution".

the new clipboard API just works on HTTPS or localhost. In some usage scenarios, such as intranet environments without domain name resolution and custom certificates, the new API is not available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants