| 1 | // 顶栏弹出面板的"原地变形"(FLIP):面板盖在触发按钮原位,首帧压到按钮的 |
| 2 | // 真实边界(圆角 999px),下一帧过渡到面板全形——按钮"长成"面板;关闭时反向缩回。 |
| 3 | // |
| 4 | // **抽成一份共用**,而不是让用户菜单和告警各写一遍:这两个面板紧挨着, |
| 5 | // 时长或缓动差一点点,来回点两下就看得出来。 |
| 6 | import { useEffect, useLayoutEffect, useRef, useState } from "react"; |
| 7 | |
| 8 | const OPEN_MS = 260; |
| 9 | const CLOSE_MS = 190; |
| 10 | |
| 11 | function reduced(): boolean { |
| 12 | return window.matchMedia("(prefers-reduced-motion: reduce)").matches; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * 返回 `{ open, setOpen, close, anchorRef, panelRef }`。 |
| 17 | * |
| 18 | * `close()` 会先播收回动画再卸载;要立刻关(比如导航走了)直接 `setOpen(false)`。 |
| 19 | * 面板外点击与 Esc 已经接好,挂在 `rootRef` 上。 |
| 20 | */ |
| 21 | export function usePopoverFlip<A extends HTMLElement, P extends HTMLElement>( |
| 22 | /** 变形的锚点角。**面板贴哪边就写哪边**:顶栏右侧的面板贴右上角, |
| 23 | * 贴左边的面板(比如图例的「+N 个类」)要写 "top left", |
| 24 | * 否则它会从右边缘往左长出来,看着像从别处飞过来的 */ |
| 25 | origin: "top right" | "top left" | "bottom left" = "top right", |
| 26 | ) { |
| 27 | const [open, setOpen] = useState(false); |
| 28 | const rootRef = useRef<HTMLDivElement>(null); |
| 29 | const anchorRef = useRef<A>(null); |
| 30 | const panelRef = useRef<P>(null); |
| 31 | const closingRef = useRef(false); |
| 32 | |
| 33 | useLayoutEffect(() => { |
| 34 | if (!open) return; |
| 35 | const panel = panelRef.current; |
| 36 | const anchor = anchorRef.current; |
| 37 | if (!panel || !anchor || reduced()) return; |
| 38 | const a = anchor.getBoundingClientRect(); |
| 39 | const p = panel.getBoundingClientRect(); |
| 40 | if (p.width < 1 || p.height < 1) return; |
| 41 | panel.style.transformOrigin = origin; |
| 42 | panel.style.transform = `scale(${a.width / p.width}, ${a.height / p.height})`; |
| 43 | panel.style.borderRadius = "999px"; |
| 44 | panel.style.opacity = "0.35"; |
| 45 | let done: number | undefined; |
| 46 | const raf = requestAnimationFrame(() => |
| 47 | requestAnimationFrame(() => { |
| 48 | panel.style.transition = `transform ${OPEN_MS}ms cubic-bezier(0.16,1,0.3,1), border-radius ${OPEN_MS}ms cubic-bezier(0.16,1,0.3,1), opacity 0.18s ease`; |
| 49 | panel.style.transform = "scale(1, 1)"; |
| 50 | panel.style.borderRadius = "12px"; |
| 51 | panel.style.opacity = "1"; |
| 52 | // 动画完把行内样式**清干净**,别留一个 `scale(1,1)`。 |
| 53 | // 恒等变换看着无害,但它照样生成合成层,于是面板里绝对定位的子元素 |
| 54 | // 会按设备像素吸附一次——在 DPR 1.5 上就是 0.67px 的偏移, |
| 55 | // 而关闭按钮要跟触发它的那个按钮**原位重合**,差一个物理像素也看得出来 |
| 56 | done = window.setTimeout(() => { |
| 57 | panel.style.transition = ""; |
| 58 | panel.style.transform = ""; |
| 59 | panel.style.borderRadius = ""; |
| 60 | panel.style.opacity = ""; |
| 61 | panel.style.transformOrigin = ""; |
| 62 | }, OPEN_MS + 20); |
| 63 | }), |
| 64 | ); |
| 65 | return () => { |
| 66 | cancelAnimationFrame(raf); |
| 67 | if (done !== undefined) window.clearTimeout(done); |
| 68 | }; |
| 69 | }, [open, origin]); |
| 70 | |
| 71 | const close = () => { |
| 72 | const panel = panelRef.current; |
| 73 | const anchor = anchorRef.current; |
| 74 | if (closingRef.current) return; |
| 75 | if (!panel || !anchor || reduced()) { |
| 76 | setOpen(false); |
| 77 | return; |
| 78 | } |
| 79 | closingRef.current = true; |
| 80 | panel.style.transformOrigin = origin; |
| 81 | const a = anchor.getBoundingClientRect(); |
| 82 | // offsetWidth/Height 是布局尺寸,不受当前 transform 影响—— |
| 83 | // 用 getBoundingClientRect 会拿到已经缩过的值,越缩越小 |
| 84 | panel.style.transition = `transform ${CLOSE_MS}ms cubic-bezier(0.5,0,0.9,0.4), border-radius ${CLOSE_MS}ms cubic-bezier(0.5,0,0.9,0.4), opacity 0.16s ease`; |
| 85 | panel.style.transform = `scale(${a.width / panel.offsetWidth}, ${a.height / panel.offsetHeight})`; |
| 86 | panel.style.borderRadius = "999px"; |
| 87 | panel.style.opacity = "0.3"; |
| 88 | window.setTimeout(() => { |
| 89 | closingRef.current = false; |
| 90 | setOpen(false); |
| 91 | }, CLOSE_MS); |
| 92 | }; |
| 93 | |
| 94 | useEffect(() => { |
| 95 | if (!open) return; |
| 96 | const onDown = (e: MouseEvent) => { |
| 97 | if (rootRef.current && !rootRef.current.contains(e.target as Node)) |
| 98 | close(); |
| 99 | }; |
| 100 | const onKey = (e: KeyboardEvent) => { |
| 101 | if (e.key === "Escape") close(); |
| 102 | }; |
| 103 | document.addEventListener("mousedown", onDown); |
| 104 | document.addEventListener("keydown", onKey); |
| 105 | return () => { |
| 106 | document.removeEventListener("mousedown", onDown); |
| 107 | document.removeEventListener("keydown", onKey); |
| 108 | }; |
| 109 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 110 | }, [open]); |
| 111 | |
| 112 | return { open, setOpen, close, rootRef, anchorRef, panelRef }; |
| 113 | } |