Skip to content

Commit 3b80f4f

Browse files
authored
Implement copy support (#12)
* Implement copy support * Remove unused import
1 parent 690eb97 commit 3b80f4f

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -199,31 +199,37 @@ Example usage:
199199
import { getShareUrl, SocialPlatforms } from "@phntms/react-share";
200200
201201
const Share = () => (
202-
<a
203-
href={getShareUrl(SocialPlatforms.Facebook, {url: "https://phantom.land/" })}
204-
>
202+
<a href={getShareUrl(SocialPlatforms.Facebook, {url: "https://phantom.land/" })}>
205203
Share to Facebook
206204
</a>
207-
<a
208-
href={getShareUrl(SocialPlatforms.Linkedin, { url: "https://phantom.land/" })}
209-
>
205+
<a href={getShareUrl(SocialPlatforms.Linkedin, { url: "https://phantom.land/" })}>
210206
Share to Linkedin
211207
</a>
212-
<a
213-
href={getShareUrl(SocialPlatforms.Twitter, { url: "https://phantom.land/" })}
214-
>
208+
<a href={getShareUrl(SocialPlatforms.Twitter, { url: "https://phantom.land/" })}>
215209
Share to Twitter
216210
</a>
217-
<a
218-
href={getShareUrl(SocialPlatforms.WhatsApp, { url: "https://phantom.land/" })}
219-
>
211+
<a href={getShareUrl(SocialPlatforms.WhatsApp, { url: "https://phantom.land/" })}>
220212
Share to WhatsApp
221213
</a>
222214
);
223215
224216
export default Share;
225217
```
226218

219+
### getCurrentUrlAndCopyToClipboard()
220+
221+
Method used to copy current window URL and copy it into your clipboard.
222+
223+
```jsx
224+
import { getCurrentUrlAndCopyToClipboard } from "@phntms/react-share";
225+
226+
const Copy = () => (
227+
<div onClick={() => getCurrentUrlAndCopyToClipboard()}>Copy</div>
228+
);
229+
230+
export default Copy;
231+
```
232+
227233
## Further Resources
228234

229235
Useful resources for testing meta properties:

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ export {
2626
AllSocialPlatformProps,
2727
} from "./utils/getShareUrl";
2828

29+
export { default as getCurrentUrlAndCopyToClipboard } from "./utils/getCurrentUrlAndCopyToClipboard";
30+
2931
export { SocialPlatforms } from "./types";
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const fallbackCopyToClipboard = (url: string) => {
2+
const placeholder = document.createElement("textarea");
3+
placeholder.value = url;
4+
5+
// Avoid scrolling to bottom
6+
placeholder.style.top = "0";
7+
placeholder.style.left = "0";
8+
placeholder.style.position = "fixed";
9+
10+
// Append element, and focus
11+
document.body.appendChild(placeholder);
12+
placeholder.focus();
13+
placeholder.select();
14+
15+
// Finally, remove element after copy
16+
document.body.removeChild(placeholder);
17+
};
18+
19+
export const getCurrentUrlAndCopyToClipboard = () => {
20+
const url = window.location.href;
21+
if (!navigator.clipboard) fallbackCopyToClipboard(url);
22+
else navigator.clipboard.writeText(url);
23+
return url;
24+
};
25+
26+
export default getCurrentUrlAndCopyToClipboard;

0 commit comments

Comments
 (0)