Simple, clean, customizable sharing buttons.
The default share buttons used by the social networks are not only ugly to look at (sorry, they just are) but they usually depend on iframes, are slow and generally heavy. That led to me creating shr (short for share).
- Accessible - built using progressive enhancement
- Lightweight - just 3KB minified and gzipped
- Customisable - make the buttons and count look how you want with the markup you want
- Semantic - uses the right elements. There's no
<span>s as buttons type hacks
- Fast - uses local storage to cache results to keep things fast
Check out the changelog
Here's an example for a Facebook button, see HTML section below for other examples.
This markup assumes you're using the SVG sprite (which is optional) and the default CSS. If you're not using either of these then you can omit the
shr__* classNames completely and the
href attribute value is used to determine the type of network. It is also used as the fallback so must be valid.
Once Shr has been initialized on a button and data has been fetched, it is manipulated as below:
- The outer
<span>is a wrapper so that we can prevent the count wrapping under the button and just looking odd
- The count
<span>is used as the bubble for the current count for share, star or subscriber, etc
- The className for both of these elements can be changed in options
via the npm package
If you're using npm/yarn to manage your dependencies, you can add
npm install --save shr-buttons
Add the following before you're closing
Alternatively add the script to your main app bundle.
You can initialise a single button using the constructor:
const button = '.js-shr' ...options ;
This will setup all elements that match the
.js-shr selector. The first argument must be either a:
- CSS string selector that's compatible with
- a NodeList
- an Array of HTMLElement
This will return an instance that you can use for API calls later.
More information about the
options can be found in options section below.
Alternatively, an easier way if you have multiple buttons is to use the static method:
const buttons = Shr;
This will return an array of instances it setup.
Shr.setup will also look for mutations of the DOM and and matching elements will also be setup if they are injected into the DOM after initial setup.
3. CSS (optional)
You don't have to use the Shr CSS. You're free to style the buttons how you like. You can either include the SASS in your build or use the CDN hosted CSS in your
4. SVG Sprite (optional)
Ir you want to display the icons for each social network as per the demo, you can use the included SVG sprite. If you already have a sprite system, then you can include the SVG icons as-is. Otherwise, you can use something like sprite.js.
A few useful methods are exposed. To call an API method, you need a reference to the instance. This is returned from
Shr.setup or your call the the constructor (
new Shr), e.g.:
const button = '.js-shr-facebook' ...options ;button;
||Open the associated dialog. This will be blocked unless it is as a result of user input. We'd suggest calling this as the callback for
The following needs revision.
There are a ton of options that ship with Shr. These allow you to customize the library to your needs.
If you are are just debugging Shr in a development environment, you can turn on debugging when you setup.
When adding the share count element to the screen, this is the className used to style it.
Sometimes your URL has not been shared yet. You can choose whether or not you want to display 0 shares or not. Some APIs don't allow for the actual count, so those APIs will just be a link to share (such as Twitter) and won't show the count if this is turned on or not.
By default, Shr shortens the amount of shares to an easier to read number. Say you have a URL that went viral and you have over 1 million shares. By default, Shr shows this as 1 M. You can, however, turn this off and show the exact amount of shares.
Type: Enum (
By default, the number of shares shows up after the social icon. This means it's to the right of the icon. You can change this to be before the social icon (the left of the icon) by setting this value to
When the user clicks the share icon, we automatically update the count. However, this is assuming that the user went through with the sharing. This is for speed and reactivity. If you don't want this behavior, you can set this value to false.
enabled: truekey: 'shr'ttl: 300000
enabled: Boolean // Whether storage is supportedkey: String // Key to be used in local storage - (NOTE: can't contain special characters or whitespace)ttl: Number // Seconds to keep the storage valid
To save requests and speed up your site, Shr saves all of the values used to local storage. The two keys you can set are the
key of the local storage and the time to live (AKA: how long do you want these values to last before we refresh). These can be customized to your liking in the setup like.
Shr provides a ton of networks that can be used on your site. Each button has certain attributes that need to be defined in order for Shr to operate efficiently and effictively. Below are descriptions and an example of each button and how to use it.
This button allows you to tweet a URL on Twitter. A count is not available for this button due to Twitter deciding to remove the API endpoint, for whatever reason.
There are 3 variables you can add to your Twitter share:
text- This is the text of your tweet. Makes ure it's properly URL encoded.
url- This is the URL you wish to share via Twitter. The URL needs to be properly encoded as well.
via- This is who is sharing the tweet (your username)
This button allows you to post a pin to Pinterest.
There are 3 variables you can add to your Pinterest pin:
url- This is the URL you wish to pin on Pinterest. Make sure it's properly URL encoded.
media- This is the URL to an image you wish to pin. Make sure it's properly URL encoded.
description- This is a URL encoded description for your pin.
This button allows you to share on Facebook.
When entering your URL for Facebook, make sure it's properly URL encoded! The number of shares will appear next to the button for the URL you are sharing.
This button allows you to star a repo on GitHub and shows the current number of stars for the project.
This button allows you to subscribe to a channel on YouTube and shows the current number of subscribers.
Shr is supported in all modern browsers and IE11.
If you find anything weird with Shr, please let us know using the GitHub issues tracker.
Shr costs money to run, not my time (I donate that for free) but domains, hosting and more. Any help is appreciated... Donate to support Shr
Thanks to Fastly for providing the CDN services.