Skip to main content
Vuetify0 v1.0 is here
Vuetify0 Logo
Theme
Mode
Palettes
Accessibility
Vuetify One
Sign in to Vuetify One

Access premium tools across the Vuetify ecosystem — Bin, Play, Studio, and more.

Not a subscriber? See what's included

useToggleScope

A composable for conditionally managing Vue effect scopes based on reactive boolean conditions with automatic cleanup.

Edit this page
Report a Bug
Open issues
View on GitHub
Copy Markdown

PreviewIntermediateJun 29, 2026

Usage

The useToggleScope composable wraps Vue’s effectScope API to create and destroy reactive effect scopes based on a boolean condition. When the condition becomes true, a new scope is created and your callback runs. When false, the scope is stopped and all effects are cleaned up automatically.

vue
<script setup lang="ts">
  import { useToggleScope } from '@vuetify/v0'
  import { shallowRef, watch } from 'vue'

  const isEnabled = shallowRef(false)
  const data = shallowRef(0)

  const { isActive } = useToggleScope(isEnabled, () => {
    // This watch is only active when isEnabled is true
    watch(data, (value) => {
      console.log('Data changed:', value)
    })
  })
</script>

<template>
  <div>
    <button @click="isEnabled = !isEnabled">
      {{ isEnabled ? 'Disable' : 'Enable' }} Watcher
    </button>
    <p>Scope active: {{ isActive }}</p>
    <input v-model.number="data" type="number">
  </div>
</template>

Architecture

useToggleScope wraps Vue’s effectScope for conditional reactive effect management:

Toggle Scope Hierarchy

Use controls to zoom and pan. Click outside or press Escape to close.

Toggle Scope Hierarchy

Reactivity

Property/MethodReactiveNotes
isActiveComputed from scope ref
sourceWatchSource, triggers scope on/off
start()Create and run the effect scope
stop()Stop and clean up all effects in the scope
reset()Stop then immediately restart the scope

Examples

Conditional Effects

A mouse-position tracker where the mousemove listener exists only while the toggle is active. When the scope starts, useEventListener(window, 'mousemove', ...) registers the listener inside the new effectScope; when the scope stops, Vue disposes the scope and useEventListener’s onScopeDispose cleanup fires automatically — no manual removeEventListener required. The move counter, X, and Y values freeze as soon as tracking is disabled.

The example demonstrates the core use case: wrapping useEventListener (or any composable that registers effects) inside useToggleScope so the effect lifecycle follows a reactive boolean rather than the component lifecycle. This is the recommended pattern for feature-flag-controlled behaviors, debug overlays, admin-only polling, and any functionality that should only run under a specific condition. The isActive ref returned by useToggleScope is a computed view of the internal scope state — useful for rendering status indicators. Note the feedback on toggle scope: do not use this to guard watchers that must run synchronously on value changes, as the scope creation introduces an async tick.

Scope stopped
0
X position
0
Y position
0
Move events

The mousemove listener only exists while the scope is active. Starting the scope creates the listener; stopping it removes it automatically via effectScope cleanup.

FAQ

Discord
Need help? Join our community for support and discussions ↗

API Reference

The following API details are for the useToggleScope composable.
Was this page helpful?

© 2016-1970 Vuetify, LLC
Services
Ctrl+/