Skip to content

useToggle ​

Category
Export Size
208 B
Last Changed
last year

A boolean switcher with utility functions.

Demo ​

Value: OFF

Usage ​

ts
import { 
useToggle
} from '@vueuse/core'
const [
value
,
toggle
] =
useToggle
()

When you pass a ref, useToggle will return a simple toggle function instead:

ts
import { 
useDark
,
useToggle
} from '@vueuse/core'
const
isDark
=
useDark
()
const
toggleDark
=
useToggle
(
isDark
)

Toggle Function ​

The toggle function can be called in two ways:

ts
const [
value
,
toggle
] = useToggle()
toggle
() // toggle between true and false
toggle
(true) // set to specific value
// The toggle function returns the new value const
newValue
=
toggle
() // returns the new value after toggling
js
'use strict'
const [value, toggle] = useToggle()
toggle() // toggle between true and false
toggle(true) // set to specific value
// The toggle function returns the new value
const newValue = toggle() // returns the new value after toggling

Custom Values ​

You can use custom truthy and falsy values instead of true and false:

ts
import { 
useToggle
} from '@vueuse/core'
const [
value
,
toggle
] =
useToggle
('on', {
truthyValue
: 'on',
falsyValue
: 'off',
})
toggle
() // 'off'
toggle
() // 'on'

The custom values can also be reactive:

ts
import { 
useToggle
} from '@vueuse/core'
import {
ref
} from 'vue'
const
truthy
=
ref
('yes')
const
falsy
=
ref
('no')
const [
value
,
toggle
] =
useToggle
('yes', {
truthyValue
:
truthy
,
falsyValue
:
falsy
,
})

Caution ​

Be aware that the toggle function accepts the first argument as the override value. You might want to avoid directly passing the function to events in the template, as the event object will be passed in.

html
<!-- caution: $event will be passed in -->
<button @click="toggleDark" />
<!-- recommended to do this -->
<button @click="toggleDark()" />

Type Declarations ​

ts
export type 
ToggleFn
= (
value
?: boolean) => void
export type
UseToggleReturn
= [
ShallowRef
<boolean>,
ToggleFn
] |
ToggleFn
export interface
UseToggleOptions
<
Truthy
,
Falsy
> {
truthyValue
?:
MaybeRefOrGetter
<
Truthy
>
falsyValue
?:
MaybeRefOrGetter
<
Falsy
>
} export declare function
useToggle
<
Truthy
,
Falsy
,
T
=
Truthy
|
Falsy
>(
initialValue
:
Ref
<
T
>,
options
?:
UseToggleOptions
<
Truthy
,
Falsy
>,
): (
value
?:
T
) =>
T
export declare function
useToggle
<
Truthy
= true,
Falsy
= false,
T
=
Truthy
|
Falsy
,
>(
initialValue
?:
T
,
options
?:
UseToggleOptions
<
Truthy
,
Falsy
>,
): [
ShallowRef
<
T
>, (
value
?:
T
) =>
T
]

Source ​

Source • Demo • Docs

Contributors ​

Anthony Fu
Anthony Fu
SerKo
IlyaL
IlyaL
Robin
elky
Jelf
webfansplz
Alex Kozack
Alexey Iskhakov

Changelog ​

8c521 - feat(components)!: refactor components and make them consistent (#4912)
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
c1d6e - feat(shared): ensure return types exists (#4659)
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)

Released under the MIT License.