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-03-11 14:29:29 +08:00
|
|
|
import {
|
|
|
|
CSSInterpolation,
|
|
|
|
CSSObject,
|
|
|
|
Theme,
|
|
|
|
useCacheToken,
|
|
|
|
useStyleRegister,
|
|
|
|
} from '@ant-design/cssinjs';
|
2022-02-18 14:17:32 +08:00
|
|
|
import defaultDesignToken from './default';
|
|
|
|
import version from '../../version';
|
2022-03-09 10:38:02 +08:00
|
|
|
import { resetComponent, resetIcon, clearFix } from './util';
|
2022-03-08 10:29:00 +08:00
|
|
|
import {
|
|
|
|
initSlideMotion,
|
|
|
|
slideUpIn,
|
|
|
|
slideUpOut,
|
|
|
|
slideDownIn,
|
|
|
|
slideDownOut,
|
|
|
|
slideLeftIn,
|
|
|
|
slideLeftOut,
|
|
|
|
slideRightIn,
|
|
|
|
slideRightOut,
|
|
|
|
} from './util/slide';
|
|
|
|
|
|
|
|
export {
|
|
|
|
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-02-18 14:17:32 +08:00
|
|
|
|
2022-03-15 15:33:50 +08:00
|
|
|
export interface PresetColorType {
|
|
|
|
blue: string;
|
|
|
|
purple: string;
|
|
|
|
cyan: string;
|
|
|
|
green: string;
|
|
|
|
magenta: string;
|
|
|
|
pink: string;
|
|
|
|
red: string;
|
|
|
|
orange: string;
|
|
|
|
yellow: string;
|
|
|
|
volcano: string;
|
|
|
|
geekblue: string;
|
|
|
|
lime: string;
|
|
|
|
gold: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const PresetColorKeys: ReadonlyArray<keyof PresetColorType> = [
|
|
|
|
'blue',
|
|
|
|
'purple',
|
|
|
|
'cyan',
|
|
|
|
'green',
|
|
|
|
'magenta',
|
|
|
|
'pink',
|
|
|
|
'red',
|
|
|
|
'orange',
|
|
|
|
'yellow',
|
|
|
|
'volcano',
|
|
|
|
'geekblue',
|
|
|
|
'lime',
|
|
|
|
'gold',
|
|
|
|
];
|
|
|
|
|
|
|
|
export interface DesignToken extends PresetColorType {
|
2022-02-18 14:17:32 +08:00
|
|
|
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
|
|
|
|
2022-03-16 19:10:36 +08:00
|
|
|
disabledColor: 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
|
|
|
}
|
|
|
|
|
2022-03-15 15:33:50 +08:00
|
|
|
type ColorPaletteKeyIndexes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
|
|
|
|
|
|
type ColorPalettes = {
|
|
|
|
[key in `${keyof PresetColorType}-${ColorPaletteKeyIndexes[number]}`]: string;
|
|
|
|
};
|
|
|
|
|
2022-02-18 14:17:32 +08:00
|
|
|
/** This is temporary token definition since final token definition is not ready yet. */
|
2022-03-15 15:33:50 +08:00
|
|
|
export interface DerivativeToken extends ColorPalettes, 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
|
|
|
|
2022-03-09 00:29:00 +08:00
|
|
|
highlightColor: string;
|
|
|
|
|
2022-02-18 14:17:32 +08:00
|
|
|
linkColor: string;
|
2022-03-16 19:10:36 +08:00
|
|
|
linkHoverColor: string;
|
|
|
|
linkActiveColor: string;
|
|
|
|
linkDecoration: CSSObject['textDecoration'];
|
|
|
|
linkHoverDecoration: CSSObject['textDecoration'];
|
|
|
|
linkFocusDecoration: CSSObject['textDecoration'];
|
|
|
|
|
2022-02-18 14:17:32 +08:00
|
|
|
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-03-14 20:57:01 +08:00
|
|
|
paddingLG: number;
|
2022-02-18 14:17:32 +08:00
|
|
|
marginXS: number;
|
2022-03-10 13:59:49 +08:00
|
|
|
marginLG: number;
|
2022-03-09 10:38:02 +08:00
|
|
|
marginXXS: 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
|
|
|
|
2022-03-16 19:10:36 +08:00
|
|
|
heading1Size: number;
|
|
|
|
heading2Size: number;
|
|
|
|
heading3Size: number;
|
|
|
|
heading4Size: number;
|
|
|
|
heading5Size: number;
|
|
|
|
|
|
|
|
primaryColors: string[];
|
|
|
|
errorColors: string[];
|
|
|
|
warningColors: string[];
|
|
|
|
|
2022-03-02 20:32:25 +08:00
|
|
|
// TMP
|
2022-03-14 19:12:10 +08:00
|
|
|
tmpPrimaryColorWeak: string;
|
2022-03-02 20:32:25 +08:00
|
|
|
tmpPrimaryHoverColorWeak: string;
|
2022-03-15 15:33:50 +08:00
|
|
|
// Checked background for Checkable Tag
|
|
|
|
tmpPrimaryColor6: string;
|
|
|
|
// Active background for Checkable Tag
|
|
|
|
tmpPrimaryColor7: string;
|
|
|
|
|
|
|
|
tmpSuccessColorDeprecatedBg: string;
|
|
|
|
tmpWarningColorDeprecatedBg: string;
|
|
|
|
tmpErrorColorDeprecatedBg: string;
|
|
|
|
tmpInfoColorDeprecatedBg: string;
|
|
|
|
|
|
|
|
tmpSuccessColorDeprecatedBorder: string;
|
|
|
|
tmpWarningColorDeprecatedBorder: string;
|
|
|
|
tmpErrorColorDeprecatedBorder: string;
|
|
|
|
tmpInfoColorDeprecatedBorder: string;
|
2022-02-18 14:17:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export { useStyleRegister };
|
|
|
|
|
|
|
|
// =============================== Derivative ===============================
|
|
|
|
function derivative(designToken: DesignToken): DerivativeToken {
|
2022-03-15 15:33:50 +08:00
|
|
|
const { primaryColor, errorColor, warningColor, infoColor, successColor } = designToken;
|
2022-03-08 10:29:00 +08:00
|
|
|
|
|
|
|
const primaryColors = generate(primaryColor);
|
|
|
|
const errorColors = generate(errorColor);
|
|
|
|
const warningColors = generate(warningColor);
|
2022-03-15 15:33:50 +08:00
|
|
|
const infoColors = generate(infoColor);
|
|
|
|
const successColors = generate(successColor);
|
2022-03-08 10:29:00 +08:00
|
|
|
|
|
|
|
const paddingSM = (designToken.padding / 4) * 3;
|
|
|
|
const paddingXS = designToken.padding * 0.5;
|
2022-02-18 14:17:32 +08:00
|
|
|
|
2022-03-15 15:33:50 +08:00
|
|
|
const colorPalettes = PresetColorKeys.map((colorKey: keyof PresetColorType) => {
|
|
|
|
const colors = generate(designToken[colorKey]);
|
|
|
|
|
|
|
|
const ret = new Array(10).fill(1).reduce((prev, _, i) => {
|
|
|
|
prev[`${colorKey}-${i + 1}`] = colors[i];
|
|
|
|
return prev;
|
|
|
|
}, {}) as ColorPalettes;
|
|
|
|
return ret;
|
|
|
|
}).reduce((prev, cur) => {
|
|
|
|
prev = {
|
|
|
|
...prev,
|
|
|
|
...cur,
|
|
|
|
};
|
|
|
|
return prev;
|
|
|
|
}, {} as ColorPalettes);
|
|
|
|
|
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,
|
|
|
|
|
|
|
|
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-18 17:26:17 +08:00
|
|
|
highlightColor: errorColors[5], // FIXME: Should not align with error color
|
|
|
|
// FIXME: fix2 badge-color
|
2022-03-09 00:29:00 +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-03-16 19:10:36 +08:00
|
|
|
linkHoverColor: primaryColors[4],
|
|
|
|
linkActiveColor: primaryColors[6],
|
|
|
|
linkDecoration: 'none',
|
|
|
|
linkHoverDecoration: 'none',
|
|
|
|
linkFocusDecoration: 'none',
|
|
|
|
|
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-03-14 20:57:01 +08:00
|
|
|
paddingLG: designToken.padding * 1.5,
|
2022-02-18 14:17:32 +08:00
|
|
|
marginXS: designToken.margin * 0.5,
|
2022-03-10 13:59:49 +08:00
|
|
|
marginLG: designToken.margin * 1.5,
|
2022-03-09 10:38:02 +08:00
|
|
|
marginXXS: designToken.margin * 0.25,
|
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-03-15 15:33:50 +08:00
|
|
|
|
|
|
|
...colorPalettes,
|
|
|
|
|
2022-03-16 19:10:36 +08:00
|
|
|
heading1Size: Math.ceil(designToken.fontSize * 2.71),
|
|
|
|
heading2Size: Math.ceil(designToken.fontSize * 2.14),
|
|
|
|
heading3Size: Math.ceil(designToken.fontSize * 1.71),
|
|
|
|
heading4Size: Math.ceil(designToken.fontSize * 1.42),
|
|
|
|
heading5Size: Math.ceil(designToken.fontSize * 1.14),
|
|
|
|
|
|
|
|
primaryColors,
|
|
|
|
errorColors,
|
|
|
|
warningColors,
|
|
|
|
|
2022-03-15 15:33:50 +08:00
|
|
|
// TMP
|
|
|
|
tmpPrimaryColorWeak: primaryColors[2],
|
|
|
|
tmpPrimaryHoverColorWeak: primaryColors[0],
|
|
|
|
tmpPrimaryColor6: primaryColors[5],
|
|
|
|
tmpPrimaryColor7: primaryColors[6],
|
|
|
|
|
|
|
|
tmpSuccessColorDeprecatedBg: successColors[0],
|
|
|
|
tmpWarningColorDeprecatedBg: warningColors[0],
|
|
|
|
tmpErrorColorDeprecatedBg: errorColors[0],
|
|
|
|
tmpInfoColorDeprecatedBg: infoColors[0],
|
|
|
|
|
|
|
|
tmpSuccessColorDeprecatedBorder: successColors[2],
|
|
|
|
tmpWarningColorDeprecatedBorder: warningColors[2],
|
|
|
|
tmpErrorColorDeprecatedBorder: errorColors[2],
|
|
|
|
tmpInfoColorDeprecatedBorder: infoColors[2],
|
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-03-11 14:29:29 +08:00
|
|
|
export type GenerateStyle<ComponentToken extends object, ReturnType = CSSInterpolation> = (
|
|
|
|
token: ComponentToken,
|
|
|
|
hashId?: string,
|
|
|
|
) => ReturnType;
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|