refactor: slider with cssinjs (#34489)

* chore: slider cssinjs

* chore: clean up
This commit is contained in:
二货机器人 2022-03-14 19:12:10 +08:00 committed by GitHub
parent ade0a5c5e7
commit cd30664ce3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 483 additions and 182 deletions

View File

@ -128,6 +128,7 @@ export interface DerivativeToken extends Omit<DesignToken, 'duration'> {
durationFast: string;
// TMP
tmpPrimaryColorWeak: string;
tmpPrimaryHoverColorWeak: string;
}
@ -153,6 +154,7 @@ function derivative(designToken: DesignToken): DerivativeToken {
...designToken,
tmpPrimaryColorWeak: primaryColors[2],
tmpPrimaryHoverColorWeak: primaryColors[0],
primaryHoverColor: primaryColors[4],
primaryActiveColor: primaryColors[6],

View File

@ -4,6 +4,7 @@ import classNames from 'classnames';
import { TooltipPlacement } from '../tooltip';
import SliderTooltip from './SliderTooltip';
import { ConfigContext } from '../config-provider';
import useStyle from './style';
export type SliderMarks = RcSliderProps['marks'];
@ -95,9 +96,16 @@ const Slider = React.forwardRef<unknown, SliderSingleProps | SliderRangeProps>(
} = props;
const prefixCls = getPrefixCls('slider', customizePrefixCls);
const tooltipPrefixCls = getPrefixCls('tooltip', customizeTooltipPrefixCls);
const cls = classNames(className, {
[`${prefixCls}-rtl`]: direction === 'rtl',
});
const [wrapSSR, hashId] = useStyle(prefixCls);
const cls = classNames(
className,
{
[`${prefixCls}-rtl`]: direction === 'rtl',
},
hashId,
);
// make reverse default on rtl direction
if (direction === 'rtl' && !restProps.vertical) {
@ -145,7 +153,7 @@ const Slider = React.forwardRef<unknown, SliderSingleProps | SliderRangeProps>(
);
};
return (
return wrapSSR(
<RcSlider
{...(restProps as SliderRangeProps)}
step={restProps.step!}
@ -155,7 +163,7 @@ const Slider = React.forwardRef<unknown, SliderSingleProps | SliderRangeProps>(
ref={ref}
prefixCls={prefixCls}
handleRender={handleRender}
/>
/>,
);
},
);

View File

@ -1,206 +1,206 @@
@import '../../style/themes/index';
@import '../../style/mixins/index';
// @import '../../style/themes/index';
// @import '../../style/mixins/index';
@slider-prefix-cls: ~'@{ant-prefix}-slider';
// @slider-prefix-cls: ~'@{ant-prefix}-slider';
.@{slider-prefix-cls} {
.reset-component();
// .@{slider-prefix-cls} {
// .reset-component();
position: relative;
height: 12px;
margin: @slider-margin;
padding: 4px 0;
cursor: pointer;
touch-action: none;
// position: relative;
// height: 12px;
// margin: @slider-margin;
// padding: 4px 0;
// cursor: pointer;
// touch-action: none;
.vertical();
// .vertical();
&-with-marks {
margin-bottom: 28px;
}
// &-with-marks {
// margin-bottom: 28px;
// }
&-rail {
position: absolute;
width: 100%;
height: 4px;
background-color: @slider-rail-background-color;
border-radius: @border-radius-base;
transition: background-color 0.3s;
}
// &-rail {
// position: absolute;
// width: 100%;
// height: 4px;
// background-color: @slider-rail-background-color;
// border-radius: @border-radius-base;
// transition: background-color 0.3s;
// }
&-track {
position: absolute;
height: 4px;
background-color: @slider-track-background-color;
border-radius: @border-radius-base;
transition: background-color 0.3s;
}
// &-track {
// position: absolute;
// height: 4px;
// background-color: @slider-track-background-color;
// border-radius: @border-radius-base;
// transition: background-color 0.3s;
// }
&-handle {
position: absolute;
width: @slider-handle-size;
height: @slider-handle-size;
margin-top: @slider-handle-margin-top;
background-color: @slider-handle-background-color;
border: solid @slider-handle-border-width @slider-handle-color;
border-radius: 50%;
box-shadow: @slider-handle-shadow;
cursor: pointer;
transition: border-color 0.3s, box-shadow 0.6s,
transform 0.3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
// &-handle {
// position: absolute;
// width: @slider-handle-size;
// height: @slider-handle-size;
// margin-top: @slider-handle-margin-top;
// background-color: @slider-handle-background-color;
// border: solid @slider-handle-border-width @slider-handle-color;
// border-radius: 50%;
// box-shadow: @slider-handle-shadow;
// cursor: pointer;
// transition: border-color 0.3s, box-shadow 0.6s,
// transform 0.3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
// &-dragging&-dragging&-dragging {
// border-color: @slider-handle-color-focus;
// box-shadow: 0 0 0 5px @slider-handle-color-focus-shadow;
// }
&-dragging {
z-index: 1;
}
// // &-dragging&-dragging&-dragging {
// // border-color: @slider-handle-color-focus;
// // box-shadow: 0 0 0 5px @slider-handle-color-focus-shadow;
// // }
// &-dragging {
// z-index: 1;
// }
&:focus {
border-color: @slider-handle-color-focus;
outline: none;
box-shadow: 0 0 0 5px @slider-handle-color-focus-shadow;
}
// &:focus {
// border-color: @slider-handle-color-focus;
// outline: none;
// box-shadow: 0 0 0 5px @slider-handle-color-focus-shadow;
// }
&.@{ant-prefix}-tooltip-open {
border-color: @slider-handle-color-tooltip-open;
}
}
// &.@{ant-prefix}-tooltip-open {
// border-color: @slider-handle-color-tooltip-open;
// }
// }
&:hover {
.@{slider-prefix-cls}-rail {
background-color: @slider-rail-background-color-hover;
}
.@{slider-prefix-cls}-track {
background-color: @slider-track-background-color-hover;
}
.@{slider-prefix-cls}-handle:not(.@{ant-prefix}-tooltip-open) {
border-color: @slider-handle-color-hover;
}
}
// &:hover {
// .@{slider-prefix-cls}-rail {
// background-color: @slider-rail-background-color-hover;
// }
// .@{slider-prefix-cls}-track {
// background-color: @slider-track-background-color-hover;
// }
// .@{slider-prefix-cls}-handle:not(.@{ant-prefix}-tooltip-open) {
// border-color: @slider-handle-color-hover;
// }
// }
&-mark {
position: absolute;
top: 14px;
left: 0;
width: 100%;
font-size: @font-size-base;
}
// &-mark {
// position: absolute;
// top: 14px;
// left: 0;
// width: 100%;
// font-size: @font-size-base;
// }
&-mark-text {
position: absolute;
display: inline-block;
color: @text-color-secondary;
text-align: center;
word-break: keep-all;
cursor: pointer;
user-select: none;
// &-mark-text {
// position: absolute;
// display: inline-block;
// color: @text-color-secondary;
// text-align: center;
// word-break: keep-all;
// cursor: pointer;
// user-select: none;
&-active {
color: @text-color;
}
}
// &-active {
// color: @text-color;
// }
// }
&-step {
position: absolute;
width: 100%;
height: 4px;
background: transparent;
pointer-events: none;
}
// &-step {
// position: absolute;
// width: 100%;
// height: 4px;
// background: transparent;
// pointer-events: none;
// }
&-dot {
position: absolute;
top: -2px;
width: 8px;
height: 8px;
background-color: @component-background;
border: 2px solid @slider-dot-border-color;
border-radius: 50%;
cursor: pointer;
// &-dot {
// position: absolute;
// top: -2px;
// width: 8px;
// height: 8px;
// background-color: @component-background;
// border: 2px solid @slider-dot-border-color;
// border-radius: 50%;
// cursor: pointer;
&-active {
border-color: @slider-dot-border-color-active;
}
}
// &-active {
// border-color: @slider-dot-border-color-active;
// }
// }
&-disabled {
cursor: not-allowed;
// &-disabled {
// cursor: not-allowed;
.@{slider-prefix-cls}-rail {
background-color: @slider-rail-background-color !important;
}
// .@{slider-prefix-cls}-rail {
// background-color: @slider-rail-background-color !important;
// }
.@{slider-prefix-cls}-track {
background-color: @slider-disabled-color !important;
}
// .@{slider-prefix-cls}-track {
// background-color: @slider-disabled-color !important;
// }
.@{slider-prefix-cls}-handle,
.@{slider-prefix-cls}-dot {
background-color: @component-background;
border-color: @slider-disabled-color !important;
box-shadow: none;
cursor: not-allowed;
}
// .@{slider-prefix-cls}-handle,
// .@{slider-prefix-cls}-dot {
// background-color: @component-background;
// border-color: @slider-disabled-color !important;
// box-shadow: none;
// cursor: not-allowed;
// }
.@{slider-prefix-cls}-mark-text,
.@{slider-prefix-cls}-dot {
cursor: not-allowed !important;
}
}
}
// .@{slider-prefix-cls}-mark-text,
// .@{slider-prefix-cls}-dot {
// cursor: not-allowed !important;
// }
// }
// }
.vertical() {
&-vertical {
width: 12px;
height: 100%;
margin: 6px 10px;
padding: 0 4px;
// .vertical() {
// &-vertical {
// width: 12px;
// height: 100%;
// margin: 6px 10px;
// padding: 0 4px;
.@{slider-prefix-cls}-rail {
width: 4px;
height: 100%;
}
// .@{slider-prefix-cls}-rail {
// width: 4px;
// height: 100%;
// }
.@{slider-prefix-cls}-track {
width: 4px;
}
// .@{slider-prefix-cls}-track {
// width: 4px;
// }
.@{slider-prefix-cls}-handle {
margin-top: -6px; // we chould consider border width as well: (10 + 2 ) / 2
margin-left: -5px;
}
// .@{slider-prefix-cls}-handle {
// margin-top: -6px; // we chould consider border width as well: (10 + 2 ) / 2
// margin-left: -5px;
// }
.@{slider-prefix-cls}-mark {
top: 0;
left: 12px;
width: 18px;
height: 100%;
}
// .@{slider-prefix-cls}-mark {
// top: 0;
// left: 12px;
// width: 18px;
// height: 100%;
// }
.@{slider-prefix-cls}-mark-text {
left: 4px;
white-space: nowrap;
}
// .@{slider-prefix-cls}-mark-text {
// left: 4px;
// white-space: nowrap;
// }
.@{slider-prefix-cls}-step {
width: 4px;
height: 100%;
}
// .@{slider-prefix-cls}-step {
// width: 4px;
// height: 100%;
// }
.@{slider-prefix-cls}-dot {
top: auto;
margin-left: -2px;
}
}
// .@{slider-prefix-cls}-dot {
// top: auto;
// margin-left: -2px;
// }
// }
&-tooltip {
// https://github.com/ant-design/ant-design/issues/20014
.@{ant-prefix}-tooltip-inner {
min-width: unset;
}
}
}
// &-tooltip {
// // https://github.com/ant-design/ant-design/issues/20014
// .@{ant-prefix}-tooltip-inner {
// min-width: unset;
// }
// }
// }
@import './rtl';
// @import './rtl';

View File

@ -1,5 +1,296 @@
import '../../style/index.less';
import './index.less';
// import '../../style/index.less';
// import './index.less';
// style dependencies
import '../../tooltip/style';
// // style dependencies
// import '../../tooltip/style';
// deps-lint-skip-all
import * as React from 'react';
import { CSSObject } from '@ant-design/cssinjs';
import { TinyColor } from '@ctrl/tinycolor';
import {
DerivativeToken,
useStyleRegister,
useToken,
UseComponentStyleResult,
GenerateStyle,
resetComponent,
} from '../../_util/theme';
interface SliderToken extends DerivativeToken {
sliderCls: string;
handleSize: number;
sliderSize: number;
railSize: number;
dotSize: number;
}
// =============================== Base ===============================
const genBaseStyle: GenerateStyle<SliderToken> = token => {
const { sliderCls, sliderSize, dotSize } = token;
const FIXED_RAIL_HOVER_COLOR = '#e1e1e1';
return {
[sliderCls]: {
...resetComponent(token),
position: 'relative',
height: sliderSize,
margin: '10px 6px', // FIXME: hard code in v4
padding: 0,
cursor: 'pointer',
touchAction: 'none',
// .vertical();
[`${sliderCls}-rail`]: {
position: 'absolute',
backgroundColor: token.background,
borderRadius: token.borderRadius,
transition: `background-color ${token.duration}`,
},
[`${sliderCls}-track`]: {
position: 'absolute',
backgroundColor: token.tmpPrimaryColorWeak, // FIXME: Origin @primary-3
borderRadius: token.borderRadius,
transition: `background-color ${token.duration}`,
},
[`${sliderCls}-handle`]: {
position: 'absolute',
width: token.handleSize,
height: token.handleSize,
backgroundColor: token.componentBackground,
border: `2px solid ${token.tmpPrimaryColorWeak}`,
borderRadius: '50%',
boxShadow: 'none',
cursor: 'pointer',
transition: `
border-color ${token.duration},
box-shadow ${token.duration},
transform ${token.duration} cubic-bezier(0.18, 0.89, 0.32, 1.28)
`,
outline: 'none',
[`${sliderCls}-dragging`]: {
zIndex: 1,
},
'&:focus': {
borderColor: token.primaryHoverColor,
boxShadow: 'none',
},
'&:focus-visible': {
// FIXME: This is a inline color calculation
boxShadow: `0 0 0 5px ${new TinyColor(token.primaryHoverColor)
.setAlpha(0.2)
.toRgbString()}`,
},
// FIXME: Seems useless?
// &.@{ant-prefix}-tooltip-open {
// border-color: @slider-handle-color-tooltip-open;
// }
},
'&:hover': {
[`${sliderCls}-rail`]: {
backgroundColor: FIXED_RAIL_HOVER_COLOR, // FIXME: Not match color
},
[`${sliderCls}-track`]: {
backgroundColor: token.primaryHoverColor, // FIXME: origin primary-4
},
[`${sliderCls}-dot`]: {
borderColor: FIXED_RAIL_HOVER_COLOR,
},
// FIXME: We use below style instead
// ${sliderCls}-handle:not(.@{ant-prefix}-tooltip-open) {
// border-color: @slider-handle-color-hover;
// }
[`
${sliderCls}-handle,
${sliderCls}-dot-active
`]: {
borderColor: token.primaryHoverColor,
},
},
[`${sliderCls}-mark`]: {
position: 'absolute',
fontSize: token.fontSize,
},
[`${sliderCls}-mark-text`]: {
position: 'absolute',
display: 'inline-block',
color: token.textColorSecondary,
textAlign: 'center',
wordBreak: 'keep-all',
cursor: 'pointer',
userSelect: 'none',
'&-active': {
color: token.textColor,
},
},
[`${sliderCls}-step`]: {
position: 'absolute',
background: 'transparent',
pointerEvents: 'none',
},
[`${sliderCls}-dot`]: {
position: 'absolute',
width: dotSize,
height: dotSize,
backgroundColor: token.componentBackground,
border: `2px solid ${token.borderColorSplit}`, // FIXME: hardcode in v4
borderRadius: '50%',
cursor: 'pointer',
transition: `border-color ${token.duration}`,
'&-active': {
borderColor: token.tmpPrimaryColorWeak,
},
},
'&-disabled': {
cursor: 'not-allowed',
[`${sliderCls}-rail`]: {
backgroundColor: `${token.background} !important`,
},
[`${sliderCls}-track`]: {
backgroundColor: `${token.textColorDisabled} !important`,
},
[`
${sliderCls}-handle,
${sliderCls}-dot
`]: {
backgroundColor: token.componentBackground,
borderColor: `${token.textColorDisabled} !important`,
boxShadow: 'none',
cursor: 'not-allowed',
},
[`
${sliderCls}-mark-text,
${sliderCls}-dot
`]: {
cursor: `not-allowed !important`,
},
},
},
};
};
// ============================ Horizontal ============================
const genDirectionStyle = (token: SliderToken, horizontal: boolean): CSSObject => {
const { sliderCls, railSize, sliderSize, handleSize, dotSize } = token;
const railPadding: keyof React.CSSProperties = horizontal ? 'paddingBlock' : 'paddingInline';
const stretch: keyof React.CSSProperties = horizontal ? 'width' : 'height';
const contain: keyof React.CSSProperties = horizontal ? 'height' : 'width';
const handlePos: keyof React.CSSProperties = horizontal ? 'insetBlockStart' : 'insetInlineStart';
const markInset: keyof React.CSSProperties = horizontal ? 'top' : 'insetInlineStart';
return {
[railPadding]: railSize,
[`${sliderCls}-rail`]: {
[stretch]: '100%',
[contain]: railSize,
},
[`${sliderCls}-track`]: {
[contain]: railSize,
},
[`${sliderCls}-handle`]: {
[handlePos]: (sliderSize - handleSize) / 2,
},
[`${sliderCls}-mark`]: {
// Reset all
insetInlineStart: 0,
top: 0,
[markInset]: handleSize,
[stretch]: '100%',
},
[`${sliderCls}-step`]: {
// Reset all
insetInlineStart: 0,
top: 0,
[markInset]: railSize,
[stretch]: '100%',
[contain]: railSize,
},
[`${sliderCls}-dot`]: {
position: 'absolute',
[handlePos]: (railSize - dotSize) / 2,
},
};
};
// ============================ Horizontal ============================
const genHorizontalStyle: GenerateStyle<SliderToken> = token => {
const { sliderCls } = token;
return {
[`${sliderCls}-horizontal`]: {
...genDirectionStyle(token, true),
[`&${sliderCls}-with-marks`]: {
marginBottom: 28, // FIXME: hard code in v4
},
},
};
};
// ============================= Vertical =============================
const genVerticalStyle: GenerateStyle<SliderToken> = token => {
const { sliderCls } = token;
return {
[`${sliderCls}-vertical`]: {
...genDirectionStyle(token, false),
height: '100%',
},
};
};
// ============================== Export ==============================
export default function useStyle(prefixCls: string): UseComponentStyleResult {
const [theme, token, hashId] = useToken();
const sliderSize = 12; // FIXME: hard code in v4
const sliderToken: SliderToken = {
...token,
sliderCls: `.${prefixCls}`,
handleSize: 14, // FIXME: hard code in v4
sliderSize,
railSize: sliderSize / 3,
dotSize: 8,
};
return [
useStyleRegister({ theme, token, hashId, path: [prefixCls] }, () => [
genBaseStyle(sliderToken, hashId),
genHorizontalStyle(sliderToken),
genVerticalStyle(sliderToken),
]),
hashId,
];
}