Skip to content
On this page

useActiveElement

Category
Export Size
1.08 kB
Last Changed
2 months 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
export interface UseActiveElementOptions
  extends ConfigurableWindow,
    ConfigurableDocumentOrShadowRoot {}
/**
 * Reactive `document.activeElement`
 *
 * @see https://vueuse.org/useActiveElement
 * @param options
 */
export declare function useActiveElement<T extends HTMLElement>(
  options?: UseActiveElementOptions
): ComputedRefWithControl<T | null | undefined>
export interface UseActiveElementOptions
  extends ConfigurableWindow,
    ConfigurableDocumentOrShadowRoot {}
/**
 * Reactive `document.activeElement`
 *
 * @see https://vueuse.org/useActiveElement
 * @param options
 */
export declare function useActiveElement<T extends HTMLElement>(
  options?: UseActiveElementOptions
): ComputedRefWithControl<T | null | undefined>

Source

SourceDemoDocs

Contributors

Anthony Fu
Dominik Pschenitschni
vaakian X
James Garbutt
vaakian X
wheat
Alex Kozack

Changelog

v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)
v9.10.0 on 1/3/2023
07f20 - feat: support shadow roots (#2592)
ae78d - fix: ignore if relatedTarget is set (#2600)
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.