跳到主要內容

useActiveElement

類別
匯出大小
1.02 kB
上次變更
3 週前

響應式 document.activeElement

示範

選取下方的輸入框以查看變更
目前作用中的元素: null

用法

js
import { useActiveElement } from '@vueuse/core'

const activeElement = useActiveElement()

watch(activeElement, (el) => {
  console.log('focus changed to', el)
})

元件用法

此函式也透過 @vueuse/components 套件提供無渲染元件版本。深入了解用法

vue
<template>
  <UseActiveElement v-slot="{ element }">
    Active element is {{ element.dataset.id }}
  </UseActiveElement>
</template>

類型宣告

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.dev.org.tw/useActiveElement
 * @param options
 */
export declare function useActiveElement<T extends HTMLElement>(
  options?: UseActiveElementOptions,
): ShallowRef<T | null | undefined, T | null | undefined>

原始碼

原始碼示範文件

貢獻者

Anthony Fu
Anthony Fu
Dominik Pschenitschni
IlyaL
Fernando Fernández
Ben Lau
Alex
Jake Tigchelaar
duoduoObama
vaakian X
James Garbutt
vaakian X
wheat
Alex Kozack

變更日誌

v12.4.0 於 2025/1/10
dd316 - feat: 盡可能在所有地方使用被動事件處理器 (#4477)
v12.3.0 於 2025/1/2
08cf5 - feat(onElementRemoval): 新增函式,重構 useActiveElement useElementHover (#4410)
v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: 移除 Vue 2 支援,最佳化 bundle 並清理 (#4349)
v10.10.0 於 2024/5/27
f3515 - feat: 新增 triggerOnRemoval 選項 (#3958)
v10.9.0 於 2024/2/27
b1426 - fix(useActiveElement/useFocusWithin): 將 computedWithControl 替換為本地追蹤的 ref (#3815)
v10.3.0 於 2023/7/30
296dc - feat: 在 shadow dom 中深度搜尋 (#3208)

以 MIT 授權條款發布。