fix(frontend): Drawer 焦点还原提取 useRestoreFocus + 回归测试 + DRY(CR-H11)

This commit is contained in:
Yaojia Wang
2026-07-08 11:10:23 +02:00
parent 5c02792e5f
commit 6e2b478ca9
4 changed files with 97 additions and 20 deletions

View File

@@ -0,0 +1,24 @@
"use client";
import { useEffect, useRef, type RefObject } from "react";
// 弹层Drawer / ContextDrawer关闭后把焦点还给触发元素a11y 焦点管理CR-H11
// wasOpenRef 门闩:仅在「曾打开过」后的关闭才还原,避免从未打开时误抢焦点。
// null-safetriggerRef 或其 current 为空时静默跳过。
export function useRestoreFocus(
open: boolean,
triggerRef?: RefObject<HTMLElement | null>,
): void {
const wasOpenRef = useRef(false);
useEffect(() => {
if (!open) {
if (wasOpenRef.current) {
wasOpenRef.current = false;
triggerRef?.current?.focus();
}
return;
}
wasOpenRef.current = true;
}, [open, triggerRef]);
}