Skip to content

Commit

Permalink
fix: hide submenu before hide menu
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Aug 25, 2022
1 parent 553e3a5 commit e8c47ab
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/solid-contextmenu/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<link rel="shortcut icon" type="image/ico" href="/src/assets/favicon.ico" />
<title>Solid Lib</title>
<title>solid-contextmenu</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
3 changes: 2 additions & 1 deletion packages/solid-contextmenu/src/bus.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import mitt from "mitt";
import { ShowContextMenuParams } from ".";
import { ShowContextMenuParams, MenuId } from ".";

type Events = {
show: ShowContextMenuParams;
hideAll: void;
hide: MenuId;
};

export const bus = mitt<Events>();
11 changes: 7 additions & 4 deletions packages/solid-contextmenu/src/components/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,21 @@ export const Menu = (props: MenuProps) => {
)
);
});

const hide = () => {
bus.emit("hide", local.id);
setShown(false);
};
const contextValue = mergeProps(
{
shown: shown,
animation: "scale",
hide: () => setShown(false),
hide: hide,
props: showProps,
},
local
);
bus.on("hideAll", () => {
setShown(false);
hide();
});

onCleanup(() => {
Expand Down Expand Up @@ -133,7 +136,7 @@ export const Menu = (props: MenuProps) => {
top: pos().y + "px",
}}
// @ts-ignore
use:clickOutside={() => setShown(false)}
use:clickOutside={hide()}
ref={el!}
/>
</Show>
Expand Down
13 changes: 12 additions & 1 deletion packages/solid-contextmenu/src/components/Submenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
createSignal,
JSX,
mergeProps,
onCleanup,
Show,
splitProps,
} from "solid-js";
import { useMenu } from "../context";
import { STYLE, BooleanPredicate, Pos, Size } from "..";
import { getPredicateValue } from "../utils";
import { bus } from "../bus";

export interface LocalSubMenuProps {
label: JSX.Element;
Expand Down Expand Up @@ -37,7 +39,7 @@ export const Submenu = (props: SubMenuProps) => {
},
local
);
const { props: showProps } = useMenu();
const { props: showProps, id: menuId } = useMenu();
const handlerParams = createMemo(() => ({
props: showProps(),
}));
Expand Down Expand Up @@ -74,12 +76,21 @@ export const Submenu = (props: SubMenuProps) => {
}
}
setPos({ x, y });
console.log(pos());
};
const [shown, setShown] = createSignal(false);
const show = () => {
setShown(true);
updatePos();
};
bus.on("hide", (id) => {
if (id === menuId) {
setShown(false);
}
});
onCleanup(() => {
bus.off("hide");
});
return (
<Show when={!isHidden()}>
<div
Expand Down
2 changes: 1 addition & 1 deletion sites/site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<link rel="shortcut icon" type="image/ico" href="/src/assets/favicon.ico" />
<title>Solid App</title>
<title>solid-contextmenu</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down

1 comment on commit e8c47ab

@vercel
Copy link

@vercel vercel bot commented on e8c47ab Aug 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

solid-contextmenu – ./

solid-contextmenu-xhofe.vercel.app
solid-contextmenu.vercel.app
solid-contextmenu-git-main-xhofe.vercel.app

Please sign in to comment.