Git Product home page Git Product logo

sharon's Introduction

Sauce Test Status

Sharon

A lightweight and modular social sharing library:

  • a toolkit to build your own share buttons;
  • supports 14 sharing platforms;
  • gzipped size is 1.71 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(function (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:

var sharon = require('sharon');

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

var facebook = require('sharon/facebook');
var twitter = require('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 own 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
Google+ sharon.plus Yes Reference
LinkedIn sharon.linkedin Yes 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
WhatsApp sharon.whatsapp
XING sharon.xing Reference

This table also shows which of the 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.

Examples Share the current page:
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.

Examples Get the share popup URL for the current page:
var link = sharon.twitter.href();

With a custom title:

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

For example.com:

var link = sharon.twitter.href('http://example.com');

For example.com with a custom title:

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

Examples Share count for the current page:
sharon.facebook.count(function (err, count) {
  if (err) throw err;
  console.log(count);
});

For example.com:

sharon.facebook.count('http://example.com', function (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 the features is different for the most of the sharing platforms. To find them out check their documentation, links to which are 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's automatically translated into a one that corresponds to a chosen platform.

More examples

Poor man's tweet button

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

Angular

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

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

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

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

React component

class LinkedInShareButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = {href: sharon.linkedin.href()};

    sharon.linkedin.count((err, count) => {
      if (err) throw err;
      this.setState({count});
    });
  }

  share(event) {
    event.preventDefault();
    sharon.linkedin();
  }

  render() {
    return <a onClick={this.share} href={this.state.href}>Share on LinkedIn {this.state.count}</a>;
  }
}

:heart:

sharon's People

Contributors

borodean avatar maqsim avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.