2022-02-18 14:17:32 +08:00
|
|
|
import React from 'react';
|
2022-03-24 18:44:42 +08:00
|
|
|
import { CSSInterpolation, Theme, useCacheToken, useStyleRegister } from '@ant-design/cssinjs';
|
2022-03-21 16:48:25 +08:00
|
|
|
import defaultSeedToken, { derivative as defaultDerivative } from './themes/default';
|
2022-02-18 14:17:32 +08:00
|
|
|
import version from '../../version';
|
2022-03-09 10:38:02 +08:00
|
|
|
import { resetComponent, resetIcon, clearFix } from './util';
|
2022-03-21 16:48:25 +08:00
|
|
|
import formatToken from './util/alias';
|
2022-03-08 10:29:00 +08:00
|
|
|
import {
|
|
|
|
initSlideMotion,
|
|
|
|
slideUpIn,
|
|
|
|
slideUpOut,
|
|
|
|
slideDownIn,
|
|
|
|
slideDownOut,
|
|
|
|
slideLeftIn,
|
|
|
|
slideLeftOut,
|
|
|
|
slideRightIn,
|
|
|
|
slideRightOut,
|
|
|
|
} from './util/slide';
|
2022-03-24 18:44:42 +08:00
|
|
|
import { GlobalToken, PresetColors } from './interface';
|
2022-03-21 16:48:25 +08:00
|
|
|
import type {
|
|
|
|
SeedToken,
|
|
|
|
DerivativeToken,
|
|
|
|
AliasToken,
|
|
|
|
OverrideToken,
|
|
|
|
PresetColorType,
|
|
|
|
} from './interface';
|
2022-03-08 10:29:00 +08:00
|
|
|
|
|
|
|
export {
|
2022-03-21 16:48:25 +08:00
|
|
|
PresetColors,
|
2022-03-08 10:29:00 +08:00
|
|
|
resetComponent,
|
|
|
|
resetIcon,
|
2022-03-09 10:38:02 +08:00
|
|
|
clearFix,
|
2022-03-08 10:29:00 +08:00
|
|
|
initSlideMotion,
|
|
|
|
slideUpIn,
|
|
|
|
slideUpOut,
|
|
|
|
slideDownIn,
|
|
|
|
slideDownOut,
|
|
|
|
slideLeftIn,
|
|
|
|
slideLeftOut,
|
|
|
|
slideRightIn,
|
|
|
|
slideRightOut,
|
2022-03-21 16:48:25 +08:00
|
|
|
useStyleRegister,
|
2022-03-08 10:29:00 +08:00
|
|
|
};
|
2022-02-18 14:17:32 +08:00
|
|
|
|
2022-03-21 16:48:25 +08:00
|
|
|
export type {
|
|
|
|
SeedToken,
|
|
|
|
AliasToken,
|
|
|
|
PresetColorType,
|
|
|
|
// FIXME: Remove this type
|
|
|
|
AliasToken as DerivativeToken,
|
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-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
|
|
|
|
|
|
|
const salt = `${version}-${hashed || ''}`;
|
|
|
|
|
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-03-11 14:29:29 +08:00
|
|
|
export type GenerateStyle<ComponentToken extends object, ReturnType = CSSInterpolation> = (
|
|
|
|
token: ComponentToken,
|
|
|
|
hashId?: string,
|
|
|
|
) => ReturnType;
|