ant-design/components/theme/util/genStyleUtils.ts
2024-12-06 17:48:03 +08:00

40 lines
1.3 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, genLinkStyle, genIconStyle } 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] = useLocalToken();
return { theme, realToken, hashId, token, cssVar };
},
useCSP: () => {
const { csp } = useContext(ConfigContext);
return csp ?? {};
},
getResetStyles: (token, config) => [
{ '&': genLinkStyle(token) },
genIconStyle(config?.prefix.iconPrefixCls ?? defaultIconPrefixCls),
],
getCommonStyle: genCommonStyle,
getCompUnitless: (() => unitless) as GetCompUnitless<ComponentTokenMap, AliasToken>,
});