Skip to content

Commit

Permalink
feat: compact theme (ant-design#38105)
Browse files Browse the repository at this point in the history
* feat: compact theme

* chore: code clean

* chore: code clean

* chore: show dynamic theme in preview

* chore: code clean

* feat: compact algorithm
  • Loading branch information
MadCcc authored Oct 25, 2022
1 parent d08deee commit ffc09a2
Show file tree
Hide file tree
Showing 35 changed files with 268 additions and 171 deletions.
16 changes: 9 additions & 7 deletions components/alert/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface ComponentToken {}

type AlertToken = FullToken<'Alert'> & {
alertIconSizeLG: number;
alertPaddingHorizontal: number;
};

const genAlertTypeStyle = (
Expand Down Expand Up @@ -36,10 +37,10 @@ export const genBaseStyle: GenerateStyle<AlertToken> = (token: AlertToken): CSSO
motionEaseInOutCirc,
alertIconSizeLG,
colorText,
paddingSM,
paddingXS,
paddingTmp,
paddingLG,
paddingContentVerticalSM,
alertPaddingHorizontal,
paddingMD,
paddingContentHorizontalLG,
} = token;

return {
Expand All @@ -48,7 +49,7 @@ export const genBaseStyle: GenerateStyle<AlertToken> = (token: AlertToken): CSSO
position: 'relative',
display: 'flex',
alignItems: 'center',
padding: `${paddingXS}px ${paddingSM}px`,
padding: `${paddingContentVerticalSM}px ${alertPaddingHorizontal}px`, // Fixed horizontal padding here.
wordWrap: 'break-word',
borderRadius,

Expand Down Expand Up @@ -95,8 +96,8 @@ export const genBaseStyle: GenerateStyle<AlertToken> = (token: AlertToken): CSSO

[`${componentCls}-with-description`]: {
alignItems: 'flex-start',
paddingInline: paddingLG,
paddingBlock: paddingTmp,
paddingInline: paddingContentHorizontalLG,
paddingBlock: paddingMD,

[`${componentCls}-icon`]: {
marginInlineEnd: marginSM,
Expand Down Expand Up @@ -232,6 +233,7 @@ export default genComponentStyleHook('Alert', token => {

const alertToken = mergeToken<AlertToken>(token, {
alertIconSizeLG: fontSizeHeading3,
alertPaddingHorizontal: 12, // Fixed value here.
});

return [genAlertStyle(alertToken)];
Expand Down
7 changes: 5 additions & 2 deletions components/button/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ComponentToken {}
export interface ButtonToken extends FullToken<'Button'> {
// FIXME: should be removed
colorOutlineDefault: string;
buttonPaddingHorizontal: number;
}

// ============================== Shared ==============================
Expand Down Expand Up @@ -344,7 +345,7 @@ const genSizeButtonStyle = (token: ButtonToken, sizePrefixCls: string = ''): CSS
0,
(token.controlHeight - token.fontSize * token.lineHeight) / 2 - token.controlLineWidth,
);
const paddingHorizontal = token.padding - token.controlLineWidth;
const paddingHorizontal = token.buttonPaddingHorizontal - token.controlLineWidth;

const iconOnlyCls = `${componentCls}-icon-only`;

Expand Down Expand Up @@ -399,6 +400,7 @@ const genSizeSmallButtonStyle: GenerateStyle<ButtonToken> = token => {
const smallToken = mergeToken<ButtonToken>(token, {
controlHeight: token.controlHeightSM,
padding: token.paddingXS,
buttonPaddingHorizontal: 8, // Fixed padding
controlRadius: token.controlRadiusSM,
});

Expand All @@ -417,10 +419,11 @@ const genSizeLargeButtonStyle: GenerateStyle<ButtonToken> = token => {

// ============================== Export ==============================
export default genComponentStyleHook('Button', token => {
const { controlTmpOutline } = token;
const { controlTmpOutline, paddingContentHorizontal } = token;

const buttonToken = mergeToken<ButtonToken>(token, {
colorOutlineDefault: controlTmpOutline,
buttonPaddingHorizontal: paddingContentHorizontal,
});

return [
Expand Down
26 changes: 15 additions & 11 deletions components/card/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import type { FullToken, GenerateStyle } from '../../theme';
import { genComponentStyleHook, mergeToken } from '../../theme';
import { clearFix, resetComponent } from '../../style';

export interface ComponentToken {}

interface CardToken extends FullToken<'Card'> {
cardHeaderHeight: number;
cardHeaderHeightSM: number;
cardShadow: string;
cardHeadHeight: number;
cardHeadPadding: number;
cardPaddingSM: number;
cardPaddingBase: number;
cardHeadTabsMarginBottom: number;
cardInnerHeadPadding: number;
Expand Down Expand Up @@ -236,9 +241,9 @@ const genCardStyle: GenerateStyle<CardToken> = (token): CSSObject => {
cardShadow,
cardHeadHeight,
cardHeadPadding,
cardPaddingBase,
colorBorderSecondary,
boxShadow,
cardPaddingBase,
} = token;

return {
Expand Down Expand Up @@ -345,15 +350,14 @@ const genCardStyle: GenerateStyle<CardToken> = (token): CSSObject => {

// ============================== Size ==============================
const genCardSizeStyle: GenerateStyle<CardToken> = (token): CSSObject => {
const { componentCls, cardPaddingBase, cardHeadPadding } = token;
const cardPaddingBaseSM = cardPaddingBase / 2;
const cardHeadPaddingSM = cardHeadPadding / 2;
const { componentCls, cardPaddingSM, fontSize, lineHeight, cardHeaderHeightSM } = token;
const cardHeadPaddingSM = (cardHeaderHeightSM - fontSize * lineHeight) / 2;

return {
[`${componentCls}-small`]: {
[`> ${componentCls}-head`]: {
minHeight: cardHeadPaddingSM * 2 + token.fontSize,
padding: `0 ${cardPaddingBaseSM}px`,
minHeight: cardHeaderHeightSM,
padding: `0 ${cardPaddingSM}px`,
fontSize: token.fontSize,

[`> ${componentCls}-head-wrapper`]: {
Expand All @@ -369,25 +373,25 @@ const genCardSizeStyle: GenerateStyle<CardToken> = (token): CSSObject => {
},

[`> ${componentCls}-body`]: {
padding: cardPaddingBaseSM,
padding: cardPaddingSM,
},
},
};
};

// ============================== Export ==============================
export default genComponentStyleHook('Card', token => {
const cardHeadPadding = token.padding;

const cardToken = mergeToken<CardToken>(token, {
cardShadow: token.boxShadowCard,
cardHeadHeight: token.fontSizeLG + cardHeadPadding * 2,
cardHeadPadding,
cardHeaderHeight: token.fontSizeLG * token.lineHeightLG + token.padding * 2,
cardHeaderHeightSM: token.fontSize * token.lineHeight + token.paddingXS * 2,
cardHeadPadding: token.padding,
cardPaddingBase: token.paddingLG,
cardHeadTabsMarginBottom: -token.padding - token.lineWidth,
cardInnerHeadPadding: token.paddingSM,
cardActionsLiMargin: `${token.paddingSM}px 0`,
cardActionsIconSize: token.fontSize,
cardPaddingSM: 12, // Fixed padding.
});

return [
Expand Down
11 changes: 7 additions & 4 deletions components/collapse/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ import type { FullToken, GenerateStyle } from '../../theme';
import { genComponentStyleHook, mergeToken } from '../../theme';
import { resetComponent, resetIcon } from '../../style';

export interface ComponentToken {}

type CollapseToken = FullToken<'Collapse'> & {
collapseContentBg: string;
collapseContentPadding: number;
collapseHeaderBg: string;
collapseHeaderPadding: string;
collapsePanelBorderRadius: number;
collapseContentPaddingHorizontal: number;
};

export const genBaseStyle: GenerateStyle<CollapseToken> = token => {
const {
componentCls,
collapseContentBg,
collapseContentPadding,
padding,
collapseContentPaddingHorizontal,
collapseHeaderBg,
collapseHeaderPadding,
collapsePanelBorderRadius,
Expand Down Expand Up @@ -130,7 +133,7 @@ export const genBaseStyle: GenerateStyle<CollapseToken> = token => {
borderTop: borderBase,

[`& > ${componentCls}-content-box`]: {
padding: collapseContentPadding,
padding: `${padding}px ${collapseContentPaddingHorizontal}px`,
},

[`&-hidden`]: {
Expand Down Expand Up @@ -255,10 +258,10 @@ const genGhostStyle: GenerateStyle<CollapseToken> = token => {
export default genComponentStyleHook('Collapse', token => {
const collapseToken = mergeToken<CollapseToken>(token, {
collapseContentBg: token.colorBgContainer,
collapseContentPadding: token.padding,
collapseHeaderBg: token.colorFillAlter,
collapseHeaderPadding: `${token.paddingSM}px ${token.padding}px`,
collapsePanelBorderRadius: token.radiusLG,
collapseContentPaddingHorizontal: 16, // Fixed value
});

return [
Expand Down
30 changes: 29 additions & 1 deletion components/config-provider/__tests__/theme.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { useToken } from '../../theme';
import theme from '../../theme/export';
import { resetWarned } from '../../_util/warning';

const { defaultAlgorithm, darkAlgorithm, defaultAlgorithmV4, darkAlgorithmV4 } = theme;
const { defaultAlgorithm, darkAlgorithm, defaultAlgorithmV4, darkAlgorithmV4, compactAlgorithm } =
theme;

let mockCanUseDom = true;

Expand Down Expand Up @@ -102,6 +103,33 @@ describe('ConfigProvider.Theme', () => {
expect(tokenRef?.colorPrimaryText).toBe('#177ddc');
});

it('compactAlgorithm should work', () => {
let tokenRef: any;
const Demo = () => {
const [, token] = useToken();
tokenRef = token;
return null;
};
render(
<ConfigProvider theme={{ token: { sizeBaseStep: 2 }, algorithm: compactAlgorithm }}>
<Demo />
</ConfigProvider>,
);
expect(tokenRef).toEqual(
expect.objectContaining({
sizeXXL: 48,
sizeXL: 32,
sizeLG: 16,
sizeMD: 16,
sizeMS: 12,
size: 8,
sizeSM: 8,
sizeXS: 4,
sizeXXS: 4,
}),
);
});

it('should support algorithm array', () => {
let tokenRef: any;
const Demo = () => {
Expand Down
6 changes: 3 additions & 3 deletions components/list/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ export default genComponentStyleHook(
const listToken = mergeToken<ListToken>(token, {
listBorderedCls: `${token.componentCls}-bordered`,
minHeight: token.controlHeightLG,
listItemPadding: `${token.paddingSM}px 0`,
listItemPaddingSM: `${token.paddingXS}px ${token.padding}px`,
listItemPaddingLG: `${token.padding}px ${token.paddingLG}px`,
listItemPadding: `${token.paddingContentVertical}px ${token.paddingContentHorizontalLG}px`,
listItemPaddingSM: `${token.paddingContentVerticalSM}px ${token.paddingContentHorizontal}px`,
listItemPaddingLG: `${token.paddingContentVerticalLG}px ${token.paddingContentHorizontalLG}px`,
});

return [genBaseStyle(listToken), genBorderedStyle(listToken), genResponsiveStyle(listToken)];
Expand Down
2 changes: 1 addition & 1 deletion components/message/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default genComponentStyleHook(
const combinedToken = mergeToken<MessageToken>(token, {
messageNoticeContentPadding: `${
(token.controlHeightLG - token.fontSize * token.lineHeight) / 2
}px ${token.paddingSM}px`,
}px ${token.paddingContentVertical}px`,
});
return [genMessageStyle(combinedToken)];
},
Expand Down
2 changes: 1 addition & 1 deletion components/modal/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ const genModalStyle: GenerateStyle<ModalToken> = token => {
borderRadius: token.radiusLG,
boxShadow: token.boxShadowSecondary,
pointerEvents: 'auto',
padding: `${token.paddingTmp}px ${token.paddingLG}px`,
padding: `${token.paddingMD}px ${token.paddingContentHorizontalLG}px`,
},

[`${componentCls}-close`]: {
Expand Down
4 changes: 2 additions & 2 deletions components/notification/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ const genNotificationStyle: GenerateStyle<NotificationToken> = token => {
export default genComponentStyleHook(
'Notification',
token => {
const notificationPaddingVertical = token.paddingTmp;
const notificationPaddingVertical = token.paddingMD;
const notificationPaddingHorizontal = token.paddingLG;

const notificationToken = mergeToken<NotificationToken>(token, {
Expand All @@ -270,7 +270,7 @@ export default genComponentStyleHook(
notificationPaddingVertical,
notificationPaddingHorizontal,
// index.less variables
notificationPadding: `${notificationPaddingVertical}px ${notificationPaddingHorizontal}px`,
notificationPadding: `${token.paddingMD}px ${token.paddingContentHorizontalLG}px`,
notificationMarginBottom: token.margin,
notificationMarginEdge: token.marginLG,
animationMaxHeight: 150,
Expand Down
6 changes: 3 additions & 3 deletions components/popover/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ComponentToken {
export type PopoverToken = FullToken<'Popover'> & {
popoverBg: string;
popoverColor: string;
popoverPadding: number;
popoverPadding: number | string;
};

const genBaseStyle: GenerateStyle<PopoverToken> = token => {
Expand Down Expand Up @@ -158,12 +158,12 @@ const genWireframeStyle: GenerateStyle<PopoverToken> = token => {
export default genComponentStyleHook(
'Popover',
token => {
const { colorBgElevated, colorText, paddingSM, wireframe } = token;
const { colorBgElevated, colorText, wireframe } = token;

const popoverToken = mergeToken<PopoverToken>(token, {
popoverBg: colorBgElevated,
popoverColor: colorText,
popoverPadding: paddingSM,
popoverPadding: 12, // Fixed Value
});

return [
Expand Down
6 changes: 3 additions & 3 deletions components/radio/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ export default genComponentStyleHook('Radio', token => {
colorPrimary,
marginXS,
controlOutlineWidth,
paddingXXS,
wireframe,
} = token;

Expand All @@ -506,10 +505,11 @@ export default genComponentStyleHook('Radio', token => {

const radioSize = fontSizeLG;
const radioTop = (Math.round(fontSize * lineHeight) - radioSize) / 2;
const radioDotDisabledSize = radioSize - paddingXXS * 2;
const dotPadding = 4; // Fixed value
const radioDotDisabledSize = radioSize - dotPadding * 2;
const radioDotSize = wireframe
? radioDotDisabledSize
: radioSize - (paddingXXS + controlLineWidth) * 2;
: radioSize - (dotPadding + controlLineWidth) * 2;
const radioCheckedColor = colorPrimary;

// Radio buttons
Expand Down
8 changes: 4 additions & 4 deletions components/slider/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ const genBaseStyle: GenerateStyle<SliderToken> = token => {

// ============================ Horizontal ============================
const genDirectionStyle = (token: SliderToken, horizontal: boolean): CSSObject => {
const { componentCls, railSize, controlSize, handleSize, dotSize } = token;
const { componentCls, railSize, handleSize, dotSize } = token;

const railPadding: keyof React.CSSProperties = horizontal ? 'paddingBlock' : 'paddingInline';
const full: keyof React.CSSProperties = horizontal ? 'width' : 'height';
Expand All @@ -247,7 +247,7 @@ const genDirectionStyle = (token: SliderToken, horizontal: boolean): CSSObject =

return {
[railPadding]: railSize,
[part]: controlSize,
[part]: railSize * 3,

[`${componentCls}-rail`]: {
[full]: '100%',
Expand All @@ -259,7 +259,7 @@ const genDirectionStyle = (token: SliderToken, horizontal: boolean): CSSObject =
},

[`${componentCls}-handle`]: {
[handlePos]: (controlSize - handleSize) / 2,
[handlePos]: (railSize * 3 - handleSize) / 2,
},

[`${componentCls}-mark`]: {
Expand Down Expand Up @@ -336,7 +336,7 @@ export default genComponentStyleHook(
const handleLineWidthHover = token.lineWidth + increaseHandleWidth * 3;
return {
controlSize,
railSize: controlSize / 3,
railSize: 4,
handleSize: controlSize,
handleSizeHover: controlSizeHover,
dotSize: (controlSize / 3) * 2,
Expand Down
Loading

0 comments on commit ffc09a2

Please sign in to comment.