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,
|
2023-01-06 11:06:46 +08:00
|
|
|
PresetColorKey,
|
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,
|
2023-01-06 11:06:46 +08:00
|
|
|
PresetColorKey,
|
2022-03-21 16:48:25 +08:00
|
|
|
// 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-08-19 17:45:03 +08:00
|
|
|
// To ensure snapshot stable. We disable hashed in test env.
|
|
|
|
export const defaultConfig = {
|
|
|
|
token: defaultSeedToken,
|
|
|
|
hashed: true,
|
|
|
|
};
|
|
|
|
|
2022-02-18 14:17:32 +08:00
|
|
|
export const DesignTokenContext = React.createContext<{
|
2022-09-15 21:26:04 +08:00
|
|
|
token: Partial<AliasToken>;
|
2022-06-29 20:34:00 +08:00
|
|
|
theme?: Theme<SeedToken, MapToken>;
|
2022-09-15 21:26:04 +08:00
|
|
|
components?: OverrideToken;
|
2022-02-18 14:17:32 +08:00
|
|
|
hashed?: string | boolean;
|
2022-08-19 17:45:03 +08:00
|
|
|
}>(defaultConfig);
|
2022-02-18 14:17:32 +08:00
|
|
|
|
|
|
|
// ================================== Hook ==================================
|
2022-06-27 11:54:31 +08:00
|
|
|
export function useToken(): [Theme<SeedToken, MapToken>, GlobalToken, string] {
|
2022-09-15 21:26:04 +08:00
|
|
|
const {
|
|
|
|
token: rootDesignToken,
|
|
|
|
hashed,
|
|
|
|
theme,
|
|
|
|
components,
|
|
|
|
} = React.useContext(DesignTokenContext);
|
2022-06-24 11:11:42 +08:00
|
|
|
|
2022-11-29 11:29:53 +08:00
|
|
|
const salt = `${version}-${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-09-15 21:26:04 +08:00
|
|
|
override: { override: rootDesignToken, ...components },
|
2022-03-21 16:48:25 +08:00
|
|
|
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;
|