import { useEffect, useRef, useState } from "react";
export function ProfileMenu({ src }) {
const [open, setOpen] = useState(false);
const ref = useRef(null);
useEffect(() => {
if (!open) return;
const onPointerDown = (e) => {
if (!ref.current?.contains(e.target)) setOpen(false);
};
const onKeyDown = (e) => e.key === "Escape" && setOpen(false);
document.addEventListener("pointerdown", onPointerDown);
document.addEventListener("keydown", onKeyDown);
return () => {
document.removeEventListener("pointerdown", onPointerDown);
document.removeEventListener("keydown", onKeyDown);
};
}, [open]);
return (
<div ref={ref} className="relative">
<button
onClick={() => setOpen((o) => !o)}
aria-haspopup="menu"
aria-expanded={open}
className="rounded-full transition-transform duration-150 ease-[cubic-bezier(0.23,1,0.32,1)] active:scale-95"
>
<img src={src} alt="Profile" className="size-9 rounded-full" />
</button>
<div
role="menu"
data-state={open ? "open" : "closed"}
className={[
"absolute right-0 top-full mt-2 w-44 origin-top-right rounded-lg border bg-white p-1 shadow-lg",
"transition-[opacity,transform,visibility] ease-[cubic-bezier(0.23,1,0.32,1)]",
open
? "visible scale-100 opacity-100 duration-200"
: "invisible pointer-events-none scale-95 opacity-0 duration-150",
// reduced motion: keep the fade, drop the scale
"motion-reduce:scale-100",
].join(" ")}
>
{["Profile", "Settings", "Log out"].map((label) => (
<button
key={label}
role="menuitem"
tabIndex={open ? 0 : -1}
onClick={() => setOpen(false)}
className="block w-full rounded-md px-3 py-2 text-left text-sm hover:bg-gray-100"
>
{label}
</button>
))}
</div>
</div>
);
}
If you'd rather use a gated hover, swap hover:bg-gray-100 for [@media(hover:hover)]:hover:bg-gray-100. The color change is subtle enough that I left it plain.
Gate: An avatar click happens occasionally and isn't keyboard-driven, so a standard animation is fine. The purpose is spatial consistency: the menu should look like it came out of the avatar.
Ingredients:
- Tool: CSS transitions toggled by state. The menu stays mounted, so no exit-animation library is needed. Transitions also retarget if the user double-clicks, where keyframes would restart.
- Properties:
opacityandtransform: scale(0.95 → 1), neverscale(0).visibilityflips at the end of the transition, which keeps the closed menu out of the tab order and screen readers without cutting off the exit. - Origin:
origin-top-right, assuming the avatar sits at the right edge of the header. Useorigin-top-leftif it's on the left. - Curve and timing:
cubic-bezier(0.23, 1, 0.32, 1), 200ms to open and 150ms to close. Closing is a bit faster because the user has already decided. - Reduced motion:
motion-reduce:scale-100drops the scale and keeps the fade.
Feel-check: Play it at 3–5× duration in the DevTools animation inspector. Confirm the menu grows out of the avatar corner and that rapid open/close clicks reverse smoothly.
This is hand-rolled, so it lacks arrow-key navigation and focus return to the avatar. If you want those, a headless primitive like Radix or Base UI gives you them, and the same classes work on its data-[state] attributes.
Test run: a profile dropdown in React and Tailwind with no animation library installed. The model read the Skill's RECIPES.md, chose CSS transitions and gave the gate, ingredients and a feel-check. The code was not run in a browser. The model noted that the hand-rolled menu lacks arrow-key navigation and focus return.
它做什么
把“加点动效”的请求变成经得起严格评审的实现。它按固定顺序做决定:先过使用频率这一关(每天用上百次的东西不做动画),再说出一个明确的目的(反馈、空间连续性、状态、避免突兀变化、说明、愉悦),选择最省事的工具(CSS 过渡、@starting-style、CSS 动画、WAAPI、Motion),决定要动的属性(只动 transform 和 opacity,绝不从 0 缩放),然后从固定表格里选缓动和时长,或者用弹簧,再处理被打断和退出的行为,以及减少动态效果和悬停的处理。它自带一张“绝不能交付”的表,以及一份 RECIPES 文件,涵盖按钮、下拉菜单、提示框、弹窗、抽屉、通知、手风琴和标签指示条等常见场景。
输出
代码,加上几行说明:关卡结果、所用要素,以及哪些需要用手感去检查。
适合什么场景
给组件加动效、想要一个明确的结论而不是一堆选项的开发者。
低风险:纯指令文件,没有脚本,不联网,不写文件。它只写前端代码、不运行,请在浏览器和真机上检查效果。已用一个 React 和 Tailwind 的头像下拉菜单试用一次:它读取了自带的 RECIPES 文件,选择了 CSS 过渡,并给出关卡结果、要素和手感检查;模型也指出手写菜单缺少方向键导航和焦点返回。没有带问题调用时,它只回复一句问候。作者 Emil Kowalski,MIT 协议。