However, it's supported on the main mobile browsers and we can add a fallback option to all the unsupported browsers.
Apart from that, some other limitations are:
It can only be used on sites served over HTTPS.
It must be triggered in response to some user action such as click.
Web Sharing API
To trigger the share menu we are gonna used the promise-based method .share(). But, as I said before and because is not fully supported, let's check if It's supported on the browser first.
Now, we can attached to the the onClick event of our component and pass the information via props.
Providing fallback
As I said earlier, let's add a second option for all the other browsers. On this case we'll add options for sharing on facebook, twitter and copy the url to the clipboard. Let's code that.
We can add a state and trigger it whenever navigator.share is unsupported.
Now, let's wrap our button into a div container and add the markup for the fallback.
Share on facebook
For facebook there actually a lot of ways to do it, but some options seem a bit overwhelming. You can give it a look if you want to, but in this case I don't want to add the sdk because I don't need to track all the shares from the page, so we are gonna do it by using facebook.com/sharer
Share on twitter
This one is actually really easy, just need to use an <a> tag and handle it as an external link.
Copy to the clipboard
And finally, we are gonna use the navigator.clipboard which is supported by most of the modern browsers.
If you need support on internet explore you might want to explore other alternatives. For now, let's work with navigator.clipboard and add some type of feedback for the users to know when it's been copied.
Now, you only need to add some styles and that's it.
The full version of our component will look like this.
Usage
One last thing
You might already notice is gonna be a bit annoying having to write the full url each time we use the component. We can change this by using window.location and changing the url prop's name to path
You'll need to replace all the url variables with fullURL
Now, we can use our component and only pass it the path.