What is @ant-design/cssinjs?
@ant-design/cssinjs is a library that allows you to write CSS-in-JS for Ant Design components. It provides a way to style your components using JavaScript, which can help in maintaining styles in a modular and reusable manner.
What are @ant-design/cssinjs's main functionalities?
Define Styles
This feature allows you to define styles using JavaScript objects. The `createUseStyle` function takes a callback that returns an object with your styles. The `token` parameter can be used to access theme variables.
const useStyle = createUseStyle(({ token }) => ({
button: {
backgroundColor: token.colorPrimary,
color: token.colorText,
padding: '10px 20px',
border: 'none',
borderRadius: '4px',
cursor: 'pointer'
}
}));
Apply Styles
This feature allows you to apply the defined styles to your components. The `useStyle` hook returns an object with the generated class names, which you can then apply to your JSX elements.
const MyButton = () => {
const { styles } = useStyle();
return <button className={styles.button}>Click Me</button>;
};
Theming
This feature allows you to use theming in your application. You can define a theme object and use the `ThemeProvider` component to pass the theme to your styled components.
const theme = {
colorPrimary: '#1890ff',
colorText: '#ffffff'
};
const App = () => (
<ThemeProvider theme={theme}>
<MyButton />
</ThemeProvider>
);
Other packages similar to @ant-design/cssinjs
styled-components
styled-components is a popular library for writing CSS-in-JS. It allows you to create styled components with tagged template literals. Compared to @ant-design/cssinjs, styled-components is more general-purpose and not specifically tied to Ant Design.
emotion
Emotion is another library for writing CSS-in-JS. It offers both a styled API similar to styled-components and a css function for creating class names. Emotion is known for its performance and flexibility, making it a strong alternative to @ant-design/cssinjs.
jss
JSS is a library for writing CSS-in-JS with a focus on high performance and modularity. It provides a powerful API for defining styles and theming. JSS is more low-level compared to @ant-design/cssinjs, offering more control but requiring more setup.
@ant-design/cssinjs
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
Prop | Desc | Type | Default |
---|
autoClear | Clear inject style element when component remove. | boolean | false |
cache | Config cssinjs cache entity. Only set when you need ssr to extract style on you own. | CacheEntity | - |
hashPriority | Use :where selector to reduce hashId css selector priority | 'low' | 'high' | 'low' |
container | Tell cssinjs where to inject style in. | Element | ShadowRoot | document.head |
ssrInline | Component wil render inline <style /> for fallback in SSR. Not recommend. | boolean | false |
transformers | Transform css before inject in document. Please note that transformers do not support dynamic update | Transformer[] | - |
createCache
return CacheEntity for StyleProvider.
createTheme
Create theme object. When same algorithm provided, it will return same object.
Design Token related API
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.
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
:
px2remTransformer
Convert pixel units to rem units. px2remTransformer.options