@ant-design/cssinjs

中文文档
2.1.2发布于 2026/03/05MITTypeScriptESM + CJS近 30 天下载 1306.3万

Component level cssinjs resolution for antd

原始使用文档
文档目录11 节
  1. Live Demo
  2. Install
  3. Development
  4. License
  5. API
  6. StyleProvider
  7. createCache
  8. createTheme
  9. Design Token related API
  10. extractStyle
  11. Transform

@ant-design/cssinjs

NPM version npm download dumi build status Codecov bundle size

Component level cssinjs solution used in ant.design. It's a subset of Emotion with design token logic wrapper. Please feel free to use emotion directly if you want to find a web cssinjs solution. cssinjs related dep packages:

  • stylis
  • @emotion/hash
  • @emotion/unitless

Live Demo

https://ant-design.github.io/cssinjs/

Install

npm install @ant-design/cssinjs

or

yarn add @ant-design/cssinjs
pnpm add @ant-design/cssinjs

Development

npm install
npm start

License

@ant-design/cssinjs is released under the MIT license.

API

StyleProvider

PropDescTypeDefault
autoClearClear inject style element when component remove.booleanfalse
cacheConfig cssinjs cache entity. Only set when you need ssr to extract style on you own.CacheEntity-
hashPriorityUse :where selector to reduce hashId css selector priority'low' | 'high''low'
containerTell cssinjs where to inject style in.Element | ShadowRootdocument.head
ssrInlineComponent wil render inline <style /> for fallback in SSR. Not recommend.booleanfalse
transformersTransform css before inject in document. Please note that transformers do not support dynamic updateTransformer[]-

createCache

return CacheEntity for StyleProvider.

createTheme

Create theme object. When same algorithm provided, it will return same object.

Since @ant-design/cssinjs use strong constraints for cache hit performance, we recommend to view demo basic.tsx for usage and animation.tsx for animation usage.

extractStyle

Extracts the styles from the cache and returns them as a string.

Parameters

  • cache (Cache): The cache instance containing the styles.
  • options (object | boolean, optional): Options for extracting the styles.
    • plain (boolean, optional): If true, the styles will be returned in plain format. Default is false.
    • types (string | string[], optional): The types of styles to extract. Default is ['style', 'token', 'cssVar'].

Returns

  • (string): The extracted styles as a string.

Example

import { extractStyle, createCache } from '@ant-design/cssinjs';

// 创建并填充缓存
const cache = createCache();
// 注意:在实际使用中,缓存通常会在渲染组件时自动填充

// 提取样式
const styles = extractStyle(cache, { plain: true, types: ['style', 'token'] });

// 使用提取的样式
const styleElement = document.createElement('style');
styleElement.innerHTML = styles;
document.head.appendChild(styleElement);

Transform

When you need transform CSSObject before inject style. You can use transformers to handle this:

import {
  legacyLogicalPropertiesTransformer,
  StyleProvider,
} from '@ant-design/cssinjs';

export default () => (
  <StyleProvider transformers={[legacyLogicalPropertiesTransformer]}>
    <MyApp />
  </StyleProvider>
);

Follow are the transform we provide:

legacyLogicalPropertiesTransformer

Convert logical properties to legacy properties. e.g. marginBlockStart to marginTop:

  • inset
  • margin
  • padding
  • border

px2remTransformer

Convert pixel units to rem units. px2remTransformer.options

版本与发布记录

@ant-design/cssinjs 共发布 136 个版本,首次发布于 2022/01/27;过去 12 个月发布了 7 个版本。

版本发布日期距今
2.1.2latest2026/03/058 个月前
2.1.12026/03/048 个月前
2.1.02026/02/098 个月前
2.0.32026/01/149 个月前
2.0.22026/01/059 个月前
2.0.12025/11/2511 个月前

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

关于 @ant-design/cssinjs 的常见问题

@ant-design/cssinjs 是什么?

Component level cssinjs resolution for antd

如何安装 @ant-design/cssinjs?

在项目目录运行 npm install @ant-design/cssinjs,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16.0.0)、react-dom(>=16.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ant-design/cssinjs 支持 TypeScript 吗?

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

@ant-design/cssinjs 支持 ESM 还是 CommonJS?

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

@ant-design/cssinjs 可以商用吗?

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

@ant-design/cssinjs 的下载量有多少?

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

@ant-design/cssinjs 还在维护吗?

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