Skip to content

useActiveElement

类别
导出体积
712 B
上次更改
2 minutes ago

响应式地获取 document.activeElement

示例

选择下面的 input 查看当前激活的元素
当前激活的元素: null

用法

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>

源码

源码演示文档

贡献者

Anthony Fu
一纸忘忧
Dominik Pschenitschni
Anthony Fu
Alex
Jake Tigchelaar
duoduoObama
vaakian X
James Garbutt
vaakian X
wheat
Alex Kozack

更新日志

v10.10.0 on 5/27/2024
f3515 - feat: add triggerOnRemoval option (#3958)
v10.9.0 on 2/27/2024
b1426 - fix(useActiveElement/useFocusWithin): replace computedWithControl with locally tracked ref (#3815)
v10.3.0 on 7/30/2023
296dc - feat: search deeply in shadow dom (#3208)
v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)
v9.10.0 on 1/3/2023
07f20 - feat: support shadow roots (#2592)
ae78d - fix: ignore if relatedTarget is set (#2600)
v9.7.0 on 12/16/2022
88b44 - fix: ignore blur for relatedTarget (#2540)