mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 05:20:33 +08:00
- Settings → General gains a classic/modern layout choice; classic is a frozen snapshot of dev's sidebar and learning dashboard, modern keeps iterating. Variants differ only through slots, tokens and per-variant CSS on <html data-ui-layout>; the preference lives in localStorage. - Modern chrome: warm neutral tokens, WorkspaceMenu header, ⌘K command palette and keyboard shortcuts, PageBar location strip, swipeable PartnerRoster. - Remove dead HomeShortcuts, WorkspaceSwitcher and the inline sidebar search query chain.
44 lines
1.6 KiB
TypeScript
44 lines
1.6 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useRef, type Dispatch, type SetStateAction } from "react";
|
|
|
|
/**
|
|
* Menu-button behaviour for a small popover: focus the first item on open,
|
|
* close on outside pointer or Escape (returning focus to the trigger), and
|
|
* move between `[role="menuitem"]`s with the arrow keys.
|
|
*/
|
|
export function useMenuPopover<T extends HTMLElement>(
|
|
open: boolean,
|
|
setOpen: Dispatch<SetStateAction<boolean>>,
|
|
) {
|
|
const rootRef = useRef<T>(null);
|
|
useEffect(() => {
|
|
if (!open) return;
|
|
const root = rootRef.current;
|
|
root?.querySelector<HTMLElement>('[role="menuitem"][aria-current="page"], [role="menuitem"]')?.focus();
|
|
const dismiss = (event: PointerEvent) => {
|
|
if (!root?.contains(event.target as Node)) setOpen(false);
|
|
};
|
|
const keys = (event: KeyboardEvent) => {
|
|
if (event.key === "Escape") {
|
|
setOpen(false);
|
|
root?.querySelector<HTMLElement>('[aria-haspopup="menu"]')?.focus();
|
|
return;
|
|
}
|
|
if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return;
|
|
event.preventDefault();
|
|
const items = [...(root?.querySelectorAll<HTMLElement>('[role="menuitem"]') ?? [])];
|
|
const index = items.indexOf(document.activeElement as HTMLElement);
|
|
const next = event.key === "ArrowDown" ? index + 1 : index - 1;
|
|
items[(next + items.length) % items.length]?.focus();
|
|
};
|
|
document.addEventListener("pointerdown", dismiss);
|
|
document.addEventListener("keydown", keys);
|
|
return () => {
|
|
document.removeEventListener("pointerdown", dismiss);
|
|
document.removeEventListener("keydown", keys);
|
|
};
|
|
}, [open, setOpen]);
|
|
return rootRef;
|
|
}
|