Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useClamp.md
1---2category: '@Math'3---45# useClamp67Reactively clamp a value between two other values.89## Usage1011```ts12import { useClamp } from '@vueuse/math'1314const min = shallowRef(0)15const max = shallowRef(10)16const value = useClamp(0, min, max)17```1819### Writable Ref2021When you pass a mutable `ref`, the returned value is a **writable computed** that clamps values when setting:2223```ts24import { useClamp } from '@vueuse/math'2526const number = shallowRef(0)27const clamped = useClamp(number, 0, 10)2829clamped.value = 15 // clamped.value will be 1030clamped.value = -5 // clamped.value will be 031```3233### Read-only Mode3435When you pass a getter function or readonly ref, the returned value is a read-only computed:3637```ts38import { useClamp } from '@vueuse/math'3940const value = ref(5)41const clamped = useClamp(() => value.value * 2, 0, 10)4243// clamped.value is computed from the getter44```4546### Reactive Bounds4748All arguments (value, min, max) can be reactive:4950```ts51import { useClamp } from '@vueuse/math'5253const value = shallowRef(5)54const min = shallowRef(0)55const max = shallowRef(10)5657const clamped = useClamp(value, min, max)5859max.value = 3 // clamped.value automatically becomes 360```6162## Type Declarations6364```ts65/**66* Reactively clamp a value between two other values.67*68* @see https://vueuse.org/useClamp69* @param value number70* @param min71* @param max72*73* @__NO_SIDE_EFFECTS__74*/75export declare function useClamp(76value: ReadonlyRefOrGetter<number>,77min: MaybeRefOrGetter<number>,78max: MaybeRefOrGetter<number>,79): ComputedRef<number>80export declare function useClamp(81value: MaybeRefOrGetter<number>,82min: MaybeRefOrGetter<number>,83max: MaybeRefOrGetter<number>,84): Ref<number>85```86