import type { MenuRootContextValue } from "../root/menu-root-context";
/** Payload of the floating tree's `itemhover` event. */
export interface MenuItemHoverEvent {
/** Floating node of the menu the hovered item belongs to. */
nodeId: string;
/** The hovered item element. */
target: EventTarget | null;
}
/**
* Returns an `onMouseMove` handler that announces an item hover on the
* floating tree, so an open submenu of the same menu can close when a
* different item is hovered (see `
`). Mirrors React's
* `useMenuItemCommonProps` `onMouseMove`.
*
* `nodeId` is the floating node of the menu the item renders in, which is what
* a child submenu sees as its `floatingParentNodeId`.
*/
export function createItemHoverEmitter(
menu: Pick,
nodeId: string | null | undefined,
): (event: MouseEvent) => void {
return (event) => {
if (!nodeId) return;
const payload: MenuItemHoverEvent = {
nodeId,
target: event.currentTarget,
};
menu.floatingTreeRoot()?.events.emit("itemhover", payload);
};
}