Skip to content
On this page

useActiveElement

Category
Export Size
1.08 kB
Last Changed
4 days ago

Reactive document.activeElement

Demo

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

Usage

js
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 Usage

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.

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

Type Declarations

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

Source

SourceDemoDocs

Contributors

Anthony Fu
Dominik Pschenitschni
vaakian X
wheat
Alex Kozack

Changelog

v9.7.0 on 12/16/2022
88b44 - fix: ignore blur for relatedTarget (#2540)
v9.1.1 on 8/23/2022
c534e - fix: use computedWithControl instead of counter ref (#2093)

Released under the MIT License.