Files
NODEDC_DESIGN_GUIDELINE/packages/ui-react/src/useToolbarMenus.ts
T
Codex cb534dbe39 feat(ui): add docking, viewport controls and composed settings
Add shared dock layouts, navigation cube, range bands, progress actions and comparison surfaces. Extend toolbar menus and loading composition, and align tokens, registry entries, documentation and the living catalog.
2026-10-29 09:38:25 +03:00

34 lines
1.8 KiB
TypeScript

import {useEffect,useRef} from "react";
import {movingToMenu,type MenuPoint} from "@nodedc/ui-core";
import type {DropdownTriggerApi} from "./Dropdown.js";
export function useToolbarMenus() {
const apis=useRef<Record<string,DropdownTriggerApi>>({});
const active=useRef<string|null>(null);
useEffect(()=>{
let previous:MenuPoint|null=null, origin:MenuPoint|null=null, pending:string|null=null, timer:number|undefined;
const clear=()=>{window.clearTimeout(timer);timer=undefined;pending=null;};
const open=(key:string)=>{clear();apis.current[key]?.show();};
const move=(event:PointerEvent)=>{
const current={x:event.clientX,y:event.clientY}, old=previous;previous=current;
const key=active.current;if(!key){clear();origin=null;return;}
const api=apis.current[key], surface=document.getElementById(api?.surfaceId);
const target=event.target instanceof Element?event.target:null;
if(surface?.contains(target)){clear();origin=current;return;}
const button=target?.closest<HTMLButtonElement>('[data-toolbar-menu]');
const next=button?.dataset.toolbarMenu;
if(next===key){clear();origin=current;return;}
if(!next||!apis.current[next]||button?.disabled){clear();return;}
if(!origin)origin=old??current;
const delay=surface&&old&&movingToMenu(origin,old,current,surface.getBoundingClientRect());
if(!delay){open(next);origin=current;return;}
if(pending===next)return;
clear();pending=next;
// Bounded grace: pausing on a sibling still switches, crossing diagonally into the menu does not.
timer=window.setTimeout(()=>open(next),300);
};
document.addEventListener("pointermove",move,{passive:true});
return()=>{clear();document.removeEventListener("pointermove",move);};
},[]);
return {apis,active};
}