Logo
BlogDashboardAboutProjects
Logo
HomeBlogDashboardAboutProjects
GithubGithub
LinkedinLinkedin
TwitterTwitter
v1.12.1
19 Jan 2023
·
1 min read

Using the Web Share API to trigger the OS share sheet

Instead of building a custom share UI, you can hand off to the OS share sheet with one function call. navigator.share() works on mobile browsers. Here's how to use it.

With the Web Share API, we are now able to share contents using the Operating System (OS) share sheet. This API allows us to share links, text, and even files to any other apps that is supported and installed on the device.

The following screenshots should look familiar for anyone while sharing contents on a smartphone today.

navigator.share()

The Navigator.share() API allows us to quickly implement using the respective OS share sheet in the screenshot above.

const data = {
  title: "Ru Chern",
  text: "Passionate Frontend Developer",
  url: "https://ruchern.dev",
};

navigator.share(data);

Just a few lines of code and we are now able to leverage the beauty of the share sheet to share contents to our favourite apps easily.

I believe this is a much better user experience for sharing rather having to use the primitive methods.

Of course, the API also allows us to share files. Do refer more to the MDN docs for the comprehensive details for using the navigator.share() API.

Caveat

This API only works for mobile browsers since it requires the OS functionality to work. As for sharing via desktop browsers, we will have to find other interesting ways as a workaround!

Related Articles

23 Jan 2023
1 tag
Adding JSON-LD structured data to your React blog
Google uses structured data to understand what's on your page. JSON-LD is the recommended format. Here's a minimal React component to add it, and how to verify it works.
11 Nov 2023
1 tag
process.env vs import.meta: environment variables in Node and the browser
process.env only runs in Node. It won't work in the browser. import.meta is the module-aware alternative, and it works with TypeScript too.
23 Nov 2023
1 tag
What the ! operator does in TypeScript and Swift
The ! at the end of a TypeScript expression tells the compiler you know the value isn't null or undefined. It's the same idea as force unwrapping in Swift. Useful, but worth understanding before reaching for it.
29 Mar 2023
1 tag
Feature flags: ship code without shipping features
A feature flag is an if/else around a piece of code, controlled by an environment variable. It lets you merge and deploy work-in-progress without turning it on. Fewer conflicts, safer releases.