Focus Scope provides enhanced control over keyboard focus management within component boundaries. It can trap focus within its container and optionally loop focus navigation, making it ideal for modal interfaces and other interactive components that need to manage focus states.
| Prop | Default | Type |
|---|---|---|
as | 'div' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
loop | false | booleanWhen |
trapped | false | booleanWhen |
| Event | Type |
|---|---|
mountAutoFocus | [event: Event]Event handler called when auto-focusing on mount. Can be prevented. |
unmountAutoFocus | [event: Event]Event handler called when auto-focusing on unmount. Can be prevented. |
Basic usage with focus trapping
<template>
<AFocusScope :trapped="true">
<div>
<button>Action 1</button>
<button>Action 2</button>
<button>Close</button>
</div>
</AFocusScope>
</template>
Enable both trapping and looping for complete focus management:
<template>
<AFocusScope
:trapped="true"
:loop="true"
>
<div>
<button
v-for="item in items"
:key="item.id"
>
{{ item.label }}
</button>
</div>
</AFocusScope>
</template>
<script setup>
function handleMountFocus(event) {
// Prevent default auto-focus behavior if needed
event.preventDefault();
}
</script>
<template>
<AFocusScope
@mount-auto-focus="handleMountFocus"
@unmount-auto-focus="handleUnmountFocus"
>
<div>
…
</div>
</AFocusScope>
</template>