Button.vue
162 lines
| 1 | <script lang="ts" setup> |
| 2 | import { ref } from "vue"; |
| 3 | |
| 4 | import Icon from "@/components/Icon/Icon.vue"; |
| 5 | import CircleLoader from "@/components/Loaders/CircleLoader.vue"; |
| 6 | import { useButton } from "@/composables/useButton"; |
| 7 | import type { IButtonProps } from "@/types"; |
| 8 | |
| 9 | type Emit = { |
| 10 | click: [event: Event]; |
| 11 | }; |
| 12 | |
| 13 | const props = withDefaults(defineProps<IButtonProps>(), { |
| 14 | size: "medium", |
| 15 | variant: "contain", |
| 16 | color: "primary", |
| 17 | isDisabled: null, |
| 18 | isLoading: false |
| 19 | }); |
| 20 | const emit = defineEmits<Emit>(); |
| 21 | const buttonTextRef = ref<HTMLElement>(); |
| 22 | const { style, tag } = useButton(props); |
| 23 | |
| 24 | const handleClick = (event: Event) => { |
| 25 | if (props.isDisabled) { |
| 26 | event.preventDefault(); |
| 27 | event.stopPropagation(); |
| 28 | |
| 29 | return; |
| 30 | } |
| 31 | emit("click", event); |
| 32 | }; |
| 33 | </script> |
| 34 | |
| 35 | <template> |
| 36 | <Component |
| 37 | :is="tag" |
| 38 | :to="isDisabled ? undefined : to" |
| 39 | :target="isDisabled ? undefined : target" |
| 40 | :href="isDisabled ? undefined : to" |
| 41 | class="button-v2" |
| 42 | :class="{ |
| 43 | 'button-v2--disabled': isDisabled, |
| 44 | 'button-v2--hovered': isHovered, |
| 45 | 'button-v2--loading': isLoading, |
| 46 | }" |
| 47 | :disabled="isDisabled || null" |
| 48 | @click="handleClick" |
| 49 | > |
| 50 | <Icon |
| 51 | v-if="iconPrepend && !isLoading" |
| 52 | class="button-v2__icon" |
| 53 | :name="iconPrepend" |
| 54 | :color="isDisabled ? 'gray' : style.icon.color" |
| 55 | :dimensions="style.icon.size" |
| 56 | /> |
| 57 | |
| 58 | <div class="button-v2__loader"> |
| 59 | <CircleLoader |
| 60 | v-show="isLoading" |
| 61 | :dimensions="style.loader.size" |
| 62 | :border-color="style.loader.borderColor" |
| 63 | :border-size="style.loader.border" |
| 64 | :color="props.color" |
| 65 | /> |
| 66 | </div> |
| 67 | |
| 68 | <span |
| 69 | v-if="$slots.default" |
| 70 | ref="buttonTextRef" |
| 71 | class="button-v2__text" |
| 72 | > |
| 73 | <slot /> |
| 74 | </span> |
| 75 | |
| 76 | <Icon |
| 77 | v-if="iconAppend && !isLoading" |
| 78 | class="button-v2__icon" |
| 79 | :name="iconAppend" |
| 80 | :color="isDisabled ? 'gray' : style.icon.color" |
| 81 | :dimensions="style.icon.size" |
| 82 | /> |
| 83 | </Component> |
| 84 | </template> |
| 85 | |
| 86 | <style lang="scss"> |
| 87 | .button-v2 { |
| 88 | $this: &; |
| 89 | padding: v-bind("style.padding"); |
| 90 | color: v-bind("style.color"); |
| 91 | background-color: v-bind("style.backgroundColor"); |
| 92 | border: v-bind("style.border"); |
| 93 | border-radius: 8px; |
| 94 | display: inline-flex; |
| 95 | align-items: center; |
| 96 | gap: 8px; |
| 97 | position: relative; |
| 98 | transition: background-color 0.1s ease-in-out; |
| 99 | text-decoration: none; |
| 100 | font-size: 12px; |
| 101 | line-height: 24px; |
| 102 | font-weight: 700; |
| 103 | width: fit-content; |
| 104 | flex-wrap: nowrap; |
| 105 | justify-content: center; |
| 106 | text-wrap: nowrap; |
| 107 | |
| 108 | &--disabled, |
| 109 | &[disabled]] { |
| 110 | color: v-bind("style.colorDisabled") !important; |
| 111 | background-color: v-bind("style.backgroundColorDisabled"); |
| 112 | font-size: 12px !important; |
| 113 | pointer-events: none; |
| 114 | cursor: not-allowed; |
| 115 | } |
| 116 | |
| 117 | &--loading { |
| 118 | pointer-events: none; |
| 119 | |
| 120 | #{$this}__text { |
| 121 | opacity: 0; |
| 122 | } |
| 123 | |
| 124 | #{$this}__loader { |
| 125 | opacity: 1; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | &__text { |
| 130 | opacity: 1; |
| 131 | transition: opacity 0.2s ease-in-out; |
| 132 | } |
| 133 | |
| 134 | &__loader { |
| 135 | display: flex; |
| 136 | align-items: center; |
| 137 | justify-content: center; |
| 138 | position: absolute; |
| 139 | left: 0; |
| 140 | top: 0; |
| 141 | width: 100%; |
| 142 | height: 100%; |
| 143 | pointer-events: none; |
| 144 | opacity: 0; |
| 145 | transition: opacity 0.2s ease-in-out; |
| 146 | } |
| 147 | |
| 148 | &--hovered:not(&--disabled):not([disabled]) { |
| 149 | background-color: v-bind("style.backgroundHoverColor"); |
| 150 | } |
| 151 | |
| 152 | &:hover:not(&--disabled):not([disabled]) { |
| 153 | background-color: v-bind("style.backgroundHoverColor"); |
| 154 | cursor: pointer; |
| 155 | } |
| 156 | |
| 157 | @media (max-width: 576px) { |
| 158 | width: 100%; |
| 159 | } |
| 160 | } |
| 161 | </style> |
| 162 |