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/useFocusWithin.md
1---2category: Sensors3---45# useFocusWithin67Reactive utility to track if an element or one of its decendants has focus. It is meant to match the behavior of the `:focus-within` CSS pseudo-class. A common use case would be on a form element to see if any of its inputs currently have focus.89## Basic Usage1011```vue12<script setup lang="ts">13import { useFocusWithin } from '@vueuse/core'14import { ref, watch } from 'vue'1516const target = ref()17const { focused } = useFocusWithin(target)1819watch(focused, (focused) => {20if (focused)21console.log('Target contains the focused element')22else23console.log('Target does NOT contain the focused element')24})25</script>2627<template>28<form ref="target">29<input type="text" placeholder="First Name">30<input type="text" placeholder="Last Name">31<input type="text" placeholder="Email">32<input type="text" placeholder="Password">33</form>34</template>35```3637## Type Declarations3839```ts40export interface UseFocusWithinReturn {41/**42* True if the element or any of its descendants are focused43*/44focused: ComputedRef<boolean>45}46/**47* Track if focus is contained within the target element48*49* @see https://vueuse.org/useFocusWithin50* @param target The target element to track51* @param options Focus within options52*/53export declare function useFocusWithin(54target: MaybeElementRef,55options?: ConfigurableWindow,56): UseFocusWithinReturn57```58