Skip to content

Commit c38a2da

Browse files
jriallPauloMFJ
andauthored
Add getWhatsAppUrl function (#6)
* Add getWhatsAppUrl function * Run prettier on WhatsApp changes * Apply prettier fixes to WhatApp code Ran the correct npm command this time 😅 * Update README.md Co-authored-by: Paulo <[email protected]> Co-authored-by: Paulo <[email protected]>
1 parent 7a528dd commit c38a2da

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,29 @@ const ShareToTwitter = () => (
161161
export default ShareToTwitter;
162162
```
163163

164+
### getWhatsAppUrl()
165+
166+
| Parameter | Type | Required | Notes |
167+
| --------- | ------ | -------- | ---------------------------------------------------- |
168+
| url | string | **Yes** | URL of shared webpage. |
169+
| text | string | **No** | Text to show in the WhatsApp message before the URL. |
170+
171+
Basic component example usage:
172+
173+
```jsx
174+
import { getWhatsAppUrl } from "@phntms/react-share";
175+
176+
const ShareToWhatsApp = () => (
177+
<a href={getWhatsAppUrl({ url: "https://phantom.land/" })}>
178+
Share to WhatsApp
179+
</a>
180+
);
181+
182+
export default ShareToWhatsApp;
183+
```
184+
185+
**Note**: WhatsApp links will only work on mobile, so be sure to hide any WhatsApp links if the user is not on a mobile device!
186+
164187
### getShareUrl()
165188

166189
If you would rather have all share urls in one place, `getShareUrl()` can be used! It includes props from every social platform listed above, so simply pass in a `SocialPlatform`, and the platforms corresponding props.
@@ -186,6 +209,11 @@ const Share = () => (
186209
>
187210
Share to Twitter
188211
</a>
212+
<a
213+
href={getShareUrl(SocialPlatforms.WhatsApp, { url: "https://phantom.land/" })}
214+
>
215+
Share to WhatsApp
216+
</a>
189217
);
190218
191219
export default Share;

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export {
1616
FacebookProps,
1717
} from "./utils/getFacebookUrl";
1818

19+
export {
20+
default as getWhatsAppUrl,
21+
WhatsAppProps,
22+
} from "./utils/getWhatsAppUrl";
23+
1924
export {
2025
default as getShareUrl,
2126
AllSocialPlatformProps,

src/types.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export enum SocialPlatforms {
77
Facebook,
88
Linkedin,
99
Twitter,
10+
WhatsApp,
1011
}

src/utils/getShareUrl.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { SocialPlatforms } from "../types";
22
import getFacebookUrl, { FacebookProps } from "./getFacebookUrl";
33
import getLinkedinUrl, { LinkedinProps } from "./getLinkedinUrl";
44
import getTwitterUrl, { TwitterProps } from "./getTwitterUrl";
5+
import getWhatsAppUrl, { WhatsAppProps } from "./getWhatsAppUrl";
56

67
export type AllSocialPlatformProps = FacebookProps &
78
LinkedinProps &
8-
TwitterProps;
9+
TwitterProps &
10+
WhatsAppProps;
911

1012
export const getShareUrl = (
1113
socialPlatform: SocialPlatforms,
@@ -30,6 +32,9 @@ export const getShareUrl = (
3032

3133
case SocialPlatforms.Twitter:
3234
return getTwitterUrl({ url, text, hashtags, related });
35+
36+
case SocialPlatforms.WhatsApp:
37+
return getWhatsAppUrl({ url, text });
3338
}
3439
};
3540

src/utils/getWhatsAppUrl.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { BaseShareProps } from "../types";
2+
import objectToUrlParams from "./objectToUrlParams";
3+
4+
export interface WhatsAppProps extends BaseShareProps {
5+
/** Text to prefill into the WhatsApp message. Appears prior to the URL. */
6+
text?: string;
7+
}
8+
9+
export const getWhatsAppUrl = ({ url, text }: WhatsAppProps) =>
10+
`whatsapp://send?text=${objectToUrlParams({
11+
text: text ? `${text} ${url}` : url,
12+
})}`;
13+
14+
export default getWhatsAppUrl;

0 commit comments

Comments
 (0)