;
return [childEl.props.panelId ?? "root", childEl];
}
})
.filter(Boolean),
),
[children],
);
useEffect(() => {
startTransition(() => {
onPanelChange?.(history[0] ?? "root");
stackRef.current?.dispatchEvent(new Event("updatePopper", { bubbles: true }));
});
}, [onPanelChange, history]);
const goBack = () => {
if (onBack?.(history[0], history[1] ?? "root") !== false) {
setHistory(history.slice(1));
}
};
const currentPanel = useMemo(() => panels.get(history[0]) ?? panels.values().next().value, [history, panels]);
const checkMenuClick = (e: React.MouseEvent) => {
const panelKey = (e.target as HTMLElement)?.dataset?.panel;
if (panelKey) {
if (panelKey === "back") {
goBack();
} else if (panelKey === "root") {
if (onBack?.(history[0], "root") !== false) {
setHistory([]);
}
} else if (panels.has(panelKey)) {
setHistory([panelKey, ...history]);
}
e.stopPropagation();
e.preventDefault();
return false;
}
return true;
};
return (
{currentPanel &&
cloneElement(currentPanel, {
// @ts-expect-error ignore
onBack: history.length > 0 ? goBack : undefined,
})}
);
}