style: update switch

feat-css-var
tangjinzhou 2022-03-06 21:50:58 +08:00
parent 2e3dcab8c1
commit cc387b9bbd
7 changed files with 208 additions and 149 deletions

View File

@ -795,11 +795,17 @@
// ---
@switch-height: 22px;
@switch-sm-height: 16px;
@switch-sm-checked-margin-left: -(@switch-sm-height - 3px);
@switch-min-width: 44px;
@switch-sm-min-width: 28px;
@switch-disabled-opacity: 0.4;
@switch-color: @primary-color;
@switch-bg: @component-background;
@switch-shadow-color: fade(#00230b, 20%);
@switch-padding: 2px;
@switch-inner-margin-min: ceil(@switch-height * 0.3);
@switch-inner-margin-max: ceil(@switch-height * 1.1);
@switch-sm-inner-margin-min: ceil(@switch-sm-height * 0.3);
@switch-sm-inner-margin-max: ceil(@switch-sm-height * 1.1);
// Pagination
// ---

View File

@ -2,34 +2,54 @@
exports[`renders ./components/switch/demo/basic.vue correctly 1`] = `
<button type="button" role="switch" aria-checked="false" class="ant-switch">
<!----><span class="ant-switch-inner"><!----></span>
<div class="ant-switch-handle">
<!---->
</div><span class="ant-switch-inner"><!----></span>
</button>
`;
exports[`renders ./components/switch/demo/disabled.vue correctly 1`] = `
<div><button disabled="" style="margin-bottom: 5px;" type="button" role="switch" aria-checked="true" class="ant-switch-checked ant-switch-disabled ant-switch">
<!----><span class="ant-switch-inner"><!----></span>
<div class="ant-switch-handle">
<!---->
</div><span class="ant-switch-inner"><!----></span>
</button><br><button class="ant-btn ant-btn-primary" type="button">
<!----><span>Toggle disabled</span>
</button></div>
`;
exports[`renders ./components/switch/demo/loading.vue correctly 1`] = `<div><button disabled="" loading="true" type="button" role="switch" aria-checked="true" class="ant-switch-loading ant-switch-checked ant-switch"><span role="img" aria-label="loading" class="anticon anticon-loading ant-switch-loading-icon"><svg focusable="false" class="anticon-spin" data-icon="loading" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="0 0 1024 1024"><path d="M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z"></path></svg></span><span class="ant-switch-inner"><!----></span></button><br><button size="small" disabled="" loading="true" type="button" role="switch" aria-checked="false" class="ant-switch-small ant-switch-loading ant-switch"><span role="img" aria-label="loading" class="anticon anticon-loading ant-switch-loading-icon"><svg focusable="false" class="anticon-spin" data-icon="loading" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="0 0 1024 1024"><path d="M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z"></path></svg></span><span class="ant-switch-inner"><!----></span></button></div>`;
exports[`renders ./components/switch/demo/loading.vue correctly 1`] = `
<div><button disabled="" loading="true" type="button" role="switch" aria-checked="true" class="ant-switch-loading ant-switch-checked ant-switch">
<div class="ant-switch-handle"><span role="img" aria-label="loading" class="anticon anticon-loading ant-switch-loading-icon"><svg focusable="false" class="anticon-spin" data-icon="loading" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="0 0 1024 1024"><path d="M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z"></path></svg></span></div><span class="ant-switch-inner"><!----></span>
</button><br><button size="small" disabled="" loading="true" type="button" role="switch" aria-checked="false" class="ant-switch-small ant-switch-loading ant-switch">
<div class="ant-switch-handle"><span role="img" aria-label="loading" class="anticon anticon-loading ant-switch-loading-icon"><svg focusable="false" class="anticon-spin" data-icon="loading" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="0 0 1024 1024"><path d="M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z"></path></svg></span></div><span class="ant-switch-inner"><!----></span>
</button></div>
`;
exports[`renders ./components/switch/demo/size.vue correctly 1`] = `
<div><button type="button" role="switch" aria-checked="true" class="ant-switch-checked ant-switch">
<!----><span class="ant-switch-inner"><!----></span>
<div class="ant-switch-handle">
<!---->
</div><span class="ant-switch-inner"><!----></span>
</button><br><button size="small" type="button" role="switch" aria-checked="false" class="ant-switch-small ant-switch">
<!----><span class="ant-switch-inner"><!----></span>
<div class="ant-switch-handle">
<!---->
</div><span class="ant-switch-inner"><!----></span>
</button></div>
`;
exports[`renders ./components/switch/demo/text.vue correctly 1`] = `
<div><button type="button" role="switch" aria-checked="true" class="ant-switch-checked ant-switch">
<!----><span class="ant-switch-inner">开</span>
<div class="ant-switch-handle">
<!---->
</div><span class="ant-switch-inner">开</span>
</button><br><button type="button" role="switch" aria-checked="false" class="ant-switch">
<!----><span class="ant-switch-inner">0</span>
<div class="ant-switch-handle">
<!---->
</div><span class="ant-switch-inner">0</span>
</button><br><button type="button" role="switch" aria-checked="false" class="ant-switch">
<!----><span class="ant-switch-inner"><span role="img" aria-label="close" class="anticon anticon-close"><svg focusable="false" class="" data-icon="close" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896"><path d="M563.8 512l262.5-312.9c4.4-5.2.7-13.1-6.1-13.1h-79.8c-4.7 0-9.2 2.1-12.3 5.7L511.6 449.8 295.1 191.7c-3-3.6-7.5-5.7-12.3-5.7H203c-6.8 0-10.5 7.9-6.1 13.1L459.4 512 196.9 824.9A7.95 7.95 0 00203 838h79.8c4.7 0 9.2-2.1 12.3-5.7l216.5-258.1 216.5 258.1c3 3.6 7.5 5.7 12.3 5.7h79.8c6.8 0 10.5-7.9 6.1-13.1L563.8 512z"></path></svg></span></span>
<div class="ant-switch-handle">
<!---->
</div><span class="ant-switch-inner"><span role="img" aria-label="close" class="anticon anticon-close"><svg focusable="false" class="" data-icon="close" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896"><path d="M563.8 512l262.5-312.9c4.4-5.2.7-13.1-6.1-13.1h-79.8c-4.7 0-9.2 2.1-12.3 5.7L511.6 449.8 295.1 191.7c-3-3.6-7.5-5.7-12.3-5.7H203c-6.8 0-10.5 7.9-6.1 13.1L459.4 512 196.9 824.9A7.95 7.95 0 00203 838h79.8c4.7 0 9.2-2.1 12.3-5.7l216.5-258.1 216.5 258.1c3 3.6 7.5 5.7 12.3 5.7h79.8c6.8 0 10.5-7.9 6.1-13.1L563.8 512z"></path></svg></span></span>
</button></div>
`;

View File

@ -2,6 +2,8 @@
exports[`Switch should has click wave effect 1`] = `
<button type="button" role="switch" aria-checked="true" class="ant-switch-checked ant-switch">
<!----><span class="ant-switch-inner"><!----></span>
<div class="ant-switch-handle">
<!---->
</div><span class="ant-switch-inner"><!----></span>
</button>
`;

View File

@ -84,7 +84,7 @@ const Switch = defineComponent({
},
);
const { prefixCls } = useConfigInject('switch', props);
const { prefixCls, direction, size } = useConfigInject('switch', props);
const refSwitchNode = ref();
const focus = () => {
refSwitchNode.value?.focus();
@ -138,11 +138,12 @@ const Switch = defineComponent({
};
const classNames = computed(() => ({
[`${prefixCls.value}-small`]: props.size === 'small',
[`${prefixCls.value}-small`]: size.value === 'small',
[`${prefixCls.value}-loading`]: props.loading,
[`${prefixCls.value}-checked`]: checkedStatus.value,
[`${prefixCls.value}-disabled`]: props.disabled,
[prefixCls.value]: true,
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
}));
return () => (
@ -171,7 +172,9 @@ const Switch = defineComponent({
class={[attrs.class, classNames.value]}
ref={refSwitchNode}
>
{props.loading ? <LoadingOutlined class={`${prefixCls.value}-loading-icon`} /> : null}
<div class={`${prefixCls.value}-handle`}>
{props.loading ? <LoadingOutlined class={`${prefixCls.value}-loading-icon`} /> : null}
</div>
<span class={`${prefixCls.value}-inner`}>
{checkedStatus.value
? getPropsSlot(slots, props, 'checkedChildren')

View File

@ -2,7 +2,10 @@
@import '../../style/mixins/index';
@switch-prefix-cls: ~'@{ant-prefix}-switch';
@switch-duration: 0.36s;
@switch-duration: 0.2s;
@switch-pin-size: @switch-height - 4px;
@switch-sm-pin-size: @switch-sm-height - 4px;
.@{switch-prefix-cls} {
.reset-component();
@ -10,169 +13,142 @@
position: relative;
display: inline-block;
box-sizing: border-box;
min-width: 44px;
min-width: @switch-min-width;
height: @switch-height;
line-height: @switch-height - 2px;
line-height: @switch-height;
vertical-align: middle;
background-color: @disabled-color;
border: 1px solid transparent;
border: 0;
border-radius: 100px;
cursor: pointer;
transition: all @switch-duration;
user-select: none;
&-inner {
display: block;
margin-right: 6px;
margin-left: 24px;
color: @text-color-inverse;
font-size: @font-size-sm;
}
&-loading-icon,
&::after {
position: absolute;
top: 1px;
left: 1px;
width: @switch-height - 4px;
height: @switch-height - 4px;
background-color: @switch-bg;
border-radius: 18px;
cursor: pointer;
transition: all @switch-duration @ease-in-out-circ;
content: ' ';
}
&::after {
box-shadow: 0 2px 4px 0 @switch-shadow-color;
}
&:not(&-disabled):active::before,
&:not(&-disabled):active::after {
width: 24px;
}
&-loading-icon {
z-index: 1;
display: none;
font-size: 12px;
// loading default use animation
// animation: loadingCircle 1s infinite linear;
background: transparent;
svg {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
}
&-loading &-loading-icon {
display: inline-block;
color: @text-color;
}
&-checked&-loading &-loading-icon {
color: @switch-color;
}
&:focus {
outline: 0;
box-shadow: 0 0 0 2px fade(@switch-color, 20%);
box-shadow: 0 0 0 2px fade(@disabled-color, 10%);
}
&-checked:focus {
box-shadow: 0 0 0 2px @primary-1;
}
&:focus:hover {
box-shadow: none;
}
&-small {
min-width: 28px;
height: @switch-sm-height;
line-height: @switch-sm-height - 2px;
.@{switch-prefix-cls}-inner {
margin-right: 3px;
margin-left: 18px;
font-size: @font-size-sm;
}
&::after {
width: @switch-sm-height - 4px;
height: @switch-sm-height - 4px;
}
&:active::before,
&:active::after {
width: 16px;
}
}
&-small &-loading-icon {
width: @switch-sm-height - 4px;
height: @switch-sm-height - 4px;
}
&-small&-checked {
.@{switch-prefix-cls}-inner {
margin-right: 18px;
margin-left: 3px;
}
}
&-small&-checked &-loading-icon {
left: 100%;
margin-left: @switch-sm-checked-margin-left;
}
&-small&-loading &-loading-icon {
font-weight: bold;
// animation: AntSwitchSmallLoadingCircle 1s infinite linear;
transform: scale(0.66667);
}
&-checked {
background-color: @switch-color;
.@{switch-prefix-cls}-inner {
margin-right: 24px;
margin-left: 6px;
}
&::after {
left: 100%;
margin-left: -1px;
transform: translateX(-100%);
}
}
&-checked &-loading-icon {
left: 100%;
margin-left: -19px;
}
&-loading,
&-disabled {
cursor: not-allowed;
opacity: @switch-disabled-opacity;
* {
box-shadow: none;
cursor: not-allowed;
}
&::before,
&::after {
cursor: not-allowed;
}
// ========================= Inner ==========================
&-inner {
display: block;
margin: 0 @switch-inner-margin-min 0 @switch-inner-margin-max;
color: @text-color-inverse;
font-size: @font-size-sm;
transition: margin @switch-duration;
}
&-checked &-inner {
margin: 0 @switch-inner-margin-max 0 @switch-inner-margin-min;
}
// ========================= Handle =========================
&-handle {
position: absolute;
top: @switch-padding;
left: @switch-padding;
width: @switch-pin-size;
height: @switch-pin-size;
transition: all @switch-duration ease-in-out;
&::before {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: @switch-bg;
border-radius: (@switch-pin-size / 2);
box-shadow: 0 2px 4px 0 @switch-shadow-color;
transition: all @switch-duration ease-in-out;
content: '';
}
}
&-checked &-handle {
left: calc(100% - @switch-pin-size - @switch-padding);
}
&:not(&-disabled):active {
.@{switch-prefix-cls}-handle::before {
right: -30%;
left: 0;
}
&.@{switch-prefix-cls}-checked {
.@{switch-prefix-cls}-handle::before {
right: 0;
left: -30%;
}
}
}
// ======================== Loading =========================
&-loading-icon.@{iconfont-css-prefix} {
position: relative;
top: ((@switch-pin-size - @font-size-base) / 2);
color: rgba(0, 0, 0, 0.65);
vertical-align: top;
}
&-checked &-loading-icon {
color: @switch-color;
}
// ========================== Size ==========================
&-small {
min-width: @switch-sm-min-width;
height: @switch-sm-height;
line-height: @switch-sm-height;
.@{switch-prefix-cls}-inner {
margin: 0 @switch-sm-inner-margin-min 0 @switch-sm-inner-margin-max;
font-size: @font-size-sm;
}
.@{switch-prefix-cls}-handle {
width: @switch-sm-pin-size;
height: @switch-sm-pin-size;
}
.@{switch-prefix-cls}-loading-icon {
top: ((@switch-sm-pin-size - 9px) / 2);
font-size: 9px;
}
&.@{switch-prefix-cls}-checked {
.@{switch-prefix-cls}-inner {
margin: 0 @switch-sm-inner-margin-max 0 @switch-sm-inner-margin-min;
}
.@{switch-prefix-cls}-handle {
left: calc(100% - @switch-sm-pin-size - @switch-padding);
}
}
}
}
@keyframes AntSwitchSmallLoadingCircle {
0% {
transform: rotate(0deg) scale(0.66667);
transform-origin: 50% 50%;
}
100% {
transform: rotate(360deg) scale(0.66667);
transform-origin: 50% 50%;
}
}
@import './rtl';

View File

@ -0,0 +1,52 @@
@import '../../style/themes/index';
@import '../../style/mixins/index';
@switch-prefix-cls: ~'@{ant-prefix}-switch';
@switch-pin-size: @switch-height - 4px;
@switch-sm-pin-size: @switch-sm-height - 4px;
.@{switch-prefix-cls}-rtl {
direction: rtl;
.@{switch-prefix-cls}-inner {
margin: 0 @switch-inner-margin-max 0 @switch-inner-margin-min;
}
.@{switch-prefix-cls}-handle {
right: @switch-padding;
left: auto;
}
&:not(&-disabled):active {
.@{switch-prefix-cls}-handle::before {
right: 0;
left: -30%;
}
&.@{switch-prefix-cls}-checked {
.@{switch-prefix-cls}-handle::before {
right: -30%;
left: 0;
}
}
}
&.@{switch-prefix-cls}-checked {
.@{switch-prefix-cls}-inner {
margin: 0 @switch-inner-margin-min 0 @switch-inner-margin-max;
}
.@{switch-prefix-cls}-handle {
right: calc(100% - @switch-pin-size - @switch-padding);
}
}
&.@{switch-prefix-cls}-small {
&.@{switch-prefix-cls}-checked {
.@{switch-prefix-cls}-handle {
right: calc(100% - @switch-sm-pin-size - @switch-padding);
}
}
}
}