Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/estevanmaito/sharect

πŸ”— A lightweight JavaScript library to let users share their text selections to social networks.
https://github.com/estevanmaito/sharect

facebook-share javascript-library medium selection share social-network text twitter-share

Last synced: 5 days ago
JSON representation

πŸ”— A lightweight JavaScript library to let users share their text selections to social networks.

Awesome Lists containing this project

README

        


Sharect






A **lightweight** (8kb, 2.9kb gziped) JavaScript library to let people share their text selections to social networks. (Yes, like Medium)

[Watch it live.](https://estevanmaito.github.io/sharect)

### Get started with 2 lines

```javascript

Sharect.init()
```

## Table of contents

- [Demo](https://estevanmaito.github.io/sharect)
- [Quick Start](#quick-start)
- [Browser Support](#browser-support)
- [Documentation](#documentation)
- [Usage](#usage)
- [Adding more social icons](#adding-more-social-icons)
- [Upgrade to 2.0](#upgrade)
- [Contributing](#contributing)

## Quick Start

### Install (choose one)

- `npm install sharect`
- [Download from Github](https://github.com/estevanmaito/sharect/releases/download/2.0.0/sharect.js)
- CDN ``

### Initialize

```javascript
Sharect.init();
```

## Browser Support

![Internet Explore 9+](/docs/browsers/ie.png)
![Chrome 4+](/docs/browsers/chrome.png)
![Firefox 3.5+](/docs/browsers/firefox.png)
![Safari 5.1+](/docs/browsers/safari.png)
![Opera 10+](/docs/browsers/opera.png)
![Edge 12+](/docs/browsers/edge.png)

## Documentation

The complete API is composed by 3 methods:

- [.config](#config)
- [.appendCustomShareButtons](#appendcustomsharebuttons)
- [.init](#init)

### .config

**Optional**. Configure the core library. Expects an object as argument with any of the following options.

Property | Default | Type | Description
-- | -- | -- | --
`twitter` | `true` | `boolean` | If Twitter should be shown in tooltip
`facebook` | `true` | `boolean` | If Facebook should be shown in tooltip.
`twitterUsername` | `""` | `string` | The username that should be cited when shared. Ex: `estevanmaito`
`backgroundColor` | `#333333` | `string` | The background color of the tooltip. Can be any valid CSS color name
`iconColor` | `#FFFFFF` | `string` | The color of the icons in the tooltip. Can be any valid CSS color name
`selectableElements` | `['body']` | `array` | Define the elements that can be selected, including it's children. **It expects a valid selector string** like `['p', '.article', '#main']`

Example

```javascript
Sharect.config({
twitterUsername: 'estevanmaito',
backgroundColor: '#C53364'
}).init()
```

Result

![result](/docs/simple-pink.png)

### .appendCustomShareButtons

**⚠️DISCLAIMER⚠️: If you don't plan to share on any social media other than Facebook and Twitter (covered by the `.config` method above) you can safely ignore this method.**

**Optional**. Extends the core social buttons. Expects an array of object(s) as argument containing an `icon` and a `url`.

#### `icon`

`icon` must be a string containing a monochromatic 24x24px SVG.

#### `url`

`url` must be a string containing the sharing URL (a list of options can be found [in this project](https://github.com/bradvin/social-share-urls)). Note that `PAGE_URL` and `TEXT_SELECTION` are required placeholders that will be replaced by the library.

Example

```javascript
Sharect.appendCustomShareButtons([{
icon: '',
url: 'https://api.whatsapp.com/send?text=TEXT_SELECTION%20PAGE_URL'
}]).init()
```

Result

![result](/docs/complete.png)

### .init

**Required**. Initialize the library.

## Usage

### Smallest example

```html

Sharect.init();

```

### Complete core API

```html

Sharect.config({
facebook: true,
twitter: true,
twitterUsername: 'estevanmaito',
backgroundColor: '#C53364',
iconColor: '#fff',
selectableElements: ['p']
}).init();

```

## Adding more social icons

You can find a list of social share options [in this project](https://github.com/bradvin/social-share-urls), and below are some common social networks so you can save time or use it as reference.

Whatsapp

```javascript
{
icon: '',
url: 'https://api.whatsapp.com/send?text=TEXT_SELECTION%20PAGE_URL'
}
```

Reddit

```javascript
{
icon: '',
url: 'https://reddit.com/submit?url=PAGE_URL&title=TEXT_SELECTION'
}
```

## Upgrade

If you are coming from 1.x, please refer to our [guide on upgrading](/UPGRADING.md).

## Contributing

Please, read the [CONTRIBUTING.md](CONTRIBUTING.md).