2022-04-02 22:55:02 +08:00
|
|
|
/* eslint-disable no-redeclare */
|
|
|
|
import { CSSInterpolation, useStyleRegister } from '@ant-design/cssinjs';
|
|
|
|
import { useContext } from 'react';
|
2022-04-06 12:20:52 +08:00
|
|
|
import { GlobalToken, OverrideToken } from '../interface';
|
|
|
|
import { mergeToken, statisticToken, UseComponentStyleResult, useToken } from '../index';
|
|
|
|
import { ConfigContext } from '../../../config-provider';
|
2022-04-02 22:55:02 +08:00
|
|
|
|
|
|
|
export type OverrideTokenWithoutDerivative = Omit<OverrideToken, 'derivative'>;
|
|
|
|
export type OverrideComponent = keyof OverrideTokenWithoutDerivative;
|
|
|
|
export type GlobalTokenWithComponent<ComponentName extends OverrideComponent> = GlobalToken &
|
|
|
|
OverrideToken[ComponentName];
|
2022-04-25 10:54:00 +08:00
|
|
|
|
|
|
|
export interface StyleInfo {
|
2022-04-02 22:55:02 +08:00
|
|
|
hashId: string;
|
|
|
|
prefixCls: string;
|
|
|
|
rootPrefixCls: string;
|
|
|
|
iconPrefixCls: string;
|
2022-04-25 10:54:00 +08:00
|
|
|
}
|
|
|
|
|
2022-04-07 00:19:37 +08:00
|
|
|
export type TokenWithCommonCls<T> = T & {
|
2022-04-25 10:54:00 +08:00
|
|
|
/** Wrap component class with `.` prefix */
|
2022-04-06 12:20:52 +08:00
|
|
|
componentCls: string;
|
2022-04-25 10:54:00 +08:00
|
|
|
/** Origin prefix which do not have `.` prefix */
|
2022-04-06 12:20:52 +08:00
|
|
|
prefixCls: string;
|
2022-04-25 10:54:00 +08:00
|
|
|
/** Wrap icon class with `.` prefix */
|
2022-04-06 12:20:52 +08:00
|
|
|
iconCls: string;
|
2022-04-25 10:54:00 +08:00
|
|
|
/** Wrap ant prefixCls class with `.` prefix */
|
2022-04-07 00:19:37 +08:00
|
|
|
antCls: string;
|
2022-04-06 12:20:52 +08:00
|
|
|
};
|
2022-04-07 00:19:37 +08:00
|
|
|
export type FullToken<ComponentName extends OverrideComponent> = TokenWithCommonCls<
|
2022-04-02 22:55:02 +08:00
|
|
|
GlobalTokenWithComponent<ComponentName>
|
|
|
|
>;
|
|
|
|
|
|
|
|
function genComponentStyleHook<ComponentName extends OverrideComponent>(
|
|
|
|
component: ComponentName,
|
|
|
|
styleFn: (token: FullToken<ComponentName>, info: StyleInfo) => CSSInterpolation,
|
|
|
|
getDefaultToken?:
|
|
|
|
| OverrideTokenWithoutDerivative[ComponentName]
|
|
|
|
| ((token: GlobalToken) => OverrideTokenWithoutDerivative[ComponentName]),
|
|
|
|
) {
|
|
|
|
return (prefixCls: string): UseComponentStyleResult => {
|
|
|
|
const [theme, token, hashId] = useToken();
|
|
|
|
const { getPrefixCls, iconPrefixCls } = useContext(ConfigContext);
|
2022-04-07 00:19:37 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2022-04-02 22:55:02 +08:00
|
|
|
|
|
|
|
return [
|
|
|
|
useStyleRegister({ theme, token, hashId, path: [prefixCls] }, () => {
|
|
|
|
const { token: proxyToken, flush } = statisticToken(token);
|
|
|
|
|
|
|
|
const defaultComponentToken =
|
2022-04-11 16:04:00 +08:00
|
|
|
typeof getDefaultToken === 'function' ? getDefaultToken(proxyToken) : getDefaultToken;
|
2022-04-02 22:55:02 +08:00
|
|
|
const overrideComponentToken = token[component] as any;
|
|
|
|
|
|
|
|
// Only merge token specified in interface
|
|
|
|
const mergedComponentToken = { ...defaultComponentToken } as any;
|
|
|
|
if (overrideComponentToken) {
|
|
|
|
Object.keys(mergedComponentToken).forEach(key => {
|
|
|
|
mergedComponentToken[key] = overrideComponentToken[key] ?? mergedComponentToken[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const mergedToken = mergeToken<
|
2022-04-07 00:19:37 +08:00
|
|
|
TokenWithCommonCls<GlobalTokenWithComponent<OverrideComponent>>
|
2022-04-06 12:20:52 +08:00
|
|
|
>(
|
|
|
|
proxyToken,
|
2022-04-07 00:19:37 +08:00
|
|
|
{
|
|
|
|
componentCls: `.${prefixCls}`,
|
|
|
|
prefixCls,
|
|
|
|
iconCls: `.${iconPrefixCls}`,
|
|
|
|
antCls: `.${rootPrefixCls}`,
|
|
|
|
},
|
2022-04-06 12:20:52 +08:00
|
|
|
mergedComponentToken,
|
|
|
|
);
|
2022-04-02 22:55:02 +08:00
|
|
|
|
|
|
|
const styleInterpolation = styleFn(mergedToken as unknown as FullToken<ComponentName>, {
|
|
|
|
hashId,
|
|
|
|
prefixCls,
|
2022-04-07 00:19:37 +08:00
|
|
|
rootPrefixCls,
|
2022-04-02 22:55:02 +08:00
|
|
|
iconPrefixCls,
|
|
|
|
});
|
|
|
|
flush(component);
|
|
|
|
return styleInterpolation;
|
|
|
|
}),
|
|
|
|
hashId,
|
|
|
|
];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default genComponentStyleHook;
|