Skip to content

useClamp ​

Category
Export Size
214 B
Package
@vueuse/math
Last Changed
6 minutes ago

Reactively clamp a value between two other values.

Demo ​

min: max: value:0

Usage ​

ts
import { useClamp } from '@vueuse/math'

const min = ref(0)
const max = ref(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 = ref(0)
const clamped = useClamp(number, 0, 10)

Type Declarations ​

typescript
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 ​

Source • Demo • Docs

Contributors ​

Anthony Fu
Anthony Fu
ClemDee

Changelog ​

v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.0.0-beta.5 on 4/13/2023
cb644 - refactor!: remove isFunction and isString utils
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue
v9.13.0 on 2/18/2023
08a07 - feat: composable computed usage improvements (#2696)

Released under the MIT License.