React high order component to drive web share widget on react applications 📌.
Web Share api is supported on Chrome for Android and Android Webview v. ^55.0.
You can find a working demo here. Be sure to use a supported browser.
npm install react-web-share-api --save
Consume the UI component in the higher order component button.js
:
import React from "react";
import webShare from 'react-web-share-api';
const Button = ({ share, isSupported }) => isSupported
? <button onClick={share}>Share now!</button>
: <span>Web Share not supported</span>;
export default webShare<OwnProps>()(Button);
Pass the configuration to the higher order component smartComponent.js
:
import React from "react";
import Button from "./button";
const SmartComponent = (config) =>
<Button config={config} foo="bar" />;
export default SmartComponent;
It takes a configuration prop that define how the native widget should behave and any other property you want to pass to the UI component. It spread all the properties a part from the configuration to the enhanced UI component. The UI component will also receive other props that will help improving the experience allowing complete control on the renderer and on the action handler.
Yes, the configuration is injected from the parent component. In this way it does not matter which flux implementation you are using. At the same time, we are preserving the high order component pattern so you have complete control on the renderer and on the action handler.
Yes, you don't need to install any typescript dependecies because types come with the library. It export WebShareConfigContainer
(injected configuration) and WebShareInterface
(UI component extended props) typescript interfaces. All the examples are written in typescript.
Your wrapped component will be decorated with this injected props:
Parameter | Type | Description |
---|---|---|
isSupported | boolean | True if the web share api is supported by the browser. |
share | function: () => WebShare | It will open the share native widget. |
Configuration of the high order component:
Parameter | Type | Description |
---|---|---|
params | object | Required. At the moment it consist in the following props: { title, text, url } |
onShareSuccess | Promise based callback: () | Handler executed after the share api is resolved. |
onShareError | Promise based callback: (error) | Handler executed after the share api is rejected. |
See the LICENSE file for license rights and limitations (MIT).