woocommerce-plugin icon indicating copy to clipboard operation
woocommerce-plugin copied to clipboard

Mobile view address copy issue

Open jusasiiv opened this issue 1 year ago • 3 comments

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.

jusasiiv avatar Jul 03 '22 20:07 jusasiiv

@DarrenWestwood

We can solve the issue by:

  1. 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.
  2. 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.

thisisayush avatar Jul 12 '22 09:07 thisisayush

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.

DarrenWestwood avatar Jul 12 '22 10:07 DarrenWestwood

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.

thisisayush avatar Jul 12 '22 10:07 thisisayush