极简多行输入

textarea · minimal · 灵感来源:Refactoring UI(实底 + 发丝线,延续极简输入框)

最多 200 字,换行会保留20 / 200
多行不限高:拖右下角改高度
必填项,不能留空
disabled 态:可见但不可改

复用卡

Autosizing Textarea · 极简多行输入 · 多行 / Minimal

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

做一个 Autosizing Textarea(极简多行输入):
- 白面 + 发丝边 + shadow-sm,没有模糊与 inset
- focus 只换边框颜色,底色不动
- autosize 与玻璃多行同一实现,差别只在材质
- error 态:1px warning 描边 + 提示行转语义色
用这些 CSS 变量:--color-accent --color-divider --color-surface --color-text --color-text-muted --color-text-subtle --color-warning-fg --dur-fast --glass-border --glass-recess --shadow-sm --text-2xs --text-base --text-xs
灵感参考:Refactoring UI(实底 + 发丝线,延续极简输入框)

参数(CSS 变量)

--color-accent --color-divider --color-surface --color-text --color-text-muted --color-text-subtle --color-warning-fg --dur-fast --glass-border --glass-recess --shadow-sm --text-2xs --text-base --text-xs

必缺状态

焦点 / 禁用 / 错误

整页场景

(尚未接进整页场景)

安装(一条链接)

调研笔记

极简多行输入

  • 白面 + --color-divider 发丝边 + --shadow-sm,与极简输入框同一外壳;focus 只换边框颜色,不换底
  • props 与玻璃多行输入完全一致:label / hint / errorMessage / required / showCount / autosize / maxRows

autosize:height:auto → 读 scrollHeight → 按 maxRows 截断,超限才允许内部滚动。

与玻璃的差别只在材质:没有模糊与 inset,长文本落在实底上反而更好读;代价是失去"凹陷"的触觉暗示。

实现

textarea/minimal
import { useRef, useState, useEffect, forwardRef, type TextareaHTMLAttributes } from 'react';
import { Field } from '~/components/Field';

interface Props extends TextareaHTMLAttributes<HTMLTextAreaElement> {
  label?: string;
  hint?: string;
  loading?: boolean;
  error?: boolean;
  errorMessage?: string;
  required?: boolean;
  showCount?: boolean;
  /** 随内容长高;超过 maxRows 转为内部滚动 */
  autosize?: boolean;
  maxRows?: number;
}

export const MinimalTextarea = forwardRef<HTMLTextAreaElement, Props>(
  (
    {
      label,
      hint,
      loading = false,
      error = false,
      errorMessage,
      required = false,
      showCount = false,
      autosize = false,
      maxRows = 8,
      className = '',
      id,
      value,
      defaultValue,
      onChange,
      rows,
      ...rest
    },
    ref,
  ) => {
    const localRef = useRef<HTMLTextAreaElement>(null);
    const setRef = (node: HTMLTextAreaElement | null) => {
      localRef.current = node;
      if (typeof ref === 'function') ref(node);
      else if (ref) ref.current = node;
    };

    const [inner, setInner] = useState(String(defaultValue ?? ''));
    const rowCount = rows ?? (autosize ? 1 : 3);
    const text = value !== undefined ? String(value) : inner;

    useEffect(() => {
      const el = localRef.current;
      if (!el || !autosize) return;
      const cs = getComputedStyle(el);
      const line = parseFloat(cs.lineHeight) || 20;
      const chrome =
        parseFloat(cs.paddingTop) +
        parseFloat(cs.paddingBottom) +
        parseFloat(cs.borderTopWidth) +
        parseFloat(cs.borderBottomWidth);
      const max = Math.max(maxRows, rowCount) * line + chrome;
      el.style.height = 'auto';
      el.style.height = `${Math.min(el.scrollHeight, max)}px`;
      el.style.overflowY = el.scrollHeight > max ? 'auto' : 'hidden';
    }, [text, autosize, maxRows, rowCount]);

    return (
      <Field
        id={id}
        label={label}
        hint={hint}
        error={error}
        errorMessage={errorMessage}
        required={required}
        loading={loading}
        counter={showCount ? `${text.length}${rest.maxLength ? ` / ${rest.maxLength}` : ''}` : undefined}
        className={className}
      >
        {(field) => (
          <div
            className={`
              rounded-md
              bg-[var(--color-surface)] border
              shadow-[var(--shadow-sm)]
              transition-[border-color,box-shadow] duration-[var(--dur-fast)]
              ${field.invalid
                ? 'border-[var(--color-warning-fg)]'
                : 'border-[var(--color-divider)] focus-within:border-[var(--color-accent)]'}
              ${rest.disabled ? 'opacity-50' : ''}
            `}
          >
            <textarea
              {...rest}
              ref={setRef}
              id={field.id}
              rows={rowCount}
              value={value}
              defaultValue={defaultValue}
              required={required}
              aria-required={field.required || undefined}
              aria-invalid={field.invalid || undefined}
              aria-describedby={field.describedBy}
              onChange={(e) => {
                setInner(e.target.value);
                onChange?.(e);
              }}
              className={`
                block w-full bg-transparent px-4 py-2.5
                text-[var(--color-text)] leading-relaxed
                placeholder:text-[var(--color-text-subtle)]
                focus:outline-none
                disabled:cursor-not-allowed
                ${autosize ? 'resize-none' : 'resize-y'}
              `}
            />
          </div>
        )}
      </Field>
    );
  },
);
MinimalTextarea.displayName = 'MinimalTextarea';

元信息

标识
textarea/minimal
状态
done
最后提交
2026-11-01
风格轴
minimal
依赖 token
--color-accent --color-divider --color-surface --color-text --color-text-muted --color-text-subtle --color-warning-fg --dur-fast --glass-border --glass-recess --shadow-sm --text-2xs --text-base --text-xs
在工作台打开 源码与 notes 都在 src/components/textarea/minimal/