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/useFocusTrap.md
1---2category: '@Integrations'3---45# useFocusTrap67Reactive wrapper for [`focus-trap`](https://github.com/focus-trap/focus-trap).89For more information on what options can be passed, see [`createOptions`](https://github.com/focus-trap/focus-trap#createoptions) in the `focus-trap` documentation.1011## Install1213```bash14npm i focus-trap@^715```1617## Usage1819**Basic Usage**2021```vue22<script setup lang="ts">23import { useFocusTrap } from '@vueuse/integrations/useFocusTrap'24import { useTemplateRef } from 'vue'2526const target = useTemplateRef('target')27const { hasFocus, activate, deactivate } = useFocusTrap(target)28</script>2930<template>31<div>32<button @click="activate()">33Activate34</button>35<div ref="target">36<span>Has Focus: {{ hasFocus }}</span>37<input type="text">38<button @click="deactivate()">39Deactivate40</button>41</div>42</div>43</template>44```4546**Multiple Refs**4748```vue49<script setup lang="ts">50import { useFocusTrap } from '@vueuse/integrations/useFocusTrap'51import { useTemplateRef } from 'vue'5253const targetOne = useTemplateRef('targetOne')54const targetTwo = useTemplateRef('targetTwo')55const { hasFocus, activate, deactivate } = useFocusTrap([targetOne, targetTwo])56</script>5758<template>59<div>60<button @click="activate()">61Activate62</button>63<div ref="targetOne">64<span>Has Focus: {{ hasFocus }}</span>65<input type="text">66</div>67...68<div ref="targetTow">69<p>Another target here</p>70<input type="text">71<button @click="deactivate()">72Deactivate73</button>74</div>75</div>76</template>77```7879**Dynamic Focus Target**8081```vue82<script setup lang="ts">83import { useFocusTrap } from '@vueuse/integrations/useFocusTrap'84import { computed, shallowRef, useTemplateRef } from 'vue'8586const left = useTemplateRef('left')87const right = useTemplateRef('right')88const currentRef = shallowRef<'left' | 'right'>('left')8990const target = computed(() =>91currentRef.value === 'left'92? left93: currentRef.value === 'right'94? right95: null,96)9798const { activate } = useFocusTrap(target)99</script>100101<template>102<div>103<div ref="left" class="left">104...105</div>106<div ref="right" class="right">107...108</div>109</div>110</template>111```112113**Automatically Focus**114115```vue116<script setup lang="ts">117import { useFocusTrap } from '@vueuse/integrations/useFocusTrap'118import { useTemplateRef } from 'vue'119120const target = useTemplateRef('target')121const { hasFocus, activate, deactivate } = useFocusTrap(target, { immediate: true })122</script>123124<template>125<div>126<div ref="target">127...128</div>129</div>130</template>131```132133**Conditional Rendering**134135This function can't properly activate focus on elements with conditional rendering using `v-if`. This is because they do not exist in the DOM at the time of the focus activation. To solve this you need to activate on the next tick.136137```vue138<script setup lang="ts">139import { useFocusTrap } from '@vueuse/integrations/useFocusTrap'140import { nextTick, useTemplateRef } from 'vue'141142const target = useTemplateRef('target')143const { activate, deactivate } = useFocusTrap(target, { immediate: true })144145const show = ref(false)146147async function reveal() {148show.value = true149150await nextTick()151activate()152}153</script>154155<template>156<div>157<div v-if="show" ref="target">158...159</div>160161<button @click="reveal">162Reveal and Focus163</button>164</div>165</template>166```167168## Using Component169170With the `UseFocusTrap` component, Focus Trap will be activated automatically on mounting this component and deactivated on unmount.171172```vue173<script setup lang="ts">174import { UseFocusTrap } from '@vueuse/integrations/useFocusTrap/component'175import { shallowRef } from 'vue'176177const show = shallowRef(false)178</script>179180<template>181<UseFocusTrap v-if="show" :options="{ immediate: true }">182<div class="modal">183...184</div>185</UseFocusTrap>186</template>187```188189## Type Declarations190191```ts192export interface UseFocusTrapOptions extends Options {193/**194* Immediately activate the trap195*/196immediate?: boolean197}198export interface UseFocusTrapReturn {199/**200* Indicates if the focus trap is currently active201*/202hasFocus: ShallowRef<boolean>203/**204* Indicates if the focus trap is currently paused205*/206isPaused: ShallowRef<boolean>207/**208* Activate the focus trap209*210* @see https://github.com/focus-trap/focus-trap#trapactivateactivateoptions211* @param opts Activate focus trap options212*/213activate: (opts?: ActivateOptions) => void214/**215* Deactivate the focus trap216*217* @see https://github.com/focus-trap/focus-trap#trapdeactivatedeactivateoptions218* @param opts Deactivate focus trap options219*/220deactivate: (opts?: DeactivateOptions) => void221/**222* Pause the focus trap223*224* @see https://github.com/focus-trap/focus-trap#trappause225*/226pause: Fn227/**228* Unpauses the focus trap229*230* @see https://github.com/focus-trap/focus-trap#trapunpause231*/232unpause: Fn233}234/**235* Reactive focus-trap236*237* @see https://vueuse.org/useFocusTrap238*/239export declare function useFocusTrap(240target: MaybeRefOrGetter<241Arrayable<MaybeRefOrGetter<string> | MaybeComputedElementRef>242>,243options?: UseFocusTrapOptions,244): UseFocusTrapReturn245```246