2022-07-04 11:00:19 +08:00
|
|
|
import type { CSSInterpolation, Theme } from '@ant-design/cssinjs';
|
|
|
|
import { createTheme, useCacheToken, useStyleRegister } from '@ant-design/cssinjs';
|
2022-05-27 11:07:58 +08:00
|
|
|
import React from 'react';
|
2022-06-27 14:25:59 +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
|
|
|
GlobalToken,
|
2022-06-27 11:54:31 +08:00
|
|
|
MapToken,
|
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';
|
2022-06-24 11:11:42 +08:00
|
|
|
import defaultDerivative from './themes/default';
|
|
|
|
import defaultSeedToken from './themes/seed';
|
2022-05-27 11:07:58 +08:00
|
|
|
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';
|
2022-06-17 18:17:39 +08:00
|
|
|
import statisticToken, { merge as mergeToken, statistic } from './util/statistic';
|
2022-03-08 10:29:00 +08:00
|
|
|
|
2022-07-04 11:00:19 +08:00
|
|
|
const defaultTheme = createTheme(defaultDerivative);
|
2022-06-29 20:34:00 +08:00
|
|
|
|
2022-03-08 10:29:00 +08:00
|
|
|
export {
|
2022-04-24 18:05:08 +08:00
|
|
|
// 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 =================================
|
|
|
|
export const DesignTokenContext = React.createContext<{
|
2022-03-21 16:48:25 +08:00
|
|
|
token: Partial<SeedToken>;
|
2022-06-29 20:34:00 +08:00
|
|
|
theme?: Theme<SeedToken, MapToken>;
|
2022-06-29 16:41:42 +08:00
|
|
|
override?: OverrideToken;
|
2022-02-18 14:17:32 +08:00
|
|
|
hashed?: string | boolean;
|
|
|
|
}>({
|
2022-03-21 16:48:25 +08:00
|
|
|
token: defaultSeedToken,
|
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-06-27 11:54:31 +08:00
|
|
|
export function useToken(): [Theme<SeedToken, MapToken>, GlobalToken, string] {
|
2022-06-29 20:34:00 +08:00
|
|
|
const { token: rootDesignToken, override, hashed, theme } = React.useContext(DesignTokenContext);
|
2022-06-24 11:11:42 +08:00
|
|
|
|
2022-04-20 15:18:25 +08:00
|
|
|
const salt = `${saltPrefix}-${hashed || ''}`;
|
2022-02-18 14:17:32 +08:00
|
|
|
|
2022-06-29 20:34:00 +08:00
|
|
|
const mergedTheme = theme || defaultTheme;
|
|
|
|
|
2022-03-24 18:44:42 +08:00
|
|
|
const [token, hashId] = useCacheToken<GlobalToken, SeedToken>(
|
2022-06-29 20:34:00 +08:00
|
|
|
mergedTheme,
|
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-06-29 20:34:00 +08:00
|
|
|
return [mergedTheme, 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;
|