@bbc/psammead-image

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

React styled components for an Image

原始使用文档
文档目录13 节
  1. Description
  2. When to use this component
  3. Installation
  4. Usage
  5. Img
  6. AmpImg
  7. Props
  8. Img
  9. AmpImg
  10. Accessibility notes
  11. Contributing
  12. Code of Conduct
  13. License

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

Description

The psammead-image component has two images available. The default is a styled img element that uses Styled Components.

There is an alternative AMP Image which can be used. It is a React component returning an amp-img element.

When to use this component

This component can be used at any point on a page.

Installation

npm install @bbc/psammead-image

Usage

Img

Importing the standard Image component renders an <img /> tag.

import Image from '@bbc/psammead-image';

const WrappingContainer = ({ alt, src, height, width, sizes }) => (
  <Img alt={alt} src={src} height={height} width={width} sizes={sizes} fade />
);

AmpImg

If you're to use the AmpImg component, please read the AMP documentation here for further details.

The Amp Image component renders an <amp-img> tag.

Example:

import { AmpImg } from '@bbc/psammead-image';

const WrappingContainer = ({ alt, src, height, width, sizes }) => (
  <AmpImg
    alt={alt}
    layout="responsive"
    sizes={sizes}
    src={src}
    height={height}
    width={width}
  />
);

Props

Img

PropTypeRequiredDefaultExample
altstringYes-"A picture of a cat"
heightnumber/stringNonull450
sizesstringNonull"100vw"
srcstringYes-"https://bbc.com/300/cat.jpg"
srcsetstringNonull"https://bbc.com/300/cat.jpg.webp 300w, https://bbc.com/450/cat.jpg.webp 450w, https://bbc.com/600/cat.jpg.webp 600w"
fallbackSrcsetstringNonull"https://bbc.com/300/cat.jpg 300w, https://bbc.com/450/cat.jpg 450w, https://bbc.com/600/cat.jpg 600w"
primaryMimeTypestringNonull"image/webp"
fallbackMimeTypestringNonull"image/jpeg"
widthnumber/stringNonull600
fadebooleanNofalsetrue

The height and width props are optional, in some cases to preserve the image ratio you might specify either height or width and let the browser scale the image accordingly.

However when not specified the browser will not be able to determine the size of the image, the browser will therefore build the page twice or more depending on the number of images you have. First paint is for the browser to display all the text and once the image is downloaded and size determined a second paint to wrap the texts around the image.

Specifying the width and height allows the browser to reserve space for the image which prevent content moving around while the image is being loaded.

The srcset prop is optional since some projects might not want to use the srcset attribute on images.

The sizes prop is optional since some projects might not want to use the sizes attribute on images.

The fade prop is optional and set to false by default. It's been used to apply a fade-in animation effect on the Img component.

AmpImg

PropTypeRequiredDefaultExample
altstringYes-"A picture of a cat"
attributionstringNo''"Getty Images"
heightnumber/stringYesnull450
layoutstringYes-"responsive"
sizesstringNonull"100vw"
srcstringYes-"https://bbc.com/300/cat.jpg"
srcsetstringNonull"https://bbc.com/300/cat.jpg.webp 300w, https://bbc.com/450/cat.jpg.webp 450w, https://bbc.com/600/cat.jpg.webp 600w"
fallbackSrcsetstringNonull"https://bbc.com/300/cat.jpg 300w, https://bbc.com/450/cat.jpg 450w, https://bbc.com/600/cat.jpg 600w"
widthnumber/stringYes-600

The attribution prop is available to pass in strings to include the image source. For further details, please refer to the amp-img attribute docs. The layout prop can be one of several, including responsive, fixed, intrinsic etc. We recommend using responsive for most use-cases, with height and width props passed in, so the AMP can use the correct ratio for scaling the image. For further details, please refer to the AMP docs. The srcset prop is a string with comma separated string of image URLs with varying sizes. For further details, please refer to the MDN docs. The sizes prop is string containing a list of comma separated strings indicating a set of source sizes. For further details, please refer to the MDN docs.

Accessibility notes

This component requires an alt property to describe the image. This alt text is crucial for users of Assistive Technology, and by any user whose internet connection is so slow the browser decides to render the text instead of the image.

Some images are purely presentational - in these cases, an alt attribute must still be passed, but the value would be an empty string: "".

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 respository.

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-image 共发布 46 个版本,首次发布于 2018/12/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.3latest2022/03/114 年前
3.1.22022/02/164 年前
3.1.12022/02/104 年前
3.1.02021/12/164 年前
3.0.12021/12/144 年前
3.0.02021/12/134 年前

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

@bbc/psammead-image 是什么?

React styled components for an Image

如何安装 @bbc/psammead-image?

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

@bbc/psammead-image 支持 TypeScript 吗?

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

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

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

@bbc/psammead-image 可以商用吗?

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

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

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

@bbc/psammead-image 还在维护吗?

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