Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

BaseButton.vue

33 lines759 Bytes

1<script setup lang="ts">
2import { computed, inject, nextTick, ref, watch } from 'vue'
3import { isPendingKey } from '../transition'
4import BaseSpinner from './BaseSpinner.vue'
5​
6const emit = defineEmits<{ click: [] }>()
7​
8const isPending = inject(isPendingKey, ref(false))
9const isStarted = ref(false)
10const isLoading = computed(() => isStarted.value && isPending.value)
11​
12watch(isPending, (pending) => {
13 if (!pending) {
14 isStarted.value = false
15 }
16})
17​
18const handleClick = async () => {
19 isStarted.value = true
20 emit('click')
21 await nextTick()
22 if (!isPending.value) {
23 isStarted.value = false
24 }
26</script>
27​
28<template>
29 <button :disabled="isLoading" @click="handleClick">
30 <slot />
31 <BaseSpinner v-if="isLoading" />
32 </button>
33</template>