Open source CapCut alternative.
TypeScript
fix(site): slider detents snap pointer drags only, through shared snapNear
David Le729e79f96705
diff --git a/site/src/components/ui/slider.tsx b/site/src/components/ui/slider.tsxindex b5c15997..827fecbb 100644--- a/site/src/components/ui/slider.tsx+++ b/site/src/components/ui/slider.tsx@@ -1,9 +1,7 @@import { Slider as SliderPrimitive } from "@base-ui/react/slider"import { cn } from "@/lib/utils"--/** Fraction of the range around a snap point that lands exactly on it. */-const SNAP_REACH = 0.025+import { snapNear } from "@/lib/snap"function Slider({className,@@ -18,17 +16,16 @@ function Slider({/*** Detent values: a pointer drag that comes within reach lands exactly on* one, which is how a nudged setting finds its way back to 100% or 0°.- * Keyboard steps stay exact, so arrow keys can walk out of a detent.+ * Keyboard steps, rail clicks, and typed values stay exact, so every+ * deliberate placement can walk out of a detent.*/snap?: number[]}) {const handleValueChange: typeof onValueChange =snap && onValueChange? (v, details) => {- if (typeof v === "number" && details.reason !== "keyboard") {- const reach = (max - min) * SNAP_REACH- const n = v- v = snap.find((s) => Math.abs(n - s) <= reach) ?? n+ if (typeof v === "number" && details.reason === "drag") {+ v = snapNear(v, snap, min, max)}onValueChange(v, details)}diff --git a/site/src/cut/components/ScrubValue.tsx b/site/src/cut/components/ScrubValue.tsxindex 68b28f7e..6a5239dd 100644--- a/site/src/cut/components/ScrubValue.tsx+++ b/site/src/cut/components/ScrubValue.tsx@@ -2,6 +2,7 @@import { useEffect, useRef, useState } from "react";import { cn } from "@/lib/utils";+import { snapNear } from "@/lib/snap";/*** Hot-text numeric value: reads as the plain mono readout it replaces, drag@@ -33,7 +34,7 @@ export function ScrubValue({max: number;step: number;keyStep?: number;- /** Detent values: a drag passing within a few steps lands exactly on one.+ /** Detent values: a drag passing within reach lands exactly on one.* Arrow keys and typed entries stay exact. */snap?: number[];format: (v: number) => string;@@ -164,8 +165,7 @@ export function ScrubValue({d.moved = true;// 2px per step; Shift coarsens to ×10.let next = clamp(d.startValue + (dx / 2) * step * (e.shiftKey ? 10 : 1));- const near = snap?.find((s) => Math.abs(next - s) <= 3 * step);- if (near !== undefined) next = clamp(near);+ if (snap) next = clamp(snapNear(next, snap, min, max));d.last = next;if (onScrub) onScrub(d.last);else setPreview(d.last);diff --git a/site/src/lib/snap.ts b/site/src/lib/snap.tsnew file mode 100644index 00000000..a8018146--- /dev/null+++ b/site/src/lib/snap.ts@@ -0,0 +1,9 @@+/** Fraction of a control's range around a detent that lands exactly on it. */+const SNAP_REACH = 0.025;++/** The detent within reach of `v`, or `v` unchanged. Reach scales with the+ * control's range, so paired controls over the same span detent identically. */+export function snapNear(v: number, detents: number[], min: number, max: number): number {+ const reach = (max - min) * SNAP_REACH;+ return detents.find((s) => Math.abs(v - s) <= reach) ?? v;+}