Skip to content

Commit

Permalink
fix: make context menu and global menu (top level) dismiss on menu it…
Browse files Browse the repository at this point in the history
…em click (#142)
  • Loading branch information
seanes authored Dec 19, 2024
1 parent 447d702 commit 294900d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 32 deletions.
1 change: 0 additions & 1 deletion .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { StorybookConfig } from "@storybook/react-vite";
const config: StorybookConfig = {
stories: ["../lib/components/**/*.stories.@(ts|tsx)", "../lib/stories/**/*.stories.@(ts|tsx)"],
addons: [
"@storybook/addon-onboarding",
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-themes",
Expand Down
16 changes: 14 additions & 2 deletions lib/components/ContextMenu/ContextMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';
import cx from 'classnames';
import { useRef } from 'react';
import { useMemo, useRef } from 'react';
import { DropdownBase, type DropdownPlacement, IconButton, type MenuItemProps } from '../';
import { type MenuItemGroups, MenuItems } from '../';
import { useClickOutside } from '../../hooks';
Expand Down Expand Up @@ -32,6 +32,18 @@ export const ContextMenu = ({
const onToggle = () => toggleId(id);
const expanded = currentId === id;

const itemsWithToggle = useMemo(() => {
return items.map((item) => {
return {
...item,
onClick: () => {
item.onClick?.();
closeAll();
},
};
});
}, [items, closeAll]);

return (
<div className={cx(styles.toggle, className)} data-theme="neutral" ref={ref}>
<IconButton
Expand All @@ -44,7 +56,7 @@ export const ContextMenu = ({
onClick={onToggle}
/>
<DropdownBase className={styles.dropdown} placement={placement} open={expanded}>
<MenuItems groups={groups} items={items} />
<MenuItems groups={groups} items={itemsWithToggle} />
</DropdownBase>
</div>
);
Expand Down
20 changes: 17 additions & 3 deletions lib/components/GlobalMenu/GlobalMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client';
import { useState } from 'react';
import { useMemo, useState } from 'react';
import { Menu, type MenuItemGroups, type MenuItemProps, MenuListItem } from '../Menu';
import { type Account, AccountButton } from './AccountButton';
import { AccountMenu, type AccountMenuProps } from './AccountMenu';
Expand All @@ -17,6 +17,7 @@ export interface GlobalMenuProps extends AccountMenuProps {
className?: string;
currentAccount?: Account;
onSelectAccount?: (id: string) => void;
onClose?: () => void;
}

export const GlobalMenu = ({
Expand All @@ -29,6 +30,7 @@ export const GlobalMenu = ({
backLabel = 'Back',
currentAccount,
onSelectAccount,
onClose,
logoutButton,
}: GlobalMenuProps) => {
const [selectingAccount, setSelectingAccount] = useState<boolean>(false);
Expand All @@ -42,6 +44,18 @@ export const GlobalMenu = ({
onSelectAccount?.(id);
};

const itemsWithToggle = useMemo(() => {
return items.map((item) => {
return {
...item,
onClick: () => {
item.onClick?.();
onClose?.();
},
};
});
}, [items, onClose]);

if (selectingAccount) {
return (
<GlobalMenuBase>
Expand Down Expand Up @@ -72,7 +86,7 @@ export const GlobalMenu = ({
/>
</GlobalMenuHeader>
<MenuListItem as="div" role="separator" />
<Menu groups={groups} items={items} />
<Menu groups={groups} items={itemsWithToggle} />
{logoutButton && (
<>
<MenuListItem as="div" role="separator" />
Expand All @@ -87,7 +101,7 @@ export const GlobalMenu = ({

return (
<GlobalMenuBase>
<Menu groups={groups} items={items} />
<Menu groups={groups} items={itemsWithToggle} />
</GlobalMenuBase>
);
};
4 changes: 2 additions & 2 deletions lib/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export const Header = ({ search, menu, currentAccount, logo = {}, badge }: Heade
label={menu?.menuLabel}
/>
{menu && (
<DropdownBase padding={true} placement="right" open={currentId === 'menu'} className={styles.dropdown}>
<GlobalMenu {...menu} currentAccount={currentAccount} />
<DropdownBase padding placement="right" open={currentId === 'menu'} className={styles.dropdown}>
<GlobalMenu {...menu} currentAccount={currentAccount} onClose={closeAll} />
</DropdownBase>
)}
</HeaderMenu>
Expand Down
11 changes: 2 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
"version": "0.10.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist",
"!dist/stories",
"!dist/examples"
],
"files": ["dist", "!dist/stories", "!dist/examples"],
"type": "module",
"description": "Reusable react components",
"sideEffects": [
"*.css"
],
"sideEffects": ["*.css"],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"storybook": "storybook dev -p 6006",
Expand Down Expand Up @@ -41,7 +35,6 @@
"@storybook/addon-essentials": "^8.4.4",
"@storybook/addon-interactions": "^8.4.4",
"@storybook/addon-links": "^8.4.4",
"@storybook/addon-onboarding": "^8.4.4",
"@storybook/addon-themes": "^8.4.4",
"@storybook/blocks": "^8.4.4",
"@storybook/react": "^8.4.4",
Expand Down
15 changes: 0 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 294900d

Please sign in to comment.