/
Toggle.vue
186 lines (173 loc) · 4.76 KB
/
Toggle.vue
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<template>
<HSwitch
:id="inputId"
v-model="active"
:name="name"
:disabled="disabled || loading"
:class="switchClass"
v-bind="attrs"
>
<span :class="containerClass">
<span v-if="loading" :class="[ui.icon.active, ui.icon.base]" aria-hidden="true">
<UIcon :name="loadingIcon" :class="loadingIconClass" />
</span>
<span
v-if="!loading && onIcon"
:class="[active ? ui.icon.active : ui.icon.inactive, ui.icon.base]"
aria-hidden="true"
>
<UIcon :name="onIcon" :class="onIconClass" />
</span>
<span
v-if="!loading && offIcon"
:class="[active ? ui.icon.inactive : ui.icon.active, ui.icon.base]"
aria-hidden="true"
>
<UIcon :name="offIcon" :class="offIconClass" />
</span>
</span>
</HSwitch>
</template>
<script lang="ts">
import { computed, toRef, defineComponent } from 'vue'
import type { PropType } from 'vue'
import { Switch as HSwitch, provideUseId } from '@headlessui/vue'
import { twMerge, twJoin } from 'tailwind-merge'
import UIcon from '../elements/Icon.vue'
import { useUI } from '../../composables/useUI'
import { useFormGroup } from '../../composables/useFormGroup'
import { mergeConfig } from '../../utils'
import type { ToggleSize, ToggleColor, Strategy } from '../../types'
// @ts-expect-error
import appConfig from '#build/app.config'
import { toggle } from '#ui/ui.config'
import { useId } from '#imports'
const config = mergeConfig<typeof toggle>(appConfig.ui.strategy, appConfig.ui.toggle, toggle)
export default defineComponent({
components: {
HSwitch,
UIcon
},
inheritAttrs: false,
props: {
id: {
type: String,
default: null
},
name: {
type: String,
default: null
},
modelValue: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
},
loading: {
type: Boolean,
default: false
},
onIcon: {
type: String,
default: () => config.default.onIcon
},
offIcon: {
type: String,
default: () => config.default.offIcon
},
loadingIcon: {
type: String,
default: () => config.default.loadingIcon
},
color: {
type: String as PropType<ToggleColor>,
default: () => config.default.color,
validator (value: string) {
return appConfig.ui.colors.includes(value)
}
},
size: {
type: String as PropType<ToggleSize>,
default: () => config.default.size,
validator (value: string) {
return Object.keys(config.size).includes(value)
}
},
class: {
type: [String, Object, Array] as PropType<any>,
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
emits: ['update:modelValue', 'change'],
setup (props, { emit }) {
const { ui, attrs } = useUI('toggle', toRef(props, 'ui'), config)
const { emitFormChange, color, inputId, name } = useFormGroup(props)
const active = computed({
get () {
return props.modelValue
},
set (value) {
emit('update:modelValue', value)
emit('change', value)
emitFormChange()
}
})
const switchClass = computed(() => {
return twMerge(twJoin(
ui.value.base,
ui.value.size[props.size],
ui.value.rounded,
color.value && ui.value.ring.replaceAll('{color}', color.value),
color.value && (active.value ? ui.value.active : ui.value.inactive).replaceAll('{color}', color.value)
), props.class)
})
const containerClass = computed(() => {
return twJoin(
ui.value.container.base,
ui.value.container.size[props.size],
(active.value ? ui.value.container.active[props.size] : ui.value.container.inactive)
)
})
const onIconClass = computed(() => {
return twJoin(
ui.value.icon.size[props.size],
color.value && ui.value.icon.on.replaceAll('{color}', color.value)
)
})
const offIconClass = computed(() => {
return twJoin(
ui.value.icon.size[props.size],
color.value && ui.value.icon.off.replaceAll('{color}', color.value)
)
})
const loadingIconClass = computed(() => {
return twJoin(
ui.value.icon.size[props.size],
color.value && ui.value.icon.loading.replaceAll('{color}', color.value)
)
})
provideUseId(() => useId())
return {
// eslint-disable-next-line vue/no-dupe-keys
ui,
attrs,
// eslint-disable-next-line vue/no-dupe-keys
name,
inputId,
active,
switchClass,
containerClass,
onIconClass,
offIconClass,
loadingIconClass
}
}
})
</script>