feat(module)!: use tailwind-merge for class merging (#509)

This commit is contained in:
Benjamin Canac
2023-08-12 17:17:00 +02:00
parent 6d7973f6e1
commit 8880bdc456
47 changed files with 685 additions and 376 deletions

View File

@@ -10,7 +10,7 @@
:step="step"
type="range"
:class="[inputClass, thumbClass, trackClass]"
v-bind="$attrs"
v-bind="attrs"
@change="onChange"
>
@@ -21,8 +21,9 @@
<script lang="ts">
import { computed, defineComponent } from 'vue'
import type { PropType } from 'vue'
import { defu } from 'defu'
import { classNames } from '../../utils'
import { omit } from 'lodash-es'
import { twMerge, twJoin } from 'tailwind-merge'
import { defuTwMerge } from '../../utils'
import { useFormGroup } from '../../composables/useFormGroup'
import { useAppConfig } from '#imports'
// TODO: Remove
@@ -70,17 +71,21 @@ export default defineComponent({
return appConfig.ui.colors.includes(value)
}
},
inputClass: {
type: String,
default: null
},
ui: {
type: Object as PropType<Partial<typeof appConfig.ui.range>>,
default: () => appConfig.ui.range
default: () => ({})
}
},
emits: ['update:modelValue', 'change'],
setup (props, { emit }) {
setup (props, { emit, attrs }) {
// TODO: Remove
const appConfig = useAppConfig()
const ui = computed<Partial<typeof appConfig.ui.range>>(() => defu({}, props.ui, appConfig.ui.range))
const ui = computed<Partial<typeof appConfig.ui.range>>(() => defuTwMerge({}, props.ui, appConfig.ui.range))
const { emitFormChange, formGroup } = useFormGroup()
const color = computed(() => formGroup?.error?.value ? 'red' : props.color)
@@ -101,24 +106,24 @@ export default defineComponent({
}
const wrapperClass = computed(() => {
return classNames(
return twMerge(twJoin(
ui.value.wrapper,
ui.value.size[size.value]
)
), attrs.class as string)
})
const inputClass = computed(() => {
return classNames(
return twMerge(twJoin(
ui.value.base,
ui.value.background,
ui.value.rounded,
ui.value.ring.replaceAll('{color}', color.value),
ui.value.size[size.value]
)
), props.inputClass)
})
const thumbClass = computed(() => {
return classNames(
return twJoin(
ui.value.thumb.base,
// Intermediate class to allow thumb ring or background color (set to `current`) as it's impossible to safelist with arbitrary values
ui.value.thumb.color.replaceAll('{color}', color.value),
@@ -129,7 +134,7 @@ export default defineComponent({
})
const trackClass = computed(() => {
return classNames(
return twJoin(
ui.value.track.base,
ui.value.track.background,
ui.value.track.rounded,
@@ -138,7 +143,7 @@ export default defineComponent({
})
const progressClass = computed(() => {
return classNames(
return twJoin(
ui.value.progress.base,
ui.value.progress.rounded,
ui.value.progress.background.replaceAll('{color}', color.value),
@@ -156,10 +161,12 @@ export default defineComponent({
})
return {
attrs: omit(attrs, ['class']),
// eslint-disable-next-line vue/no-dupe-keys
ui,
value,
wrapperClass,
// eslint-disable-next-line vue/no-dupe-keys
inputClass,
thumbClass,
trackClass,