@bbc/psammead-social-embed

中文文档
3.3.13发布于 2022/03/30Apache-2.0ESM + CJS近 30 天下载 2852

A component that renders a rich social media embed or a fallback.

原始使用文档
文档目录19 节
  1. Description
  2. Supported providers
  3. Installation
  4. Props
  5. Canonical
  6. fallback
  7. skipLink
  8. caption
  9. Usage
  10. Canonical
  11. onRender
  12. AMP
  13. When to use this component
  14. When not to use this component
  15. Accessibility notes
  16. Miscellaneous
  17. Contributing
  18. Code of Conduct
  19. License

psammead-social-embed - Known Vulnerabilities Dependency Status peerDependencies Status Storybook GitHub license npm version PRs Welcome

Description

The SocialEmbed component renders a rich social media embed for a number of supported providers or a fallback containing a link to the source content.

Supported providers

NameValue
Instagraminstagram
Twittertwitter
YouTubeyoutube

Installation

npm install @bbc/psammead-social-embed --save

Props

Canonical

ArgumentTypeRequiredDefaultExample
providerStringYesn/aSee supported providers.
serviceStringYesn/a'news'
oEmbedObjectYesn/aSee @bbc/psammead-oembed.
fallbackObjectYesn/aSee fallback.
skipLinkObjectYesn/aSee skipLink.
captionObjectNonullSee caption.
onRenderFunctionNonull() => console.log('rendered')
### AMP
ArgumentTypeRequiredDefaultExample
providerStringYesn/aSee supported providers.
serviceStringYesn/a'news'
idStringYesn/a'1237210910835392512'
fallbackObjectYesn/aSee fallback.
skipLinkObjectYesn/aSee skipLink.
captionObjectNonullSee caption.

fallback

ArgumentTypeRequiredDefaultExample
textStringYesn/a"Sorry but we're having trouble displaying this content"
linkTextStringYesn/a'View content on %provider_name%'
linkTextSuffixVisuallyHiddenStringNonull', external'
linkHrefStringYesn/a'https://twitter.com/MileyCyrus/status/1237210910835392512'
warningTextStringNonullWarning: BBC is not responsible for third party content
ArgumentTypeRequiredDefaultExample
textStringYesn/a'Skip %provider_name% content'
endTextIdStringYesn/a'skip-%provider%-content'
endTextVisuallyHiddenStringYesn/a'End of %provider_name% content'

caption

ArgumentTypeRequiredDefaultExample
textPrefixVisuallyHiddenStringNonull'Video caption,'
textStringYesn/a'Warning: Third party content may contain adverts'

See accessibility notes for more information.

Note: For your convenience, instances of %provider% and %provider_name% in fallback, skipLink and caption strings will be replaced with the current provider and, where the provider is known, the name of the provider. E.G. youtube and YouTube respectively.

Usage

Canonical

Pass a supported provider and valid oEmbed response. If neither of these cases can be met, a fallback will be rendered containing a link to the source content.

Example

import { CanonicalSocialEmbed } from '@bbc/psammead-social-embed';

<CanonicalSocialEmbed
  provider="instagram"
  service="news"
  oEmbed={{ html: '...' }}
  skipLink={{
    text: 'Skip %provider_name% content',
    endTextId: 'skip-%provider%-content',
    endTextVisuallyHidden: 'End of %provider_name% content',
  }}
  fallback={{
    text: "Sorry but we're having trouble displaying this content",
    linkText: 'View content on %provider_name%',
    linkTextSuffixVisuallyHidden: ', external',
    linkHref: 'https://www.instagram.com/p/B8FPf4ZphHi/',
    warningText: 'Warning: BBC is not responsible for third party content',
  }}
/>;

The component supports integration with the react-lazyload package ensuring the social media posts are rendered when they come into view.

Example

<LazyLoad height={200}>
  <CanonicalSocialEmbed provider="instagram" .../>
</LazyLoad>

onRender

This component takes an onRender prop which is invoked when the embed is fully rendered (currently only for twitter embeds), this can be used to change styling on render to help reduce layout shift.

Example

import React, { useState } from 'react';
import styled from '@emotion/styled';

const DEFAULT_MIN_HEIGHT = '18.75rem';

const Wrapper = styled.div`
  min-height: ${({ minHeight }) => minHeight};
`;

const SocialEmbedWithWrapper = ({
  provider,
  oEmbed,
  skipLink,
  fallback,
  service,
  }) => {

  const [wrapperMinHeight, setWrapperMinHeight] = useState(DEFAULT_MIN_HEIGHT);

  return (
    <Wrapper minHeight={wrapperMinHeight}>
      <CanonicalSocialEmbed
        provider={provider}
        oEmbed={oEmbed}
        skipLink={skipLink}
        fallback={fallback}
        service={service}
        onRender={() => {
          setMinHeight('0');
        }}
      />
    </Wrapper>
  );
};

export default SocialEmbedWithWrapper;

AMP

Pass a supported provider. If this case cannot be met, a fallback will be rendered containing a link to the source content.

Pass a valid id. This refers to the unique segment of the URL, which identifies the source content. E.G. 1237210910835392512 in https://twitter.com/MileyCyrus/status/1237210910835392512.

import { AmpSocialEmbed } from '@bbc/psammead-social-embed';

<AmpSocialEmbed
  provider="instagram"
  service="news"
  id="B8FPf4ZphHi"
  skipLink={{
    text: 'Skip %provider_name% content',
    endTextId: 'skip-%provider%-content',
    endTextVisuallyHidden: 'End of %provider_name% content',
  }}
  fallback={{
    text: "Sorry but we're having trouble displaying this content",
    linkText: 'View content on %provider_name%',
    linkTextSuffixVisuallyHidden: ', external',
    linkHref: 'https://www.instagram.com/p/B8FPf4ZphHi/',
    warningText: 'Warning: BBC is not responsible for third party content',
  }}
/>;

When to use this component

This component is designed to embed rich social media content from a number of supported providers in primary content, such as an article.

When not to use this component

This component will not provide a rich social media embed for providers outside of the supported providers – these will use fallback content instead.

Accessibility notes

This component provides a Skip Link, which allows users to identify and skip over social media content in your pages. skipLink.endTextId should be set to a value that uniquely identifies skipLink.endTextVisuallyHidden. This is especially important when there are more than one social media embeds from the same provider on a page.

fallback.linkTextSuffixVisuallyHidden is used to add a suffix to fallback.text. This will not be visible on the UI, but will be captured by assistive technology.

caption.textPrefixVisuallyHidden is used to add a prefix to caption.text. This will not be visible on the UI, but will be captured by assistive technology.

Miscellaneous

Some components within SocialEmbed render the same result given the same props and are memoized using React.memo to prevent unnecessary renders.

Contributing

Psammead is completely open source. We are grateful for any contributions, whether they be new components, bug fixes or general improvements. Please see our primary contributing guide which can be found at the root of the Psammead repository.

Code of Conduct

We welcome feedback and help on this work. By participating in this project, you agree to abide by the code of conduct. Please take a moment to read it.

License

Psammead is Apache 2.0 licensed.

版本与发布记录

@bbc/psammead-social-embed 共发布 61 个版本,首次发布于 2020/03/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.3.13latest2022/03/304 年前
3.3.122022/03/294 年前
3.3.112022/01/064 年前
3.3.102021/12/034 年前
3.3.92021/10/065 年前
3.3.82021/09/305 年前

其他发布通道:alpha → 2.0.0-alpha.0。

关于 @bbc/psammead-social-embed 的常见问题

@bbc/psammead-social-embed 是什么?

A component that renders a rich social media embed or a fallback.

如何安装 @bbc/psammead-social-embed?

在项目目录运行 npm install @bbc/psammead-social-embed,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16.9.0)、@emotion/styled(^11.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bbc/psammead-social-embed 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bbc__psammead-social-embed 是否存在。

@bbc/psammead-social-embed 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bbc/psammead-social-embed 可以商用吗?

@bbc/psammead-social-embed 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bbc/psammead-social-embed 的下载量有多少?

根据 npm 官方下载统计,@bbc/psammead-social-embed 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,852 次,日均约 95 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bbc/psammead-social-embed 还在维护吗?

最新版本 3.3.13 发布于 2022/03/30(4 年前);至今共发布 61 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/03/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。