slider · minimal · 灵感来源:Refactoring UI(细轨道 + 墨色方块旋钮,不靠光影分层)
音量
亮度
步进 · 每档 20
禁用
细轨道 + 墨色方块旋钮,层级只靠对比不靠光影
Minimal Range · 极简滑块 · 滑块 / Minimal
做一个 Minimal Range(极简滑块):
- 4px 发丝线轨道,无边框无内阴影
- 已走段与旋钮同为墨色,层级只靠对比
- 圆角收到 2 / 3px,与极简轴一致
- 行为与另外两支完全一致,只换材质
用这些 CSS 变量:--color-divider --color-text --color-text-muted
灵感参考:Refactoring UI(细轨道 + 墨色方块旋钮,不靠光影分层)--color-divider --color-text --color-text-muted
焦点 / 禁用
(尚未接进整页场景)
/r/slider--minimal.json
免外壳的 markdown 版:/llms/components/slider--minimal.md--color-divider 发丝线,无边框无内阴影--color-text:层级靠对比,不靠浮起rounded-full 降到 2px / 3px,与极简轴的"圆角收小一档"一致check:props 会比对opacity-40,不另做一套颜色变量:--color-divider / --color-text / --color-text-muted。
import { useRef, useState, type KeyboardEvent, type PointerEvent } from 'react';
interface Props {
value?: number;
defaultValue?: number;
min?: number;
max?: number;
step?: number;
onChange?: (value: number) => void;
disabled?: boolean;
className?: string;
showValue?: boolean;
unit?: string;
'aria-label'?: string;
}
const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));
const THUMB = 16;
export function MinimalSlider({
value,
defaultValue,
min = 0,
max = 100,
step = 1,
onChange,
disabled,
className = '',
showValue,
unit = '',
...rest
}: Props) {
const isControlled = value !== undefined;
const [internal, setInternal] = useState(defaultValue ?? min);
const current = clamp(isControlled ? value : internal, min, max);
const pct = max === min ? 0 : ((current - min) / (max - min)) * 100;
const travelRef = useRef<HTMLDivElement>(null);
const commit = (next: number) => {
const snapped = clamp(min + Math.round((next - min) / step) * step, min, max);
if (!isControlled) setInternal(snapped);
if (snapped !== current) onChange?.(snapped);
};
const fromPointer = (clientX: number) => {
const el = travelRef.current;
if (!el) return;
const rect = el.getBoundingClientRect();
if (rect.width === 0) return;
commit(min + ((clientX - rect.left) / rect.width) * (max - min));
};
const onPointerDown = (e: PointerEvent<HTMLDivElement>) => {
if (disabled) return;
e.currentTarget.setPointerCapture(e.pointerId);
fromPointer(e.clientX);
};
const onPointerMove = (e: PointerEvent<HTMLDivElement>) => {
if (disabled || !e.currentTarget.hasPointerCapture(e.pointerId)) return;
fromPointer(e.clientX);
};
const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {
if (disabled) return;
const big = step * 10;
const targets: Record<string, number> = {
ArrowRight: current + step,
ArrowUp: current + step,
ArrowLeft: current - step,
ArrowDown: current - step,
PageUp: current + big,
PageDown: current - big,
Home: min,
End: max,
};
const next = targets[e.key];
if (next === undefined) return;
e.preventDefault();
commit(next);
};
return (
<div className={`flex items-center gap-3 ${className}`}>
<div
className={`
relative h-6 flex-1 select-none
${disabled ? 'opacity-40 cursor-not-allowed' : 'cursor-pointer'}
`}
>
<div className="absolute inset-x-0 top-1/2 h-1 -translate-y-1/2 rounded-[2px] bg-[var(--color-divider)]" />
<div
ref={travelRef}
onPointerDown={onPointerDown}
onPointerMove={onPointerMove}
className="absolute inset-y-0 touch-none"
style={{ left: THUMB / 2, right: THUMB / 2 }}
>
<div
className="absolute top-1/2 left-0 h-1 -translate-y-1/2 rounded-[2px] bg-[var(--color-text)]"
style={{ width: `${pct}%` }}
/>
<div
role="slider"
tabIndex={disabled ? -1 : 0}
aria-valuemin={min}
aria-valuemax={max}
aria-valuenow={current}
aria-orientation="horizontal"
aria-disabled={disabled || undefined}
onKeyDown={onKeyDown}
className="absolute top-1/2 size-4 -translate-x-1/2 -translate-y-1/2 rounded-[3px] bg-[var(--color-text)]"
style={{ left: `${pct}%` }}
{...rest}
/>
</div>
</div>
{showValue && (
<span className="w-10 shrink-0 text-right text-xs font-mono text-[var(--color-text-muted)]">
{current}{unit}
</span>
)}
</div>
);
}
src/components/slider/minimal/