Skip to content

Commit

Permalink
Implement copy support (#12)
Browse files Browse the repository at this point in the history
* Implement copy support

* Remove unused import
  • Loading branch information
PauloMFJ authored Aug 19, 2021
1 parent 690eb97 commit 3b80f4f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,31 +199,37 @@ Example usage:
import { getShareUrl, SocialPlatforms } from "@phntms/react-share";
const Share = () => (
<a
href={getShareUrl(SocialPlatforms.Facebook, {url: "https://phantom.land/" })}
>
<a href={getShareUrl(SocialPlatforms.Facebook, {url: "https://phantom.land/" })}>
Share to Facebook
</a>
<a
href={getShareUrl(SocialPlatforms.Linkedin, { url: "https://phantom.land/" })}
>
<a href={getShareUrl(SocialPlatforms.Linkedin, { url: "https://phantom.land/" })}>
Share to Linkedin
</a>
<a
href={getShareUrl(SocialPlatforms.Twitter, { url: "https://phantom.land/" })}
>
<a href={getShareUrl(SocialPlatforms.Twitter, { url: "https://phantom.land/" })}>
Share to Twitter
</a>
<a
href={getShareUrl(SocialPlatforms.WhatsApp, { url: "https://phantom.land/" })}
>
<a href={getShareUrl(SocialPlatforms.WhatsApp, { url: "https://phantom.land/" })}>
Share to WhatsApp
</a>
);
export default Share;
```

### getCurrentUrlAndCopyToClipboard()

Method used to copy current window URL and copy it into your clipboard.

```jsx
import { getCurrentUrlAndCopyToClipboard } from "@phntms/react-share";
const Copy = () => (
<div onClick={() => getCurrentUrlAndCopyToClipboard()}>Copy</div>
);
export default Copy;
```

## Further Resources

Useful resources for testing meta properties:
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ export {
AllSocialPlatformProps,
} from "./utils/getShareUrl";

export { default as getCurrentUrlAndCopyToClipboard } from "./utils/getCurrentUrlAndCopyToClipboard";

export { SocialPlatforms } from "./types";
26 changes: 26 additions & 0 deletions src/utils/getCurrentUrlAndCopyToClipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const fallbackCopyToClipboard = (url: string) => {
const placeholder = document.createElement("textarea");
placeholder.value = url;

// Avoid scrolling to bottom
placeholder.style.top = "0";
placeholder.style.left = "0";
placeholder.style.position = "fixed";

// Append element, and focus
document.body.appendChild(placeholder);
placeholder.focus();
placeholder.select();

// Finally, remove element after copy
document.body.removeChild(placeholder);
};

export const getCurrentUrlAndCopyToClipboard = () => {
const url = window.location.href;
if (!navigator.clipboard) fallbackCopyToClipboard(url);
else navigator.clipboard.writeText(url);
return url;
};

export default getCurrentUrlAndCopyToClipboard;

0 comments on commit 3b80f4f

Please sign in to comment.