2022-05-09 22:20:07 +08:00
|
|
|
import type { CSSObject } from '@ant-design/cssinjs';
|
2022-06-02 17:18:52 +08:00
|
|
|
import { TinyColor } from '@ctrl/tinycolor';
|
2022-11-23 20:22:38 +08:00
|
|
|
import type { FullToken, GenerateStyle } from '../../theme/internal';
|
|
|
|
import { genComponentStyleHook, mergeToken } from '../../theme/internal';
|
2022-09-09 10:53:03 +08:00
|
|
|
import { genFocusStyle, resetComponent } from '../../style';
|
2022-04-08 14:36:26 +08:00
|
|
|
|
|
|
|
interface SwitchToken extends FullToken<'Switch'> {
|
2022-03-16 15:04:47 +08:00
|
|
|
switchMinWidth: number;
|
|
|
|
switchHeight: number;
|
|
|
|
switchDuration: string;
|
|
|
|
switchColor: string;
|
|
|
|
switchDisabledOpacity: number;
|
|
|
|
switchInnerMarginMin: number;
|
|
|
|
switchInnerMarginMax: number;
|
|
|
|
switchPadding: number;
|
|
|
|
switchPinSize: number;
|
|
|
|
switchBg: string;
|
|
|
|
switchMinWidthSM: number;
|
|
|
|
switchHeightSM: number;
|
|
|
|
switchInnerMarginMinSM: number;
|
|
|
|
switchInnerMarginMaxSM: number;
|
|
|
|
switchPinSizeSM: number;
|
2022-06-02 17:18:52 +08:00
|
|
|
switchHandleShadow: string;
|
|
|
|
switchLoadingIconSize: number;
|
|
|
|
switchLoadingIconColor: string;
|
|
|
|
switchHandleActiveInset: string;
|
2022-03-16 15:04:47 +08:00
|
|
|
}
|
|
|
|
|
2022-11-17 23:31:41 +08:00
|
|
|
const genSwitchSmallStyle: GenerateStyle<SwitchToken, CSSObject> = (token) => {
|
2022-09-20 21:51:28 +08:00
|
|
|
const { componentCls } = token;
|
|
|
|
const switchInnerCls = `${componentCls}-inner`;
|
2022-03-16 15:04:47 +08:00
|
|
|
|
2022-09-20 21:51:28 +08:00
|
|
|
return {
|
|
|
|
[componentCls]: {
|
|
|
|
[`&${componentCls}-small`]: {
|
|
|
|
minWidth: token.switchMinWidthSM,
|
|
|
|
height: token.switchHeightSM,
|
|
|
|
lineHeight: `${token.switchHeightSM}px`,
|
|
|
|
|
|
|
|
[`${componentCls}-inner`]: {
|
2023-01-30 16:31:54 +08:00
|
|
|
paddingInlineStart: token.switchInnerMarginMaxSM,
|
|
|
|
paddingInlineEnd: token.switchInnerMarginMinSM,
|
2022-09-20 21:51:28 +08:00
|
|
|
[`${switchInnerCls}-checked`]: {
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineStart: `calc(-100% + ${
|
2022-09-20 21:51:28 +08:00
|
|
|
token.switchPinSizeSM + token.switchPadding * 2
|
2023-01-30 16:31:54 +08:00
|
|
|
}px - ${token.switchInnerMarginMaxSM * 2}px)`,
|
|
|
|
marginInlineEnd: `calc(100% - ${token.switchPinSizeSM + token.switchPadding * 2}px + ${
|
|
|
|
token.switchInnerMarginMaxSM * 2
|
2022-09-20 21:51:28 +08:00
|
|
|
}px)`,
|
|
|
|
},
|
|
|
|
|
|
|
|
[`${switchInnerCls}-unchecked`]: {
|
|
|
|
marginTop: -token.switchHeightSM,
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineStart: 0,
|
|
|
|
marginInlineEnd: 0,
|
2022-09-20 21:51:28 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
[`${componentCls}-handle`]: {
|
|
|
|
width: token.switchPinSizeSM,
|
|
|
|
height: token.switchPinSizeSM,
|
|
|
|
},
|
2022-03-16 15:04:47 +08:00
|
|
|
|
2022-09-20 21:51:28 +08:00
|
|
|
[`${componentCls}-loading-icon`]: {
|
|
|
|
top: (token.switchPinSizeSM - token.switchLoadingIconSize) / 2,
|
|
|
|
fontSize: token.switchLoadingIconSize,
|
|
|
|
},
|
|
|
|
|
|
|
|
[`&${componentCls}-checked`]: {
|
|
|
|
[`${componentCls}-inner`]: {
|
2023-01-30 16:31:54 +08:00
|
|
|
paddingInlineStart: token.switchInnerMarginMinSM,
|
|
|
|
paddingInlineEnd: token.switchInnerMarginMaxSM,
|
2022-09-20 21:51:28 +08:00
|
|
|
[`${switchInnerCls}-checked`]: {
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineStart: 0,
|
|
|
|
marginInlineEnd: 0,
|
2022-09-20 21:51:28 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
[`${switchInnerCls}-unchecked`]: {
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineStart: `calc(100% - ${
|
2022-09-20 21:51:28 +08:00
|
|
|
token.switchPinSizeSM + token.switchPadding * 2
|
2023-01-30 16:31:54 +08:00
|
|
|
}px + ${token.switchInnerMarginMaxSM * 2}px)`,
|
|
|
|
marginInlineEnd: `calc(-100% + ${
|
2022-09-20 21:51:28 +08:00
|
|
|
token.switchPinSizeSM + token.switchPadding * 2
|
2023-01-30 16:31:54 +08:00
|
|
|
}px - ${token.switchInnerMarginMaxSM * 2}px)`,
|
2022-09-20 21:51:28 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
[`${componentCls}-handle`]: {
|
|
|
|
insetInlineStart: `calc(100% - ${token.switchPinSizeSM + token.switchPadding}px)`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
[`&:not(${componentCls}-disabled):active`]: {
|
|
|
|
[`&:not(${componentCls}-checked) ${switchInnerCls}`]: {
|
|
|
|
[`${switchInnerCls}-unchecked`]: {
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineStart: token.marginXXS / 2,
|
|
|
|
marginInlineEnd: -token.marginXXS / 2,
|
2022-09-20 21:51:28 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
[`&${componentCls}-checked ${switchInnerCls}`]: {
|
|
|
|
[`${switchInnerCls}-checked`]: {
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineStart: -token.marginXXS / 2,
|
|
|
|
marginInlineEnd: token.marginXXS / 2,
|
2022-09-20 21:51:28 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-03-16 15:04:47 +08:00
|
|
|
},
|
2022-09-20 21:51:28 +08:00
|
|
|
};
|
|
|
|
};
|
2022-03-16 15:04:47 +08:00
|
|
|
|
2022-11-17 23:31:41 +08:00
|
|
|
const genSwitchLoadingStyle: GenerateStyle<SwitchToken, CSSObject> = (token) => {
|
2022-09-20 21:51:28 +08:00
|
|
|
const { componentCls } = token;
|
|
|
|
|
|
|
|
return {
|
|
|
|
[componentCls]: {
|
|
|
|
[`${componentCls}-loading-icon${token.iconCls}`]: {
|
|
|
|
position: 'relative',
|
|
|
|
top: (token.switchPinSize - token.fontSize) / 2,
|
|
|
|
color: token.switchLoadingIconColor,
|
|
|
|
verticalAlign: 'top',
|
2022-03-16 15:04:47 +08:00
|
|
|
},
|
|
|
|
|
2022-09-20 21:51:28 +08:00
|
|
|
[`&${componentCls}-checked ${componentCls}-loading-icon`]: {
|
|
|
|
color: token.switchColor,
|
2022-03-16 15:04:47 +08:00
|
|
|
},
|
|
|
|
},
|
2022-09-20 21:51:28 +08:00
|
|
|
};
|
|
|
|
};
|
2022-03-16 15:04:47 +08:00
|
|
|
|
2022-11-17 23:31:41 +08:00
|
|
|
const genSwitchHandleStyle: GenerateStyle<SwitchToken, CSSObject> = (token) => {
|
2022-09-20 21:51:28 +08:00
|
|
|
const { componentCls } = token;
|
|
|
|
const switchHandleCls = `${componentCls}-handle`;
|
2022-03-16 15:04:47 +08:00
|
|
|
|
|
|
|
return {
|
2022-09-20 21:51:28 +08:00
|
|
|
[componentCls]: {
|
|
|
|
[switchHandleCls]: {
|
2022-03-16 15:04:47 +08:00
|
|
|
position: 'absolute',
|
2022-09-20 21:51:28 +08:00
|
|
|
top: token.switchPadding,
|
|
|
|
insetInlineStart: token.switchPadding,
|
|
|
|
width: token.switchPinSize,
|
|
|
|
height: token.switchPinSize,
|
2022-03-16 15:04:47 +08:00
|
|
|
transition: `all ${token.switchDuration} ease-in-out`,
|
|
|
|
|
2022-09-20 21:51:28 +08:00
|
|
|
'&::before': {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
insetInlineEnd: 0,
|
|
|
|
bottom: 0,
|
|
|
|
insetInlineStart: 0,
|
2022-11-01 15:06:38 +08:00
|
|
|
backgroundColor: token.colorWhite,
|
2022-09-20 21:51:28 +08:00
|
|
|
borderRadius: token.switchPinSize / 2,
|
|
|
|
boxShadow: token.switchHandleShadow,
|
|
|
|
transition: `all ${token.switchDuration} ease-in-out`,
|
|
|
|
content: '""',
|
|
|
|
},
|
|
|
|
},
|
2022-03-16 15:04:47 +08:00
|
|
|
|
2022-09-20 21:51:28 +08:00
|
|
|
[`&${componentCls}-checked ${switchHandleCls}`]: {
|
|
|
|
insetInlineStart: `calc(100% - ${token.switchPinSize + token.switchPadding}px)`,
|
2022-03-16 15:04:47 +08:00
|
|
|
},
|
|
|
|
|
2022-09-20 21:51:28 +08:00
|
|
|
[`&:not(${componentCls}-disabled):active`]: {
|
|
|
|
[`${switchHandleCls}::before`]: {
|
|
|
|
insetInlineEnd: token.switchHandleActiveInset,
|
|
|
|
insetInlineStart: 0,
|
|
|
|
},
|
|
|
|
|
|
|
|
[`&${componentCls}-checked ${switchHandleCls}::before`]: {
|
|
|
|
insetInlineEnd: 0,
|
|
|
|
insetInlineStart: token.switchHandleActiveInset,
|
|
|
|
},
|
2022-03-16 15:04:47 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-11-17 23:31:41 +08:00
|
|
|
const genSwitchInnerStyle: GenerateStyle<SwitchToken, CSSObject> = (token) => {
|
2022-09-20 21:51:28 +08:00
|
|
|
const { componentCls } = token;
|
|
|
|
const switchInnerCls = `${componentCls}-inner`;
|
2022-03-16 15:04:47 +08:00
|
|
|
|
|
|
|
return {
|
2022-09-20 21:51:28 +08:00
|
|
|
[componentCls]: {
|
|
|
|
[switchInnerCls]: {
|
|
|
|
display: 'block',
|
|
|
|
overflow: 'hidden',
|
|
|
|
borderRadius: 100,
|
2022-12-05 15:03:59 +08:00
|
|
|
height: '100%',
|
2023-01-30 16:31:54 +08:00
|
|
|
paddingInlineStart: token.switchInnerMarginMax,
|
|
|
|
paddingInlineEnd: token.switchInnerMarginMin,
|
|
|
|
transition: `padding-inline-start ${token.switchDuration} ease-in-out, padding-inline-end ${token.switchDuration} ease-in-out`,
|
2022-09-20 21:51:28 +08:00
|
|
|
|
|
|
|
[`${switchInnerCls}-checked, ${switchInnerCls}-unchecked`]: {
|
|
|
|
display: 'block',
|
|
|
|
color: token.colorTextLightSolid,
|
|
|
|
fontSize: token.fontSizeSM,
|
|
|
|
transition: `margin-inline-start ${token.switchDuration} ease-in-out, margin-inline-end ${token.switchDuration} ease-in-out`,
|
|
|
|
pointerEvents: 'none',
|
|
|
|
},
|
|
|
|
|
|
|
|
[`${switchInnerCls}-checked`]: {
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineStart: `calc(-100% + ${token.switchPinSize + token.switchPadding * 2}px - ${
|
|
|
|
token.switchInnerMarginMax * 2
|
2022-09-20 21:51:28 +08:00
|
|
|
}px)`,
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineEnd: `calc(100% - ${token.switchPinSize + token.switchPadding * 2}px + ${
|
|
|
|
token.switchInnerMarginMax * 2
|
2022-09-20 21:51:28 +08:00
|
|
|
}px)`,
|
|
|
|
},
|
|
|
|
|
|
|
|
[`${switchInnerCls}-unchecked`]: {
|
|
|
|
marginTop: -token.switchHeight,
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineStart: 0,
|
|
|
|
marginInlineEnd: 0,
|
2022-09-20 21:51:28 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
[`&${componentCls}-checked ${switchInnerCls}`]: {
|
2023-01-30 16:31:54 +08:00
|
|
|
paddingInlineStart: token.switchInnerMarginMin,
|
|
|
|
paddingInlineEnd: token.switchInnerMarginMax,
|
2022-09-20 21:51:28 +08:00
|
|
|
[`${switchInnerCls}-checked`]: {
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineStart: 0,
|
|
|
|
marginInlineEnd: 0,
|
2022-09-20 21:51:28 +08:00
|
|
|
},
|
2022-03-16 15:04:47 +08:00
|
|
|
|
2022-09-20 21:51:28 +08:00
|
|
|
[`${switchInnerCls}-unchecked`]: {
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineStart: `calc(100% - ${token.switchPinSize + token.switchPadding * 2}px + ${
|
|
|
|
token.switchInnerMarginMax * 2
|
2022-09-20 21:51:28 +08:00
|
|
|
}px)`,
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineEnd: `calc(-100% + ${token.switchPinSize + token.switchPadding * 2}px - ${
|
|
|
|
token.switchInnerMarginMax * 2
|
2022-09-20 21:51:28 +08:00
|
|
|
}px)`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
[`&:not(${componentCls}-disabled):active`]: {
|
|
|
|
[`&:not(${componentCls}-checked) ${switchInnerCls}`]: {
|
|
|
|
[`${switchInnerCls}-unchecked`]: {
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineStart: token.switchPadding * 2,
|
|
|
|
marginInlineEnd: -token.switchPadding * 2,
|
2022-09-20 21:51:28 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
[`&${componentCls}-checked ${switchInnerCls}`]: {
|
|
|
|
[`${switchInnerCls}-checked`]: {
|
2023-01-30 16:31:54 +08:00
|
|
|
marginInlineStart: -token.switchPadding * 2,
|
|
|
|
marginInlineEnd: token.switchPadding * 2,
|
2022-09-20 21:51:28 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-03-16 15:04:47 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const genSwitchStyle = (token: SwitchToken): CSSObject => {
|
2022-04-08 14:36:26 +08:00
|
|
|
const { componentCls } = token;
|
2022-03-16 15:04:47 +08:00
|
|
|
|
|
|
|
return {
|
2022-04-08 14:36:26 +08:00
|
|
|
[componentCls]: {
|
2022-03-16 15:04:47 +08:00
|
|
|
...resetComponent(token),
|
|
|
|
|
|
|
|
position: 'relative',
|
|
|
|
display: 'inline-block',
|
|
|
|
boxSizing: 'border-box',
|
|
|
|
minWidth: token.switchMinWidth,
|
|
|
|
height: token.switchHeight,
|
|
|
|
lineHeight: `${token.switchHeight}px`,
|
|
|
|
verticalAlign: 'middle',
|
2022-09-07 12:00:00 +08:00
|
|
|
background: token.colorTextQuaternary,
|
2022-03-16 15:04:47 +08:00
|
|
|
border: '0',
|
|
|
|
borderRadius: 100,
|
|
|
|
cursor: 'pointer',
|
2022-11-17 23:31:41 +08:00
|
|
|
transition: `all ${token.motionDurationMid}`,
|
2022-03-16 15:04:47 +08:00
|
|
|
userSelect: 'none',
|
|
|
|
|
2022-09-20 21:50:36 +08:00
|
|
|
[`&:hover:not(${componentCls}-disabled)`]: {
|
2022-09-07 12:00:00 +08:00
|
|
|
background: token.colorTextTertiary,
|
|
|
|
},
|
|
|
|
|
2022-09-09 10:53:03 +08:00
|
|
|
...genFocusStyle(token),
|
2022-03-16 15:04:47 +08:00
|
|
|
|
2022-09-20 21:51:28 +08:00
|
|
|
[`&${componentCls}-checked`]: {
|
2022-04-20 11:30:55 +08:00
|
|
|
background: token.switchColor,
|
2022-09-07 12:00:00 +08:00
|
|
|
|
2022-09-20 21:50:36 +08:00
|
|
|
[`&:hover:not(${componentCls}-disabled)`]: {
|
2022-09-07 12:00:00 +08:00
|
|
|
background: token.colorPrimaryHover,
|
|
|
|
},
|
2022-03-16 15:04:47 +08:00
|
|
|
},
|
|
|
|
|
2022-09-20 21:51:28 +08:00
|
|
|
[`&${componentCls}-loading, &${componentCls}-disabled`]: {
|
2022-03-16 15:04:47 +08:00
|
|
|
cursor: 'not-allowed',
|
|
|
|
opacity: token.switchDisabledOpacity,
|
|
|
|
|
|
|
|
'*': {
|
|
|
|
boxShadow: 'none',
|
|
|
|
cursor: 'not-allowed',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// rtl style
|
2022-09-20 21:51:28 +08:00
|
|
|
[`&${componentCls}-rtl`]: {
|
2022-03-16 15:04:47 +08:00
|
|
|
direction: 'rtl',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// ============================== Export ==============================
|
2022-11-17 23:31:41 +08:00
|
|
|
export default genComponentStyleHook('Switch', (token) => {
|
2022-06-02 17:18:52 +08:00
|
|
|
const switchHeight = token.fontSize * token.lineHeight;
|
|
|
|
const switchHeightSM = token.controlHeight / 2;
|
2022-10-25 17:04:36 +08:00
|
|
|
const switchPadding = 2; // This is magic
|
2022-06-02 17:18:52 +08:00
|
|
|
const switchPinSize = switchHeight - switchPadding * 2;
|
|
|
|
const switchPinSizeSM = switchHeightSM - switchPadding * 2;
|
2022-03-16 15:04:47 +08:00
|
|
|
|
2022-04-11 16:04:00 +08:00
|
|
|
const switchToken = mergeToken<SwitchToken>(token, {
|
2022-06-02 17:18:52 +08:00
|
|
|
switchMinWidth: switchPinSize * 2 + switchPadding * 4,
|
2022-03-16 15:04:47 +08:00
|
|
|
switchHeight,
|
2022-06-02 17:18:52 +08:00
|
|
|
switchDuration: token.motionDurationMid,
|
2022-03-22 09:37:37 +08:00
|
|
|
switchColor: token.colorPrimary,
|
2022-06-17 18:47:47 +08:00
|
|
|
switchDisabledOpacity: token.opacityLoading,
|
2022-06-02 17:18:52 +08:00
|
|
|
switchInnerMarginMin: switchPinSize / 2,
|
2022-10-25 17:04:36 +08:00
|
|
|
switchInnerMarginMax: switchPinSize + switchPadding + switchPadding * 2,
|
2022-06-02 17:18:52 +08:00
|
|
|
switchPadding,
|
|
|
|
switchPinSize,
|
2022-06-28 20:33:01 +08:00
|
|
|
switchBg: token.colorBgContainer,
|
2022-06-02 17:18:52 +08:00
|
|
|
switchMinWidthSM: switchPinSizeSM * 2 + switchPadding * 2,
|
2022-03-16 15:04:47 +08:00
|
|
|
switchHeightSM,
|
2022-06-02 17:18:52 +08:00
|
|
|
switchInnerMarginMinSM: switchPinSizeSM / 2,
|
2022-10-25 17:04:36 +08:00
|
|
|
switchInnerMarginMaxSM: switchPinSizeSM + switchPadding + switchPadding * 2,
|
2022-06-02 17:18:52 +08:00
|
|
|
switchPinSizeSM,
|
|
|
|
switchHandleShadow: `0 2px 4px 0 ${new TinyColor('#00230b').setAlpha(0.2).toRgbString()}`,
|
|
|
|
switchLoadingIconSize: token.fontSizeIcon * 0.75,
|
2022-06-17 18:47:47 +08:00
|
|
|
switchLoadingIconColor: `rgba(0, 0, 0, ${token.opacityLoading})`,
|
2022-06-02 17:18:52 +08:00
|
|
|
switchHandleActiveInset: '-30%',
|
2022-04-11 16:04:00 +08:00
|
|
|
});
|
2022-03-16 15:04:47 +08:00
|
|
|
|
2022-09-20 21:51:28 +08:00
|
|
|
return [
|
|
|
|
genSwitchStyle(switchToken),
|
|
|
|
|
|
|
|
// inner style
|
|
|
|
genSwitchInnerStyle(switchToken),
|
|
|
|
|
|
|
|
// handle style
|
|
|
|
genSwitchHandleStyle(switchToken),
|
|
|
|
|
|
|
|
// loading style
|
|
|
|
genSwitchLoadingStyle(switchToken),
|
|
|
|
|
|
|
|
// small style
|
|
|
|
genSwitchSmallStyle(switchToken),
|
|
|
|
];
|
2022-04-08 14:36:26 +08:00
|
|
|
});
|