@ampproject/amp-twitter v1.2110011758.0
$category@: social formats:
- websites teaser: text: Displays a Twitter Tweet or Moment. experimental: true bento: true
Bento Twitter
Behavior
The Bento Twitter component allows you to embed a Tweet or Moment. Use it as a web component <bento-twitter>
, or a Preact/React functional component <BentoTwitter>
.
Web Component
You must include each Bento component's required CSS library to guarantee proper loading and before adding custom styles. Or use the light-weight pre-upgrade styles available inline. See Layout and style.
The examples below demonstrate use of the <bento-twitter>
web component.
Example: Import via npm
example preview="top-frame" playground="false"
Install via npm:
npm install @ampproject/bento-twitter
import '@ampproject/bento-twitter';
Example: Include via <script>
example preview="top-frame" playground="false"
<head>
<script src="https://cdn.ampproject.org/custom-elements-polyfill.js"></script>
<!-- These styles prevent Cumulative Layout Shift on the unupgraded custom element -->
<style data-bento-boilerplate>
bento-twitter {
display: block;
overflow: hidden;
position: relative;
}
</style>
<!-- TODO(wg-bento): Once available, change src to bento-twitter.js -->
<script async src="https://cdn.ampproject.org/v0/amp-twitter-1.0.js"></script>
<style>
bento-twitter {
width: 375px;
height: 472px;
}
</style>
</head>
<bento-twitter id="my-tweet" data-tweetid="885634330868850689">
</bento-twitter>
<div class="buttons" style="margin-top: 8px;">
<button id="change-tweet">
Change tweet
</button>
</div>
<script>
(async () => {
const twitter = document.querySelector('#my-tweet');
await customElements.whenDefined('bento-twitter');
// set up button actions
document.querySelector('#change-tweet').onclick = () => {
twitter.setAttribute('data-tweetid', '495719809695621121')
}
})();
</script>
Layout and style
Each Bento component has a small CSS library you must include to guarantee proper loading without content shifts. Because of order-based specificity, you must manually ensure that stylesheets are included before any custom styles.
<link rel="stylesheet" type="text/css" href="https://cdn.ampproject.org/v0/amp-twitter-1.0.css">
Alternatively, you may also make the light-weight pre-upgrade styles available inline:
<style data-bento-boilerplate>
bento-twitter {
display: block;
overflow: hidden;
position: relative;
}
</style>
Container type
The bento-twitter
component has a defined layout size type. To ensure the component renders correctly, be sure to apply a size to the component and its immediate children (slides) via a desired CSS layout (such as one defined with height
, width
, aspect-ratio
, or other such properties):
bento-twitter {
height: 100px;
width: 100%;
}
Attributes
Preact/React Component
The examples below demonstrate use of the <BentoTwitter>
as a functional component usable with the Preact or React libraries.
Example: Import via npm
example preview="top-frame" playground="false"
Install via npm:
npm install @ampproject/bento-twitter
import React from 'react';
import { BentoTwitter } from '@ampproject/bento-twitter/react';
import '@ampproject/bento-twitter/styles.css';
function App() {
return (
<BentoTwitter tweetid="1356304203044499462">
</BentoTwitter>
);
}
Layout and style
Container type
The BentoTwitter
component has a defined layout size type. To ensure the component renders correctly, be sure to apply a size to the component and its immediate children (slides) via a desired CSS layout (such as one defined with height
, width
, aspect-ratio
, or other such properties). These can be applied inline:
<BentoTwitter style={{width: '300px', height: '100px'}} tweetid="1356304203044499462">
</BentoTwitter>
Or via className
:
<BentoTwitter className='custom-styles' tweetid="1356304203044499462">
</BentoTwitter>
.custom-styles {
height: 100px;
width: 100%;
}
Props
function MyComponent() { // etc return ( ); }
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago