|
| 1 | +<script setup lang="ts"> |
| 2 | +import FAIcon from "@/components/FAIcon.vue"; |
| 3 | +import { IconDefinition, faRefresh } from "@fortawesome/free-solid-svg-icons"; |
| 4 | +
|
| 5 | +export type ButtonVariant = "primary" | "secondary" | "danger" | "link" | "default"; |
| 6 | +export type ButtonSize = "sm" | "lg" | "default"; |
| 7 | +
|
| 8 | +interface Props { |
| 9 | + variant?: ButtonVariant; |
| 10 | + size?: ButtonSize; |
| 11 | + icon?: IconDefinition; |
| 12 | + iconPosition?: "left" | "right"; |
| 13 | + disabled?: boolean; |
| 14 | + loading?: boolean; |
| 15 | + tooltip?: string; |
| 16 | + ariaLabel?: string; |
| 17 | + type?: "button" | "submit" | "reset"; |
| 18 | +} |
| 19 | +
|
| 20 | +const props = withDefaults(defineProps<Props>(), { |
| 21 | + variant: "default", |
| 22 | + size: "default", |
| 23 | + iconPosition: "left", |
| 24 | + disabled: false, |
| 25 | + loading: false, |
| 26 | + type: "button", |
| 27 | +}); |
| 28 | +
|
| 29 | +const variantClasses = { |
| 30 | + primary: "btn-primary", |
| 31 | + secondary: "btn-secondary", |
| 32 | + danger: "btn-danger", |
| 33 | + link: "btn-link", |
| 34 | + default: "btn-default", |
| 35 | +}; |
| 36 | +
|
| 37 | +const sizeClasses = { |
| 38 | + sm: "btn-sm", |
| 39 | + lg: "btn-lg", |
| 40 | + default: "", |
| 41 | +}; |
| 42 | +</script> |
| 43 | + |
| 44 | +<template> |
| 45 | + <button |
| 46 | + class="btn" |
| 47 | + :class="[variantClasses[props.variant], sizeClasses[props.size], { disabled: props.disabled || props.loading }]" |
| 48 | + :disabled="props.disabled || props.loading" |
| 49 | + :type="props.type" |
| 50 | + :aria-label="props.ariaLabel" |
| 51 | + v-tippy="props.tooltip" |
| 52 | + > |
| 53 | + <FAIcon v-if="props.icon && props.iconPosition === 'left' && !props.loading" :icon="props.icon" class="icon-left" /> |
| 54 | + <FAIcon v-if="props.loading" class="rotate" :icon="faRefresh" /> |
| 55 | + <span v-if="$slots.default" class="button-text"> |
| 56 | + <slot /> |
| 57 | + </span> |
| 58 | + <FAIcon v-if="props.icon && props.iconPosition === 'right' && !props.loading" :icon="props.icon" class="icon-right" /> |
| 59 | + </button> |
| 60 | +</template> |
| 61 | + |
| 62 | +<style scoped> |
| 63 | +.btn { |
| 64 | + display: inline-flex; |
| 65 | + align-items: center; |
| 66 | + gap: 0.375rem; |
| 67 | + cursor: pointer; |
| 68 | +} |
| 69 | +
|
| 70 | +.btn.disabled { |
| 71 | + cursor: not-allowed; |
| 72 | + opacity: 0.65; |
| 73 | +} |
| 74 | +
|
| 75 | +.icon-left, |
| 76 | +.icon-right { |
| 77 | + color: var(--reduced-emphasis); |
| 78 | +} |
| 79 | +
|
| 80 | +.icon-left { |
| 81 | + margin-right: 0.25rem; |
| 82 | +} |
| 83 | +
|
| 84 | +.icon-right { |
| 85 | + margin-left: 0.25rem; |
| 86 | +} |
| 87 | +
|
| 88 | +.rotate { |
| 89 | + animation: spin 1s linear infinite; |
| 90 | +} |
| 91 | +
|
| 92 | +@keyframes spin { |
| 93 | + from { |
| 94 | + transform: rotate(0deg); |
| 95 | + } |
| 96 | + to { |
| 97 | + transform: rotate(360deg); |
| 98 | + } |
| 99 | +} |
| 100 | +
|
| 101 | +.button-text { |
| 102 | + flex: 1; |
| 103 | +} |
| 104 | +</style> |
0 commit comments