Détail du package

web-social-share

peterpeterparker1.4kMIT10.0.0

A Web Component to share urls and text on social networks

social, share, facebook, instagram

readme

Web Social Share

Web Social Share is a Web Component to share urls and text.

version npm

Table of contents

Goals

The idea behind this web component was to add a "social share" feature to Progressive Web Apps (pwa).

Nowadays, the Web Share API is supported by most recent mobile OS.

Even though, it may remain interesting to use such a component for the desktop version of web apps.

Features

The component present a dialog which contains the sharing options you selected.

Following providers or targets are currently supported:

Installation

$ npm install web-social-share

Integration

This Web Component is developed with Stencil.

The Stencil documentation provide examples of Javascript and framework integration for Angular, React, Vue and Ember.

NodeJS

You can either import the class directly or the defineCustomElement helper to register the web component to the CustomElementRegistry:

import { WebSocialShare, defineCustomElement } 'web-social-share';

With that you can extend the component, e.g.:

class WebSocialExtended extends WebSocialShare {
  render () {
    // custom render behavior
  }
}

or just register it to the CustomElementRegistry and use it in your application, e.g.:

defineCustomElement()

Getting Started

The Web Social Share Component can be use like following:

<web-social-share show={true} share={options}></web-social-share>

Both show and share are mandatory.

show

Trigger the display, or close, of the action sheet which contains the social-share options.

show is a boolean parameter

share

For details about them you could have a look to the interface WebSocialShareInput located under folder src/types/web-social-share/.

share is a property of type WebSocialShareInput.

Note: share is an object. Therefore, it has to be parsed with JavaScript if you use the component in a vanilla Javascript application.

Example

For example, if you would like to allow your users to share a website through Facebook and Twitter, you could define basic options like following:

const share = {
    config: [{
          facebook: {
            socialShareUrl: 'https://peterpeterparker.io'
          }
        },{
          twitter: {
            socialShareUrl: 'https://peterpeterparker.io'
          }
    }]
};

If you would like to display the action default name, you could extend your configuration using the attribute displayNames like the following:

const share = {
    displayNames: true,
    config: [{
          facebook: {
            socialShareUrl: 'https://peterpeterparker.io'
          }
        },{
          twitter: {
            socialShareUrl: 'https://peterpeterparker.io'
          }
    }]
};

Worth to notice, you could also provide your own custom brand name, for example in case you would translate the word Email, for example:

const share = {
    displayNames: true,
    config: [{
          email: {
            brandName: 'E-Mail-Adresse',
            socialShareTo: 'me@outlook.com',
            socialShareBody: 'https://peterpeterparker.io'
          }
    }]
};

Slots

Slots have to be used to display the icon or text for your actions.

The available slots are: facebook, twitter, pinterest, linkedin, reddit, email, copy, hackernews, whatsapp and telegram.

These are sorted according the order of your configuration.

NOTE: Slot names MUST be lower case.

<web-social-share show="false">
    <i class="fab fa-reddit" slot="reddit" style="color: #cee3f8;"></ion-icon>
</web-social-share>

const share = [{
    reddit: {
      socialShareUrl: 'https://peterpeterparker.io'
    }
  }
}];

Note that you may have to add the style="display: block" on the slotted elements, notably if you would use <ion-icon/>, for them to be shown.

Styling your icons

The style of your icons is up to you and have to be applied on the icons provided as slots.

For example:

<web-social-share show="false">
    <i class="fab fa-twitter" slot="twitter" style="color: #00aced; width: 1.4rem;"></i>
</web-social-share>

Theming

Checkout the auto-generated readme.md for the list of customizable CSS variables.

Events

The component trigger an event closed when the modal is close. It is emitted regardless if the user shared or not aka "just" closed it.

@Event() closed<void>();

Typically, this use case can be used to post process the data you pass to the component. For example, I use a store to handle these and listen to the event to clean it afterwards.

Fallback and detection

This component is a dumb component. It does not proceed detection or fallback to anything in case one of the share options would not be supported by the device or browser where the component is used.

For example, the share options "Copy (to clipboard)" use the Web Api Clipboard.writeText() which might not be supported. Anyway the action will be displayed and if used by the user, nothing will happen.

Web Share API

When I develop web apps I generally develop a mixed solution between Web Share API and this component. If the Web Share API is supported, and maybe sometimes in combination to detecting desktop or mobile, I use the browser API. If not supported, I fallback on this component.

If interested to implement such a solution, check out the blog post I published about it

Showcase

A showcase is available at https://websocialshare.com

The above showcase is the www folder of this project deployed in Firebase. If you clone the repository you could run it locally using npm run start.

Credits

I didn't want to reinvent the wheel when it comes to the sharing actions themselves. Therefore, I forked the features of angular-socialshare. Kudos to 45kb :+1:

License

MIT © David Dal Busco

changelog

9.1.0 (2022-09-04)

Build

  • improve support for vite

9.0.0 (2022-07-30)

Features

  • share to DSCVR
  • per default, open share link in new window (*)
  • new option openWindowTarget to set another target to open the url than _blank (*)

(*) except "email" and "copy" targets

8.1.0 (2022-07-29)

Features

Fix

  • improve mobile detection to share Twitter or Whatsapp accordingly

Refactor

  • move utils to providers

Build

  • bump dev dependencies
  • remove pretty-quick and husky
  • format code (max 100 chars instead of 140)

8.0.1 (2021-12-21)

Fix

  • add manual support for ESM in NodeJS (#55)

8.0.0 (2021-12-06)

Breaking Changes

  • custom element entry modified (Stencil dist-custom-elements instead of deprecated dist-custom-elements-bundle | #48)
import 'web-social-share/dist/components/web-social-share';

7.3.1 (2021-12-05)

Fix

  • undefined Reddit title if socialShareText not provided

7.3.0 (2021-12-05)

Features

  • improve user feedback on click with a ripple effect

Fix

  • module entry point (#48)

Build

  • bump dev dependencies
  • package-lock version v2 - npm v7

7.2.0 (2021-06-06)

Build

  • output custom elements as a single bundle (dist-custom-elements-bundle)
  • bump dependencies

7.1.0 (2021-02-21)

Features

  • share to Telegram

7.0.0 (2021-01-24)

Breaking changes

  • drop support of IE11, Edge 16-18 and Safari 10

Features

  • update dependencies (Stencil v2)
  • code refactored to hopefully make it easier to read
  • isMobile detection improved

Chore

  • use prettier to format code

6.4.1 (2020-08-15)

Chore

  • update Stencil v1.17.x

6.4.0 (2020-03-20)

Features

  • update dependencies

6.3.0 (2020-02-07)

Features

  • add "Hacker News"
  • exposes interfaces in bundle
  • improve UI (max. width 540px instead of 500px, box-shadow and border-radius for the container)
  • refactor duplicate actions functions code
  • GitHub action to deploy demo website to Firebase
  • transfer GitHub repo to my self (peterpeterparker)

6.2.0 (2019-07-30)

Fix

  • refactor component to avoid the need to move slots in order to be compatible with Font Awesome (CSS import) (#18)

Special note

There isn't any "breaking changes" but according the type and how you style your icons, you might need to change some CSS variables to keep styles in terms of size the same way.

For example I had to change width to font-size in the index.html of the component.

From:

<i class="fab fa-twitter" slot="twitter" style="color: #00aced; width: 1.4rem;"></i>

To:

<i class="fab fa-twitter" slot="twitter" style="color: #00aced; font-size: 1.6rem;"></i>

6.1.0 (2019-07-12)

Features

  • CSS variable to set the base zIndex of the component

6.0.0 (2019-06-11)

Features

  • add "Copy (to clipboard)" support (#17)

5.1.0 (2019-06-05)

Libs

  • Stencil One

5.0.0 (2019-02-17)

Breaking changes

  • transform component to a shadowed Web Component (#14)

Notes:

As of version v5.0.0 this Web Component will be shadowed. Therefore, you will have to use slots to inject the icons for the sharing actions and will have to style these in order to apply for example the proper colors. The README of the component has been updated to reflect these changes.

If you would miss variables to style the component, ping me, I would be happy to add more options.

4.0.2 (2019-02-02)

Fix

  • incorrect URL when WhatsApp socialShareText is empty (#13, thx @fmendoza for the PR 👍)

4.0.1 (2019-01-29)

Fix

4.0.0 (2019-01-29)

Features

  • add WhatsApp support (#11)
  • add a CSS4 variable to customize the height of the action sheet on small devices (#12)

3.0.1 (2019-01-26)

Fix

  • the move slot method might be executed before the sub-component is loaded (#9)

3.0.0 (2019-01-26)

Features

  • add pseudo slots to the social share actions (#8)
  • ability to customize / override rendered names (#7)

Website

  • a bit of styling and text editing on the index.html file for the website

Libs

  • update libs

2.1.4 (2018-08-24)

Fix

2.1.3 (2018-08-23)

Fix

  • fix action sheet not closed on middle click (#5)

2.1.2 (2018-08-23)

Fix

  • use full page height (#4)

2.1.1 (2018-08-23)

Fix

2.1.0 (2018-08-19)

Libs

  • update Stencil (#2)
  • target ES2017 (#3)

2.0.0 (2018-08-12)

Features

  • breaking changes: Update the component in order to be compatible with Ionic v4 and Angular v6

Important note

I did not gave a try but this version might no be suitable anymore for Ionic v3 and Angular v5. If you are using the web-social-share component in such apps, you might better not upgrade and stick to the component version v1.1.0