极简分段控件

segmented · minimal · 灵感来源:Refactoring UI(用墨色块与反白文字分层,不用滑块光影)

统计范围:本周 · 试试 ← → 方向键;「月」是禁用项

外观:跟随系统 · sm 尺寸 + emoji 标签

复用卡

Flat Segmented Control · 极简分段控件 · 分段 / Minimal

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

做一个 Flat Segmented Control(极简分段控件):
- APG radiogroup:方向键遍历 + roving tabindex
- 轨道白底描边,选中项是实心前景块 + 反色文字
- 滑块 translate 平移,切换时一次 squash & stretch
- 选项可单独禁用,禁用项不响应方向键
用这些 CSS 变量:--color-bg --color-divider --color-surface --color-text --color-text-muted --color-text-subtle --dur-base --dur-fast --ease-standard --fw-medium --fw-normal --text-sm --text-xs
灵感参考:Refactoring UI(用墨色块与反白文字分层,不用滑块光影)

参数(CSS 变量)

--color-bg --color-divider --color-surface --color-text --color-text-muted --color-text-subtle --dur-base --dur-fast --ease-standard --fw-medium --fw-normal --text-sm --text-xs

必缺状态

焦点 / 禁用

整页场景

(尚未接进整页场景)

安装(一条链接)

调研笔记

极简分段控件

  • 与玻璃分段同一份 props(options / value / onChange / size / className),键盘与 roving tabindex 一致
  • 轨道 = 白底 + 发丝边;选中滑块 = --color-text 实心前景块,文字反成 --color-bg
  • 没有阴影、没有内高光:选中状态只由「实心块 + 反色文字」表达(暗色下自动反转)
  • 圆角收小:轨道 rounded-md、滑块 rounded-sm
  • 平移仍用 --dur-base + --ease-standard,拉伸沿用的是同一个 .segmented-stretch

对比玻璃分段:少一层材质,多一层对比;在密集工具栏里比玻璃滑块更稳。

实现

segmented/minimal
import { useRef, useState, type KeyboardEvent, type ReactNode } from 'react';

export interface SegmentOption {
  value: string;
  label: ReactNode;
  /** 禁用项:不可选也不响应方向键 */
  disabled?: boolean;
}

interface Props {
  options: SegmentOption[];
  value: string;
  onChange?: (value: string) => void;
  size?: 'sm' | 'md';
  className?: string;
}

const sizeClasses = {
  sm: 'h-8 text-[length:var(--text-xs)] px-2.5',
  md: 'h-9 text-[length:var(--text-sm)] px-3.5',
};

/**
 * 极简分段控件:与玻璃分段同一份 props,
 * 选中项从"凸起滑块"换成"实心墨块"——不靠光影,靠墨色与反白文字分层。
 */
export function MinimalSegmented({
  options,
  value,
  onChange,
  size = 'md',
  className = '',
}: Props) {
  const index = Math.max(0, options.findIndex((o) => o.value === value));
  const [stretching, setStretching] = useState(false);
  const btnRefs = useRef<(HTMLButtonElement | null)[]>([]);

  const select = (next: number, focus = false) => {
    const clamped = Math.max(0, Math.min(options.length - 1, next));
    if (options[clamped]?.disabled) return;
    if (clamped !== index) {
      setStretching(true);
      window.setTimeout(() => setStretching(false), 280);
    }
    onChange?.(options[clamped].value);
    if (focus) btnRefs.current[clamped]?.focus();
  };

  const onKeyDown = (e: KeyboardEvent) => {
    if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
      e.preventDefault();
      select(index + 1, true);
    } else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
      e.preventDefault();
      select(index - 1, true);
    } else if (e.key === 'Home') {
      e.preventDefault();
      select(0, true);
    } else if (e.key === 'End') {
      e.preventDefault();
      select(options.length - 1, true);
    }
  };

  return (
    <div
      role="radiogroup"
      onKeyDown={onKeyDown}
      className={`
        relative grid items-stretch
        rounded-md p-1
        bg-[var(--color-surface)] border border-[var(--color-divider)]
        ${className}
      `}
      style={{ gridTemplateColumns: `repeat(${options.length}, 1fr)` }}
    >
      {/* 滑块:实心墨块,定位零测量(宽度按分格 + translate 百分比) */}
      <span
        aria-hidden="true"
        className={`
          absolute top-1 bottom-1 left-1 rounded-sm
          bg-[var(--color-text)]
          ${stretching ? 'segmented-stretch' : ''}
        `}
        style={{
          width: `calc((100% - 8px) / ${options.length})`,
          translate: `${index * 100}% 0`,
          transition: 'translate var(--dur-base) var(--ease-standard)',
        }}
      />

      {options.map((opt, i) => {
        const selected = i === index;
        return (
          <button
            key={opt.value}
            ref={(el) => { btnRefs.current[i] = el; }}
            type="button"
            role="radio"
            aria-checked={selected}
            tabIndex={selected ? 0 : -1}
            onClick={() => select(i)}
            disabled={opt.disabled}
            aria-disabled={opt.disabled}
            className={`
              relative z-[1] rounded-sm whitespace-nowrap
              ${sizeClasses[size]}
              transition-colors duration-[var(--dur-fast)]
              select-none
              ${opt.disabled
                ? 'text-[var(--color-text-subtle)] cursor-not-allowed opacity-50'
                : `cursor-pointer ${selected
                    ? 'text-[var(--color-bg)]'
                    : 'text-[var(--color-text-muted)] hover:text-[var(--color-text)]'}`}
            `}
            style={{ fontWeight: selected ? 'var(--fw-medium)' : 'var(--fw-normal)' }}
          >
            {opt.label}
          </button>
        );
      })}
    </div>
  );
}

元信息

标识
segmented/minimal
状态
done
最后提交
2026-10-31
风格轴
minimal
依赖 token
--color-bg --color-divider --color-surface --color-text --color-text-muted --color-text-subtle --dur-base --dur-fast --ease-standard --fw-medium --fw-normal --text-sm --text-xs
在工作台打开 源码与 notes 都在 src/components/segmented/minimal/