-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.tsx
More file actions
224 lines (208 loc) · 7.63 KB
/
Menu.tsx
File metadata and controls
224 lines (208 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { isValidElement, ReactNode, useEffect, useState } from 'react';
import useHotkeys from '../../hooks/useHotKeys';
export type MenuItemType<T> =
| { separator: true }
| {
name?: string;
separator?: false;
disabled?: (target: T) => boolean;
isTitle?: (target: T) => boolean;
icon?: React.ForwardRefExoticComponent<{ size?: number | string }>;
keyboardShortcutOptions?: {
keyboardShortcutIcon?: React.ForwardRefExoticComponent<{ size?: number | string }>;
keyboardShortcutText?: string;
};
action?: (target: T) => void | Promise<void>;
onClick?: () => void;
node?: ReactNode;
};
export type MenuItemsType<T> = Array<MenuItemType<T>>;
export interface MenuProps<T> {
item?: T;
isOpen: boolean;
menu?: MenuItemsType<T>;
handleMenuClose: () => void;
genericEnterKey?: () => void;
paddingX?: string;
paddingY?: string;
}
/**
* Menu component
*
* @template T
* @param {MenuProps<T>} props - Properties of the Menu component.
*
* @property {T} [item]
* - Optional item that may be used in menu actions (e.g., data passed for actions).
*
* @property {boolean} [isOpen]
* - To know is Menu is visible.
*
* @property {MenuItemsType<T>} [menu]
* - Optional array of menu items. Each item can define a separator, title, icon, action, etc.
*
* @property {() => void} handleMenuClose
* - Function to close the menu.
*
* @property {() => void} [genericEnterKey]
* - Optional callback for when the Enter key is pressed without selecting a menu item.
*
* @property {string} [paddingX='px-4']
* - Optional padding for the X axis (horizontal) of each menu item. Defaults to `px-4`.
*
* @property {string} [paddingY='py-1.5']
* - Optional padding for the Y axis (vertical) of each menu item. Defaults to `py-1.5`.
*
* @returns {JSX.Element}
* - The rendered Menu component.
*
* The component handles key events such as Arrow Down, Arrow Up, and Enter for navigation and action execution.
* It also supports mouse interactions for hovering and selecting menu items.
*
* Each menu item can have an action, an optional keyboard shortcut, and an icon.
* If the item is disabled or marked as a title, it won't be clickable.
*
* It features a dynamic index for item selection, with keyboard and mouse-based navigation.
*/
const Menu = <T,>({
item,
menu,
isOpen,
genericEnterKey,
handleMenuClose,
paddingX = 'px-4',
paddingY = 'py-1.5',
}: MenuProps<T>): JSX.Element => {
const [selectedIndex, setSelectedIndex] = useState<number | null>(null);
const [enterPressed, setEnterPressed] = useState<boolean>(false);
const handleMouseEnter = (index: number) => {
setSelectedIndex(index);
};
const isUnClickableItem = (menuItem: MenuItemType<T>) =>
menuItem?.separator || (item && menuItem?.disabled?.(item)) || (item && menuItem?.isTitle?.(item));
const handleArrowDown = () => {
menu &&
isOpen &&
setSelectedIndex((prevIndex) => {
const getNextEnabledIndex = (startIndex: number): number => {
let newIndex = startIndex;
let menuItem = menu[newIndex];
while (isUnClickableItem(menuItem)) {
newIndex = (newIndex + 1) % menu.length;
menuItem = menu[newIndex];
if (startIndex === newIndex) break;
}
return newIndex;
};
const newCurrentIndex = prevIndex === null ? 0 : (prevIndex + 1) % menu.length;
const newIndex = getNextEnabledIndex(newCurrentIndex);
const menuItem = menu[newIndex];
return isUnClickableItem(menuItem) ? null : newIndex;
});
};
const handleArrowUp = () => {
menu &&
isOpen &&
setSelectedIndex((prevIndex) => {
const getPreviousEnabledIndex = (startIndex: number): number => {
let newIndex = startIndex;
let menuItem = menu[newIndex];
while (isUnClickableItem(menuItem)) {
newIndex = (newIndex - 1 + menu.length) % menu.length;
menuItem = menu[newIndex];
if (startIndex === newIndex) break;
}
return newIndex;
};
const newCurrentIndex = prevIndex === null ? menu.length - 1 : (prevIndex - 1 + menu.length) % menu.length;
const newIndex = getPreviousEnabledIndex(newCurrentIndex);
const menuItem = menu[newIndex];
return isUnClickableItem(menuItem) ? null : newIndex;
});
};
const handleEnterKey = () => {
menu &&
isOpen &&
setSelectedIndex((prevIndex) => {
if (prevIndex !== null) {
const menuItem = menu ? menu[prevIndex] : undefined;
if (item && menuItem && 'action' in menuItem && menuItem.action) menuItem.action(item);
if (item && menuItem && 'node' in menuItem && menuItem.node && isValidElement(menuItem.node)) {
const onClick = menuItem.node.props.onClick;
onClick && onClick();
}
} else if (genericEnterKey) genericEnterKey();
setEnterPressed(true);
return null;
});
};
useEffect(() => {
if (enterPressed) {
handleMenuClose();
setEnterPressed(false);
}
}, [enterPressed]);
useHotkeys(
{
arrowdown: handleArrowDown,
arrowup: handleArrowUp,
enter: handleEnterKey,
},
[isOpen],
);
return (
<div className={`z-20 mt-0 flex flex-col rounded-lg bg-surface ${paddingY} outline-none dark:bg-gray-5`}>
{menu?.map((option, i) => (
<div key={i}>
{option && option.separator ? (
<div className="my-0.5 flex w-full flex-row px-4">
<div className="h-px w-full bg-gray-10" />
</div>
) : (
option && (
<div
className={`${isUnClickableItem(option) ? 'pointer-events-none' : ''}`}
onClick={(e) => {
if (!isUnClickableItem(option)) {
e.stopPropagation();
item && option.action?.(item);
option.onClick && option.onClick();
handleMenuClose();
}
}}
onMouseEnter={() => handleMouseEnter(i)}
>
<div
className={`flex cursor-pointer flex-row whitespace-nowrap ${paddingX} ${paddingY} text-base
${item && option.disabled?.(item) && 'font-medium text-gray-50'}
${item && option.isTitle?.(item) && !option.disabled?.(item) && 'font-medium text-gray-100'}
${selectedIndex === i && item && !option.disabled?.(item) && 'bg-gray-5 text-gray-100 dark:bg-gray-10'}
${item && !option.disabled?.(item) && !option.isTitle?.(item) && selectedIndex !== i && 'text-gray-80'}
`}
>
{option.node ? (
option.node
) : (
<div className="flex flex-row items-center space-x-2">
{option.icon && <option.icon size={20} />}
<span>{option.name}</span>
</div>
)}
{option.keyboardShortcutOptions && (
<span className="ml-5 flex grow items-center justify-end text-sm text-gray-40">
{option.keyboardShortcutOptions?.keyboardShortcutIcon && (
<option.keyboardShortcutOptions.keyboardShortcutIcon size={14} />
)}
{option.keyboardShortcutOptions?.keyboardShortcutText ?? ''}
</span>
)}
</div>
</div>
)
)}
</div>
))}
</div>
);
};
export default Menu;