Skip to content

Commit bce7046

Browse files
authored
fix(VBtn, VChip): correct link active state after navigation cancellation (#21651)
fixes #21594
1 parent 11c256a commit bce7046

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

packages/vuetify/src/components/VBtn/VBtn.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export const VBtn = genericComponent<VBtnSlots>()({
153153
: props.value
154154
})
155155

156-
function onClick (e: MouseEvent) {
156+
async function onClick (e: MouseEvent) {
157157
if (
158158
isDisabled.value ||
159159
(link.isLink.value && (
@@ -165,8 +165,9 @@ export const VBtn = genericComponent<VBtnSlots>()({
165165
))
166166
) return
167167

168-
link.navigate?.(e)
169-
group?.toggle()
168+
if (await (link.navigateWithCheck?.(e) ?? true)) {
169+
group?.toggle()
170+
}
170171
}
171172

172173
useSelectLink(link, group?.select)

packages/vuetify/src/components/VChip/VChip.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,14 @@ export const VChip = genericComponent<VChipSlots>()({
162162
})
163163
})
164164

165-
function onClick (e: MouseEvent) {
165+
async function onClick (e: MouseEvent) {
166166
emit('click', e)
167167

168168
if (!isClickable.value) return
169169

170-
link.navigate?.(e)
171-
group?.toggle()
170+
if (await (link.navigateWithCheck?.(e) ?? true)) {
171+
group?.toggle()
172+
}
172173
}
173174

174175
function onKeyDown (e: KeyboardEvent) {

packages/vuetify/src/composables/router.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
resolveDynamicComponent,
77
toRef,
88
} from 'vue'
9+
import { isNavigationFailure } from 'vue-router'
910
import { deepEqual, getCurrentInstance, hasEvent, IN_BROWSER, propsFactory } from '@/util'
1011

1112
// Types
@@ -47,6 +48,7 @@ export interface UseLink extends Omit<Partial<ReturnType<typeof _useLink>>, 'hre
4748
isClickable: Readonly<Ref<boolean>>
4849
href: Ref<string | undefined>
4950
linkProps: Record<string, string | undefined>
51+
navigateWithCheck?: (e: MouseEvent | undefined) => Promise<boolean>
5052
}
5153

5254
export function useLink (props: LinkProps & LinkListeners, attrs: SetupContext['attrs']): UseLink {
@@ -84,12 +86,18 @@ export function useLink (props: LinkProps & LinkListeners, attrs: SetupContext['
8486
})
8587
const href = computed(() => props.to ? link.value?.route.value.href : props.href)
8688

89+
async function navigateWithCheck (e: MouseEvent | undefined) {
90+
const result = await link.value?.navigate(e)
91+
return !isNavigationFailure(result)
92+
}
93+
8794
return {
8895
isLink,
8996
isClickable,
9097
isActive,
9198
route: link.value?.route,
9299
navigate: link.value?.navigate,
100+
navigateWithCheck,
93101
href,
94102
linkProps: reactive({
95103
href,

0 commit comments

Comments
 (0)