Skip to content
On this page

useActiveElement

Category
Last Changed
3 months ago

Reactive document.activeElement

Demo

Select the inputs below to see the changes
Current Active Element: null

Usage

import { useActiveElement } from '@vueuse/core'

const activeElement = useActiveElement()

watch(activeElement, (el) => {
  console.log('focus changed to', el)
})
import { useActiveElement } from '@vueuse/core'

const activeElement = useActiveElement()

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

Component

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.
<UseActiveElement v-slot="{ element }">
  Active element is {{ element.dataset.id }}
</UseActiveElement>
<UseActiveElement v-slot="{ element }">
  Active element is {{ element.dataset.id }}
</UseActiveElement>

Type Declarations

/**
 * Reactive `document.activeElement`
 *
 * @see https://vueuse.org/useActiveElement
 * @param options
 */
export declare function useActiveElement<T extends HTMLElement>(
  options?: ConfigurableWindow
): ComputedRef<T | null | undefined>
/**
 * Reactive `document.activeElement`
 *
 * @see https://vueuse.org/useActiveElement
 * @param options
 */
export declare function useActiveElement<T extends HTMLElement>(
  options?: ConfigurableWindow
): ComputedRef<T | null | undefined>

Source

SourceDemoDocs

Contributors

Anthony Fu
wheat
Alex Kozack

Changelog

v5.0.0-beta.2 on 5/25/2021
5bede - feat: introduce components & directives (#486)
v4.9.0 on 4/21/2021
df7de - feat: new function
useActiveElement has loaded