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