Components
- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Autocomplete
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Checkbox Group
- Collapsible
- Combobox
- Command
- Context Menu
- Date Picker
- Dialog
- Drawer
- Dropdown Menu
- Empty
- Field
- Fieldset
- Form
- Frame
- Group
- Input
- Input Group
- Kbd
- Label
- Menu
- Menubar
- Message
- Meter
- Number Field
- OTP Field
- Pagination
- Popover
- Preview Card
- Progress
- Questionnaire
- Radio Group
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toggle
- Toggle Group
- Toolbar
- Tooltip
Shared
- Artifact
- Audio Player
- Canvas
- Chain of Thought
- Checkpoint
- Code Block
- Commit
- Connection
- Context
- Controls
- Edge
- Environment Variables
- Mic Selector
- Model Selector
- Node
- Node Toolbar
- Open in Chat
- Panel
- Plan
- Prompt Input
- Queue
- Reasoning
- Sandbox
- Schema Display
- Shimmer
- Snippet
- Sources
- Speech Input
- Stack Trace
- Suggestion
- Tool
- Transcription
- Web Preview
Hooks
Resources
useThreshold
Latched threshold crossings with hysteresis.
A React hook that watches a numeric value (scroll, drag, progress) and latches which named thresholds it has crossed. Supports hysteresis dead zones and per-threshold onCross callbacks.
Installation
pnpm dlx shadcn@latest add @frontal-labs/use-threshold
Usage
import { useThreshold } from "@/hooks/use-threshold";
function ScrollHeader({ scrollY }: { scrollY: number }) {
const { crossed } = useThreshold(scrollY, [
{ id: "compact", at: 80, hysteresis: 8 },
{ id: "solid", at: 200, hysteresis: 16 },
]);
return <header data-compact={crossed.compact} data-solid={crossed.solid} />;
}Callbacks on crossing
const { lastCrossedId } = useThreshold(progress, [
{
id: "complete",
at: 100,
onCross: (direction) => direction === "up" && celebrate(),
},
]);Thresholds added later are seeded from the current value, so they never fire on the commit they appear — only on subsequent moves that clear their edge.
API
function useThreshold(
value: number,
thresholds: readonly {
id: string;
at: number;
hysteresis?: number;
onCross?: (direction: "up" | "down") => void;
}[],
): { crossed: Record<string, boolean>; lastCrossedId: string | null };Parameters
| Property | Type | Description |
|---|---|---|
value | number | Watched numeric value. |
thresholds[].id | string | Stable key used in crossed. |
thresholds[].at | number | Crossing point. crossed is true at or above it. |
thresholds[].hysteresis | number | Dead zone (clamped ≥ 0). Cross up at at + h, back down at at − h. Prevents flicker. |
thresholds[].onCross | (direction) => void | Fired on each real crossing with "up" or "down". |
Return value
| Property | Type | Description |
|---|---|---|
crossed | Record<string, boolean> | Latched state per threshold id. |
lastCrossedId | string | null | Id of the most recently crossed threshold. |