File tree Expand file tree Collapse file tree 3 files changed +46
-12
lines changed Expand file tree Collapse file tree 3 files changed +46
-12
lines changed Original file line number Diff line number Diff line change @@ -199,31 +199,37 @@ Example usage:
199
199
import { getShareUrl, SocialPlatforms } from "@phntms/react-share";
200
200
201
201
const Share = () => (
202
- <a
203
- href={getShareUrl(SocialPlatforms.Facebook, {url: "https://phantom.land/" })}
204
- >
202
+ <a href={getShareUrl(SocialPlatforms.Facebook, {url: "https://phantom.land/" })}>
205
203
Share to Facebook
206
204
</a>
207
- <a
208
- href={getShareUrl(SocialPlatforms.Linkedin, { url: "https://phantom.land/" })}
209
- >
205
+ <a href={getShareUrl(SocialPlatforms.Linkedin, { url: "https://phantom.land/" })}>
210
206
Share to Linkedin
211
207
</a>
212
- <a
213
- href={getShareUrl(SocialPlatforms.Twitter, { url: "https://phantom.land/" })}
214
- >
208
+ <a href={getShareUrl(SocialPlatforms.Twitter, { url: "https://phantom.land/" })}>
215
209
Share to Twitter
216
210
</a>
217
- <a
218
- href={getShareUrl(SocialPlatforms.WhatsApp, { url: "https://phantom.land/" })}
219
- >
211
+ <a href={getShareUrl(SocialPlatforms.WhatsApp, { url: "https://phantom.land/" })}>
220
212
Share to WhatsApp
221
213
</a>
222
214
);
223
215
224
216
export default Share;
225
217
` ` `
226
218
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
+
227
233
## Further Resources
228
234
229
235
Useful resources for testing meta properties:
Original file line number Diff line number Diff line change @@ -26,4 +26,6 @@ export {
26
26
AllSocialPlatformProps ,
27
27
} from "./utils/getShareUrl" ;
28
28
29
+ export { default as getCurrentUrlAndCopyToClipboard } from "./utils/getCurrentUrlAndCopyToClipboard" ;
30
+
29
31
export { SocialPlatforms } from "./types" ;
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments