动漫输入框
每打一个字,光标位置就扬起一点灰尘,删除时留下两道短速度线。
适合使用的场景
- 游戏或动漫页面的名称、昵称与短文本字段
- 希望打字有生命力、又不干扰输入的注册表单
注意事项
- 只用在短字段;长文本区每个键都扬尘会让人累
- 错误信息会替换提示,并把字段标记为无效
- 开启减少动态效果时一个特效都不画,状态照常改变,不看特效也能使用
交给你的 agent 安装
一次复制就包含安装命令、使用时机、props 与设计规则。粘贴到 Claude Code、Cursor 或 Codex 即可。
用 CLI 安装
使用 shadcn CLI。源码会复制到你的项目中,每一行都由你掌控。
$ npx shadcn@latest add https://sunui.synvize.com/r/anime-input.json或手动安装
- 1. 安装依赖。
$ npm install clsx tailwind-merge - 2. 如果还没有,在
lib/utils.ts添加cn辅助函数。没有用 shadcn/ui?再加上 sunui-theme.css 提供颜色变量(做法)。然后复制组件代码。anime-input.tsx"use client"; import { useId, useRef, type InputHTMLAttributes, type KeyboardEvent } from "react"; import { at, lines, paletteOf, puff } from "@/components/ui/anime-fx"; import { cn } from "@/lib/utils"; export type AnimeInputProps = Omit<InputHTMLAttributes<HTMLInputElement>, "children"> & { label: string; /** Help text under the field */ hint?: string; /** Error text; marks the field invalid */ error?: string; }; const SHELL = "border-[3px] border-foreground bg-card shadow-[4px_5px_0_color-mix(in_oklch,var(--sunui-spot,var(--primary))_38%,var(--card))]"; let measurer: CanvasRenderingContext2D | null = null; /** * A text field where every key kicks up a little dust where the caret is, and a deletion leaves two short speed lines. * Nothing fires while an input method is composing (Chinese, Japanese), only when the character is committed. * A plain input underneath: label, hint, error and every input attribute work as usual. */ export function AnimeInput({ label, hint, error, id, className, onKeyDown, onCompositionEnd, ...rest }: AnimeInputProps) { const auto = useId(); const fieldId = id ?? auto; const ref = useRef<HTMLInputElement>(null); const composing = useRef(false); // Where the caret is, in page coordinates: text width up to the caret, from the field's own font const caret = () => { const el = ref.current!; const cs = getComputedStyle(el); measurer ??= document.createElement("canvas").getContext("2d"); if (!measurer) return at(el, 0.5, 0.5); measurer.font = `${cs.fontWeight} ${cs.fontSize} ${cs.fontFamily}`; const text = el.type === "password" ? "•".repeat(el.selectionStart ?? el.value.length) : el.value.slice(0, el.selectionStart ?? el.value.length); const box = at(el, 0, 0.5); const x = box.x + parseFloat(cs.paddingLeft) + parseFloat(cs.borderLeftWidth) + measurer.measureText(text).width - el.scrollLeft; return { x: Math.min(x, box.x + box.w - 10), y: box.y, h: box.h }; }; const kick = (deleting: boolean) => { const el = ref.current; if (!el) return; // Wait one frame so the character is in the field and the caret has moved requestAnimationFrame(() => { const c = caret(); const P = paletteOf(el); if (deleting) lines(P, c.x + 6, c.y, { n: 2, dir: 0, len: 14, spread: 5, life: 0.22 }); else puff(P, c.x, c.y + c.h / 2 - 2, { size: 4 + Math.random() * 2, n: 3, vx: (Math.random() - 0.5) * 30, vy: -16, life: 0.5 }); }); }; const key = (e: KeyboardEvent<HTMLInputElement>) => { onKeyDown?.(e); if (e.defaultPrevented || composing.current || e.nativeEvent.isComposing || e.metaKey || e.ctrlKey || e.altKey) return; if (e.key === "Backspace" || e.key === "Delete") kick(true); else if (e.key.length === 1) kick(false); }; return ( <div className={cn("w-full max-w-xs", className)}> <label htmlFor={fieldId} className="mb-2 block text-[13px] font-bold"> {label} </label> <input ref={ref} id={fieldId} aria-invalid={error ? true : undefined} aria-describedby={error || hint ? `${fieldId}-d` : undefined} onKeyDown={key} onCompositionStart={() => (composing.current = true)} onCompositionEnd={(e) => { composing.current = false; onCompositionEnd?.(e); kick(false); }} className={cn( "h-11 w-full rounded-[12px] px-3.5 text-[15px] outline-none placeholder:text-muted-foreground disabled:opacity-60", "focus-visible:ring-[3px] focus-visible:ring-[var(--sunui-spot,var(--primary))] focus-visible:ring-offset-2 focus-visible:ring-offset-background", SHELL, error && "border-[var(--sunui-like,#dc3d43)]", )} {...rest} /> {(error || hint) && ( <p id={`${fieldId}-d`} className={cn("mt-2 text-[13px]", error ? "font-medium text-[var(--sunui-like,#dc3d43)]" : "text-muted-foreground")}> {error || hint} </p> )} </div> ); }
相似区块
- Pro
- Pro



