Skip to content

Commit

Permalink
refactor: merge preset colors (ant-design#39742)
Browse files Browse the repository at this point in the history
* style: remove redundant code

* feat(style): add preset colour style generation method

(cherry picked from commit 1266e42)

* feat: uniform preset colour generation css selector method

(cherry picked from commit 5af87e8)

* chore: merge preset colors

(cherry picked from commit 05040df)

* chore: update

(cherry picked from commit 241b40a)

* chore: remove Badge preset inverse colors

* chore: remove fix

删除的这部分其实一个 Bug,但是为了给 PR 减负(尽可能单一, 后面新开一个 PR 来修复这个问题

* suggestions accepted

Update components/style/presetColor.tsx

Co-authored-by: MadCcc <1075746765@qq.com>

Co-authored-by: MadCcc <1075746765@qq.com>
  • Loading branch information
Wxh16144 and MadCcc authored Jan 6, 2023
1 parent 67e958f commit 7f189d5
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 126 deletions.
41 changes: 24 additions & 17 deletions components/_util/colors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import type { PresetColorKey } from '../theme/interface';
import { PresetColors } from '../theme/interface';

type InverseColor = `${PresetColorKey}-inverse`;
const inverseColors = PresetColors.map<InverseColor>((color) => `${color}-inverse`);

export const PresetStatusColorTypes = [
'success',
'processing',
Expand All @@ -6,22 +12,23 @@ export const PresetStatusColorTypes = [
'warning',
] as const;

export const PresetColorTypes = [
'pink',
'red',
'yellow',
'orange',
'cyan',
'green',
'blue',
'purple',
'geekblue',
'magenta',
'volcano',
'gold',
'lime',
] as const;

export type PresetColorType = typeof PresetColorTypes[number];
export type PresetColorType = PresetColorKey | InverseColor;

export type PresetStatusColorType = typeof PresetStatusColorTypes[number];

/**
* determine if the color keyword belongs to the `Ant Design` {@link PresetColors}.
* @param color color to be judged
* @param includeInverse whether to include reversed colors
*/
export function isPresetColor(color?: any, includeInverse = true) {
if (includeInverse) {
return [...inverseColors, ...PresetColors].includes(color);
}

return PresetColors.includes(color);
}

export function isPresetStatusColor(color?: any): color is PresetStatusColorType {
return PresetStatusColorTypes.includes(color);
}
4 changes: 2 additions & 2 deletions components/badge/Ribbon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ConfigContext } from '../config-provider';
import type { PresetColorType } from '../_util/colors';
import type { LiteralUnion } from '../_util/type';
import useStyle from './style';
import { isPresetColor } from './utils';
import { isPresetColor } from '../_util/colors';

type RibbonPlacement = 'start' | 'end';

Expand All @@ -29,7 +29,7 @@ const Ribbon: React.FC<RibbonProps> = function Ribbon({
}) {
const { getPrefixCls, direction } = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('ribbon', customizePrefixCls);
const colorInPreset = isPresetColor(color);
const colorInPreset = isPresetColor(color, false);
const ribbonCls = classNames(
prefixCls,
`${prefixCls}-placement-${placement}`,
Expand Down
18 changes: 11 additions & 7 deletions components/badge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import CSSMotion from 'rc-motion';
import * as React from 'react';
import { useMemo, useRef } from 'react';
import { ConfigContext } from '../config-provider';
import type { PresetColorType, PresetStatusColorType } from '../_util/colors';
import type { PresetStatusColorType } from '../_util/colors';
import { cloneElement } from '../_util/reactNode';
import type { LiteralUnion } from '../_util/type';
import Ribbon from './Ribbon';
import ScrollNumber from './ScrollNumber';
import useStyle from './style';
import { isPresetColor } from './utils';
import { isPresetColor } from '../_util/colors';
import type { PresetColorKey } from '../theme/internal';

export type { ScrollNumberProps } from './ScrollNumber';

Expand All @@ -30,7 +31,7 @@ export interface BadgeProps {
scrollNumberPrefixCls?: string;
className?: string;
status?: PresetStatusColorType;
color?: LiteralUnion<PresetColorType>;
color?: LiteralUnion<PresetColorKey>;
text?: React.ReactNode;
size?: 'default' | 'small';
offset?: [number | string, number | string];
Expand Down Expand Up @@ -144,15 +145,18 @@ const Badge: CompoundedComponent = ({
},
}));

// InternalColor
const isInternalColor = isPresetColor(color, false);

// Shared styles
const statusCls = classNames({
[`${prefixCls}-status-dot`]: hasStatus,
[`${prefixCls}-status-${status}`]: !!status,
[`${prefixCls}-status-${color}`]: isPresetColor(color),
[`${prefixCls}-status-${color}`]: isInternalColor,
});

const statusStyle: React.CSSProperties = {};
if (color && !isPresetColor(color)) {
if (color && !isInternalColor) {
statusStyle.color = color;
statusStyle.background = color;
}
Expand Down Expand Up @@ -207,11 +211,11 @@ const Badge: CompoundedComponent = ({
[`${prefixCls}-multiple-words`]:
!isDot && displayCount && displayCount.toString().length > 1,
[`${prefixCls}-status-${status}`]: !!status,
[`${prefixCls}-status-${color}`]: isPresetColor(color),
[`${prefixCls}-status-${color}`]: isInternalColor,
});

let scrollNumberStyle: React.CSSProperties = { ...mergedStyle };
if (color && !isPresetColor(color)) {
if (color && !isInternalColor) {
scrollNumberStyle = scrollNumberStyle || {};
scrollNumberStyle.background = color;
}
Expand Down
38 changes: 14 additions & 24 deletions components/badge/style/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { CSSObject } from '@ant-design/cssinjs';
import { Keyframes } from '@ant-design/cssinjs';
import type { FullToken, GenerateStyle, PresetColorType } from '../../theme/internal';
import { genComponentStyleHook, mergeToken, PresetColors } from '../../theme/internal';
import { resetComponent } from '../../style';
import type { FullToken, GenerateStyle } from '../../theme/internal';
import { genComponentStyleHook, mergeToken } from '../../theme/internal';
import { genPresetColor, resetComponent } from '../../style';

interface BadgeToken extends FullToken<'Badge'> {
badgeFontHeight: number;
Expand Down Expand Up @@ -73,28 +73,18 @@ const genSharedBadgeStyle: GenerateStyle<BadgeToken> = (token: BadgeToken): CSSO
const ribbonPrefixCls = `${antCls}-ribbon`;
const ribbonWrapperPrefixCls = `${antCls}-ribbon-wrapper`;

const statusPreset = PresetColors.reduce((prev: CSSObject, colorKey: keyof PresetColorType) => {
const darkColor = token[`${colorKey}-6`];
return {
...prev,
[`${componentCls}-status-${colorKey}`]: {
background: darkColor,
},
};
}, {} as CSSObject);
const statusRibbonPreset = PresetColors.reduce(
(prev: CSSObject, colorKey: keyof PresetColorType) => {
const darkColor = token[`${colorKey}-6`];
return {
...prev,
[`&${ribbonPrefixCls}-color-${colorKey}`]: {
background: darkColor,
color: darkColor,
},
};
const statusPreset = genPresetColor(token, (colorKey, { darkColor }) => ({
[`${componentCls}-status-${colorKey}`]: {
background: darkColor,
},
}));

const statusRibbonPreset = genPresetColor(token, (colorKey, { darkColor }) => ({
[`&${ribbonPrefixCls}-color-${colorKey}`]: {
background: darkColor,
color: darkColor,
},
{} as CSSObject,
);
}));

return {
[componentCls]: {
Expand Down
6 changes: 0 additions & 6 deletions components/badge/utils.ts

This file was deleted.

1 change: 1 addition & 0 deletions components/style/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { DerivativeToken } from '../theme/internal';

export { operationUnit } from './operationUnit';
export { roundedArrow } from './roundedArrow';
export { genPresetColor } from './presetColor';

export const textEllipsis: CSSObject = {
overflow: 'hidden',
Expand Down
35 changes: 35 additions & 0 deletions components/style/presetColor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable import/prefer-default-export */
import type { CSSObject } from '@ant-design/cssinjs';
import type { AliasToken, PresetColorKey } from '../theme/internal';
import { PresetColors } from '../theme/internal';
import type { TokenWithCommonCls } from '../theme/util/genComponentStyleHook';

interface CalcColor {
/** token[`${colorKey}-1`] */
lightColor: string;
/** token[`${colorKey}-3`] */
lightBorderColor: string;
/** token[`${colorKey}-6`] */
darkColor: string;
/** token[`${colorKey}-7`] */
textColor: string;
}

type GenCSS = (colorKey: PresetColorKey, calcColor: CalcColor) => CSSObject;

export function genPresetColor<Token extends TokenWithCommonCls<AliasToken>>(
token: Token,
genCss: GenCSS,
): CSSObject {
return PresetColors.reduce((prev: CSSObject, colorKey: PresetColorKey) => {
const lightColor = token[`${colorKey}-1`];
const lightBorderColor = token[`${colorKey}-3`];
const darkColor = token[`${colorKey}-6`];
const textColor = token[`${colorKey}-7`];

return {
...prev,
...genCss(colorKey, { lightColor, lightBorderColor, darkColor, textColor }),
};
}, {} as CSSObject);
}
19 changes: 5 additions & 14 deletions components/tag/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import classNames from 'classnames';
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import type { PresetColorType, PresetStatusColorType } from '../_util/colors';
import { PresetColorTypes, PresetStatusColorTypes } from '../_util/colors';
import { isPresetColor, isPresetStatusColor } from '../_util/colors';
import Wave from '../_util/wave';
import warning from '../_util/warning';
import CheckableTag from './CheckableTag';
Expand All @@ -25,9 +25,6 @@ export interface TagProps extends React.HTMLAttributes<HTMLSpanElement> {
icon?: React.ReactNode;
}

const PresetColorRegex = new RegExp(`^(${PresetColorTypes.join('|')})(-inverse)?$`);
const PresetStatusColorRegex = new RegExp(`^(${PresetStatusColorTypes.join('|')})$`);

export interface TagType
extends React.ForwardRefExoticComponent<TagProps & React.RefAttributes<HTMLElement>> {
CheckableTag: typeof CheckableTag;
Expand Down Expand Up @@ -66,28 +63,22 @@ const InternalTag: React.ForwardRefRenderFunction<HTMLSpanElement, TagProps> = (
}
}, [props.visible]);

const isPresetColor = (): boolean => {
if (!color) {
return false;
}
return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
};
const isInternalColor = isPresetColor(color) || isPresetStatusColor(color);

const tagStyle = {
backgroundColor: color && !isPresetColor() ? color : undefined,
backgroundColor: color && !isInternalColor ? color : undefined,
...style,
};

const presetColor = isPresetColor();
const prefixCls = getPrefixCls('tag', customizePrefixCls);
// Style
const [wrapSSR, hashId] = useStyle(prefixCls);

const tagClassName = classNames(
prefixCls,
{
[`${prefixCls}-${color}`]: presetColor,
[`${prefixCls}-has-color`]: color && !presetColor,
[`${prefixCls}-${color}`]: isInternalColor,
[`${prefixCls}-has-color`]: color && !isInternalColor,
[`${prefixCls}-hidden`]: !visible,
[`${prefixCls}-rtl`]: direction === 'rtl',
},
Expand Down
38 changes: 16 additions & 22 deletions components/tag/style/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { CSSInterpolation, CSSObject } from '@ant-design/cssinjs';
import type { CSSInterpolation } from '@ant-design/cssinjs';
import type React from 'react';
import type { FullToken, PresetColorType } from '../../theme/internal';
import { genComponentStyleHook, mergeToken, PresetColors } from '../../theme/internal';
import type { FullToken } from '../../theme/internal';
import { genComponentStyleHook, mergeToken } from '../../theme/internal';
import capitalize from '../../_util/capitalize';
import { resetComponent } from '../../style';
import { genPresetColor, resetComponent } from '../../style';

export interface ComponentToken {}

Expand Down Expand Up @@ -35,27 +35,21 @@ const genTagStatusStyle = (
};
};

// FIXME: special preset colors
const genTagColorStyle = (token: TagToken): CSSInterpolation =>
PresetColors.reduce((prev: CSSObject, colorKey: keyof PresetColorType) => {
const lightColor = token[`${colorKey}-1`];
const lightBorderColor = token[`${colorKey}-3`];
const darkColor = token[`${colorKey}-6`];
const textColor = token[`${colorKey}-7`];
return {
...prev,
[`${token.componentCls}-${colorKey}`]: {
color: textColor,
background: lightColor,
borderColor: lightBorderColor,
},
[`${token.componentCls}-${colorKey}-inverse`]: {
const genPresetStyle = (token: TagToken) =>
genPresetColor(token, (colorKey, { textColor, lightBorderColor, lightColor, darkColor }) => ({
[`${token.componentCls}-${colorKey}`]: {
color: textColor,
background: lightColor,
borderColor: lightBorderColor,

// Inverse color
'&-inverse': {
color: token.colorTextLightSolid,
background: darkColor,
borderColor: darkColor,
},
};
}, {} as CSSObject);
},
}));

const genBaseStyle = (token: TagToken): CSSInterpolation => {
const { paddingXXS, lineWidth, tagPaddingHorizontal } = token;
Expand Down Expand Up @@ -168,7 +162,7 @@ export default genComponentStyleHook('Tag', (token) => {

return [
genBaseStyle(tagToken),
genTagColorStyle(tagToken),
genPresetStyle(tagToken),
genTagStatusStyle(tagToken, 'success', 'Success'),
genTagStatusStyle(tagToken, 'processing', 'Info'),
genTagStatusStyle(tagToken, 'error', 'Error'),
Expand Down
2 changes: 1 addition & 1 deletion components/theme/interface/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type OverrideToken = {
export type GlobalToken = AliasToken & ComponentTokenMap;

export { PresetColors } from './presetColors';
export type { PresetColorType, ColorPalettes } from './presetColors';
export type { PresetColorType, ColorPalettes, PresetColorKey } from './presetColors';
export type { SeedToken } from './seeds';
export type {
MapToken,
Expand Down
2 changes: 1 addition & 1 deletion components/theme/interface/presetColors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const PresetColors = [
'gold',
] as const;

type PresetColorKey = typeof PresetColors[number];
export type PresetColorKey = typeof PresetColors[number];

export type PresetColorType = Record<PresetColorKey, string>;

Expand Down
2 changes: 2 additions & 0 deletions components/theme/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
MapToken,
OverrideToken,
PresetColorType,
PresetColorKey,
SeedToken,
} from './interface';
import { PresetColors } from './interface';
Expand Down Expand Up @@ -35,6 +36,7 @@ export type {
SeedToken,
AliasToken,
PresetColorType,
PresetColorKey,
// FIXME: Remove this type
AliasToken as DerivativeToken,
FullToken,
Expand Down
Loading

0 comments on commit 7f189d5

Please sign in to comment.