useClamp
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)
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)
import { useClamp } from '@vueuse/math'
const number = ref(0)
const clamped = useClamp(number, 0, 10)
Type Declarations
typescript
export declare function useClamp(
value: MaybeReadonlyRef<number>,
min: MaybeComputedRef<number>,
max: MaybeComputedRef<number>
): ComputedRef<number>
export declare function useClamp(
value: MaybeRef<number>,
min: MaybeComputedRef<number>,
max: MaybeComputedRef<number>
): Ref<number>
export declare function useClamp(
value: MaybeReadonlyRef<number>,
min: MaybeComputedRef<number>,
max: MaybeComputedRef<number>
): ComputedRef<number>
export declare function useClamp(
value: MaybeRef<number>,
min: MaybeComputedRef<number>,
max: MaybeComputedRef<number>
): Ref<number>
Source
Contributors
ClemDee
Anthony Fu