Utilities · @vueuse/shared v15.0.0 · Reviewed
useToggle
Create a boolean ref with a small helper to toggle or explicitly set its value.
Edit the code and the preview updates as you type.
What it does
A panel, notice or compact UI state only needs a boolean, but several components repeat the same ref and toggle handler.
The playground is a small, runnable example. Change the code and try the interaction before copying it into an app.
The code
const [open, toggle] = useToggle(false)
toggle() // switch value
toggle(false) // set it explicitlyWhere you'd use it
- Opening and closing a disclosure panel
- A temporary success notice
- Switching a simple preview mode
In Nuxt (and other gotchas)
This is ordinary reactive state and works during SSR. If the initial value depends on browser storage or client-only preferences, defer that value or use a storage composable with an SSR-aware strategy.
When not to use it
A plain ref is clearer for a boolean used once. Do not use a boolean toggle for a multi-step state machine with more than two meaningful states.
Links
- Official useToggle docs for every option and type
- Source code on GitHub