Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<script setup lang="ts">
import { computed } from 'vue';
import type { TThemeColor, TThemeColorNoWhite } from '@interfaces/common';
import { convert500ThemeToColor, convert800ThemeToColor } from '@helpers/colors';
const props = withDefaults(
defineProps<{
size?: 'small' | 'medium' | 'large' | 'huge';
theme?: TThemeColorNoWhite;
negativeTheme?: TThemeColor;
darkNegative?: boolean;
}>(),
{
size: 'medium',
theme: 'sky',
negativeTheme: 'black',
darkNegative: true,
},
);
const active = defineModel<boolean>('active');
const themeColor = computed(() => convert500ThemeToColor(props.theme));
const inactiveColor = computed(() =>
props.darkNegative
? convert800ThemeToColor(props.negativeTheme)
: convert500ThemeToColor(props.negativeTheme),
);
const sizes = computed(() => {
if (!props?.size) {
return {
containerWidth: 45,
containerHeight: 27,
padding: 4,
borderRadius: 14,
circleSize: 19,
transformXCircle: 18,
};
}
switch (props.size) {
case 'small':
return {
containerWidth: 35,
containerHeight: 21,
padding: 3,
borderRadius: 11,
circleSize: 15,
transformXCircle: 14,
};
case 'large':
return {
containerWidth: 55,
containerHeight: 33,
padding: 4,
borderRadius: 17,
circleSize: 25,
transformXCircle: 22,
};
case 'huge':
return {
containerWidth: 70,
containerHeight: 42,
padding: 6,
borderRadius: 21,
circleSize: 30,
transformXCircle: 28,
};
}
return {
containerWidth: 45,
containerHeight: 27,
padding: 4,
borderRadius: 14,
circleSize: 19,
transformXCircle: 18,
};
});
</script>
<template>
<button
:style="`width: ${sizes.containerWidth}px; min-height: ${sizes.containerHeight}px; border-radius: ${sizes.borderRadius}px; padding: ${sizes.padding}px;`"
class="switcher"
@click.prevent="active = !active"
>
<span
:style="`background-color: ${themeColor}; border-radius: ${sizes.borderRadius}px;`"
:class="[
'activeBackground',
{
inactiveBackground: !active,
},
]"
></span>
<span
:style="`width: ${sizes.circleSize}px; height: ${sizes.circleSize}px; transform: translateX(${active ? sizes.transformXCircle : 0}px);`"
class="switcherCircle"
></span>
</button>
</template>
<style scoped>
.switcher {
position: relative;
cursor: pointer;
}
.activeBackground {
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transition: background-color 0.2s ease-in-out;
}
.inactiveBackground {
background-color: v-bind(inactiveColor) !important;
transition: background-color 0.2s ease-in-out;
}
.switcher:hover .inactiveBackground {
filter: brightness(90%);
}
.switcherCircle {
display: block;
background-color: white;
border-radius: 50%;
transition: transform 0.2s ease-in-out;
}
</style>