Appearance
useClamp
Reactively clamp a value between two other values.
Demo
Usage
tsx
import { useClamp } from '@reause/math'
import { useState } from 'react'
const [min, setMin] = useState(0)
const [max, setMax] = useState(10)
const [value, setValue] = useClamp(0, min, max)
setValue(15) // value is 10
setValue(-5) // value is 0Writable Ref
value seeds the hook's internal state; the returned setter clamps on write:
tsx
import { useClamp } from '@reause/math'
const [clamped, setClamped] = useClamp(0, 0, 10)
setClamped(15) // clamped is 10
setClamped(-5) // clamped is 0Reactive Bounds
value, min and max are plain read-only numbers (upstream takes MaybeRefOrGetter<number>). Bounds are re-resolved on every render, so shrinking max re-clamps the current value:
tsx
import { useClamp } from '@reause/math'
import { useState } from 'react'
const [max, setMax] = useState(10)
const [clamped] = useClamp(5, 0, max)
setMax(3) // clamped is 3 on the next renderType Declarations
ts
export function useClamp(value: number, min: number, max: number): [
number,
(value: number) => void
]