Skip to content

useActiveElement

Category
Export Size
1.18 kB
Last Changed
last week

Reactive document.activeElement. Returns a shallow ref that updates when focus changes.

Demo

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

Usage

vue
<script setup lang="ts">
import { 
useActiveElement
} from '@vueuse/core'
import {
watch
} from 'vue'
const
activeElement
=
useActiveElement
()
watch
(
activeElement
, (
el
) => {
console
.
log
('focus changed to',
el
)
}) </script>

Shadow DOM Support

By default, useActiveElement will traverse into shadow DOM to find the deeply active element. Set deep: false to disable this behavior.

ts
import { 
useActiveElement
} from '@vueuse/core'
// Only get the shadow host, not the element inside shadow DOM const
activeElement
=
useActiveElement
({
deep
: false })

Track Element Removal

Set triggerOnRemoval: true to update the active element when the currently active element is removed from the DOM. This uses a MutationObserver under the hood.

ts
import { 
useActiveElement
} from '@vueuse/core'
const
activeElement
=
useActiveElement
({
triggerOnRemoval
: true })

Component Usage

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

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

Type Declarations

ts
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
} export type
UseActiveElementReturn
<
T
extends HTMLElement = HTMLElement> =
ShallowRef
<
T
| null | undefined>
/** * Reactive `document.activeElement` * * @see https://vueuse.org/useActiveElement * @param options * * @__NO_SIDE_EFFECTS__ */ export declare function
useActiveElement
<
T
extends HTMLElement>(
options
?: UseActiveElementOptions,
):
UseActiveElementReturn
<
T
>

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
Dominik Pschenitschni
Vida Xie
IlyaL
SerKo
青椒肉丝
IlyaL
Fernando Fernández
Ben Lau
Alex
Jake Tigchelaar
duoduoObama
vaakian X
James Garbutt
vaakian X
wheat
Alex Kozack

Changelog

v14.0.0-alpha.0 on
8c521 - feat(components)!: refactor components and make them consistent (#4912)
v13.6.0 on
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
v12.4.0 on
dd316 - feat: use passive event handlers everywhere is possible (#4477)
v12.3.0 on
08cf5 - feat(onElementRemoval): new function, refactor useActiveElement useElementHover (#4410)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.10.0 on
f3515 - feat: add triggerOnRemoval option (#3958)
v10.9.0 on
b1426 - fix(useActiveElement/useFocusWithin): replace computedWithControl with locally tracked ref (#3815)
v10.3.0 on
296dc - feat: search deeply in shadow dom (#3208)

Released under the MIT License.

2 for 1 SALE
Free JavaScript Certification with any Certification purchased
Get Certified
05
hours
:
17
minutes
:
25
seconds
: