npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sharon

v2.0.0

Published

A lightweight and modular social sharing library

Downloads

123

Readme

Sauce Test Status

Sharon

A lightweight and modular social sharing library:

  • a toolkit to build your own share buttons;
  • supports 13 sharing platforms;
  • gzipped size is 1.72 KB;
  • you can cherry-pick which sharing platforms to use to make it even smaller.

Here how it looks when you want Sharon to open a tweet popup:

sharon.twitter({
  title: "One last quarter as defending champs!",
  hashtags: ["SuperBowl", "DenverBroncos"],
});

Or to get a Facebook share count for your page:

sharon.facebook.count((err, count) => {
  if (err) throw err;
  console.log("Whoa, we have " + count + " shares!");
});

Table of contents

Setup

CommonJS

Install Sharon using npm:

npm install sharon --save

Load the whole library:

import sharon from "sharon";

Or cherry-pick platforms for smaller Webpack, Rollup, or Browserify bundles:

import facebook from "sharon/facebook";
import twitter from "sharon/twitter";

Browser

<script src="dist/sharon.js"></script>

For the sharon.js file, check the dist directory of the installed module or directly download it:

API

Supported sharing platforms

Each sharing platform has its endpoint under the Sharon API:

| Sharing platform | Endpoint | Share count support | Share parameters | | ---------------- | ------------------ | ------------------- | ----------------------------- | | Buffer | sharon.buffer | Yes | Reference | | Facebook | sharon.facebook | Yes | | | Gmail | sharon.gmail | | | | LinkedIn | sharon.linkedin | | Reference | | Odnoklassniki | sharon.ok | Yes | | | Pinterest | sharon.pinterest | Yes | Reference | | Reddit | sharon.reddit | Yes | Reference | | Telegram | sharon.telegram | | | | Tumblr | sharon.tumblr | Yes | Reference | | Twitter | sharon.twitter | | Reference | | Vkontakte | sharon.vk | Yes | Reference | | Weibo | sharon.weibo | | | | XING | sharon.xing | | Reference |

This table also shows which platforms support retrieving share counts and links to the share parameters references.

sharon.platform(url = location.href, parameters = { title: document.title })

  • url <String> The URL to share. Defaults to the current location.
  • parameters <Object> Share parameters. Default to an object with the title property equal to the current page title.

Opens a share popup.

sharon.twitter();

With a custom title:

sharon.twitter({ title: "Check it out" });

Share example.com:

sharon.twitter("http://example.com");

Share example.com with a custom title:

sharon.twitter("http://example.com", { title: "Check it out" });

sharon.platform.href(url = location.href, parameters = { title: document.title })

  • url <String> The URL to share. Defaults to the current location.
  • parameters <Object> Share parameters. Default to an object with the title property equal to the current page title.
  • Returns: <String>

Returns a share popup URL.

const link = sharon.twitter.href();

With a custom title:

const link = sharon.twitter.href({ title: "Check it out" });

For example.com:

const link = sharon.twitter.href("http://example.com");

For example.com with a custom title:

const link = sharon.twitter.href("http://example.com", {
  title: "Check it out",
});

sharon.platform.count(url = location.href, callback)

  • url <String> The URL of which to retrive the share count. Defaults to the current location.
  • callback <Function(err, count)> A callback function that receives the count.

Retrieves the share count of a URL.

sharon.facebook.count((err, count) => {
  if (err) throw err;
  console.log(count);
});

For example.com:

sharon.facebook.count("http://example.com", (err, count) => {
  if (err) throw err;
  console.log(count);
});

Share parameters

When using sharon.platform or sharon.platform.href functions you can specify the share parameters by passing an object as the last argument. They are added to the query parameters of the share popup URL and are specifying additional features:

sharon.twitter({
  title: "One last quarter as defending champs!",
  hashtags: ["SuperBowl", "DenverBroncos"],
});

This produces a popup with a predefined title and hashtags:

Example

The set of features is different for most of the sharing platforms. To find them out, check their documentation, links provided in the Supported sharing platforms table.

There is an inconsistency between different platforms: for instance, Twitter expects the text parameter to contain a link title, while Pinterest expects the description one. Sharon normalizes this behavior: when you pass a title parameter, it is automatically translated into one corresponding to a chosen platform.

More examples

Poor man's tweet button

<button type="button" onclick="sharon.twitter()">Tweet</button>

React component

function LinkedInShareButton {
  const [count, setCount] = useState();

  useEffect(() => {
    sharon.linkedin.count((err, count) => {
      if (err) throw err;
      setCount(count);
    });
  }, []);

  const share = useCallback((event) => {
    event.preventDefault();
    sharon.linkedin();
  }, []);

  return (
    <a onClick={share} href={sharon.linkedin.href()}>
      Share on LinkedIn {count}
    </a>
  );
}

AngularJS

<a ng-click="share($event)" ng-href="{{href}}">Share on Facebook {{count}}</a>
$scope.href = sharon.facebook.href();

$scope.share = (event) => {
  event.preventDefault();
  sharon.facebook();
};

sharon.facebook.count((err, count) => {
  if (err) throw err;

  $scope.$apply(() => {
    $scope.count = count;
  });
});

:heart: