Socket
Socket
Sign inDemoInstall

@phantomstudios/react-share

Package Overview
Dependencies
6
Maintainers
3
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @phantomstudios/react-share

An all-in-one React library to implement custom Sharing Meta and Social Media Sharing Buttons.


Version published
Maintainers
3
Install size
79.3 kB
Created

Readme

Source

react-share

NPM version Actions Status PR Welcome

An all-in-one React library to implement custom Page Sharing Meta and Social Media Sharing Buttons.

Introduction

Designed to use and extend OpenGraph standards, including built-in support for sharing with:

  • Facebook
  • Linkedin
  • Twitter
  • WhatsApp
  • Copy to Clipboard

Installation

Install this package with npm.

npm i @phantomstudios/react-share

Usage

Example usage in Next.js:

import Head from 'next/head';
import { MetaHeadEmbed } from "@phantomstudios/react-share";

const PageLayout: React.FC = ({children}) => {
  <>
    <MetaHeadEmbed
      render={(meta: React.ReactNode) => <Head>{meta}</Head>}
      siteTitle="PHANTOM"
      pageTitle="Our Work"
      titleTemplate="[pageTitle] | [siteTitle]"
      description="Transforming challenges of all shapes and sizes into inventive, engaging and performance driven solutions that change the game."
      baseSiteUrl="https://phantom.land"
      pagePath="work"
      keywords={["creative-agency", "phantom", "work"]}
      imageUrl="https://bit.ly/3wiUOuk"
      imageAlt="PHANTOM logo."
      twitter={{
        cardSize: "large",
        siteUsername: "@phntmLDN",
        creatorUsername: "@phntmLDN",
      }}
    />
    {children}
  </>
);

export default PageLayout;

<MetaHeadEmbed />

PropertyTypeRequiredNotes
renderReact.ReactNodeYesUnfortunately react-helmet and next/head are strict with how they accept meta tags. react-helmet doesn't support nesting. Whereas Next.JS only supports some children and not all, therefore a render function is required.
pageTitlestringNoUnique page title that describes the page, such as Home, About etc. etc.
siteTitlestringYesTitle of the site, usually the organization / brand name. If pageTitle and siteTitle are the same, only this shows.
titleTemplatestringNoTitle template used to display pageTitle and siteTitle in a template, displays the values using corresponding [pageTitle] and [siteTitle]. Example template: "[pageTitle] | [siteTitle]".
descriptionstringYesA one to two sentence description of your webpage. Keep it within 160 characters, and write it to catch the user's attention.
baseSiteUrlstringYesBase site URL, excluding trailing slash.
pagePathstringNoThe path of the current page, excluding leading slash.
disableCanonicalbooleanNoDisable canonical if not desired, defaults to false.
keywordsstring|string[]NoList of SEO keywords describing what your webpage does. Example, "your, tags" or ["your", "tags"].
imageUrlstringNoImage url of asset to share. Recommended aspect ratio for landscape is 1.9:1 (1200x630) or for squares 1:1 (1200x1200). For more info, visit here. If a relative URL is provided, baseSiteUrl is prefixed. If specifying a relative URL do not add the leading slash.
imageAltstringNoImage alt for users who are visually impaired.
imageWidthnumberNoWidth of share image, defaults to 1200.
imageHeightnumberNoHeight of share image, defaults to 630.
localestringNoThe locale these tags are marked up in, such as; en_GB, fr_FR and es_ES. Defaults to en_US.
twitterTwitterEmbedPropsNoOptional twitter embed properties to include.

To use simply add MetaHeadEmbed to a shared layout to get the best out of page specific properties such as pagePath.

Note: baseSiteUrl and imageUrl must start with https://, else they won't work when sharing.

TwitterEmbedProps

PropertyTypeRequiredNotes
cardSize'small'|'large'YesSummary card size.
titlestringNoA concise title for the related content. If left blank, page title will be inherited instead.
descriptionstringNoA description that concisely summarizes the content as appropriate for presentation within a Tweet. Should not be the same as title. If left blank, MetaHeadEmbed description will be inherited instead.
siteUsernamestringNoThe Twitter @username the card should be attributed to.
creatorUsernamestringNoThe Twitter @username for the content creator / author.
imageUrlstringNoImage to show in card. Images must be less than 5MB in size. Supported file types; JPG, PNG, WEBP and GIF.
imageAltstringNoImage alt for users who are visually impaired. Maximum 420 characters.

Note: Image used should be different based on cardSize:

  • For large cards, use a 2:1 aspect ratio (300x157 px minium or 4096x4096 px maximum).
  • For small cards, use a 1:1 aspect ratio (144x144 px minium or 4096x4096 px maximum).

A Note on Twitter Tags

Twitter will inherit og:title, og:description and og:image tags by default, so unless you want unique fields, respective fields in TwitterEmbedProps should be left blank to avoid duplication.

getFacebookUrl()

ParameterTypeRequiredNotes
urlstringYesURL of shared webpage.
hashtagstringNoHashtag to show in Facebook card.

Basic component example usage:

import { getFacebookUrl } from "@phantomstudios/react-share";

const ShareToFacebook = () => (
  <a href={getFacebookUrl({ url: "https://phantom.land/" })}>
    Share to Facebook
  </a>
);

export default ShareToFacebook;

getLinkedinUrl()

ParameterTypeRequiredNotes
urlstringYesURL of shared webpage.
titlestringNoTitle to show in card.
summarystringNoDescription to show in card.
sourcestringNoSource of the content. For example, your website or application name.

Basic component example usage:

import { getLinkedinUrl } from "@phantomstudios/react-share";

const ShareToLinkedin = () => (
  <a href={getLinkedinUrl({ url: "https://phantom.land/" })}>
    Share to Linkedin
  </a>
);

export default ShareToLinkedin;

getTwitterUrl()

ParameterTypeRequiredNotes
urlstringYesURL of shared webpage.
textstringNoText to show in Twitter card.
hashtagsstring|string[]NoHashtags to show in Twitter card. Example, "your,tags" or ["your", "tags"].
relatedstring|string[]NoAccounts to recommend following. Example, "your, tags" or ["your", "tags"].

Basic component example usage:

import { getTwitterUrl } from "@phantomstudios/react-share";

const ShareToTwitter = () => (
  <a href={getTwitterUrl({ url: "https://phantom.land/" })}>Share to Twitter</a>
);

export default ShareToTwitter;

getWhatsAppUrl()

ParameterTypeRequiredNotes
urlstringYesURL of shared webpage.
textstringNoText to show in the WhatsApp message before the URL.

Basic component example usage:

import { getWhatsAppUrl } from "@phantomstudios/react-share";

const ShareToWhatsApp = () => (
  <a href={getWhatsAppUrl({ url: "https://phantom.land/" })}>
    Share to WhatsApp
  </a>
);

export default ShareToWhatsApp;

Note: WhatsApp links will only work on mobile, so be sure to hide any WhatsApp links if the user is not on a mobile device!

getShareUrl()

If you would rather have all share urls in one place, getShareUrl() can be used! It includes props from every social platform listed above, so simply pass in a SocialPlatform, and the platforms corresponding props.

Example usage:

import { getShareUrl, SocialPlatforms } from "@phantomstudios/react-share";

const Share = () => (
  <a href={getShareUrl(SocialPlatforms.Facebook, {url: "https://phantom.land/" })}>
    Share to Facebook
  </a>
  <a href={getShareUrl(SocialPlatforms.Linkedin, { url: "https://phantom.land/" })}>
    Share to Linkedin
  </a>
  <a href={getShareUrl(SocialPlatforms.Twitter, { url: "https://phantom.land/" })}>
    Share to Twitter
  </a>
  <a href={getShareUrl(SocialPlatforms.WhatsApp, { url: "https://phantom.land/" })}>
    Share to WhatsApp
  </a>
);

export default Share;

copyToClipboard()

Method used to copy a given text into your clipboard.

import { copyToClipboard } from "@phantomstudios/react-share";

const Copy = () => (
  <div onClick={() => copyToClipboard("https://phantom.land")}>Copy</div>
);

export default Copy;

Further Resources

Useful resources for testing meta properties:

  • Meta Tags - With Meta Tags you can preview how your webpage will look on Google, Facebook, Twitter and more.
  • Social Share Preview - Chrome browser extension to live preview how the webpage will look when shared. Especially useful for testing when app is auth protected.

🍰 Requests and Contributing

If a social media platform you want to use isn't already supported, or found an issue? Get involved! Please contribute using the GitHub Flow. Create a branch, add commits, and open a Pull Request or submit a new issue.

Please read CONTRIBUTING for details on our CODE_OF_CONDUCT, and the process for submitting pull requests to us!

Keywords

FAQs

Last updated on 10 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc