Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useActiveElement.md
1---2category: Elements3---45# useActiveElement67Reactive `document.activeElement`. Returns a shallow ref that updates when focus changes.89## Usage1011```vue12<script setup lang="ts">13import { useActiveElement } from '@vueuse/core'14import { watch } from 'vue'1516const activeElement = useActiveElement()1718watch(activeElement, (el) => {19console.log('focus changed to', el)20})21</script>22```2324### Shadow DOM Support2526By default, `useActiveElement` will traverse into shadow DOM to find the deeply active element. Set `deep: false` to disable this behavior.2728```ts29import { useActiveElement } from '@vueuse/core'3031// Only get the shadow host, not the element inside shadow DOM32const activeElement = useActiveElement({ deep: false })33```3435### Track Element Removal3637Set `triggerOnRemoval: true` to update the active element when the currently active element is removed from the DOM. This uses a `MutationObserver` under the hood.3839```ts40import { useActiveElement } from '@vueuse/core'4142const activeElement = useActiveElement({ triggerOnRemoval: true })43```4445## Component Usage4647```vue48<template>49<UseActiveElement v-slot="{ element }">50Active element is {{ element?.dataset.id }}51</UseActiveElement>52</template>53```5455## Type Declarations5657```ts58export interface UseActiveElementOptions59extends ConfigurableWindow, ConfigurableDocumentOrShadowRoot {60/**61* Search active element deeply inside shadow dom62*63* @default true64*/65deep?: boolean66/**67* Track active element when it's removed from the DOM68* Using a MutationObserver under the hood69* @default false70*/71triggerOnRemoval?: boolean72}73export type UseActiveElementReturn<T extends HTMLElement = HTMLElement> =74ShallowRef<T | null | undefined>75/**76* Reactive `document.activeElement`77*78* @see https://vueuse.org/useActiveElement79* @param options80*81* @__NO_SIDE_EFFECTS__82*/83export declare function useActiveElement<T extends HTMLElement>(84options?: UseActiveElementOptions,85): UseActiveElementReturn<T>86```87