|
| 1 | +<template> |
| 2 | + <button :type="htmlType" |
| 3 | + :disabled="disabled || loading" |
| 4 | + :class="buttonClasses" |
| 5 | + class="flex items-center space-x-2 justify-center rounded-md transition-all duration-200 cursor-pointer" |
| 6 | + @click="handleClick" |
| 7 | + v-bind="$attrs"> |
| 8 | + <!-- 加载状态图标 --> |
| 9 | + <div v-if="loading" class="animate-spin mr-2"> |
| 10 | + <Loader2 class="w-4 h-4"/> |
| 11 | + </div> |
| 12 | + |
| 13 | + <!-- 左侧图标 --> |
| 14 | + <component v-else-if="icon && iconPosition === 'left'" |
| 15 | + :is="icon" |
| 16 | + :class="iconClasses"/> |
| 17 | + |
| 18 | + <!-- 按钮文本 --> |
| 19 | + <span v-if="$slots.default || text" :class="textClasses"> |
| 20 | + <slot>{{ text }}</slot> |
| 21 | + </span> |
| 22 | + |
| 23 | + <!-- 右侧图标 --> |
| 24 | + <component v-if="icon && iconPosition === 'right' && !loading" |
| 25 | + :is="icon" |
| 26 | + :class="iconClasses"/> |
| 27 | + </button> |
| 28 | +</template> |
| 29 | + |
| 30 | +<script setup lang="ts"> |
| 31 | +import type { Component } from 'vue' |
| 32 | +import { computed } from 'vue' |
| 33 | +import { Loader2 } from 'lucide-vue-next' |
| 34 | +
|
| 35 | +interface Props |
| 36 | +{ |
| 37 | + // 按钮类型 |
| 38 | + type?: 'primary' | 'secondary' | 'success' | 'warning' | 'danger' | 'info' | 'ghost' | 'link' |
| 39 | + // 按钮尺寸 |
| 40 | + size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' |
| 41 | + // 按钮变体 |
| 42 | + variant?: 'solid' | 'outline' | 'ghost' | 'soft' |
| 43 | + // HTML 按钮类型 |
| 44 | + htmlType?: 'button' | 'submit' | 'reset' |
| 45 | + // 禁用状态 |
| 46 | + disabled?: boolean |
| 47 | + // 加载状态 |
| 48 | + loading?: boolean |
| 49 | + // 圆形按钮 |
| 50 | + circle?: boolean |
| 51 | + // 圆角按钮 |
| 52 | + round?: boolean |
| 53 | + // 块级按钮 |
| 54 | + block?: boolean |
| 55 | + // 图标 |
| 56 | + icon?: Component | string |
| 57 | + // 图标位置 |
| 58 | + iconPosition?: 'left' | 'right' |
| 59 | + // 仅图标按钮 |
| 60 | + iconOnly?: boolean |
| 61 | + // 按钮文本 |
| 62 | + text?: string |
| 63 | + // 自定义类名 |
| 64 | + customClass?: string | string[] |
| 65 | +} |
| 66 | +
|
| 67 | +const props = withDefaults(defineProps<Props>(), { |
| 68 | + type: 'primary', |
| 69 | + size: 'md', |
| 70 | + variant: 'solid', |
| 71 | + htmlType: 'button', |
| 72 | + disabled: false, |
| 73 | + loading: false, |
| 74 | + circle: false, |
| 75 | + round: false, |
| 76 | + block: false, |
| 77 | + iconPosition: 'left', |
| 78 | + iconOnly: false |
| 79 | +}) |
| 80 | +
|
| 81 | +const emit = defineEmits<{ |
| 82 | + click: [event: MouseEvent] |
| 83 | +}>() |
| 84 | +
|
| 85 | +// 基础样式 |
| 86 | +const baseClasses = computed(() => [ |
| 87 | + 'inline-flex items-center justify-center font-medium transition-all duration-200', |
| 88 | + 'focus:outline-none focus:ring-2 focus:ring-offset-2', |
| 89 | + 'disabled:cursor-not-allowed disabled:opacity-50', |
| 90 | + // 圆形按钮 |
| 91 | + props.circle ? 'rounded-full' : props.round ? 'rounded-full' : 'rounded-lg', |
| 92 | + // 块级按钮 |
| 93 | + props.block ? 'w-full' : '', |
| 94 | + // 仅图标按钮 |
| 95 | + props.iconOnly ? 'p-0' : '' |
| 96 | +]) |
| 97 | +
|
| 98 | +// 尺寸样式 |
| 99 | +const sizeClasses = computed(() => { |
| 100 | + const sizes = { |
| 101 | + xs: props.iconOnly ? 'w-6 h-6' : 'px-2 py-1 text-xs', |
| 102 | + sm: props.iconOnly ? 'w-8 h-8' : 'px-3 py-1.5 text-sm', |
| 103 | + md: props.iconOnly ? 'w-10 h-10' : 'px-4 py-2 text-sm', |
| 104 | + lg: props.iconOnly ? 'w-12 h-12' : 'px-6 py-3 text-base', |
| 105 | + xl: props.iconOnly ? 'w-14 h-14' : 'px-8 py-4 text-lg' |
| 106 | + } |
| 107 | + return sizes[props.size] |
| 108 | +}) |
| 109 | +
|
| 110 | +// 类型和变体样式 |
| 111 | +const typeClasses = computed(() => { |
| 112 | + const { type, variant } = props |
| 113 | +
|
| 114 | + const styles = { |
| 115 | + primary: { |
| 116 | + solid: 'bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500 border border-transparent', |
| 117 | + outline: 'border border-blue-600 text-blue-600 hover:bg-blue-50 focus:ring-blue-500', |
| 118 | + ghost: 'text-blue-600 hover:bg-blue-50 focus:ring-blue-500 border border-transparent', |
| 119 | + soft: 'bg-blue-50 text-blue-600 hover:bg-blue-100 focus:ring-blue-500 border border-transparent' |
| 120 | + }, |
| 121 | + secondary: { |
| 122 | + solid: 'bg-gray-600 text-white hover:bg-gray-700 focus:ring-gray-500 border border-transparent', |
| 123 | + outline: 'border border-gray-300 text-gray-700 hover:bg-gray-50 focus:ring-gray-500', |
| 124 | + ghost: 'text-gray-700 hover:bg-gray-50 focus:ring-gray-500 border border-transparent', |
| 125 | + soft: 'bg-gray-50 text-gray-700 hover:bg-gray-100 focus:ring-gray-500 border border-transparent' |
| 126 | + }, |
| 127 | + success: { |
| 128 | + solid: 'bg-green-600 text-white hover:bg-green-700 focus:ring-green-500 border border-transparent', |
| 129 | + outline: 'border border-green-600 text-green-600 hover:bg-green-50 focus:ring-green-500', |
| 130 | + ghost: 'text-green-600 hover:bg-green-50 focus:ring-green-500 border border-transparent', |
| 131 | + soft: 'bg-green-50 text-green-600 hover:bg-green-100 focus:ring-green-500 border border-transparent' |
| 132 | + }, |
| 133 | + warning: { |
| 134 | + solid: 'bg-yellow-500 text-white hover:bg-yellow-600 focus:ring-yellow-500 border border-transparent', |
| 135 | + outline: 'border border-yellow-500 text-yellow-600 hover:bg-yellow-50 focus:ring-yellow-500', |
| 136 | + ghost: 'text-yellow-600 hover:bg-yellow-50 focus:ring-yellow-500 border border-transparent', |
| 137 | + soft: 'bg-yellow-50 text-yellow-600 hover:bg-yellow-100 focus:ring-yellow-500 border border-transparent' |
| 138 | + }, |
| 139 | + danger: { |
| 140 | + solid: 'bg-red-600 text-white hover:bg-red-700 focus:ring-red-500 border border-transparent', |
| 141 | + outline: 'border border-red-600 text-red-600 hover:bg-red-50 focus:ring-red-500', |
| 142 | + ghost: 'text-red-600 hover:bg-red-50 focus:ring-red-500 border border-transparent', |
| 143 | + soft: 'bg-red-50 text-red-600 hover:bg-red-100 focus:ring-red-500 border border-transparent' |
| 144 | + }, |
| 145 | + info: { |
| 146 | + solid: 'bg-cyan-600 text-white hover:bg-cyan-700 focus:ring-cyan-500 border border-transparent', |
| 147 | + outline: 'border border-cyan-600 text-cyan-600 hover:bg-cyan-50 focus:ring-cyan-500', |
| 148 | + ghost: 'text-cyan-600 hover:bg-cyan-50 focus:ring-cyan-500 border border-transparent', |
| 149 | + soft: 'bg-cyan-50 text-cyan-600 hover:bg-cyan-100 focus:ring-cyan-500 border border-transparent' |
| 150 | + }, |
| 151 | + link: { |
| 152 | + solid: 'text-blue-600 hover:text-blue-700 underline focus:ring-blue-500 border border-transparent bg-transparent p-0', |
| 153 | + outline: 'text-blue-600 hover:text-blue-700 underline focus:ring-blue-500 border border-transparent bg-transparent p-0', |
| 154 | + ghost: 'text-blue-600 hover:text-blue-700 underline focus:ring-blue-500 border border-transparent bg-transparent p-0', |
| 155 | + soft: 'text-blue-600 hover:text-blue-700 underline focus:ring-blue-500 border border-transparent bg-transparent p-0' |
| 156 | + } |
| 157 | + } |
| 158 | +
|
| 159 | + return styles[type as keyof typeof styles]?.[variant] || styles.primary.solid |
| 160 | +}) |
| 161 | +
|
| 162 | +// 图标样式 |
| 163 | +const iconClasses = computed(() => { |
| 164 | + const hasText = props.text || !!(props as any).$slots?.default |
| 165 | + const sizeMap = { |
| 166 | + xs: 'w-3 h-3', |
| 167 | + sm: 'w-4 h-4', |
| 168 | + md: 'w-4 h-4', |
| 169 | + lg: 'w-5 h-5', |
| 170 | + xl: 'w-6 h-6' |
| 171 | + } |
| 172 | +
|
| 173 | + const marginMap = { |
| 174 | + left: hasText && !props.iconOnly ? 'mr-2' : '', |
| 175 | + right: hasText && !props.iconOnly ? 'ml-2' : '' |
| 176 | + } |
| 177 | +
|
| 178 | + return [ |
| 179 | + sizeMap[props.size], |
| 180 | + marginMap[props.iconPosition] |
| 181 | + ].filter(Boolean) |
| 182 | +}) |
| 183 | +
|
| 184 | +// 文本样式 |
| 185 | +const textClasses = computed(() => { |
| 186 | + if (props.iconOnly) { |
| 187 | + return 'sr-only' |
| 188 | + } |
| 189 | + return '' |
| 190 | +}) |
| 191 | +
|
| 192 | +// 最终样式组合 |
| 193 | +const buttonClasses = computed(() => [ |
| 194 | + ...baseClasses.value, |
| 195 | + sizeClasses.value, |
| 196 | + typeClasses.value, |
| 197 | + ...(Array.isArray(props.customClass) ? props.customClass : [props.customClass]).filter(Boolean) |
| 198 | +]) |
| 199 | +
|
| 200 | +// 点击处理 |
| 201 | +const handleClick = (event: MouseEvent) => { |
| 202 | + if (!props.disabled && !props.loading) { |
| 203 | + emit('click', event) |
| 204 | + } |
| 205 | +} |
| 206 | +</script> |
0 commit comments