useActiveElement
响应式地获取 document.activeElement
Demo
用法
js
import { useActiveElement } from '@vueuse/core'
const activeElement = useActiveElement()
watch(activeElement, (el) => {
console.log('当前活动元素是', el)
})
组件用法
vue
<template>
<UseActiveElement v-slot="{ element }">
当前活动元素是 {{ element.dataset.id }}
</UseActiveElement>
</template>
类型声明
typescript
export interface UseActiveElementOptions
extends ConfigurableWindow,
ConfigurableDocumentOrShadowRoot {
/**
* 在 shadow dom 中深度搜索活动元素
*
* @default true
*/
deep?: boolean
/**
* Track active element when it's removed from the DOM
* Using a MutationObserver under the hood
* @default false
*/
triggerOnRemoval?: boolean
}
/**
* Reactive `document.activeElement`
*
* @see https://vueuse.org/useActiveElement
* @param options
*/
export declare function useActiveElement<T extends HTMLElement>(
options?: UseActiveElementOptions,
): Ref<T | null | undefined, T | null | undefined>
Source
贡献者
Anthony Fu
丶远方
Dominik Pschenitschni
Anthony Fu
Alex
Jake Tigchelaar
duoduoObama
vaakian X
James Garbutt
vaakian X
wheat
Alex Kozack