amp-social-share
Introduction
The amp-social-share
component 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://amp.dev/documentation/examples/components/amp-social-share/index.html">
Styling
Custom styling for <amp-social-share>
.
<style amp-custom>
:root {
--color-primary: #005AF0;
--color-text-light: #fff;
}
/* a custom sharing icon */
amp-social-share.custom-style {
background-color: #008080;
background-image: url('https://raw.githubusercontent.com/google/material-design-icons/master/social/1x_web/ic_share_white_48dp.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
/* blue icons with rounded corners */
amp-social-share.rounded {
border-radius: 50%;
background-size: 60%;
color: var(--color-text-light);
background-color: var(--color-primary);
}
</style>
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
.
<amp-social-share type="email" aria-label="Share by email"></amp-social-share>
<amp-social-share type="facebook" aria-label="Share on Facebook" data-param-app_id="254325784911610"></amp-social-share>
<amp-social-share type="linkedin" aria-label="Share on LinkedIn"></amp-social-share>
<amp-social-share type="pinterest" aria-label="Share on Pinterest" data-param-media="https://amp.dev/static/samples/img/amp.jpg"></amp-social-share>
<amp-social-share type="tumblr" aria-label="Share on Tumblr"></amp-social-share>
<amp-social-share type="twitter" aria-label="Share on Twitter"></amp-social-share>
<amp-social-share type="whatsapp" aria-label="Share on WhatsApp"></amp-social-share>
<amp-social-share type="line" aria-label="Share on Line"></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.
<amp-social-share type="linkedin" width="40" height="40" data-param-text="Check out these AMP Examples!" data-param-url="https://amp.dev/documentation/examples/" data-param-attribution="AMPhtml" aria-label="Share on LinkedIn">
</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 usingbackground-image
), set tono-repeat
.background-position
(when usingbackground-image
), set tocenter
.background-size
(when usingbackground-image
), set tocontain
.
<amp-social-share type="linkedin" aria-label="Share on LinkedIn" class="custom-style">
</amp-social-share>
Round icons
For example, you can tweak the background size, color and border radius to achieve a rounded look where all icons have use the same color scheme. Reference the .rounded
css class from the Styling section above for the effect below.
<amp-social-share class="rounded" aria-label="Share by email " type="email" width="48" height="48"></amp-social-share>
<amp-social-share class="rounded" aria-label="Share on Facebook" type="facebook" data-param-app_id="254325784911610" width="48" height="48"></amp-social-share>
<amp-social-share class="rounded" aria-label="Share on LinkedIn" type="linkedin" width="48" height="48"></amp-social-share>
<amp-social-share class="rounded" aria-label="Share on Pinterest" type="pinterest" data-param-media="https://amp.dev/static/samples/img/amp.jpg" width="48" height="48"></amp-social-share>
<amp-social-share class="rounded" aria-label="Share on Tumblr" type="tumblr" width="48" height="48"></amp-social-share>
<amp-social-share class="rounded" aria-label="Share on Twitter" type="twitter" width="48" height="48"></amp-social-share>
<amp-social-share class="rounded" aria-label="Share on WhatsApp" type="whatsapp" width="48" height="48"></amp-social-share>
<amp-social-share class="rounded" aria-label="Share on Line" type="line" width="48" height="48"></amp-social-share>
Sharing media via Pinterest
Use data-param-media
to share media via Pinterest.
<amp-social-share type="pinterest" aria-label="Share on Pinterest" data-param-media="https://amp.dev/static/samples/img/amp.jpg"></amp-social-share>
Native share dialog
The system
type will display a native share UI if the user is viewing the AMP document using a browser that supports the Web Share API.
Here's what it looks like in Chrome on Android:
<amp-social-share type="system" aria-label="Share"></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"`:
如果此页面上的说明未能涵盖您的所有问题,欢迎与其他 AMP 用户取得联系,讨论您的具体用例。
前往 Stack Overflow 一项无法解释的功能?AMP 项目强烈鼓励您参与并做出贡献!我们希望您能成为我们开放源代码社区的持续参与者,但我们也欢迎您对所热衷问题做出一次性贡献。
编辑 GitHub 上的示例-
Written by @paul-matthews