Bento

Bento Twitter

<head>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <script
      type="module"
      async
      src="https://cdn.ampproject.org/bento.mjs"
    ></script>
    <script nomodule src="https://cdn.ampproject.org/bento.js"></script>
    <script
      type="module"
      async
      src="https://cdn.ampproject.org/v0/bento-twitter-1.0.mjs"
    ></script>
    <script
      nomodule
      async
      src="https://cdn.ampproject.org/v0/bento-twitter-1.0.js"
    ></script>
    <link
      rel="stylesheet"
      type="text/css"
      href="https://cdn.ampproject.org/v0/bento-twitter-1.0.css"
    />
    <style>
      bento-twitter {
        width: 375px;
        height: 472px;
      }
    </style>
    <style>
      body {
        background: #ecf1f3;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    </style>
</head>

<body>

    <bento-twitter
      id="my-tweet"
      style="width: 375px; height: 720px"
      data-tweetid="1397995435386679302"
    >
    </bento-twitter>
</body>

嵌入 Twitter 議題或動態等內容。

將 bento-twitter 作為網路組件或 React 函數式組件使用

↓ 網路組件 ↓ React / Preact

網路組件

您必須包含每個 Bento 組件所需的 CSS 函式庫,以確保正確載入,並在加入自訂樣式之前進行載入。或使用可內嵌取得的輕量級預先升級樣式。請參閱 版面與樣式

透過 npm 匯入

npm install @bentoproject/twitter
import {defineElement as defineBentoTwitters} from '@bentoproject/twitter';
defineBentoTwitters();

透過 <script> 載入

<script type="module" src="https://cdn.ampproject.org/bento.mjs" crossorigin="anonymous"></script>
<script nomodule src="https://cdn.ampproject.org/bento.js" crossorigin="anonymous"></script>
<script type="module" src="https://cdn.ampproject.org/v0/bento-twitter-1.0.mjs" crossorigin="anonymous"></script>
<script nomodule src="https://cdn.ampproject.org/v0/bento-twitter-1.0.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.ampproject.org/v0/bento-twitter-1.0.css" crossorigin="anonymous">

範例

<head>

<script
      type="module"
      async
      src="https://cdn.ampproject.org/bento.mjs"
    ></script>
    <script nomodule src="https://cdn.ampproject.org/bento.js"></script>
    <script
      type="module"
      async
      src="https://cdn.ampproject.org/v0/bento-twitter-1.0.mjs"
    ></script>
    <script
      nomodule
      async
      src="https://cdn.ampproject.org/v0/bento-twitter-1.0.js"
    ></script>
    <link
      rel="stylesheet"
      type="text/css"
      href="https://cdn.ampproject.org/v0/bento-twitter-1.0.css"
    />
    <style>
      bento-twitter {
        width: 375px;
        height: 472px;
      }
    </style>
</head>

<body>

<bento-twitter id="my-tweet" data-tweetid="885634330868850689"></bento-twitter>
</body>

版面與樣式

每個 Bento 組件都有一個必須包含的小型 CSS 函式庫,以確保正確載入,且沒有 內容變動。由於基於順序的特定性,您必須手動確保在自訂樣式之前包含樣式表。

<link
rel="stylesheet"
type="text/css"
href="https://cdn.ampproject.org/v0/bento-twitter-1.0.css"
/>

或者,您也可以讓輕量級的預升級樣式內嵌提供

<style>
bento-twitter {
display: block;
overflow: hidden;
position: relative;
}
</style>

容器類型

bento-twitter 組件有一個已定義的版面大小類型。為確保組件正確呈現,務必透過所需的 CSS 版面 (例如定義為 heightwidthaspect-ratio 或其他此類屬性所定義的版面),將大小套用至組件及其子代元素 (投影片)。

bento-twitter {
height: 100px;
width: 100%;
}

屬性

data-tweetid / data-momentid / data-timeline-source-type (必填)推文或動態的 ID,或應顯示時間軸的來源類型。在像 https://twitter.com/joemccann/status/640300967154597888 這樣的 URL 中,640300967154597888 是推文 ID。在像 https://twitter.com/i/moments/1009149991452135424 這樣的 URL 中,1009149991452135424 是動態 ID。有效的時間軸來源類型包括:profilelikeslistcollectionurlwidget
data-timeline-* (選填)顯示時間軸時,除了 timeline-source-type 之外,還需要提供進一步的參數。例如,data-timeline-screen-name="amphtml" 搭配 data-timeline-source-type="profile" 會顯示 AMP Twitter 帳戶的時間軸。有關可用參數的詳細資訊,請參閱 Twitter JavaScript Factory Functions 指南 中的「時間軸」區段。
data-* (選填)依設定的 data- 屬性,您可以指定推文、精選貼文或時間軸外觀的選項。例如,data-cards="hidden" 會停用 Twitter 卡片。關於可用選項的詳細內容,請參閱 Twitter 的說明文件,針對推文針對精選貼文,以及 針對時間軸
標題 (選用)為元件定義 title 屬性。預設為 Twitter

互動性和 API 使用

以程式變更任何屬性值,都會自動更新該元素。例如,透過 data-tweetid 變更推文 ID,就會自動載入新的推文

<head>

<script
      type="module"
      async
      src="https://cdn.ampproject.org/bento.mjs"
    ></script>
    <script nomodule src="https://cdn.ampproject.org/bento.js"></script>
    <script
      type="module"
      async
      src="https://cdn.ampproject.org/v0/bento-twitter-1.0.mjs"
    ></script>
    <script
      nomodule
      async
      src="https://cdn.ampproject.org/v0/bento-twitter-1.0.js"
    ></script>
    <link
      rel="stylesheet"
      type="text/css"
      href="https://cdn.ampproject.org/v0/bento-twitter-1.0.css"
    />
    <style>
      bento-twitter {
        width: 375px;
        height: 472px;
      }
    </style>
</head>

<body>

<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');

        // set up button actions
        document.querySelector('#change-tweet').onclick = () => {
          twitter.setAttribute('data-tweetid', '495719809695621121');
        };
      })();
    </script>
</body>

Preact/React 元件

透過 npm 匯入

npm install @bentoproject/twitter
import React from 'react';
import {BentoTwitter} from '@bentoproject/twitter/react';
import '@bentoproject/twitter/styles.css';

function App() {
return <BentoTwitter tweetid="1356304203044499462"></BentoTwitter>;
}

版面與樣式

容器類型

BentoTwitter 元件已定義配置大小類型。為了確保元件正確呈現,請務必透過想要的 CSS 配置 (例如此一配置以 heightwidthaspect-ratio 或其他類似屬性定義),對元件及其直接子項 (投影片) 套用大小。這些可以內嵌套用

<BentoTwitter
style={{width: 300, height: 100}}
tweetid="1356304203044499462"
>
</BentoTwitter>

或者透過 className

<BentoTwitter
className="custom-styles"
tweetid="1356304203044499462"
>
</BentoTwitter>
.custom-styles {
height: 100px;
width: 100%;
}

屬性

tweetid / momentid / timelineSourceType (必要)推文或動態的 ID,或應顯示時間軸的來源類型。在像 https://twitter.com/joemccann/status/640300967154597888 這樣的 URL 中,640300967154597888 是推文 ID。在像 https://twitter.com/i/moments/1009149991452135424 這樣的 URL 中,1009149991452135424 是動態 ID。有效的時間軸來源類型包括:profilelikeslistcollectionurlwidget
card / conversations (選用)除了 tweetid 以外,在顯示推文時,還可以提供其他參數。例如,cards="hidden"conversation="none" 合用會顯示沒有其他縮圖或留言的推文。
limit (選用)除了 moment 以外,在顯示精選貼文時,還可以提供其他參數。例如,limit="5" 會顯示一張附有多達五個卡片的內嵌精選貼文。
timelineScreenName / timelineUserId (選用)除了 timelineSourceType 以外,在顯示時間軸時,還可以提供其他參數。例如,timelineScreenName="amphtml"timelineSourceType="profile" 合用會顯示 AMP Twitter 帳戶時間軸。
options (選用)依將物件傳遞給 options 屬性,您可以指定推文、精選貼文或時間軸外觀的選項。關於可用選項的詳細內容,請參閱 Twitter 的說明文件,針對推文針對精選貼文,以及 針對時間軸。注意:當傳遞 `options` 屬性時,務必最佳化或記憶化該物件
const TWITTER_OPTIONS = {
  // make sure to define these once globally!
};
function MyComponent() {
  // etc
  return (
    <Twitter optionsProps={TWITTER_OPTIONS} />
  );
}
標題 (選用)定義元件 iframe 的 title。預設為 Twitter
更多詳細資料