better-mui-menu 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +119 -0
- package/dist/index.cjs +323 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +26 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +290 -0
- package/dist/index.js.map +1 -0
- package/jest.config.cjs +15 -0
- package/jest.setup.ts +1 -0
- package/package.json +61 -0
- package/scripts/ensure-version-tag.mjs +54 -0
- package/src/MultiLevelMenu/MultiLevelMenu.test.tsx +369 -0
- package/src/MultiLevelMenu/NestedMenuItem.tsx +241 -0
- package/src/MultiLevelMenu/common.ts +18 -0
- package/src/MultiLevelMenu/index.tsx +89 -0
- package/src/MultiLevelMenu/types.ts +16 -0
- package/src/index.ts +4 -0
- package/tsconfig.jest.json +10 -0
- package/tsup.config.ts +16 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import Menu from '@mui/material/Menu';
|
|
2
|
+
import Divider from '@mui/material/Divider';
|
|
3
|
+
import MenuItem from '@mui/material/MenuItem';
|
|
4
|
+
import React, { useId } from 'react';
|
|
5
|
+
import { Typography } from '@mui/material';
|
|
6
|
+
import { NestedMenuItem } from './NestedMenuItem';
|
|
7
|
+
import type { MultiLevelMenuItem } from './types';
|
|
8
|
+
import { MenuItemContent, transitionConfig } from './common';
|
|
9
|
+
|
|
10
|
+
type Props = {
|
|
11
|
+
anchorEl: null | HTMLElement;
|
|
12
|
+
open: boolean;
|
|
13
|
+
onClose: () => void;
|
|
14
|
+
items: MultiLevelMenuItem[];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function MultiLevelMenu({ anchorEl, open, onClose, items }: Props) {
|
|
18
|
+
const generatedMenuId = useId();
|
|
19
|
+
|
|
20
|
+
const renderedMenuEntries = items.map((item, index) => {
|
|
21
|
+
if (item.type === 'divider') {
|
|
22
|
+
return <Divider key={`divider-${index}`} />;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const {
|
|
26
|
+
type: _,
|
|
27
|
+
items: nestedItems,
|
|
28
|
+
startIcon: StartIconComponent,
|
|
29
|
+
endIcon: EndIconComponent,
|
|
30
|
+
label,
|
|
31
|
+
onClick,
|
|
32
|
+
id,
|
|
33
|
+
...menuItemProps
|
|
34
|
+
} = item;
|
|
35
|
+
const entryId = id ?? `${generatedMenuId}-entry-${index}`;
|
|
36
|
+
const itemKey = `menu-item-id:${entryId}`;
|
|
37
|
+
const displayLabel = label ?? entryId;
|
|
38
|
+
|
|
39
|
+
if (nestedItems && nestedItems.length > 0) {
|
|
40
|
+
return (
|
|
41
|
+
<NestedMenuItem
|
|
42
|
+
key={itemKey}
|
|
43
|
+
id={entryId}
|
|
44
|
+
label={displayLabel}
|
|
45
|
+
startIcon={StartIconComponent}
|
|
46
|
+
endIcon={EndIconComponent}
|
|
47
|
+
parentMenuClose={onClose}
|
|
48
|
+
items={nestedItems}
|
|
49
|
+
/>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const handleItemClick = (event: React.MouseEvent<HTMLLIElement>) => {
|
|
54
|
+
onClick?.(event);
|
|
55
|
+
onClose();
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<MenuItem key={itemKey} onClick={handleItemClick} {...menuItemProps}>
|
|
60
|
+
<MenuItemContent>
|
|
61
|
+
{StartIconComponent ? <StartIconComponent /> : null}
|
|
62
|
+
<Typography sx={{ flex: 1 }}>{displayLabel}</Typography>
|
|
63
|
+
{EndIconComponent ? <EndIconComponent /> : null}
|
|
64
|
+
</MenuItemContent>
|
|
65
|
+
</MenuItem>
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<Menu
|
|
71
|
+
data-testid='root-menu'
|
|
72
|
+
anchorEl={anchorEl}
|
|
73
|
+
open={open}
|
|
74
|
+
onClose={onClose}
|
|
75
|
+
onKeyDown={e => {
|
|
76
|
+
if (e.key === 'Escape' && open) onClose();
|
|
77
|
+
}}
|
|
78
|
+
transitionDuration={transitionConfig.timeout}
|
|
79
|
+
slots={{ transition: transitionConfig.type }}
|
|
80
|
+
slotProps={{
|
|
81
|
+
list: {
|
|
82
|
+
'aria-labelledby': 'icon-menu-button',
|
|
83
|
+
},
|
|
84
|
+
}}
|
|
85
|
+
>
|
|
86
|
+
{renderedMenuEntries}
|
|
87
|
+
</Menu>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { MenuItemProps } from '@mui/material/MenuItem';
|
|
3
|
+
import type { SvgIconComponent } from '@mui/icons-material';
|
|
4
|
+
|
|
5
|
+
export type MultiLevelMenuItemBase =
|
|
6
|
+
| { type: 'divider' }
|
|
7
|
+
| {
|
|
8
|
+
type?: 'item';
|
|
9
|
+
id?: string;
|
|
10
|
+
label: ReactNode;
|
|
11
|
+
startIcon?: SvgIconComponent;
|
|
12
|
+
endIcon?: SvgIconComponent;
|
|
13
|
+
items?: MultiLevelMenuItem[];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type MultiLevelMenuItem = MultiLevelMenuItemBase & Omit<MenuItemProps, 'children'>;
|
package/src/index.ts
ADDED
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { defineConfig } from 'tsup'
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
entry: ['src/index.ts'],
|
|
5
|
+
format: ['esm', 'cjs'],
|
|
6
|
+
dts: true,
|
|
7
|
+
clean: true,
|
|
8
|
+
sourcemap: true,
|
|
9
|
+
external: [
|
|
10
|
+
'react',
|
|
11
|
+
'react-dom',
|
|
12
|
+
'@mui/material',
|
|
13
|
+
'@emotion/react',
|
|
14
|
+
'@emotion/styled'
|
|
15
|
+
]
|
|
16
|
+
})
|