woocommerce-plugin
woocommerce-plugin copied to clipboard
Mobile view address copy issue
Merchant reported that when in mobile view, clicking copy the address gets two blank spaces appended to the end of the clipboard causing issues pasting to wallets.
@DarrenWestwood
We can solve the issue by:
- Either replacing the
document.execCommand('copy')
(which is now depreciated) with the new Clipboard API of JS (94% Global Users support as per caniuse), and use that instead, as it doesn't rely on creating a temporary element to copy text. - Setting the selectionRange
selector.setSelectionRange(0, 99999);
, as suggested at various sources for Mobile.
The suggested fix is that we can use the new Clipboard API along with a fallback to the older document.execCommand to support all cases.
Please ensure you are able to replicate the issue in order to verify using Clipboard API correctly solves the issue. It looks like the issue is with selection rather than copying. Using selectionEnd or trim() appears to be recommended fix for this: https://stackoverflow.com/questions/34477425/javascript-execcommandcopy-copies-text-but-adds-extra-white-space-to-value
The suggested fix is that we can use the new Clipboard API along with a fallback to the older document.execCommand to support all cases.
Lets use Clipboard API, without fallback as this appears supported by most modern browsers.
Alright, let's move to Clipboard API. P.S. I was not able to replicate the issue on my mobile device, I tried it with Edge Mobile Browser and Chrome Mobile Browser. Both of them seemed to copy the text without any spaces.