textarea · minimal · 灵感来源:Refactoring UI(实底 + 发丝线,延续极简输入框)
Autosizing Textarea · 极简多行输入 · 多行 / Minimal
做一个 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(实底 + 发丝线,延续极简输入框)--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
焦点 / 禁用 / 错误
(尚未接进整页场景)
/r/textarea--minimal.json
免外壳的 markdown 版:/llms/components/textarea--minimal.md--color-divider 发丝边 + --shadow-sm,与极简输入框同一外壳;focus 只换边框颜色,不换底label / hint / errorMessage / required / showCount / autosize / maxRowsautosize:height:auto → 读 scrollHeight → 按 maxRows 截断,超限才允许内部滚动。
与玻璃的差别只在材质:没有模糊与 inset,长文本落在实底上反而更好读;代价是失去"凹陷"的触觉暗示。
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';
src/components/textarea/minimal/