玻璃输入框

input · glass-apple · 灵感来源:visionOS / iOS 26 浮层输入

2–20 个字符,支持中英文0 / 20
至少 8 位,含数字与字母
https://.com
前后缀也算输入框的一部分
这个邮箱格式不对
disabled 态:可见但不可改

复用卡

Floating Label Input · 浮层输入框 · 输入 / Glass

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

做一个 Floating Label Input(浮层输入框):
- 标签在框上方,不做浮动标签动画
- focus 时边框转 accent、底色提亮
- error 态:语义色描边 + aria-invalid + 文案说明
- disabled 态降透明度且不可聚焦
用这些 CSS 变量:--color-accent --color-text --color-text-muted --color-text-subtle --color-warning-fg --dur-fast --glass-bg --glass-border --glass-highlight --glass-inset --glass-recess --text-2xs --text-base --text-xs
灵感参考:visionOS / iOS 26 浮层输入

参数(CSS 变量)

--color-accent --color-text --color-text-muted --color-text-subtle --color-warning-fg --dur-fast --glass-bg --glass-border --glass-highlight --glass-inset --glass-recess --text-2xs --text-base --text-xs

必缺状态

焦点 / 禁用 / 错误

安装(一条链接)

调研笔记

玻璃输入框

  • 半透明底 + inset 内阴影,模拟"凹陷"输入区
  • focus 由外壳的 focus-within 接管:边框提亮 + 背景加重,编辑状态清晰
  • placeholder 用 --color-text-subtle,不抢主色
  • 单行变体全在这一份 props 里:label / hint / errorMessage / required / showCount / prefix / suffix / clearable / reveal / loading

校验契约:error 或 errorMessage 任一为真即 aria-invalid;提示行接 aria-describedby,计数另有 id 一并挂上;错误文案加 role="alert",读屏在失焦时能听到。clear 走原生 value setter 再派发 input,否则 React 的受控值追踪会把清空吃掉。

变量:--glass-bg(surface 28%)/ --color-text-subtle #717179(对比度修到 4.84:1)。

实现

input/glass-apple
import { useRef, useState, forwardRef, type InputHTMLAttributes, type ReactNode } from 'react';
import { Eye, EyeOff, X } from 'lucide-react';
import { Icon } from '~/components/Icon';
import { Field } from '~/components/Field';

interface Props extends Omit<InputHTMLAttributes<HTMLInputElement>, 'prefix'> {
  label?: string;
  hint?: string;
  /** 验证中:标签右侧转圈 */
  loading?: boolean;
  /** 错误态:描边转语义色 + aria-invalid(状态矩阵见 docs/standards/states.md) */
  error?: boolean;
  /** 错误文案:渲染在下方并接入 aria-describedby + role="alert" */
  errorMessage?: string;
  required?: boolean;
  prefix?: ReactNode;
  suffix?: ReactNode;
  /** 有值时显示清除按钮 */
  clearable?: boolean;
  /** password 显隐切换 */
  reveal?: boolean;
  /** 字数计数:配合 maxLength 显示 n / max */
  showCount?: boolean;
}

/** 清空需要绕过 React 的 value 追踪:直接写原生 setter 再派发 input */
const setNativeValue = (el: HTMLInputElement, value: string) => {
  const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')?.set;
  setter?.call(el, value);
  el.dispatchEvent(new Event('input', { bubbles: true }));
};

export const GlassAppleInput = forwardRef<HTMLInputElement, Props>(
  (
    {
      label,
      hint,
      loading = false,
      error = false,
      errorMessage,
      required = false,
      prefix,
      suffix,
      clearable = false,
      reveal = false,
      showCount = false,
      className = '',
      id,
      value,
      defaultValue,
      onChange,
      type = 'text',
      ...rest
    },
    ref,
  ) => {
    const localRef = useRef<HTMLInputElement>(null);
    const setRef = (node: HTMLInputElement | null) => {
      localRef.current = node;
      if (typeof ref === 'function') ref(node);
      else if (ref) ref.current = node;
    };

    const [inner, setInner] = useState(String(defaultValue ?? ''));
    const [revealed, setRevealed] = useState(false);
    const text = value !== undefined ? String(value) : inner;

    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={`
              flex items-center
              rounded-xl
              bg-[var(--glass-bg)] border backdrop-blur-xl
              shadow-[var(--glass-inset)]
              transition-[border-color,background-color,box-shadow] duration-[var(--dur-fast)]
              focus-within:bg-[var(--glass-highlight)]
              ${field.invalid
                ? 'border-[var(--color-warning-fg)]'
                : 'border-[var(--glass-border)] focus-within:border-[var(--color-accent)]'}
              ${rest.disabled ? 'opacity-50' : ''}
            `}
          >
            {prefix && (
              <span className="pl-3 shrink-0 text-[var(--color-text-muted)] text-sm">{prefix}</span>
            )}

            <input
              {...rest}
              ref={setRef}
              id={field.id}
              type={reveal && type === 'password' && revealed ? 'text' : type}
              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={`
                w-full bg-transparent py-2.5
                ${prefix ? 'pl-2' : 'pl-4'} ${suffix || clearable || reveal ? 'pr-1' : 'pr-4'}
                text-[var(--color-text)]
                placeholder:text-[var(--color-text-subtle)]
                focus:outline-none
                disabled:cursor-not-allowed
              `}
            />

            {clearable && text !== '' && (
              <button
                type="button"
                onClick={() => localRef.current && setNativeValue(localRef.current, '')}
                aria-label="清除输入"
                className="press-feedback shrink-0 grid place-items-center size-6 rounded-full text-[var(--color-text-subtle)] hover:text-[var(--color-text)] cursor-pointer"
              >
                <Icon icon={X} size={13} />
              </button>
            )}

            {reveal && type === 'password' && (
              <button
                type="button"
                onClick={() => setRevealed((v) => !v)}
                aria-label={revealed ? '隐藏密码' : '显示密码'}
                aria-pressed={revealed}
                className="press-feedback shrink-0 grid place-items-center size-6 rounded-full text-[var(--color-text-subtle)] hover:text-[var(--color-text)] cursor-pointer"
              >
                <Icon icon={revealed ? EyeOff : Eye} size={14} />
              </button>
            )}

            {suffix && (
              <span className="pr-3 shrink-0 text-[var(--color-text-muted)] text-sm">{suffix}</span>
            )}
          </div>
        )}
      </Field>
    );
  },
);
GlassAppleInput.displayName = 'GlassAppleInput';

元信息

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