optimize(components): use tailwind-merge for smarter class merging

This commit is contained in:
orangelckc 2024-05-31 22:21:22 +02:00
parent ce3ccd473a
commit d965142579
No known key found for this signature in database
GPG Key ID: ABA70086FAC6334C

View File

@ -1,7 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed } from 'vue';
import { createReusableTemplate } from '@vueuse/core'; import { createReusableTemplate } from '@vueuse/core';
import type { PopoverPlacement } from 'naive-ui'; import type { PopoverPlacement } from 'naive-ui';
import { twMerge } from 'tailwind-merge';
defineOptions({ defineOptions({
name: 'ButtonIcon', name: 'ButtonIcon',
@ -21,38 +21,22 @@ interface Props {
} }
const props = withDefaults(defineProps<Props>(), { const props = withDefaults(defineProps<Props>(), {
class: 'h-36px text-icon', class: '',
icon: '', icon: '',
tooltipContent: '', tooltipContent: '',
tooltipPlacement: 'bottom', tooltipPlacement: 'bottom',
zIndex: 98 zIndex: 98
}); });
interface ButtonProps { const [DefineButton, Button] = createReusableTemplate();
className: string;
}
const [DefineButton, Button] = createReusableTemplate<ButtonProps>(); const DEFAULT_CLASS = 'h-[36px] text-icon';
const cls = computed(() => {
let clsStr = props.class;
if (!clsStr.includes('h-')) {
clsStr += ' h-36px';
}
if (!clsStr.includes('text-')) {
clsStr += ' text-icon';
}
return clsStr;
});
</script> </script>
<template> <template>
<!-- define component start: Button --> <!-- define component start: Button -->
<DefineButton v-slot="{ $slots, className }"> <DefineButton v-slot="{ $slots }">
<NButton quaternary :class="className"> <NButton quaternary :class="twMerge(DEFAULT_CLASS, props.class)" v-bind="$attrs">
<div class="flex-center gap-8px"> <div class="flex-center gap-8px">
<component :is="$slots.default" /> <component :is="$slots.default" />
</div> </div>
@ -62,7 +46,7 @@ const cls = computed(() => {
<NTooltip v-if="tooltipContent" :placement="tooltipPlacement" :z-index="zIndex"> <NTooltip v-if="tooltipContent" :placement="tooltipPlacement" :z-index="zIndex">
<template #trigger> <template #trigger>
<Button :class-name="cls" v-bind="$attrs"> <Button>
<slot> <slot>
<SvgIcon :icon="icon" /> <SvgIcon :icon="icon" />
</slot> </slot>
@ -70,7 +54,7 @@ const cls = computed(() => {
</template> </template>
{{ tooltipContent }} {{ tooltipContent }}
</NTooltip> </NTooltip>
<Button v-else :class-name="cls" v-bind="$attrs"> <Button v-else>
<slot> <slot>
<SvgIcon :icon="icon" /> <SvgIcon :icon="icon" />
</slot> </slot>