Skip to content
This repository was archived by the owner on Mar 16, 2021. It is now read-only.

fix: Copy address to clipboard on mobile devices - #220

Closed
memoyil wants to merge 2 commits into
pancakeswap:masterfrom
memoyil:fix/copy_clipboard_on+mobile_devices
Closed

fix: Copy address to clipboard on mobile devices#220
memoyil wants to merge 2 commits into
pancakeswap:masterfrom
memoyil:fix/copy_clipboard_on+mobile_devices

Conversation

@memoyil

@memoyil memoyil commented Mar 12, 2021

Copy link
Copy Markdown

No description provided.

@ArtemKolichenkov ArtemKolichenkov left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all - if somebody reads this and wonders why the hell we need textarea to copy address - there are couple of interesting points about WebView and specifically Trust Wallet Browser mentioned in the original issue + issue in Trust Wallet repo.

Overall quite OK, you can ignore comments about html tags, it's not that important. The comment about not prioritizing queryCommandSupported would be great to fix though.

const copyToClipboard = (content: string) => {
const el = document.createElement("textarea");
el.value = content;
document.body.appendChild(el);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was going to comment that we should specify el.style.display = "none" or el.style.visibility = "hidden" just to be safe. But - apparently el.select() doesn't work if the element is not actually visible on the screen.

Thankfully this happens quick enough so it is completely not noticeable for user.

const [isTooltipDisplayed, setIsTooltipDisplayed] = useState(false);

const copyToClipboard = (content: string) => {
const el = document.createElement("textarea");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nitpick - but we could perhaps use input instead of textarea. Even though in 99% of the cases it is too quick to notice anything - in case where user has super slow computer they might notice something in theory and input just takes less space on the screen.

Comment on lines +54 to +59
if (document.queryCommandSupported("copy")) {
copyToClipboard(toCopy);
displayTooltip();
} else if (navigator.clipboard) {
navigator.clipboard.writeText(toCopy);
setIsTooltipDisplayed(true);
setTimeout(() => {
setIsTooltipDisplayed(false);
}, 1000);
displayTooltip();

@ArtemKolichenkov ArtemKolichenkov Mar 12, 2021

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This should be the fallback only for WebViews, iFrames and weird browsers, not the primary way to copy the address, not to mention it is (getting) deprecated so it's a bit icky to prioritize it.
    navigator.clipboard check should be first. And since technically Trust Wallet browser has navigator.clipboard defined, it fails because its a WebView where navigator.permissions is undefined (thus not having permission to copy), so we should check for that too.
  2. I know that it "works" as it is right now, but technically navigator.clipboard.writeText(toCopy) returns a promise, so we'd better handle it appropriately. Otherwise uses would see "Copied!" tooltip even if it didn't actually copied anything. Would be also nice to somehow handle error in .catch() but I guess (a) unless user uses extremely excotic browser it shouldn't happen (b) showing user "sorry we couldn't copy the address, browser is poop" is questionable 馃槃

EDIT: re-posted the comment to address multiple lines

Suggested change
if (document.queryCommandSupported("copy")) {
copyToClipboard(toCopy);
displayTooltip();
} else if (navigator.clipboard) {
navigator.clipboard.writeText(toCopy);
setIsTooltipDisplayed(true);
setTimeout(() => {
setIsTooltipDisplayed(false);
}, 1000);
displayTooltip();
if (navigator.clipboard && navigator.permissions) {
navigator.clipboard.writeText(toCopy).then(() => displayTooltip());
} else if (document.queryCommandSupported("copy")) {
copyToClipboard(toCopy);
displayTooltip();
}

@memoyil

memoyil commented Mar 12, 2021

Copy link
Copy Markdown
Author

Migrated to #221

@memoyil memoyil closed this Mar 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants