コンテンツへスキップ

useActiveElement

カテゴリ
エクスポートサイズ
712 B
最終更新日
先週

リアクティブなdocument.activeElement

デモ

以下の入力項目を選択して変更を確認してください。
現在のアクティブ要素: null

使用方法

JavaScript
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.dokyumento.jp/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

変更ログ

v12.0.0-beta.1 2024/11/21
0a9ed - feat!: Vue 2サポートの削除、バンドルの最適化、クリーンアップ (#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: シャドウDOM内を深く検索 (#3208)
v9.11.0 2023年1月17日
d5321 - fix(components): defineComponentをpureとしてマーク (#2623)
v9.10.0 2023年1月3日
07f20 - feat: シャドウルートのサポート (#2592)
ae78d - fix: relatedTargetが設定されている場合は無視する (#2600)
v9.7.0 2022年12月16日
88b44 - fix: relatedTargetに対するblurを無視する (#2540)

MITライセンスの下で公開されています。