packages/core/useActiveElement/index.md
Reactive document.activeElement. Returns a shallow ref that updates when focus changes.
<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>
By default, useActiveElement will traverse into shadow DOM to find the deeply active element. Set deep: false to disable this behavior.
import { useActiveElement } from '@vueuse/core'
// Only get the shadow host, not the element inside shadow DOM
const activeElement = useActiveElement({ deep: false })
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.
import { useActiveElement } from '@vueuse/core'
const activeElement = useActiveElement({ triggerOnRemoval: true })
<template>
<UseActiveElement v-slot="{ element }">
Active element is {{ element?.dataset.id }}
</UseActiveElement>
</template>