You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.4 KiB
89 lines
2.4 KiB
2 years ago
|
import type { CSSInterpolation, Theme } from '@ant-design/cssinjs';
|
||
|
import { createTheme, useCacheToken, useStyleRegister } from '@ant-design/cssinjs';
|
||
3 years ago
|
import React from 'react';
|
||
2 years ago
|
import version from '../version';
|
||
3 years ago
|
import type {
|
||
|
AliasToken,
|
||
3 years ago
|
GlobalToken,
|
||
2 years ago
|
MapToken,
|
||
3 years ago
|
OverrideToken,
|
||
|
PresetColorType,
|
||
3 years ago
|
SeedToken,
|
||
3 years ago
|
} from './interface';
|
||
3 years ago
|
import { PresetColors } from './interface';
|
||
2 years ago
|
import defaultDerivative from './themes/default';
|
||
|
import defaultSeedToken from './themes/seed';
|
||
3 years ago
|
import formatToken from './util/alias';
|
||
3 years ago
|
import type { FullToken } from './util/genComponentStyleHook';
|
||
3 years ago
|
import genComponentStyleHook from './util/genComponentStyleHook';
|
||
2 years ago
|
import statisticToken, { merge as mergeToken, statistic } from './util/statistic';
|
||
3 years ago
|
|
||
2 years ago
|
const defaultTheme = createTheme(defaultDerivative);
|
||
2 years ago
|
|
||
3 years ago
|
export {
|
||
3 years ago
|
// colors
|
||
|
PresetColors,
|
||
3 years ago
|
// Statistic
|
||
3 years ago
|
statistic,
|
||
3 years ago
|
statisticToken,
|
||
|
mergeToken,
|
||
3 years ago
|
// hooks
|
||
|
useStyleRegister,
|
||
3 years ago
|
genComponentStyleHook,
|
||
3 years ago
|
};
|
||
3 years ago
|
export type {
|
||
|
SeedToken,
|
||
|
AliasToken,
|
||
|
PresetColorType,
|
||
|
// FIXME: Remove this type
|
||
|
AliasToken as DerivativeToken,
|
||
3 years ago
|
FullToken,
|
||
3 years ago
|
};
|
||
|
|
||
3 years ago
|
// ================================ Context =================================
|
||
2 years ago
|
// To ensure snapshot stable. We disable hashed in test env.
|
||
|
export const defaultConfig = {
|
||
|
token: defaultSeedToken,
|
||
|
hashed: true,
|
||
|
};
|
||
|
|
||
3 years ago
|
export const DesignTokenContext = React.createContext<{
|
||
2 years ago
|
token: Partial<AliasToken>;
|
||
2 years ago
|
theme?: Theme<SeedToken, MapToken>;
|
||
2 years ago
|
components?: OverrideToken;
|
||
3 years ago
|
hashed?: string | boolean;
|
||
2 years ago
|
}>(defaultConfig);
|
||
3 years ago
|
|
||
|
// ================================== Hook ==================================
|
||
2 years ago
|
export function useToken(): [Theme<SeedToken, MapToken>, GlobalToken, string] {
|
||
2 years ago
|
const {
|
||
|
token: rootDesignToken,
|
||
|
hashed,
|
||
|
theme,
|
||
|
components,
|
||
|
} = React.useContext(DesignTokenContext);
|
||
2 years ago
|
|
||
2 years ago
|
const salt = `${version}-${hashed || ''}`;
|
||
3 years ago
|
|
||
2 years ago
|
const mergedTheme = theme || defaultTheme;
|
||
|
|
||
3 years ago
|
const [token, hashId] = useCacheToken<GlobalToken, SeedToken>(
|
||
2 years ago
|
mergedTheme,
|
||
3 years ago
|
[defaultSeedToken, rootDesignToken],
|
||
3 years ago
|
{
|
||
|
salt,
|
||
2 years ago
|
override: { override: rootDesignToken, ...components },
|
||
3 years ago
|
formatToken,
|
||
3 years ago
|
},
|
||
|
);
|
||
3 years ago
|
|
||
2 years ago
|
return [mergedTheme, token, hashed ? hashId : ''];
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
export type UseComponentStyleResult = [(node: React.ReactNode) => React.ReactElement, string];
|
||
|
|
||
3 years ago
|
export type GenerateStyle<
|
||
|
ComponentToken extends object = AliasToken,
|
||
|
ReturnType = CSSInterpolation,
|
||
|
> = (token: ComponentToken) => ReturnType;
|