@contentful/forma-36-react-components

中文文档
3.100.7发布于 2022/06/24MITTypeScriptESM + CJS近 30 天下载 6.2万

Contentful UI Component Library

原始使用文档
文档目录17 节
  1. Table of contents
  2. Installing package
  3. Usage
  4. Import desired component into your project
  5. Import styles
  6. Development
  7. Storybook
  8. Example component directory structure
  9. Styling
  10. Component principles
  11. Adding documentation for component
  12. Testing
  13. Run tests
  14. Building
  15. Create a build of the library
  16. Commits
  17. Alpha components

Forma 36 React Components

This is a version 3 of Forma 36, which is not the current version of the package. From 12th July 2022 it will not be supported anymore. Please install current, supported version 4 of the library

A React component library for Forma 36 design system created by Contentful and powered by Storybook. You can access a Storybook website with the latest changes of the library here

Table of contents

Installing package

yarn add @contentful/forma-36-react-components

Or

npm install @contentful/forma-36-react-components

Usage

Import desired component into your project

import { Input } from '@contentful/forma-36-react-components';

For components in alpha stage, check the instructions in Alpha components

Import styles

import '@contentful/forma-36-react-components/dist/styles.css';

Development

For local development, in the root of the repo run yarn to install all dependencies and then yarn build to build all packages. This package depends on several other Forma 36 packages so you will need to build all of them.

Storybook

We use Storybook to create a development environment for our component library. To start it locally run:

yarn storybook

To start working on your components in Storybook, you will need to create stories for them. Look in the src/components directory for any .stories.js file. Storybook will automatically include any files with the .stories.js file extension.

Storybook is configured at scripts/.storybook/main.js.

Example component directory structure

A component's directory should resemble the following:

YourComponent
├── index.js                 // A file for exporting your component
├── YourComponent.js         // Your React component
├── YourComponent.css        // Component styles
├── YourComponent.stories.js // Storybook for the component
└── YourComponent.test.js.   // Component tests

If you use yarn generate in the root of the repo, this structure will be created automatically for you

Styling

We are using postcss-preset-env for styling our components. Using postcss-preset-env allows us to use the latest CSS syntax without having to wait for browser support. tools/postcss.config.js is used for adding plugins and configuration.

Component principles

We follow a number of principles when creating our components:

  1. A component is responsible for only its internal spacing

We follow the principle that a component should only be responsible for its own internal spacing - never external spacing. This means that we're flexible in where our components can be used without having to override margins.

  1. Use descriptive PropTypes

We recommend the following naming convention for PropTypes to make them as clear as possible:

  • Number - use a prefix or suffix to imply that the prop accepts a number. E.g. numItems, itemCount, itemIndex
  • Boolean - use the prefix 'is'/'can'/'has'. E.g. isVisible, canExpand, hasImage
  • Array - use a plural noun. E.g. items
  • Object - use a noun. E.g. item
  • Node - use the suffix 'Node'. E.g. containerNode
  • Element - use the suffix 'Element'. E.g. triggerElement
  • Event handler functions - use the prefix 'on'. E.g. onOpen, onClick

Adding documentation for component

We would like to make sure that every component contains a README file with recommendations and guidelines. Using yarn add-readme it will generate a template README file for you for the component that you specify. Follow steps in the CLI to create new README. Make sure that your documentation for the component contains following parts:

  1. A short summary of the component.
  2. Variations/types - try to provide the overview of the components variants.
  3. Example of usage - try to add couple of examples, that includes import
  4. Content recommendations - notes about best practices when it comes to the content used in the component, like message or title
  5. Best practices - try to describe in couple of points when to use the component
  6. Accessibility - If possible, we strongly recommend providing accessibility guidelines.

Testing

We are using Jest and Testing Library to test our components.

Tests are kept next to their components and use the .test.js file extension.

Run tests

yarn test

It is recommended to run tests in development with the optional --watch flag.

yarn test --watch

Building

We are using a combination of Webpack and Babel to create builds of our component library. We use Webpack with the tools/webpack.production.config.js config to build a stylesheet including all CSS used for our components using the ExtractTextPlugin. Babel is used for transpiling our React components into CommonJS modules. Babel and Webpack both output the build to the dist directory.

Create a build of the library

yarn build

Commits

This project uses the Angular JS Commit Message Conventions, via semantic-release. See the semantic-release Default Commit Message Format section for more details.

You can commit the changes by running

yarn commit

Alpha components

We provide a number of alpha components which are unsupported and subject to breaking changes without warning. In short - use alpha components at your own risk.

Alpha components are provided outside of the main component library bundle and can be used as follows:

import { MyAlphaComponent } from '@contentful/forma-36-react-components/dist/alpha';

版本与发布记录

@contentful/forma-36-react-components 共发布 571 个版本,首次发布于 2018/11/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.100.7latest2022/06/244 年前
3.100.62022/03/224 年前
3.100.52022/03/034 年前
3.100.42021/11/234 年前
3.100.32021/11/174 年前
3.100.22021/11/124 年前

其他发布通道:next-v3 → 3.73.2-next-v3-2852.76,next-v4 → 3.73.2-next-v4-3280.19,next-v3-new-tokens → 3.90.6-next-v3-new-tokens.19。

关于 @contentful/forma-36-react-components 的常见问题

@contentful/forma-36-react-components 是什么?

Contentful UI Component Library

如何安装 @contentful/forma-36-react-components?

在项目目录运行 npm install @contentful/forma-36-react-components,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.8.6 || ^17.0.0)、react-dom(^16.8.6 || ^17.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@contentful/forma-36-react-components 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@contentful/forma-36-react-components 支持 ESM 还是 CommonJS?

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

@contentful/forma-36-react-components 可以商用吗?

@contentful/forma-36-react-components 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@contentful/forma-36-react-components 的下载量有多少?

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

@contentful/forma-36-react-components 还在维护吗?

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