2022-02-18 14:17:32 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { generate } from '@ant-design/colors';
|
|
|
|
import { CSSObject, Theme, useCacheToken, useStyleRegister } from '@ant-design/cssinjs';
|
|
|
|
import defaultDesignToken from './default';
|
|
|
|
import version from '../../version';
|
2022-03-01 21:12:00 +08:00
|
|
|
import { resetComponent } from './util';
|
|
|
|
|
|
|
|
export { resetComponent };
|
2022-02-18 14:17:32 +08:00
|
|
|
|
|
|
|
export interface DesignToken {
|
|
|
|
primaryColor: string;
|
|
|
|
errorColor: string;
|
|
|
|
lineHeight: number;
|
|
|
|
borderWidth: number;
|
|
|
|
borderStyle: string;
|
|
|
|
borderRadius: number;
|
|
|
|
borderColor: string;
|
|
|
|
easeInOut: string;
|
2022-03-01 21:12:00 +08:00
|
|
|
easeOutBack: string;
|
2022-02-18 14:17:32 +08:00
|
|
|
|
|
|
|
fontSize: number;
|
|
|
|
textColor: string;
|
|
|
|
textColorDisabled: string;
|
|
|
|
|
2022-03-02 20:32:25 +08:00
|
|
|
itemHoverBackground: string;
|
|
|
|
|
2022-02-18 14:17:32 +08:00
|
|
|
height: number;
|
|
|
|
|
|
|
|
padding: number;
|
|
|
|
margin: number;
|
|
|
|
|
2022-03-01 21:12:00 +08:00
|
|
|
background: string;
|
2022-02-18 14:17:32 +08:00
|
|
|
componentBackground: string;
|
|
|
|
componentBackgroundDisabled: string;
|
|
|
|
|
2022-03-01 21:12:00 +08:00
|
|
|
duration: number;
|
2022-02-18 14:17:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** This is temporary token definition since final token definition is not ready yet. */
|
2022-03-01 21:12:00 +08:00
|
|
|
export interface DerivativeToken extends Omit<DesignToken, 'duration'> {
|
2022-02-18 14:17:32 +08:00
|
|
|
primaryHoverColor: string;
|
|
|
|
primaryActiveColor: string;
|
|
|
|
errorHoverColor: string;
|
|
|
|
errorActiveColor: string;
|
2022-03-02 20:32:25 +08:00
|
|
|
itemActiveBackground: string;
|
2022-02-18 14:17:32 +08:00
|
|
|
|
|
|
|
linkColor: string;
|
|
|
|
fontSizeSM: number;
|
|
|
|
fontSizeLG: number;
|
|
|
|
heightSM: number;
|
|
|
|
heightLG: number;
|
|
|
|
paddingXS: number;
|
|
|
|
marginXS: number;
|
2022-03-01 21:12:00 +08:00
|
|
|
|
|
|
|
duration: string;
|
|
|
|
durationFast: string;
|
2022-03-02 20:32:25 +08:00
|
|
|
|
|
|
|
// TMP
|
|
|
|
tmpPrimaryHoverColorWeak: string;
|
2022-02-18 14:17:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export { useStyleRegister };
|
|
|
|
|
|
|
|
// =============================== Derivative ===============================
|
|
|
|
function derivative(designToken: DesignToken): DerivativeToken {
|
|
|
|
const primaryColors = generate(designToken.primaryColor);
|
|
|
|
const errorColors = generate(designToken.errorColor);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...designToken,
|
|
|
|
|
2022-03-02 20:32:25 +08:00
|
|
|
tmpPrimaryHoverColorWeak: primaryColors[0],
|
2022-02-18 14:17:32 +08:00
|
|
|
primaryHoverColor: primaryColors[4],
|
|
|
|
primaryActiveColor: primaryColors[6],
|
|
|
|
|
|
|
|
errorHoverColor: errorColors[4],
|
|
|
|
errorActiveColor: errorColors[6],
|
|
|
|
|
2022-03-02 20:32:25 +08:00
|
|
|
itemActiveBackground: primaryColors[1],
|
|
|
|
|
2022-02-18 14:17:32 +08:00
|
|
|
linkColor: designToken.primaryColor,
|
|
|
|
fontSizeSM: designToken.fontSize - 2,
|
|
|
|
fontSizeLG: designToken.fontSize + 2,
|
|
|
|
heightSM: designToken.height * 0.75,
|
|
|
|
heightLG: designToken.height * 1.25,
|
|
|
|
paddingXS: designToken.padding * 0.5,
|
|
|
|
marginXS: designToken.margin * 0.5,
|
2022-03-01 21:12:00 +08:00
|
|
|
|
|
|
|
duration: `${designToken.duration}s`,
|
|
|
|
durationFast: `${designToken.duration / 3}s`,
|
2022-02-18 14:17:32 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// ================================ Context =================================
|
|
|
|
export const ThemeContext = React.createContext(
|
|
|
|
new Theme<DesignToken, DerivativeToken>(derivative),
|
|
|
|
);
|
|
|
|
|
|
|
|
export const DesignTokenContext = React.createContext<{
|
|
|
|
token: Partial<DesignToken>;
|
|
|
|
hashed?: string | boolean;
|
|
|
|
}>({
|
|
|
|
token: defaultDesignToken,
|
|
|
|
});
|
|
|
|
|
|
|
|
// ================================== Hook ==================================
|
|
|
|
export function useToken() {
|
|
|
|
const { token: rootDesignToken, hashed } = React.useContext(DesignTokenContext);
|
|
|
|
const theme = React.useContext(ThemeContext);
|
|
|
|
|
|
|
|
const salt = `${version}-${hashed || ''}`;
|
|
|
|
|
|
|
|
const [token, hashId] = useCacheToken(theme, [defaultDesignToken, rootDesignToken], {
|
|
|
|
salt,
|
|
|
|
});
|
2022-02-27 22:17:17 +08:00
|
|
|
return [theme, token, hashed ? hashId : ''];
|
2022-02-18 14:17:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ================================== Util ==================================
|
|
|
|
export function withPrefix(
|
|
|
|
style: CSSObject,
|
|
|
|
prefixCls: string,
|
|
|
|
additionalClsList: string[] = [],
|
|
|
|
): CSSObject {
|
|
|
|
const fullClsList = [prefixCls, ...additionalClsList].filter(cls => cls).map(cls => `.${cls}`);
|
|
|
|
|
|
|
|
return {
|
|
|
|
[fullClsList.join('')]: style,
|
|
|
|
};
|
|
|
|
}
|