Web Share
The Web Share component provides an interface to the Web Share API, allowing web applications to leverage the native share functionality of the user's device. This creates a seamless sharing experience by integrating with the device's built-in sharing options (social media apps, messaging apps, email, etc.).
This component is particularly useful for:
Sharing articles, blog posts, or news content
Sharing product pages in e-commerce applications
Sharing user-generated content (photos, videos, text)
Enabling social media sharing without third-party widgets
Sharing files directly from your web application
Browser Support
The Web Share API is supported in most modern mobile browsers and increasingly in desktop browsers. The API requires user interaction (e.g., click) to trigger the share dialog.
Usage
Basic Text and URL Sharing
<div {{ stimulus_controller('@pwa/web-share') }}>
<article>
<h1>Amazing Article Title</h1>
<p>Read this amazing content...</p>
<button {{ stimulus_action('@pwa/web-share', 'share', 'click', {
title: 'Amazing Article Title',
text: 'Check out this amazing article!',
url: 'https://example.com/article/123'
}) }}>
Share Article
</button>
</article>
</div>
<script>
document.addEventListener('pwa--web-share:success', () => {
console.log('Content shared successfully!');
});
document.addEventListener('pwa--web-share:error', (event) => {
console.error('Share failed:', event.detail.error);
});
</script>Sharing Current Page
Sharing Files
Social Media Share with Fallback
Parameters
None
Actions
share
Triggers the native share dialog with the provided data. The action requires a data parameter containing at least one of the following:
Required parameters (at least one):
title(string): The title of the content to be sharedtext(string): Text content to be sharedurl(string): URL to be sharedfiles(Array of File objects): Files to be shared
Shareable File Types
The following file types are commonly supported for sharing:
Images:
.jpg,.jpeg,.png,.gif,.webpVideos:
.mp4,.webmAudio:
.mp3,.wav,.oggDocuments:
.pdf,.txt
Support varies by browser and operating system.
Targets
None
Events
pwa--web-share:success
Dispatched when content is successfully shared (user completed the share action).
No payload
Example:
pwa--web-share:error
Dispatched when sharing fails or is cancelled by the user.
Payload: {error} - Error object or message
Example:
Best Practices
Check for support: Always verify browser support before showing share buttons
Require user interaction: The share dialog can only be triggered by user actions (clicks, touches)
Provide fallbacks: Offer traditional share buttons for browsers without Web Share API support
Keep it simple: Avoid overusing share buttons; place them strategically
Validate URLs: Ensure URLs are absolute and properly formatted
File size limits: Keep shared files small to avoid long downloads
Handle errors gracefully: Always listen for error events and provide feedback
Error Handling
Common errors you might encounter:
AbortError: User cancelled the share dialog
NotAllowedError: Share was not triggered by user interaction
NotSupportedError: Browser doesn't support Web Share API
TypeError: Invalid data provided (e.g., malformed URL)
DataError: Issues with file sharing (unsupported type, too large, etc.)
Complete Example
Here's a complete example with proper fallback handling:
Last updated
Was this helpful?