From d8b8758cbb06b98f4fdc17fb5d7234773841bbf9 Mon Sep 17 00:00:00 2001 From: webfansplz <308241863@qq.com> Date: Sun, 8 May 2022 21:28:26 +0800 Subject: [PATCH 1/2] fix(useFocus): `focused` should be `false` when `element` is undefined --- packages/core/useFocus/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/core/useFocus/index.ts b/packages/core/useFocus/index.ts index 98aee00b5dc..52ae8f5bba6 100644 --- a/packages/core/useFocus/index.ts +++ b/packages/core/useFocus/index.ts @@ -36,6 +36,8 @@ export function useFocus(target: MaybeElementRef, options: UseFocusOptions = {}) const targetElement = computed(() => unrefElement(target)) const focused = computed({ get() { + if (activeElement.value === undefined && targetElement.value === undefined) + return false return activeElement.value === targetElement.value }, set(value: boolean) { From 6aea25a62279ba5a4559e71499b73ffbc47e2b29 Mon Sep 17 00:00:00 2001 From: webfansplz <308241863@qq.com> Date: Mon, 9 May 2022 09:32:20 +0800 Subject: [PATCH 2/2] chore: update --- packages/core/useFocus/index.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/core/useFocus/index.ts b/packages/core/useFocus/index.ts index 52ae8f5bba6..e6022d47712 100644 --- a/packages/core/useFocus/index.ts +++ b/packages/core/useFocus/index.ts @@ -1,5 +1,6 @@ import type { Ref } from 'vue-demi' import { computed, watch } from 'vue-demi' +import { isDef } from '@vueuse/shared' import type { MaybeElementRef } from '../unrefElement' import { unrefElement } from '../unrefElement' import { useActiveElement } from '../useActiveElement' @@ -36,9 +37,7 @@ export function useFocus(target: MaybeElementRef, options: UseFocusOptions = {}) const targetElement = computed(() => unrefElement(target)) const focused = computed({ get() { - if (activeElement.value === undefined && targetElement.value === undefined) - return false - return activeElement.value === targetElement.value + return isDef(activeElement.value) && isDef(targetElement.value) && activeElement.value === targetElement.value }, set(value: boolean) { if (!value && focused.value)