Skip to content

Commit da29891

Browse files
author
Sinu John
authored
feat(native share): Add support for native share using navigator.share (#75)
* feat(add navigator-share) expose handleShare prop which allows navigator share * chore(release): 1.54.2-navigator-share.0 * refactor code * chore(release): 1.54.2-navigator-share.1 * make social share a class component and add the check for support of native share as a state * update read me * change default value to false for canNativeShare * remove redundant code
1 parent dc8cd9a commit da29891

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,18 @@ import { SocialShare } from '@quintype/components';
422422

423423
class CustomComponent extends React.Component {
424424

425-
getSocialCardsTemplate({fbUrl, twitterUrl, gplusUrl, linkedinUrl}) {
425+
getSocialCardsTemplate({fbUrl, twitterUrl, gplusUrl, linkedinUrl, handleNativeShare}) {
426426
return <ul className="social-share-icons">
427427
<li className="social-share-icon">
428428
<a href={fbUrl} target="_blank">
429429
<img src={fbIcon} alt="fb icon"/>
430430
</a>
431431
</li>
432+
{handleNativeShare && <li className="social-share-icon">
433+
<button onClick={handleNativeShare}>
434+
<img src={fbIcon} alt="share icon"/>
435+
</button>
436+
</li>}
432437
</ul>
433438
}
434439

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@quintype/components",
3-
"version": "1.54.1",
3+
"version": "1.54.2-navigator-share.1",
44
"description": "Components to help build Quintype Node.js apps",
55
"main": "dist/cjs/index.js",
66
"module": "dist/es/index.js",

src/components/social-share.js

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,51 @@ import React from "react";
22
import {connect} from "react-redux";
33
import {withError} from './with-error';
44

5-
function SocialShareBase(props) {
6-
const fullUrl = `${props.publisherUrl}/${props.url}`;
7-
const hashtags = props.hashtags ? props.hashtags : '';
8-
9-
return React.createElement(props.template, Object.assign({
10-
fbUrl: `https://www.facebook.com/sharer.php?u=${fullUrl}`,
11-
twitterUrl: `https://twitter.com/intent/tweet?url=${fullUrl}&text=${props.title}&hashtags=${hashtags}`,
12-
gplusUrl: `https://plus.google.com/share?url=${fullUrl}`,
13-
linkedinUrl: `https://www.linkedin.com/shareArticle?url=${fullUrl}&title=${props.title}`,
14-
whatsappUrl: `https://api.whatsapp.com/send?text=${fullUrl}`,
15-
mailtoUrl: `mailto:${''}?subject=${props.title}&body=${fullUrl}`
16-
}, props));
5+
function getNativeShareHandler(canNativeShare, title, fullUrl) {
6+
if (!canNativeShare) {
7+
return null;
8+
}
9+
10+
return function handleShare() {
11+
window.navigator.share({
12+
title: title,
13+
url: fullUrl,
14+
}).catch(console.error);
15+
}
1716
}
1817

18+
class SocialShareBase extends React.Component {
19+
constructor(props) {
20+
super(props);
21+
this.state = {
22+
canNativeShare: false
23+
}
24+
}
25+
26+
componentDidMount() {
27+
this.setState({canNativeShare: global && global.navigator && global.navigator.share})
28+
}
29+
30+
render() {
31+
const fullUrl = `${this.props.publisherUrl}/${this.props.url}`;
32+
const hashtags = this.props.hashtags ? this.props.hashtags : '';
33+
34+
return React.createElement(this.props.template, Object.assign({
35+
fbUrl: `https://www.facebook.com/sharer.php?u=${fullUrl}`,
36+
twitterUrl: `https://twitter.com/intent/tweet?url=${fullUrl}&text=${this.props.title}&hashtags=${hashtags}`,
37+
gplusUrl: `https://plus.google.com/share?url=${fullUrl}`,
38+
linkedinUrl: `https://www.linkedin.com/shareArticle?url=${fullUrl}&title=${this.props.title}`,
39+
whatsappUrl: `https://api.whatsapp.com/send?text=${fullUrl}`,
40+
mailtoUrl: `mailto:${''}?subject=${this.props.title}&body=${fullUrl}`,
41+
handleNativeShare: getNativeShareHandler(this.state.canNativeShare, this.props.title, fullUrl)
42+
}, this.props));
43+
}
44+
}
1945

2046
function mapStateToProps(state) {
2147
return {
2248
publisherUrl: state.qt.config["sketches-host"]
2349
};
2450
}
2551

26-
2752
export const SocialShare = connect(mapStateToProps, {})(withError(SocialShareBase));

0 commit comments

Comments
 (0)