react-share

repository·master·Indexed 25 days ago

https://github.com/nygardk/react-share

Social media share buttons and share counts for React applications. Version 5.3.0 provides built-in social media icons and support for custom icons without requiring external SDKs. Includes share buttons for platforms such as X (formerly Twitter), Facebook, LinkedIn, Pinterest, Bluesky, Tumblr, and VK, along with deprecated share count components.

Tokens
4.3K
Snippets
15
Records
29
Agent score
84%

What's inside react-share

  1. Note on semantic versioning and external API changes

    master
    While react-share follows standard semver, the functionality of share buttons and share counts depends on external social media APIs (e.g., Facebook). If these platforms change or deprecate their APIs, react-share may require updates that do not necessarily trigger a major version bump. Plan your application maintenance with the understanding that external API changes are outside the library's direct control.
  2. Troubleshoot popup blockers with share buttons

    master

    Share buttons use window.open within the click handler. If you perform asynchronous work inside the beforeOnClick prop, browsers may block the resulting popup because it is no longer considered a direct user-triggered action.

    To resolve this, set openShareDialogOnClick={false} and manually manage the resulting URL.

  3. Improve accessibility for icon-only share buttons

    master
    When using icon-only buttons, ensure they have an accessible name for screen readers. The htmlTitle prop only provides a tooltip and is insufficient for accessibility. Use standard React accessibility props like aria-label or aria-labelledby directly on the share button component.
  4. Understand mobile app handoff and iOS PWA limitations

    master

    Be aware of platform-specific behaviors that react-share cannot control:

    • Mobile App Handoff: Some platforms may open a browser, an in-app browser, or an extra tab before handing off to a native app. This is determined by the target platform and browser.
    • iOS PWAs: In standalone mode, iOS PWAs may show a blank intermediary page when using schemes like mailto: or when performing native-app handoff URLs. This is a platform limitation.
  5. Display Share Counts (Deprecated)

    master

    Share count components are deprecated and will be removed in v6. They are provided in v5 for best-effort compatibility only. Upstream networks may stop returning counts without notice.

    Supported Components:

    • FacebookShareCount
    • HatenaShareCount
    • OKShareCount
    • PinterestShareCount
    • RedditShareCount
    • TumblrShareCount
    • VKShareCount

    Props:

    • url (string, required): URL to look up.
    • children ((shareCount: number) => ReactNode, optional): A render prop for custom output.
    • Supports standard <span> attributes like className and aria-*.
    // Basic usage
    <FacebookShareCount url={shareUrl} />
    
    // Custom rendering
    <FacebookShareCount url={shareUrl}>
      {(shareCount) => <span className="myShareCountWrapper">{shareCount}</span>}
    </FacebookShareCount>
  6. Customize Share Icons

    master

    All exported icons are raw SVG elements and share the following props:

    PropTypeDefaultDescription
    sizenumber | string64Icon size
    roundbooleanfalseRender a circular background instead of a rectangle
    borderRadiusnumber0Rounded corners for rectangular shape
    bgStyleCSSProperties{}Custom background styles (e.g., fill)
    iconFillColorstring"white"Fill color for icon paths
    ...svgPropsSVGProps<SVGSVGElement>-Standard SVG attributes (e.g., className, aria-hidden)

    Note: Mark decorative icons with aria-hidden="true" or provide an accessible name if the icon conveys meaning.

    import { XIcon } from "react-share";
    
    <XIcon aria-hidden="true" size={32} round />
  7. Common props for all Share Buttons

    master

    All share buttons in react-share render a native <button> element and support standard button attributes (e.g., aria-label, name, data-*).

    Required Props:

    • children (ReactNode): The content inside the button (usually an Icon component).
    • url (string): The URL of the page to be shared.

    Optional Props:

    • beforeOnClick (() => Promise<void> | void): A function that runs before the share action.
    • disabled (boolean): Disables click handling and adds a disabled class.
    • disabledStyle (CSSProperties): Styles applied when disabled is true (default: { opacity: 0.6 }).
    • htmlTitle (string): Sets the native title attribute (tooltip) on the button.
    • onClick ((event, link) => void): Callback triggered after link generation. Note: Not available on EmailShareButton.
    • onShareWindowClose (() => void): Called after a popup-based button's window closes.
    • resetButtonStyle (boolean): Resets native button styles (default: true).
    • windowHeight, windowWidth (number): Overrides popup dimensions for popup-based buttons.
    • windowPosition ("windowCenter" | "screenCenter"): Controls popup positioning.
    • openShareDialogOnClick (boolean): Automatically opens the popup on click (default: true). Not available on EmailShareButton.