components/linyi/range-slider.tsx
@base-ui/react
"use client"; import * as React from "react";import { Slider as BaseSlider } from "@base-ui/react/slider";import { cn } from "@/lib/cn"; export interface RangeSliderProps { value?: number; defaultValue?: number; onValueChange?: (value: number) => void; min?: number; max?: number; step?: number; label?: string; unit?: string; showLabels?: boolean; disabled?: boolean; className?: string;} export function RangeSlider({ value, defaultValue = 60, onValueChange, min = 0, max = 100, step = 10, label = "折射强度", unit = "%", showLabels = true, disabled = false, className,}: RangeSliderProps) { const safeStep = Math.max(1, step); const [internalVal, setInternalVal] = React.useState(defaultValue); const currentVal = value ?? internalVal; const setVal = (next: number) => { const clamped = Math.min(max, Math.max(min, next)); if (value === undefined) { setInternalVal(clamped); } onValueChange?.(clamped); }; // Build graduated major & minor ticks cleanly across [min, max] const ticks = React.useMemo(() => { const span = Math.max(1, max - min); const rawCount = Math.floor(span / safeStep); const totalIntervals = Math.min(20, Math.max(2, rawCount)); const interval = span / totalIntervals; const majorEvery = totalIntervals > 10 ? 4 : totalIntervals > 6 ? 2 : 1; return Array.from({ length: totalIntervals + 1 }, (_, idx) => { const tickValue = Math.round(min + idx * interval); const isMajor = idx === 0 || idx === totalIntervals || idx % majorEvery === 0; return { value: tickValue, isMajor, pct: ((tickValue - min) / span) * 100, }; }); }, [min, max, safeStep]); return ( <BaseSlider.Root value={currentVal} min={min} max={max} step={safeStep} disabled={disabled} onValueChange={(next) => { const numeric = Array.isArray(next) ? next[0] : next; setVal(numeric); }} className={cn("w-full select-none space-y-2", className)} > <div className="flex items-center justify-between gap-2 text-xs"> <span className="font-medium text-fg">{label}</span> <span className="font-mono-num font-semibold text-accent"> {currentVal} {unit} </span> </div> <BaseSlider.Control className="relative flex w-full touch-none items-center py-2 cursor-pointer data-[disabled]:opacity-45 data-[disabled]:cursor-not-allowed"> <BaseSlider.Track className="relative h-1.5 w-full rounded-pill bg-well border border-line-well overflow-visible"> <BaseSlider.Indicator className="h-full rounded-pill bg-accent" /> <BaseSlider.Thumb aria-label={label} className="block h-4 w-4 rounded-pill bg-white border-[1.5px] border-accent shadow-[0_2px_6px_rgba(0,0,0,0.22)] transition-transform duration-150 hover:scale-110 active:scale-95 outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-canvas" /> </BaseSlider.Track> </BaseSlider.Control> {/* Graduated tick ruler */} <div className={cn("relative w-full", showLabels ? "h-7" : "h-3")}> {ticks.map((tick) => { const reached = tick.value <= currentVal; const isFirst = tick.pct === 0; const isLast = tick.pct === 100; const alignClass = isFirst ? "translate-x-0 items-start" : isLast ? "-translate-x-full items-end" : "-translate-x-1/2 items-center"; if (tick.isMajor) { return ( <button key={tick.value} type="button" disabled={disabled} onClick={() => setVal(tick.value)} aria-label={`设为 ${tick.value}${unit}`} style={{ left: `${tick.pct}%` }} className={cn( "group absolute top-0 flex flex-col gap-1 cursor-pointer disabled:cursor-not-allowed", alignClass )} > <span className={cn( "w-px transition-colors", tick.isMajor ? "h-2.5" : "h-1.5", reached ? "bg-accent" : "bg-line-strong group-hover:bg-fg-secondary" )} /> {showLabels && ( <span className={cn( "font-mono-num text-[10px] leading-none transition-colors", tick.value === currentVal ? "font-semibold text-accent" : reached ? "text-fg-secondary" : "text-fg-muted group-hover:text-fg" )} > {tick.value} </span> )} </button> ); } return ( <span key={tick.value} aria-hidden="true" style={{ left: `${tick.pct}%` }} className={cn( "pointer-events-none absolute top-0.5 h-1.5 w-px -translate-x-1/2 transition-colors", reached ? "bg-accent/60" : "bg-line-strong/70" )} /> ); })} </div> </BaseSlider.Root> );}