Skip to content

useClamp

Category
Export Size
223 B
Package
@vueuse/math
Last Changed
3 months ago

Reactively clamp a value between two other values.

Demo

min: max: value:0

Usage

ts
import { 
useClamp
} from '@vueuse/math'
const
min
=
shallowRef
(0)
const
max
=
shallowRef
(10)
const
value
=
useClamp
(0,
min
,
max
)

You can also pass a ref and the returned computed will be updated when the source ref changes:

ts
import { 
useClamp
} from '@vueuse/math'
const
number
=
shallowRef
(0)
const
clamped
=
useClamp
(
number
, 0, 10)

Type Declarations

ts
/**
 * Reactively clamp a value between two other values.
 *
 * @see https://vueuse.org/useClamp
 * @param value number
 * @param min
 * @param max
 *
 * @__NO_SIDE_EFFECTS__
 */
export declare function 
useClamp
(
value
:
ReadonlyRefOrGetter
<number>,
min
:
MaybeRefOrGetter
<number>,
max
:
MaybeRefOrGetter
<number>,
):
ComputedRef
<number>
export declare function
useClamp
(
value
:
MaybeRefOrGetter
<number>,
min
:
MaybeRefOrGetter
<number>,
max
:
MaybeRefOrGetter
<number>,
):
Ref
<number>

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
IlyaL
SerKo
ClemDee

Changelog

v13.6.0 on
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)

Released under the MIT License.

Build smarter with AI
Join a free workshop to explore LLMs and advanced AI tools.
Register now
October 22