amp-social-share

Edit on Github
Open in Playground

Introduction

The Social Share extension provides a common interface for share buttons, styled to complement each other.

Setup

Import the amp-social-share component in the header.

<script async custom-element="amp-social-share" src="https://cdn.ampproject.org/v0/amp-social-share-0.1.js"></script>

Ensure you've configured your canonical URL.

<link rel="canonical" href="https://ampbyexample.com/components/amp-social-share/">

Origin trial tokens for ampbyexample.com and *.cdn.ampproject.org. (Needed for type=system; see below.)

<meta http-equiv="origin-trial" data-feature="Web Share" data-expires="2017-04-04" content="Ajcrk411RcpUCQ3ovgC8le4e7Te/1kARZsW5Hd/OCnW6vIHTs5Kcq1PaABs7SzcrtfvT0TIlFh9Vdb5xWi9LiQsAAABSeyJvcmlnaW4iOiJodHRwczovL2FtcGJ5ZXhhbXBsZS5jb206NDQzIiwiZmVhdHVyZSI6IldlYlNoYXJlIiwiZXhwaXJ5IjoxNDkxMzM3MDEwfQ==">

Basic Usage

Embed the amp-social-share widget choosing a share type from the supported types.

amp-social-share with type="facebook" requires to specify the facebook app id via data-attribution.

Example

  <amp-social-share type="email"></amp-social-share>
  <amp-social-share type="facebook"
    data-param-app_id="254325784911610"></amp-social-share>
  <amp-social-share type="gplus"></amp-social-share>
  <amp-social-share type="linkedin"></amp-social-share>
  <amp-social-share type="pinterest"></amp-social-share>
  <amp-social-share type="tumblr"></amp-social-share>
  <amp-social-share type="twitter"></amp-social-share>
  <amp-social-share type="whatsapp"></amp-social-share>

Configuration

Embed the amp-social-share widget choosing a type, then configure the actions.

  • width, default 60px.
  • height, default 44px.
  • data-param-text is the text to include in the share.
  • data-param-url is the URL to share, current URL by default.
  • data-param-attribution is where the share is attributed to.

All data-param-* prefixed attributes will be turned into URL parameters and passed to the share endpoint.

Example

<amp-social-share type="linkedin"
  width="40"
  height="40"
  data-param-text="Check out these AMP Examples!"
  data-param-url="https://ampbyexample.com/"
  data-param-attribution="AMPhtml">
</amp-social-share>

Custom Style

Whenever you want to provide your own style, use CSS properties within the <style amp-custom> element (Modify the presentation). This will make sure the image stays responsive and centered, and the default styling is overwritten.

  • background-color, if you want the element to be a different color.
  • background-image, provide another image if you want to change the icon.
  • background-repeat (when using background-image), set to no-repeat.
  • background-position (when using background-image), set to center.
  • background-size (when using background-image), set to contain.

Example

<amp-social-share type="linkedin"
  class="custom-style">
</amp-social-share>

Sharing Media via Pinterest

Use data-param-media to share media via Pinterest.

Example

  <amp-social-share type="pinterest"
    data-param-media="https://ampbyexample.com/img/amp.jpg"></amp-social-share>

Native Share Dialog (Chrome on Android)

If a couple of conditions are satisfied, the system type will display a native share UI. (That is, exactly the same UI that's available to native applications, reflecting the user's sharing preferences, the applications they have installed, and so on.)

The conditions are:

  • The user must be viewing the AMP using Chrome on Android. (The only supported browser as of February 2017.)
  • An appropriate token (origin-specific) must be embedded on the site to activate the feature. (Apply for a token; more information about the process.)

For this example, we've embedded the tokens via <meta> elements in the document <head>.

Here's what it looks like if conditions are right:

Example

  <amp-social-share type="system"></amp-social-share>

This type supports one option: if data-mode="replace" is specified, then all other share icons will be suppressed. (That is, the only share icon the user will see is the system share icon.)

Since it's not possible to demo this feature within the context of this example, here's some screenshots to illustrate the difference.

Without data-mode="replace":

With data-mode="replace":