0.3.1 • Published 6 years ago

sociare v0.3.1

Weekly downloads
5
License
-
Repository
github
Last release
6 years ago

Sociare Build Status Coverage Status

Super simple customizable share buttons with counts.

Installation

Sociare can be installed with npm

$ npm install --save sociare

or with Bower

$ bower install -S sociare

or you can download it directly.

Prerequisites

Sociare comes bundled with only 1 dependency, Babel runtime, which enables key ES6 features that Sociare uses. Once browser support reaches a point where this runtime is no longer needed, it will be removed from the package.

Browser support

  • Google Chrome (at least latest 2)
  • Safari (at least latest 2)
  • Firefox (at least latest 2)
  • Edge 12+
  • Internet Explorer 9+

Usage Example

var sociare = new Sociare(document.getElementById('share-container'), {
	countUrl: 'http://mysite.com/share-counts',
	buttonClass="btn btn-{network}",
	buttonTemplate: "Share on {network} <span class="count">{count}</span>",
	buttons: [
		'twitter',
		'facebook',
		{
			type: 'pinterest',
			template: 'Pin it! <span class="count">{count}</span>'
		}
	]
});

sociare.render();

API Reference

New instances of Sociare require a DOM element, within which the buttons will be rendered.

var sociare = new Sociare(elem, [config]);

Configuration can be passed in the constructor (like shown above), but also be defined globally, at window.SociareConfig. Configuration inheritance is Global Config > Default Config > Button-Specific Config (See config.options below).

Once an instance has been configured, simply call .render() on it to have it fetch counts (if specified) and render the buttons.

sociare.render();

String Tokens

In any of the button configuration options (both default and button-specific), there are special tokens you can include that will get interpolated when the buttons are rendered. These tokens are:

NameDescription
{network}Name of the network (facebook, twitter, pinterest, etc)
{count}# of times URL has been shared on that network

Configuration Parameters

NameTypeDefaultDescription
urlStringwindow.location.hrefThe URL to share
getCountsBooleantrueIndicates whether Sociare should request counts for config.url
countUrlStringURL to request counts from. Required if config.getCounts is true. See Handling Count Requests for information on what Sociare will expect to be returned
noQueryCountBooleanfalseIf true, adds query string to config.countUrl to indicate that query strings on config.url should be ignored when retrieving counts
buttonTagString"a"Default button tagName
buttonIdString""Default button id. If specified here, should use {network} to keep ids unique
buttonClassString"sociare sociare-{network}"Default button classes
buttonAttrsObject{}Additional attributes on button elements. Keys map to attribute names, values to attribute values
buttonTemplateString"Share on {network} - {count}"Default button template
buttonPreHookFunctionundefinedFunction to call immediately before opening popup. Is passed a callback, which will prevent popup from opening if called with an error, and the name of the network selected. Async functions run the risk of resulting in a blocked popup.
buttonPostHookFunctionundefinedFunction to call immediately after opening popup. Is passed the name of the network selected.
buttonsArrayString/Object[]Buttons to be rendered. Can be a string of the network name to use default configuration, or a button-specific configuration object. Available networks are "facebook", "twitter", "pinterest", "linkedin", and "googleplus"
twitterExtrasObject{}Default extra options for Twitter buttons. See Twitter Extras for full list of options.
pinterestExtrasObject{}Default extra options for Pinterest buttons. See Pinterest Extras for full list of options.
linkedinExtrasObject{}Default extra options for Twitter buttons. See LinkedIn Extras for full list of options.

Button-Specific Configuration

Instead of just passing the network name as a string, you can fine-tune any button made with Sociare by instead passing a configuration object for it in the buttons array. Option names listed below:

NameTypeDescription
urlStringURL to share
typeStringNetwork type. Available networks are "facebook", "twitter", "pinterest", "linkedin", and "googleplus"
tagStringButton element tagName
idStringButton element ID
classStringButton element classes
attrsObjectAdditional attributes on button element. Keys map to attribute names, values to attribute values
templateStringString template for innerHTML of button element
preHookFunctionFunction to call immediately before opening popup. Is passed a callback, which will prevent popup from opening if called with an error. Async functions run the risk of resulting in a blocked popup.
postHookFunctionFunction to call immediately after opening popup.
extrasObjectAdditional network options (only available in twitter, pinterest, and linkedin types). See Extras

Extras

Some button types (twitter, pinterest, and linkedin) have additional optional configuration that will alter how the URL is shared. See sections below for each networks available extra configuration. All extra values

Twitter Extras

NameDescription
textPre-filled tweet text. Users will still have the option to change this before sharing
viaA Twitter username to associate tweet. Will append "via @{username}" to end of tweet.
hashtagsComma delimited list of hashtags to append to the tweet. Do not include the "#" character

Pinterest Extras

NameDescription
mediaURL of image to use when pinned to users boards
descriptionCaption to be used on pin

LinkedIn Extras

NameDescription
titleTitle that will appear on share card
descriptionDescription to appear on share card
sourceSource of the content (such as your websites name)

Handling Count Requests

Sociare requires a url to be provided if you would like to use share counts on the buttons it generates. The data returned from this URL should be JSON formatted as network_name: count, for example:

{
	"facebook": 250,
	"twitter": 125
}

When Sociare makes this request, it applies the following query parameters to the URL to assist in determining exactly what data is needed:

  • url - URL for which counts should be collected
  • networks - Which networks Sociare requires counts from
  • omitQuery - Whether query strings on the url should be included when fetching counts

Getting these counts is a real pain, so to make this easier, we've set up a simple NodeJS utility you can use to fetch these counts and output this data.

Contributing

Pull requests for bug-fixes and additions of other services are always welcome! Please be sure to include any tests for new code & follow the current coding style as best you can.

You can run the test suite with the following command:

$ npm test

License

Sociare is distributed under the MIT license.

0.3.1

6 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.8

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago