Files
ant-design/components/theme/util/genStyleUtils.ts
MadCcc 751edfc336 feat: zeroRuntime (#54334)
* feat: zeroRuntime

* feat: generate full style

* feat: add antd.css into global css

* chore: revert site change

* docs: add docs

* chore: update scripts

* chore: add test
2025-07-22 10:20:29 +08:00

53 lines
1.7 KiB
TypeScript

import { useContext } from 'react';
import { genStyleUtils } from '@ant-design/cssinjs-utils';
import type { GetCompUnitless } from '@ant-design/cssinjs-utils/es/util/genStyleUtils';
import { ConfigContext, defaultIconPrefixCls } from '../../config-provider/context';
import { genCommonStyle, genIconStyle, genLinkStyle } from '../../style';
import type { AliasToken, ComponentTokenMap, SeedToken } from '../interface';
import useLocalToken, { unitless } from '../useToken';
export const { genStyleHooks, genComponentStyleHook, genSubStyleComponent } = genStyleUtils<
ComponentTokenMap,
AliasToken,
SeedToken
>({
usePrefix: () => {
const { getPrefixCls, iconPrefixCls } = useContext(ConfigContext);
const rootPrefixCls = getPrefixCls();
return {
rootPrefixCls,
iconPrefixCls,
};
},
useToken: () => {
const [theme, realToken, hashId, token, cssVar, zeroRuntime] = useLocalToken();
return { theme, realToken, hashId, token, cssVar, zeroRuntime };
},
useCSP: () => {
const { csp } = useContext(ConfigContext);
return csp ?? {};
},
getResetStyles: (token, config) => {
const linkStyle = genLinkStyle(token);
return [
linkStyle,
{ '&': linkStyle },
genIconStyle(config?.prefix.iconPrefixCls ?? defaultIconPrefixCls),
];
},
getCommonStyle: genCommonStyle,
getCompUnitless: (() => unitless) as GetCompUnitless<ComponentTokenMap, AliasToken>,
});
export const genCssVar = (antCls: string, componentAbbr: string) => {
const cssPrefix = `--${antCls.replace('.', '')}-${componentAbbr}-`;
return (name: string, withVar = false) => {
const raw = `${cssPrefix}${name}`;
return withVar ? `var(${raw})` : raw;
};
};