Skip to content

useActiveElement

Category
Export Size
1.02 kB
Last Changed
3 hours ago

响应式地获取 document.activeElement

Demo

选择下面的 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>

Type Declarations

typescript
export interface UseActiveElementOptions
  extends ConfigurableWindow,
    ConfigurableDocumentOrShadowRoot {
  /**
   * Search active element deeply inside 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,
): ShallowRef<T | null | undefined, T | null | undefined>

Source

SourceDemoDocs

Contributors

jorshen

Changelog

No recent changes

Released under the MIT License.