极简滑块

slider · minimal · 灵感来源:Refactoring UI(细轨道 + 墨色方块旋钮,不靠光影分层)

音量

60%

亮度

35%

步进 · 每档 20

40

禁用

70%

细轨道 + 墨色方块旋钮,层级只靠对比不靠光影

复用卡

Minimal Range · 极简滑块 · 滑块 / Minimal

规格 · 术语提示词(给 AI 用)

做一个 Minimal Range(极简滑块):
- 4px 发丝线轨道,无边框无内阴影
- 已走段与旋钮同为墨色,层级只靠对比
- 圆角收到 2 / 3px,与极简轴一致
- 行为与另外两支完全一致,只换材质
用这些 CSS 变量:--color-divider --color-text --color-text-muted
灵感参考:Refactoring UI(细轨道 + 墨色方块旋钮,不靠光影分层)

参数(CSS 变量)

--color-divider --color-text --color-text-muted

必缺状态

焦点 / 禁用

整页场景

(尚未接进整页场景)

安装(一条链接)

调研笔记

极简滑块

拆解

  • 轨道只有 4px 的 --color-divider 发丝线,无边框无内阴影
  • 已走部分与旋钮同为 --color-text:层级靠对比,不靠浮起
  • 旋钮是 16px 的圆角方块(3px 圆角),和极简按钮的圆角语言一致
  • 圆角从 rounded-full 降到 2px / 3px,与极简轴的"圆角收小一档"一致

实现取舍

  • 行为与玻璃滑块完全一致(指针 1:1、APG 键盘、受控 / 非受控),只换材质——同 type 的 props 契约必须一致,check:props 会比对
  • 不挂过渡:拖拽中途加缓动会让人以为控件卡住
  • 禁用态用整体 opacity-40,不另做一套颜色

变量:--color-divider / --color-text / --color-text-muted。

实现

slider/minimal
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>
  );
}

元信息

标识
slider/minimal
状态
done
最后提交
2026-10-31
风格轴
minimal
依赖 token
--color-divider --color-text --color-text-muted
在工作台打开 源码与 notes 都在 src/components/slider/minimal/