2022-02-18 14:17:32 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { generate } from '@ant-design/colors';
|
2022-03-08 10:29:00 +08:00
|
|
|
import { TinyColor } from '@ctrl/tinycolor';
|
2022-02-18 14:17:32 +08:00
|
|
|
import { CSSObject, Theme, useCacheToken, useStyleRegister } from '@ant-design/cssinjs';
|
|
|
|
import defaultDesignToken from './default';
|
|
|
|
import version from '../../version';
|
2022-03-08 10:29:00 +08:00
|
|
|
import { resetComponent, resetIcon } from './util';
|
|
|
|
import {
|
|
|
|
initSlideMotion,
|
|
|
|
slideUpIn,
|
|
|
|
slideUpOut,
|
|
|
|
slideDownIn,
|
|
|
|
slideDownOut,
|
|
|
|
slideLeftIn,
|
|
|
|
slideLeftOut,
|
|
|
|
slideRightIn,
|
|
|
|
slideRightOut,
|
|
|
|
} from './util/slide';
|
|
|
|
|
|
|
|
export {
|
|
|
|
resetComponent,
|
|
|
|
resetIcon,
|
|
|
|
initSlideMotion,
|
|
|
|
slideUpIn,
|
|
|
|
slideUpOut,
|
|
|
|
slideDownIn,
|
|
|
|
slideDownOut,
|
|
|
|
slideLeftIn,
|
|
|
|
slideLeftOut,
|
|
|
|
slideRightIn,
|
|
|
|
slideRightOut,
|
|
|
|
};
|
2022-02-18 14:17:32 +08:00
|
|
|
|
|
|
|
export interface DesignToken {
|
|
|
|
primaryColor: string;
|
2022-03-04 19:09:55 +08:00
|
|
|
successColor: string;
|
|
|
|
warningColor: string;
|
2022-02-18 14:17:32 +08:00
|
|
|
errorColor: string;
|
2022-03-04 19:09:55 +08:00
|
|
|
infoColor: string;
|
|
|
|
|
2022-02-18 14:17:32 +08:00
|
|
|
lineHeight: number;
|
|
|
|
borderWidth: number;
|
|
|
|
borderStyle: string;
|
|
|
|
borderRadius: number;
|
|
|
|
borderColor: string;
|
2022-03-08 10:29:00 +08:00
|
|
|
borderColorSplit: string;
|
2022-03-04 19:09:55 +08:00
|
|
|
|
2022-02-18 14:17:32 +08:00
|
|
|
easeInOut: string;
|
2022-03-04 19:09:55 +08:00
|
|
|
easeInOutCirc: string;
|
2022-03-01 21:12:00 +08:00
|
|
|
easeOutBack: string;
|
2022-03-08 10:29:00 +08:00
|
|
|
easeInQuint: string;
|
|
|
|
easeOutQuint: string;
|
|
|
|
|
|
|
|
outlineWidth: number;
|
|
|
|
outlineBlurSize: number;
|
2022-02-18 14:17:32 +08:00
|
|
|
|
|
|
|
fontSize: number;
|
2022-03-08 10:29:00 +08:00
|
|
|
fontFamily: string;
|
2022-02-18 14:17:32 +08:00
|
|
|
textColor: string;
|
2022-03-04 19:09:55 +08:00
|
|
|
textColorSecondary: string;
|
2022-02-18 14:17:32 +08:00
|
|
|
textColorDisabled: string;
|
2022-03-03 14:55:19 +08:00
|
|
|
textColorInverse: string;
|
2022-03-08 10:29:00 +08:00
|
|
|
placeholderColor: string;
|
2022-03-04 19:09:55 +08:00
|
|
|
|
|
|
|
iconColorHover: string;
|
|
|
|
|
2022-03-08 10:29:00 +08:00
|
|
|
headingColor: string;
|
|
|
|
|
2022-03-02 20:32:25 +08:00
|
|
|
itemHoverBackground: string;
|
|
|
|
|
2022-03-08 10:29:00 +08:00
|
|
|
controlHeight: number;
|
2022-02-18 14:17:32 +08:00
|
|
|
|
|
|
|
padding: number;
|
|
|
|
margin: number;
|
|
|
|
|
2022-03-01 21:12:00 +08:00
|
|
|
background: string;
|
2022-03-07 14:20:10 +08:00
|
|
|
backgroundLight: string;
|
|
|
|
|
2022-02-18 14:17:32 +08:00
|
|
|
componentBackground: string;
|
|
|
|
componentBackgroundDisabled: string;
|
|
|
|
|
2022-03-01 21:12:00 +08:00
|
|
|
duration: number;
|
2022-03-08 10:29:00 +08:00
|
|
|
|
|
|
|
zIndexDropdown: number;
|
|
|
|
|
|
|
|
boxShadow?: string;
|
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;
|
2022-03-08 10:29:00 +08:00
|
|
|
primaryOutlineColor: string;
|
2022-02-18 14:17:32 +08:00
|
|
|
errorHoverColor: string;
|
|
|
|
errorActiveColor: string;
|
2022-03-08 10:29:00 +08:00
|
|
|
errorOutlineColor: string;
|
|
|
|
warningHoverColor: string;
|
|
|
|
warningOutlineColor: 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;
|
2022-03-08 10:29:00 +08:00
|
|
|
/** @private Only Used for control inside component like Multiple Select inner selection item */
|
|
|
|
controlHeightXS: number;
|
|
|
|
controlHeightSM: number;
|
|
|
|
controlHeightLG: number;
|
|
|
|
controlPaddingHorizontal: number;
|
|
|
|
controlPaddingHorizontalSM: number;
|
|
|
|
paddingSM: number;
|
2022-02-18 14:17:32 +08:00
|
|
|
paddingXS: number;
|
2022-03-08 10:29:00 +08:00
|
|
|
paddingXXS: number;
|
2022-02-18 14:17:32 +08:00
|
|
|
marginXS: number;
|
2022-03-01 21:12:00 +08:00
|
|
|
|
|
|
|
duration: string;
|
2022-03-08 10:29:00 +08:00
|
|
|
durationMid: string;
|
2022-03-01 21:12:00 +08:00
|
|
|
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 {
|
2022-03-08 10:29:00 +08:00
|
|
|
const { primaryColor, errorColor, warningColor } = designToken;
|
|
|
|
|
|
|
|
const primaryColors = generate(primaryColor);
|
|
|
|
const errorColors = generate(errorColor);
|
|
|
|
const warningColors = generate(warningColor);
|
|
|
|
|
|
|
|
const paddingSM = (designToken.padding / 4) * 3;
|
|
|
|
const paddingXS = designToken.padding * 0.5;
|
2022-02-18 14:17:32 +08:00
|
|
|
|
|
|
|
return {
|
2022-03-08 10:29:00 +08:00
|
|
|
// FIXME: Need design token
|
|
|
|
boxShadow: `
|
|
|
|
0 3px 6px -4px rgba(0, 0, 0, 0.12),
|
|
|
|
0 6px 16px 0 rgba(0, 0, 0, 0.08),
|
|
|
|
0 9px 28px 8px rgba(0, 0, 0, 0.05)`,
|
|
|
|
|
2022-02-18 14:17:32 +08:00
|
|
|
...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],
|
2022-03-08 10:29:00 +08:00
|
|
|
primaryOutlineColor: new TinyColor(primaryColor).setAlpha(0.2).toRgbString(),
|
2022-02-18 14:17:32 +08:00
|
|
|
|
|
|
|
errorHoverColor: errorColors[4],
|
|
|
|
errorActiveColor: errorColors[6],
|
2022-03-08 10:29:00 +08:00
|
|
|
errorOutlineColor: new TinyColor(errorColor).setAlpha(0.2).toRgbString(),
|
|
|
|
|
|
|
|
warningHoverColor: warningColors[4],
|
|
|
|
warningOutlineColor: new TinyColor(warningColor).setAlpha(0.2).toRgbString(),
|
2022-02-18 14:17:32 +08:00
|
|
|
|
2022-03-08 10:29:00 +08:00
|
|
|
itemActiveBackground: primaryColors[0],
|
2022-03-02 20:32:25 +08:00
|
|
|
|
2022-03-08 10:29:00 +08:00
|
|
|
linkColor: primaryColor,
|
2022-02-18 14:17:32 +08:00
|
|
|
fontSizeSM: designToken.fontSize - 2,
|
|
|
|
fontSizeLG: designToken.fontSize + 2,
|
2022-03-08 10:29:00 +08:00
|
|
|
controlHeightXS: designToken.controlHeight / 2,
|
|
|
|
controlHeightSM: designToken.controlHeight * 0.75,
|
|
|
|
controlHeightLG: designToken.controlHeight * 1.25,
|
|
|
|
controlPaddingHorizontal: paddingSM,
|
|
|
|
controlPaddingHorizontalSM: paddingXS,
|
|
|
|
paddingSM,
|
|
|
|
paddingXS,
|
|
|
|
paddingXXS: designToken.padding * 0.25,
|
2022-02-18 14:17:32 +08:00
|
|
|
marginXS: designToken.margin * 0.5,
|
2022-03-01 21:12:00 +08:00
|
|
|
|
|
|
|
duration: `${designToken.duration}s`,
|
2022-03-08 10:29:00 +08:00
|
|
|
durationMid: `${(designToken.duration / 3) * 2}s`,
|
2022-03-01 21:12:00 +08:00
|
|
|
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 ==================================
|
2022-03-03 14:55:19 +08:00
|
|
|
export function useToken(): [Theme<DesignToken, DerivativeToken>, DerivativeToken, string] {
|
2022-02-18 14:17:32 +08:00
|
|
|
const { token: rootDesignToken, hashed } = React.useContext(DesignTokenContext);
|
|
|
|
const theme = React.useContext(ThemeContext);
|
|
|
|
|
|
|
|
const salt = `${version}-${hashed || ''}`;
|
|
|
|
|
2022-03-03 14:55:19 +08:00
|
|
|
const [token, hashId] = useCacheToken<DerivativeToken, DesignToken>(
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-03 14:55:19 +08:00
|
|
|
export type UseComponentStyleResult = [(node: React.ReactNode) => React.ReactElement, string];
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|