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.

circle-info

Always check for API support before using it, and provide a fallback for browsers that don't support the Web Share API.

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 shared

  • text (string): Text content to be shared

  • url (string): URL to be shared

  • files (Array of File objects): Files to be shared

circle-info

The title parameter may be ignored by some sharing targets. Not all apps will display the title you provide.

circle-exclamation

Shareable File Types

The following file types are commonly supported for sharing:

  • Images: .jpg, .jpeg, .png, .gif, .webp

  • Videos: .mp4, .webm

  • Audio: .mp3, .wav, .ogg

  • Documents: .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:

circle-info

This event fires when the user selects a sharing target, not necessarily when the content is actually sent to that target.

pwa--web-share:error

Dispatched when sharing fails or is cancelled by the user.

Payload: {error} - Error object or message

Example:

Best Practices

  1. Check for support: Always verify browser support before showing share buttons

  2. Require user interaction: The share dialog can only be triggered by user actions (clicks, touches)

  3. Provide fallbacks: Offer traditional share buttons for browsers without Web Share API support

  4. Keep it simple: Avoid overusing share buttons; place them strategically

  5. Validate URLs: Ensure URLs are absolute and properly formatted

  6. File size limits: Keep shared files small to avoid long downloads

  7. 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?