Skip to content

Commit b0548db

Browse files
✨ sucessful share callback (#15)
1 parent 760c4c8 commit b0548db

File tree

10 files changed

+492
-323
lines changed

10 files changed

+492
-323
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ jobs:
88
- name: Begin CI...
99
uses: actions/checkout@v2
1010

11-
- name: Use Node 12
11+
- name: Use Node 14
1212
uses: actions/setup-node@v1
1313
with:
14-
node-version: 12.x
14+
node-version: 14.x
1515

1616
- name: Use cached node_modules
1717
uses: actions/cache@v1

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
tsdx lint

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const Example = () => {
4848
url: "https://on.natgeo.com/2zHaNup",
4949
title: "Flamingos",
5050
}}
51+
onClick={() => console.log("shared successfully!")}
5152
>
5253
<button>Share 🔗</button>
5354
</RWebShare>
@@ -60,10 +61,11 @@ export default Example;
6061

6162
## 👀 Props
6263

63-
| Prop | Description | Type | Default |
64-
| ------- | ------------ | -------------------- | --------------------------------------------- |
65-
| `data` | Share Object | `{text, url, title}` | `{text: "", url: currentURL, title: "Share"}` |
66-
| `sites` | sites | `string[]` | all platforms (see list below for key list) |
64+
| Prop | Description | Type | Default |
65+
| --------- | --------------------------- | -------------------- | --------------------------------------------- |
66+
| `data` | Share Object | `{text, url, title}` | `{text: "", url: currentURL, title: "Share"}` |
67+
| `sites` | sites | `string[]` | all platforms (see list below for key list) |
68+
| `onClick` | callback on sucessful share | | |
6769

6870
## 🌎 Sites
6971

package.json

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
"react": ">=17"
2626
},
2727
"devDependencies": {
28-
"@ampproject/filesize": "^4.2.0",
29-
"@babel/core": "^7.12.10",
30-
"@size-limit/preset-small-lib": "^4.9.1",
28+
"@ampproject/filesize": "^4.3.0",
29+
"@babel/core": "^7.13.16",
30+
"@size-limit/preset-small-lib": "^4.10.2",
3131
"@storybook/addon-essentials": "^6.1.14",
3232
"@storybook/addon-info": "^5.3.21",
3333
"@storybook/addon-knobs": "^6.1.14",
@@ -38,21 +38,16 @@
3838
"@types/react": "^17.0.0",
3939
"@types/react-dom": "^17.0.0",
4040
"babel-loader": "^8.2.2",
41-
"eslint-plugin-prettier": "^3.3.1",
42-
"husky": "^4.3.8",
41+
"eslint-plugin-prettier": "^3.4.0",
42+
"husky": "^6.0.0",
4343
"react": "^16.13.1",
44-
"react-dom": "^17.0.1",
45-
"react-is": "^17.0.1",
46-
"size-limit": "^4.9.1",
47-
"ts-loader": "^8.0.14",
44+
"react-dom": "^17.0.2",
45+
"react-is": "^17.0.2",
46+
"size-limit": "^4.10.2",
47+
"ts-loader": "^9.1.0",
4848
"tsdx": "^0.14.1",
49-
"tslib": "^2.1.0",
50-
"typescript": "^4.1.3"
51-
},
52-
"husky": {
53-
"hooks": {
54-
"pre-commit": "tsdx lint"
55-
}
49+
"tslib": "^2.2.0",
50+
"typescript": "^4.2.4"
5651
},
5752
"prettier": {
5853
"printWidth": 100,

src/components/icon/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ const SocialIconStyle: CSSProperties = {
1414
fontSize: 0,
1515
};
1616

17-
export default function Icon({ name, data, onClose }: IconProps) {
17+
export default function Icon({ name, data, onClose, onClick }: IconProps) {
1818
const { path, viewBox = "0 0 24 24", color, e } = iconList[name];
1919

20+
const handleOnButtonClicked = () => {
21+
onClick && onClick(); // callback
22+
e(encodeURIComponent(data.url), data.text, data.title);
23+
onClose();
24+
};
25+
2026
return (
2127
<button
22-
onClick={() => e(encodeURIComponent(data.url), data.text, data.title) && onClose()}
28+
onClick={handleOnButtonClicked}
2329
aria-label={name}
2430
style={{ ...SocialIconStyle, background: color }}
2531
>

src/components/social-icons.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ const IconsLisStyle: CSSProperties = {
2121
gridGap: "1.25rem",
2222
};
2323

24-
export default function SocialIcons({ onClose, sites, data, closeText }: SocialIconsProps) {
24+
export default function SocialIcons({
25+
onClose,
26+
sites,
27+
data,
28+
closeText,
29+
onClick,
30+
}: SocialIconsProps) {
2531
return (
2632
<section
2733
role="dialog"
@@ -32,7 +38,7 @@ export default function SocialIcons({ onClose, sites, data, closeText }: SocialI
3238
<Header title={data.title} />
3339
<div style={IconsLisStyle}>
3440
{sites.map((name) => (
35-
<Icon name={name} key={name} data={data} onClose={onClose} />
41+
<Icon name={name} key={name} data={data} onClose={onClose} onClick={onClick} />
3642
))}
3743
</div>
3844
<CloseButton onClose={onClose} closeText={closeText} />

src/index.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const RWebShare = ({
1111
children,
1212
closeText,
1313
data,
14+
onClick,
1415
sites = Object.keys(iconList),
1516
}: RWebShareProps) => {
1617
const { onOpen, onClose, isOpen } = useDisclosure();
@@ -25,11 +26,15 @@ export const RWebShare = ({
2526
[data]
2627
);
2728

28-
const handleOnClick = () => {
29-
try {
30-
window.navigator.share(shareData);
31-
} catch (e) {
32-
console.warn(e);
29+
const handleOnClick = async () => {
30+
if (window.navigator.share) {
31+
try {
32+
await window.navigator.share(shareData);
33+
onClick();
34+
} catch (e) {
35+
console.warn(e);
36+
}
37+
} else {
3338
onOpen();
3439
}
3540
};
@@ -43,7 +48,13 @@ export const RWebShare = ({
4348
{isOpen && (
4449
<Portal>
4550
<Backdrop onClose={onClose}>
46-
<SocialIcons onClose={onClose} sites={sites} data={shareData} closeText={closeText} />
51+
<SocialIcons
52+
onClose={onClose}
53+
sites={sites}
54+
data={shareData}
55+
closeText={closeText}
56+
onClick={onClick}
57+
/>
4758
</Backdrop>
4859
</Portal>
4960
)}

src/interfaces.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ export interface RWebShareProps {
99
closeText?: string;
1010
data: ShareData;
1111
sites: string[];
12+
onClick?;
1213
}
1314

1415
export interface SocialIconsProps {
1516
onClose;
1617
closeText?: string;
1718
sites: string[];
1819
data: Required<ShareData>;
20+
onClick?;
1921
}
2022

2123
export interface IconProps {
2224
onClose;
2325
name: string;
2426
data: Required<ShareData>;
27+
onClick?;
2528
}

stories/web-share-component.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const Default = () => (
1515
url: "https://on.natgeo.com/2zHaNup",
1616
title: "Flamingos",
1717
}}
18+
onClick={() => console.log("share successful!")}
1819
>
1920
<button>Share 🔗</button>
2021
</RWebShare>

0 commit comments

Comments
 (0)