Skip to content

Commit

Permalink
✨ sucessful share callback (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshzalavadiya authored Apr 22, 2021
1 parent 760c4c8 commit b0548db
Show file tree
Hide file tree
Showing 10 changed files with 492 additions and 323 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ jobs:
- name: Begin CI...
uses: actions/checkout@v2

- name: Use Node 12
- name: Use Node 14
uses: actions/setup-node@v1
with:
node-version: 12.x
node-version: 14.x

- name: Use cached node_modules
uses: actions/cache@v1
Expand Down
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

tsdx lint
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const Example = () => {
url: "https://on.natgeo.com/2zHaNup",
title: "Flamingos",
}}
onClick={() => console.log("shared successfully!")}
>
<button>Share 🔗</button>
</RWebShare>
Expand All @@ -60,10 +61,11 @@ export default Example;

## 👀 Props

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

## 🌎 Sites

Expand Down
27 changes: 11 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"react": ">=17"
},
"devDependencies": {
"@ampproject/filesize": "^4.2.0",
"@babel/core": "^7.12.10",
"@size-limit/preset-small-lib": "^4.9.1",
"@ampproject/filesize": "^4.3.0",
"@babel/core": "^7.13.16",
"@size-limit/preset-small-lib": "^4.10.2",
"@storybook/addon-essentials": "^6.1.14",
"@storybook/addon-info": "^5.3.21",
"@storybook/addon-knobs": "^6.1.14",
Expand All @@ -38,21 +38,16 @@
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"babel-loader": "^8.2.2",
"eslint-plugin-prettier": "^3.3.1",
"husky": "^4.3.8",
"eslint-plugin-prettier": "^3.4.0",
"husky": "^6.0.0",
"react": "^16.13.1",
"react-dom": "^17.0.1",
"react-is": "^17.0.1",
"size-limit": "^4.9.1",
"ts-loader": "^8.0.14",
"react-dom": "^17.0.2",
"react-is": "^17.0.2",
"size-limit": "^4.10.2",
"ts-loader": "^9.1.0",
"tsdx": "^0.14.1",
"tslib": "^2.1.0",
"typescript": "^4.1.3"
},
"husky": {
"hooks": {
"pre-commit": "tsdx lint"
}
"tslib": "^2.2.0",
"typescript": "^4.2.4"
},
"prettier": {
"printWidth": 100,
Expand Down
10 changes: 8 additions & 2 deletions src/components/icon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ const SocialIconStyle: CSSProperties = {
fontSize: 0,
};

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

const handleOnButtonClicked = () => {
onClick && onClick(); // callback
e(encodeURIComponent(data.url), data.text, data.title);
onClose();
};

return (
<button
onClick={() => e(encodeURIComponent(data.url), data.text, data.title) && onClose()}
onClick={handleOnButtonClicked}
aria-label={name}
style={{ ...SocialIconStyle, background: color }}
>
Expand Down
10 changes: 8 additions & 2 deletions src/components/social-icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ const IconsLisStyle: CSSProperties = {
gridGap: "1.25rem",
};

export default function SocialIcons({ onClose, sites, data, closeText }: SocialIconsProps) {
export default function SocialIcons({
onClose,
sites,
data,
closeText,
onClick,
}: SocialIconsProps) {
return (
<section
role="dialog"
Expand All @@ -32,7 +38,7 @@ export default function SocialIcons({ onClose, sites, data, closeText }: SocialI
<Header title={data.title} />
<div style={IconsLisStyle}>
{sites.map((name) => (
<Icon name={name} key={name} data={data} onClose={onClose} />
<Icon name={name} key={name} data={data} onClose={onClose} onClick={onClick} />
))}
</div>
<CloseButton onClose={onClose} closeText={closeText} />
Expand Down
23 changes: 17 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const RWebShare = ({
children,
closeText,
data,
onClick,
sites = Object.keys(iconList),
}: RWebShareProps) => {
const { onOpen, onClose, isOpen } = useDisclosure();
Expand All @@ -25,11 +26,15 @@ export const RWebShare = ({
[data]
);

const handleOnClick = () => {
try {
window.navigator.share(shareData);
} catch (e) {
console.warn(e);
const handleOnClick = async () => {
if (window.navigator.share) {
try {
await window.navigator.share(shareData);
onClick();
} catch (e) {
console.warn(e);
}
} else {
onOpen();
}
};
Expand All @@ -43,7 +48,13 @@ export const RWebShare = ({
{isOpen && (
<Portal>
<Backdrop onClose={onClose}>
<SocialIcons onClose={onClose} sites={sites} data={shareData} closeText={closeText} />
<SocialIcons
onClose={onClose}
sites={sites}
data={shareData}
closeText={closeText}
onClick={onClick}
/>
</Backdrop>
</Portal>
)}
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ export interface RWebShareProps {
closeText?: string;
data: ShareData;
sites: string[];
onClick?;
}

export interface SocialIconsProps {
onClose;
closeText?: string;
sites: string[];
data: Required<ShareData>;
onClick?;
}

export interface IconProps {
onClose;
name: string;
data: Required<ShareData>;
onClick?;
}
1 change: 1 addition & 0 deletions stories/web-share-component.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const Default = () => (
url: "https://on.natgeo.com/2zHaNup",
title: "Flamingos",
}}
onClick={() => console.log("share successful!")}
>
<button>Share 🔗</button>
</RWebShare>
Expand Down
Loading

0 comments on commit b0548db

Please sign in to comment.