ant-design/components/collapse/style/index.ts

269 lines
6.7 KiB
TypeScript
Raw Normal View History

import { genCollapseMotion } from '../../style/motion';
import type { FullToken, GenerateStyle } from '../../theme/internal';
import { genComponentStyleHook, mergeToken } from '../../theme/internal';
import { resetComponent, resetIcon } from '../../style';
2022-04-25 14:45:26 +08:00
export interface ComponentToken {}
2022-04-25 14:45:26 +08:00
type CollapseToken = FullToken<'Collapse'> & {
collapseContentBg: string;
collapseHeaderBg: string;
collapseHeaderPadding: string;
collapsePanelBorderRadius: number;
collapseContentPaddingHorizontal: number;
2022-04-25 14:45:26 +08:00
};
export const genBaseStyle: GenerateStyle<CollapseToken> = (token) => {
2022-04-25 14:45:26 +08:00
const {
componentCls,
collapseContentBg,
padding,
collapseContentPaddingHorizontal,
2022-04-25 14:45:26 +08:00
collapseHeaderBg,
collapseHeaderPadding,
collapsePanelBorderRadius,
lineWidth,
lineType,
2022-04-25 14:45:26 +08:00
colorBorder,
colorText,
colorTextHeading,
colorTextDisabled,
fontSize,
2022-04-25 14:45:26 +08:00
lineHeight,
marginSM,
paddingSM,
motionDurationSlow,
2022-07-12 14:32:54 +08:00
fontSizeIcon,
2022-04-25 14:45:26 +08:00
} = token;
const borderBase = `${lineWidth}px ${lineType} ${colorBorder}`;
2022-04-25 14:45:26 +08:00
return {
[componentCls]: {
...resetComponent(token),
backgroundColor: collapseHeaderBg,
border: borderBase,
borderBottom: 0,
borderRadius: `${collapsePanelBorderRadius}px`,
[`&-rtl`]: {
direction: 'rtl',
},
[`& > ${componentCls}-item`]: {
borderBottom: borderBase,
[`&:last-child`]: {
[`
&,
2022-05-06 14:02:45 +08:00
& > ${componentCls}-header`]: {
2022-04-25 14:45:26 +08:00
borderRadius: `0 0 ${collapsePanelBorderRadius}px ${collapsePanelBorderRadius}px`,
},
},
[`> ${componentCls}-header`]: {
position: 'relative', // Compatible with old version of antd, should remove in next version
display: 'flex',
flexWrap: 'nowrap',
alignItems: 'flex-start',
padding: collapseHeaderPadding,
color: colorTextHeading,
lineHeight,
cursor: 'pointer',
transition: `all ${motionDurationSlow}, visibility 0s`,
2022-04-25 14:45:26 +08:00
2022-07-29 15:56:11 +08:00
[`> ${componentCls}-header-text`]: {
flex: 'auto',
},
'&:focus': {
outline: 'none',
2022-04-25 14:45:26 +08:00
},
// >>>>> Arrow
[`${componentCls}-expand-icon`]: {
height: fontSize * lineHeight,
display: 'flex',
alignItems: 'center',
paddingInlineEnd: marginSM,
2022-04-25 14:45:26 +08:00
},
[`${componentCls}-arrow`]: {
...resetIcon(),
2022-07-12 14:32:54 +08:00
fontSize: fontSizeIcon,
svg: {
transition: `transform ${motionDurationSlow}`,
},
},
// >>>>> Text
[`${componentCls}-header-text`]: {
marginInlineEnd: 'auto',
2022-04-25 14:45:26 +08:00
},
},
[`${componentCls}-header-collapsible-only`]: {
cursor: 'default',
[`${componentCls}-header-text`]: {
2022-07-29 15:56:11 +08:00
flex: 'none',
2022-04-25 14:45:26 +08:00
cursor: 'pointer',
},
},
[`${componentCls}-icon-collapsible-only`]: {
cursor: 'default',
[`${componentCls}-expand-icon`]: {
cursor: 'pointer',
},
},
2022-04-25 14:45:26 +08:00
[`&${componentCls}-no-arrow`]: {
[`> ${componentCls}-header`]: {
paddingInlineStart: paddingSM,
},
},
},
[`${componentCls}-content`]: {
color: colorText,
backgroundColor: collapseContentBg,
borderTop: borderBase,
[`& > ${componentCls}-content-box`]: {
padding: `${padding}px ${collapseContentPaddingHorizontal}px`,
2022-04-25 14:45:26 +08:00
},
[`&-hidden`]: {
display: 'none',
},
},
[`${componentCls}-item:last-child`]: {
[`> ${componentCls}-content`]: {
borderRadius: `0 0 ${collapsePanelBorderRadius}px ${collapsePanelBorderRadius}px`,
},
},
[`& ${componentCls}-item-disabled > ${componentCls}-header`]: {
[`
&,
& > .arrow
`]: {
color: colorTextDisabled,
cursor: 'not-allowed',
},
},
// ========================== Icon Position ==========================
[`&${componentCls}-icon-position-end`]: {
[`& > ${componentCls}-item`]: {
[`> ${componentCls}-header`]: {
[`${componentCls}-expand-icon`]: {
order: 1,
paddingInlineEnd: 0,
paddingInlineStart: marginSM,
2022-04-25 14:45:26 +08:00
},
},
},
},
},
};
2022-05-06 14:02:45 +08:00
};
2022-04-25 14:45:26 +08:00
const genArrowStyle: GenerateStyle<CollapseToken> = (token) => {
const { componentCls } = token;
const fixedSelector = `> ${componentCls}-item > ${componentCls}-header ${componentCls}-arrow svg`;
return {
[`${componentCls}-rtl`]: {
[fixedSelector]: {
transform: `rotate(180deg)`,
},
},
};
};
const genBorderlessStyle: GenerateStyle<CollapseToken> = (token) => {
2022-04-25 14:45:26 +08:00
const {
componentCls,
collapseHeaderBg,
paddingXXS,
2022-04-25 14:45:26 +08:00
colorBorder,
} = token;
return {
[`${componentCls}-borderless`]: {
backgroundColor: collapseHeaderBg,
border: 0,
[`> ${componentCls}-item`]: {
borderBottom: `1px solid ${colorBorder}`,
},
[`
> ${componentCls}-item:last-child,
> ${componentCls}-item:last-child ${componentCls}-header
`]: {
borderRadius: 0,
},
[`> ${componentCls}-item:last-child`]: {
borderBottom: 0,
},
[`> ${componentCls}-item > ${componentCls}-content`]: {
backgroundColor: 'transparent',
borderTop: 0,
},
[`> ${componentCls}-item > ${componentCls}-content > ${componentCls}-content-box`]: {
paddingTop: paddingXXS,
2022-04-25 14:45:26 +08:00
},
},
};
2022-05-06 14:02:45 +08:00
};
2022-04-25 14:45:26 +08:00
const genGhostStyle: GenerateStyle<CollapseToken> = (token) => {
const { componentCls, paddingSM } = token;
return {
[`${componentCls}-ghost`]: {
backgroundColor: 'transparent',
border: 0,
[`> ${componentCls}-item`]: {
borderBottom: 0,
[`> ${componentCls}-content`]: {
backgroundColor: 'transparent',
border: 0,
[`> ${componentCls}-content-box`]: {
paddingBlock: paddingSM,
},
},
},
},
};
};
export default genComponentStyleHook('Collapse', (token) => {
2022-04-25 14:45:26 +08:00
const collapseToken = mergeToken<CollapseToken>(token, {
collapseContentBg: token.colorBgContainer,
collapseHeaderBg: token.colorFillAlter,
2022-04-25 14:45:26 +08:00
collapseHeaderPadding: `${token.paddingSM}px ${token.padding}px`,
collapsePanelBorderRadius: token.borderRadiusLG,
collapseContentPaddingHorizontal: 16, // Fixed value
2022-04-25 14:45:26 +08:00
});
return [
genBaseStyle(collapseToken),
genBorderlessStyle(collapseToken),
genGhostStyle(collapseToken),
genArrowStyle(collapseToken),
genCollapseMotion(collapseToken),
];
2022-04-25 14:45:26 +08:00
});