### Install @ant-design/cssinjs with npm Source: https://github.com/ant-design/cssinjs/blob/master/README.md Use this command to install the library using npm. ```bash npm install @ant-design/cssinjs ``` -------------------------------- ### Install @ant-design/cssinjs with pnpm Source: https://github.com/ant-design/cssinjs/blob/master/README.md Use this command to install the library using pnpm. ```bash pnpm add @ant-design/cssinjs ``` -------------------------------- ### First Render Example Source: https://github.com/ant-design/cssinjs/blob/master/docs/demo/first-render.md This example demonstrates the basic setup for a component's first render using Ant Design's CSS-in-JS. It shows how styles are defined and applied. ```tsx import React from 'react'; import { createStyles, css } from 'antd-style'; const useStyles = createStyles(({ token }) => ({ container: { display: 'flex', flexDirection: 'column', alignItems: 'center', padding: token.paddingLG, border: `1px solid ${token.colorBorder}`, borderRadius: token.borderRadiusLG, }, title: { fontSize: token.fontSizeHeading1, color: token.colorPrimary, marginBottom: token.marginLG, }, description: { fontSize: token.fontSizeLG, color: token.colorTextSecondary, }, })); const FirstRenderDemo = () => { const { styles } = useStyles(); return (

Welcome to Ant Design CSS-in-JS

This is a demonstration of the first render.

Styles are dynamically generated and applied.

); }; export default FirstRenderDemo; ``` -------------------------------- ### Diff Salt Example in React Source: https://github.com/ant-design/cssinjs/blob/master/docs/demo/diff-salt.md This example demonstrates how to use diff salt with a React component. Ensure you have the necessary CSS-in-JS library and React setup. ```tsx import React from 'react'; import { useStyle } from '@ant-design/cssinjs'; const App = () => { const [token] = useStyle(); return (
Hello Ant Design CSS-in-JS
); }; export default App; ``` -------------------------------- ### Development Setup Source: https://github.com/ant-design/cssinjs/blob/master/README.md Commands to set up the development environment for the project. ```bash npm install npm start ``` -------------------------------- ### Install @ant-design/cssinjs with yarn Source: https://github.com/ant-design/cssinjs/blob/master/README.md Use this command to install the library using yarn. ```bash yarn add @ant-design/cssinjs ``` -------------------------------- ### Basic Animation Example Source: https://github.com/ant-design/cssinjs/blob/master/docs/demo/animation.md A simple example demonstrating how to set up and run a basic animation. Ensure necessary animation libraries are imported. ```tsx import React from 'react'; import { useSpring, animated } from '@react-spring/web'; const AnimationDemo = () => { const props = useSpring({ from: { opacity: 0, transform: 'translateY(-20px)' }, to: { opacity: 1, transform: 'translateY(0px)' }, config: { duration: 500 }, }); return ( Hello, Animation! ); }; export default AnimationDemo; ``` -------------------------------- ### Dynamic Style Example in React Source: https://github.com/ant-design/cssinjs/blob/master/docs/demo/dynamic.md This example shows how to dynamically change styles based on component state using the `useStyle` hook. Ensure you have the necessary Ant Design and CSS-in-JS dependencies installed. ```tsx import React from 'react'; import { useStyle } from '@ant-design/css-in-js'; const DynamicComponent = () => { const [styles, token] = useStyle('dynamic-component', (token) => ({ container: { padding: token.padding, background: token.colorPrimary, color: token.colorTextLightSolid, cursor: 'pointer', }, text: { fontSize: token.fontSizeHeading1, }, })); return (
alert('Clicked!')}> Click me for dynamic styles
); }; export default DynamicComponent; ``` -------------------------------- ### Define and Use CSS Variables Source: https://github.com/ant-design/cssinjs/blob/master/docs/demo/css-var.md This example shows how to define CSS variables using `css` and access them within styles. It's useful for creating themeable components or managing dynamic styles. ```tsx import React from 'react'; import { css } from '@emotion/react'; const styles = css` --primary-color: blue; --secondary-color: green; h1 { color: var(--primary-color); } p { color: var(--secondary-color); } `; export default () => (

Hello World

This is a paragraph.

); ``` -------------------------------- ### Extract Styles with @ant-design/cssinjs Source: https://github.com/ant-design/cssinjs/blob/master/README.md Example demonstrating how to create a cache, populate it (implicitly through rendering), and then extract styles using `extractStyle`. This is useful for Server-Side Rendering (SSR). ```typescript 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); ``` -------------------------------- ### Create Theme Instance with Derivatives Source: https://context7.com/ant-design/cssinjs/llms.txt Use `createTheme` to generate a Theme instance from derivative functions. Passing the same function reference ensures cache hits. Multiple derivatives can be combined in sequence. ```tsx import { createTheme } from '@ant-design/cssinjs'; import { TinyColor } from '@ctrl/tinycolor'; interface DesignToken { primaryColor: string; borderRadius: number; fontSize: number; } interface DerivativeToken extends DesignToken { primaryColorHover: string; primaryColorActive: string; fontSizeLG: number; } // Derivative function: receives base tokens, returns extended tokens function derivative(token: DesignToken): DerivativeToken { return { ...token, primaryColorHover: new TinyColor(token.primaryColor).lighten(10).toHexString(), primaryColorActive: new TinyColor(token.primaryColor).darken(10).toHexString(), fontSizeLG: token.fontSize * 1.25, }; } // Same derivative reference → same Theme object (cached) const theme = createTheme(derivative); // Multiple derivatives are applied in sequence const theme2 = createTheme([derivative, anotherDerivative]); ``` -------------------------------- ### createTheme Source: https://github.com/ant-design/cssinjs/blob/master/README.md Creates a theme object. If the same algorithm is provided, it will return the same object, optimizing theme management. ```APIDOC ## createTheme ### Description Creates a theme object. When same algorithm provided, it will return same object. ### Usage ```typescript import { createTheme } from '@ant-design/cssinjs'; // Assuming an algorithm is defined elsewhere // const theme = createTheme(algorithm); ``` ``` -------------------------------- ### createTheme Source: https://context7.com/ant-design/cssinjs/llms.txt Creates a Theme instance from derivative functions. It caches Theme objects based on function references for efficient reuse. ```APIDOC ## createTheme ### Description Creates (or retrieves from cache) a `Theme` instance from one or more derivative functions. A derivative function accepts your base design tokens and returns an expanded set of derived tokens. Passing the same function reference always returns the same `Theme` object, enabling cache hits in `useCacheToken`. ### Usage ```tsx import { createTheme } from '@ant-design/cssinjs'; import { TinyColor } from '@ctrl/tinycolor'; interface DesignToken { primaryColor: string; borderRadius: number; fontSize: number; } interface DerivativeToken extends DesignToken { primaryColorHover: string; primaryColorActive: string; fontSizeLG: number; } // Derivative function: receives base tokens, returns extended tokens function derivative(token: DesignToken): DerivativeToken { return { ...token, primaryColorHover: new TinyColor(token.primaryColor).lighten(10).toHexString(), primaryColorActive: new TinyColor(token.primaryColor).darken(10).toHexString(), fontSizeLG: token.fontSize * 1.25, }; } // Same derivative reference → same Theme object (cached) const theme = createTheme(derivative); // Multiple derivatives are applied in sequence const theme2 = createTheme([derivative, anotherDerivative]); ``` ``` -------------------------------- ### SSR Style Extraction with createCache and extractStyle Source: https://context7.com/ant-design/cssinjs/llms.txt Create a CacheEntity instance with createCache for SSR. Pass this cache to StyleProvider in mock="server" mode. Use extractStyle to retrieve all registered styles as ' // Inject into the SSR document: const fullHTML = ` ${styleHTML}
${html}
`; ``` -------------------------------- ### Apply Legacy Logical Properties Transformer Source: https://github.com/ant-design/cssinjs/blob/master/README.md Demonstrates how to use the `legacyLogicalPropertiesTransformer` within `StyleProvider` to convert logical CSS properties to their legacy equivalents. ```tsx import { legacyLogicalPropertiesTransformer, StyleProvider, } from '@ant-design/cssinjs'; export default () => ( ); ``` -------------------------------- ### Configure StyleProvider with Transformers and Linters Source: https://context7.com/ant-design/cssinjs/llms.txt Use StyleProvider to configure the global cssinjs environment, including cache, hash priority, injection container, SSR mode, transformers, and linters. All cssinjs-powered components must be rendered within a StyleProvider. ```tsx import { StyleProvider, createCache, legacyLogicalPropertiesTransformer, px2remTransformer, logicalPropertiesLinter, NaNLinter, } from '@ant-design/cssinjs'; const cache = createCache(); export default function App() { return ( tags transformers={[ legacyLogicalPropertiesTransformer, // marginInlineStart → marginLeft px2remTransformer({ rootValue: 16, precision: 5 }), ]} linters={[logicalPropertiesLinter, NaNLinter]} layer // wrap styles in @layer to avoid conflicts > ); } // StyleProvider props: // autoClear? boolean – remove ' // Inject into the SSR document: const fullHTML = ` ${styleHTML}
${html}
`; ``` ``` -------------------------------- ### `unit` utility Source: https://context7.com/ant-design/cssinjs/llms.txt A utility function that appends 'px' to a number or returns a string value unchanged. It's useful for handling CSS values that might be numeric or already formatted strings within style functions. ```APIDOC ## `unit` A utility that appends `'px'` to a number, or returns a string value unchanged. Useful inside style functions when a token value may be either a numeric pixel value or an already-formatted CSS string (e.g., `'var(--token)'`). ```tsx import { unit } from '@ant-design/cssinjs'; unit(16) // → '16px' unit(0) // → 0 (zero is returned as-is) unit('var(--my-spacing)') // → 'var(--my-spacing)' unit('1.5rem') // → '1.5rem' // In style functions with CSS variable tokens: const genButtonStyle = (token: ButtonToken): CSSObject => ({ '.btn': { // token.borderWidth might be 1 (number) or 'var(--btn-border-width)' (cssVar mode) border: `${unit(token.borderWidth)} solid ${token.borderColor}`, padding: `${unit(token.paddingXS)} ${unit(token.padding)}`, }, }); ``` ``` -------------------------------- ### legacyLogicalPropertiesTransformer Source: https://github.com/ant-design/cssinjs/blob/master/README.md A transformer that converts logical CSS properties (e.g., marginBlockStart) to their legacy physical equivalents (e.g., marginTop). ```APIDOC ## legacyLogicalPropertiesTransformer ### Description Convert logical properties to legacy properties. e.g. `marginBlockStart` to `marginTop`. ### Supported Properties - inset - margin - padding - border ### Usage ```tsx import { legacyLogicalPropertiesTransformer, StyleProvider, } from '@ant-design/cssinjs'; export default () => ( ); ``` ``` -------------------------------- ### Integrate CSS Linters with `StyleProvider` Source: https://context7.com/ant-design/cssinjs/llms.txt Development-only CSS validators like `logicalPropertiesLinter`, `NaNLinter`, `legacyNotSelectorLinter`, and `parentSelectorLinter` can be passed to `StyleProvider`'s `linters` prop to detect problematic CSS patterns. ```tsx import { StyleProvider, logicalPropertiesLinter, NaNLinter, legacyNotSelectorLinter, parentSelectorLinter, } from '@ant-design/cssinjs'; export default function App() { return ( ); } // logicalPropertiesLinter warns for: // marginLeft / marginRight / paddingLeft / paddingRight / left / right // borderLeft* / borderRight* / borderTopLeftRadius / borderTopRightRadius etc. // textAlign: 'left' | 'right' // margin: '0 8px 0 16px' (different left/right values) // NaNLinter warns for: // fontSize: NaN or width: 'NaNpx' // Linters implement: type Linter = (key: string, value: any, info: { path, hashId, parentSelectors }) => void ``` -------------------------------- ### `genCalc` factory Source: https://context7.com/ant-design/cssinjs/llms.txt A factory function that creates a chainable calculator for arithmetic CSS expressions. It supports 'css' mode for generating `calc(...)` strings and 'js' mode for immediate numerical evaluation, enabling type-safe token math. ```APIDOC ## `genCalc` A factory that creates a chainable calculator for arithmetic CSS expressions. In `'css'` mode it builds `calc(...)` strings; in `'js'` mode it evaluates numerically. Enables type-safe, composable token math without string concatenation. ```tsx import { genCalc } from '@ant-design/cssinjs'; // 'css' mode → generates CSS calc() expressions const calc = genCalc('css', new Set(['lineHeight'])); const c = calc(16); c.add(8).equal() // → 'calc(16px + 8px)' c.mul(1.5).equal() // → 'calc(16px * 1.5)' c.add(calc(4).mul(2)).equal() // → 'calc(16px + 4px * 2)' c.div(2).add(4).equal() // → 'calc(16px / 2 + 4px)' // 'js' mode → evaluates immediately (for SSR or non-CSS contexts) const jsCalc = genCalc('js', new Set()); jsCalc(16).add(8).equal() // → 24 jsCalc(100).div(4).equal() // → 25 // Usage in token derivation: function derivative(token: DesignToken) { const calc = genCalc('css', new Set(['lineHeight'])); return { ...token, controlHeight: calc(token.fontSize).mul(2.5).add(token.paddingXS * 2).equal(), // → 'calc(14px * 2.5 + 8px)' }; } ``` ``` -------------------------------- ### Extract Styles for Server-Side Rendering with extractStyle Source: https://context7.com/ant-design/cssinjs/llms.txt Use `extractStyle` to serialize styles from a `Cache` instance into `