2022-05-09 22:20:07 +08:00
|
|
|
import type { CSSInterpolation } from '@ant-design/cssinjs';
|
|
|
|
import { Theme, useCacheToken, useStyleRegister } from '@ant-design/cssinjs';
|
2022-05-27 11:07:58 +08:00
|
|
|
import React from 'react';
|
2022-02-18 14:17:32 +08:00
|
|
|
import version from '../../version';
|
2022-03-21 16:48:25 +08:00
|
|
|
import type {
|
|
|
|
AliasToken,
|
2022-05-27 11:07:58 +08:00
|
|
|
DerivativeToken,
|
|
|
|
GlobalToken,
|
2022-03-21 16:48:25 +08:00
|
|
|
OverrideToken,
|
|
|
|
PresetColorType,
|
2022-05-27 11:07:58 +08:00
|
|
|
SeedToken,
|
2022-03-21 16:48:25 +08:00
|
|
|
} from './interface';
|
2022-05-27 11:07:58 +08:00
|
|
|
import { PresetColors } from './interface';
|
|
|
|
import defaultSeedToken, { derivative as defaultDerivative } from './themes/default';
|
|
|
|
import { clearFix, operationUnit, resetComponent, resetIcon, roundedArrow } from './util';
|
|
|
|
import formatToken from './util/alias';
|
2022-04-06 12:20:52 +08:00
|
|
|
import type { FullToken } from './util/genComponentStyleHook';
|
2022-05-27 11:07:58 +08:00
|
|
|
import genComponentStyleHook from './util/genComponentStyleHook';
|
|
|
|
import statisticToken, { merge as mergeToken, statistic } from './util/statistic';
|
2022-03-08 10:29:00 +08:00
|
|
|
|
|
|
|
export {
|
|
|
|
resetComponent,
|
|
|
|
resetIcon,
|
2022-03-09 10:38:02 +08:00
|
|
|
clearFix,
|
2022-03-25 13:20:42 +08:00
|
|
|
roundedArrow,
|
2022-04-24 18:05:08 +08:00
|
|
|
operationUnit,
|
|
|
|
// colors
|
|
|
|
PresetColors,
|
2022-04-01 17:00:12 +08:00
|
|
|
// Statistic
|
2022-04-24 18:05:08 +08:00
|
|
|
statistic,
|
2022-04-01 17:00:12 +08:00
|
|
|
statisticToken,
|
|
|
|
mergeToken,
|
2022-04-24 18:05:08 +08:00
|
|
|
// hooks
|
|
|
|
useStyleRegister,
|
2022-04-06 12:20:52 +08:00
|
|
|
genComponentStyleHook,
|
2022-03-08 10:29:00 +08:00
|
|
|
};
|
2022-03-21 16:48:25 +08:00
|
|
|
export type {
|
|
|
|
SeedToken,
|
|
|
|
AliasToken,
|
|
|
|
PresetColorType,
|
|
|
|
// FIXME: Remove this type
|
|
|
|
AliasToken as DerivativeToken,
|
2022-04-06 12:20:52 +08:00
|
|
|
FullToken,
|
2022-03-15 15:33:50 +08:00
|
|
|
};
|
|
|
|
|
2022-02-18 14:17:32 +08:00
|
|
|
// ================================ Context =================================
|
2022-03-21 16:48:25 +08:00
|
|
|
const defaultTheme = new Theme(defaultDerivative);
|
2022-02-18 14:17:32 +08:00
|
|
|
|
|
|
|
export const DesignTokenContext = React.createContext<{
|
2022-03-21 16:48:25 +08:00
|
|
|
token: Partial<SeedToken>;
|
|
|
|
theme?: Theme<SeedToken, DerivativeToken>;
|
|
|
|
override?: OverrideToken;
|
2022-02-18 14:17:32 +08:00
|
|
|
hashed?: string | boolean;
|
|
|
|
}>({
|
2022-03-21 16:48:25 +08:00
|
|
|
token: defaultSeedToken,
|
|
|
|
theme: defaultTheme,
|
2022-02-18 14:17:32 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// ================================== Hook ==================================
|
2022-04-20 15:18:25 +08:00
|
|
|
// In dev env, we refresh salt per hour to avoid user use this
|
|
|
|
// Note: Do not modify this to real time update which will make debug harder
|
|
|
|
const saltPrefix =
|
|
|
|
process.env.NODE_ENV === 'production' ? version : `${version}-${new Date().getHours()}`;
|
|
|
|
|
2022-03-24 18:44:42 +08:00
|
|
|
export function useToken(): [Theme<SeedToken, DerivativeToken>, GlobalToken, string] {
|
2022-03-21 16:48:25 +08:00
|
|
|
const {
|
|
|
|
token: rootDesignToken,
|
|
|
|
theme = defaultTheme,
|
|
|
|
override,
|
|
|
|
hashed,
|
|
|
|
} = React.useContext(DesignTokenContext);
|
2022-02-18 14:17:32 +08:00
|
|
|
|
2022-04-20 15:18:25 +08:00
|
|
|
const salt = `${saltPrefix}-${hashed || ''}`;
|
2022-02-18 14:17:32 +08:00
|
|
|
|
2022-03-24 18:44:42 +08:00
|
|
|
const [token, hashId] = useCacheToken<GlobalToken, SeedToken>(
|
2022-03-03 14:55:19 +08:00
|
|
|
theme,
|
2022-03-21 16:48:25 +08:00
|
|
|
[defaultSeedToken, rootDesignToken],
|
2022-03-03 14:55:19 +08:00
|
|
|
{
|
|
|
|
salt,
|
2022-03-21 16:48:25 +08:00
|
|
|
override,
|
|
|
|
formatToken,
|
2022-03-03 14:55:19 +08:00
|
|
|
},
|
|
|
|
);
|
2022-03-21 16:48:25 +08:00
|
|
|
|
2022-02-27 22:17:17 +08:00
|
|
|
return [theme, token, hashed ? hashId : ''];
|
2022-02-18 14:17:32 +08:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:55:19 +08:00
|
|
|
export type UseComponentStyleResult = [(node: React.ReactNode) => React.ReactElement, string];
|
|
|
|
|
2022-05-27 11:07:58 +08:00
|
|
|
export type GenerateStyle<
|
|
|
|
ComponentToken extends object = AliasToken,
|
|
|
|
ReturnType = CSSInterpolation,
|
|
|
|
> = (token: ComponentToken) => ReturnType;
|