-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathDropdownMenu.tsx
More file actions
169 lines (164 loc) · 6.46 KB
/
Copy pathDropdownMenu.tsx
File metadata and controls
169 lines (164 loc) · 6.46 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
import React, { useId, useRef, useState } from 'react';
import {
ButtonBase,
Grid2 as Grid,
MenuList,
Popper,
ClickAwayListener,
MenuListProps,
ButtonBaseProps,
PopperProps,
} from '@mui/material';
import TrapFocus from '@mui/material/Unstable_TrapFocus';
import { colors } from 'styles/Theme';
import { BodyText } from 'components/common/Typography/Body';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faChevronDown } from '@fortawesome/pro-regular-svg-icons';
import { When, Unless } from 'react-if';
import { elevations } from 'components/common';
export const dropdownMenuStyles = {
padding: '1rem',
borderRadius: '8px',
border: '1px solid transparent',
'&:hover': { backgroundColor: colors.surface.blue[80] },
'&:focus-visible': {
backgroundColor: colors.surface.blue[80],
border: '1px dashed white',
},
};
export const ToggleNav = ({ isNav, children }: { isNav?: boolean; children: React.ReactNode }) => {
if (isNav) {
return <nav>{children}</nav>;
}
return <>{children}</>;
};
/**
* A dropdown menu component that expands on click and allows for navigation or selection.
* It supports custom button content, click-away behavior, and focus trapping.
*
* @param {MenuListProps} [props] - Additional properties for the MenuList component.
* @param {string} [props.name] - The name of the dropdown menu, used for accessibility.
* @param {boolean} [props.forNavigation=false] - If true, wraps the menu in a <nav> element for accessibility.
* @param {function} [props.renderButtonContent] - A function that returns custom content for the dropdown button.
* @param {ButtonBaseProps} [props.buttonProps] - Additional properties for the button.
* @param {PopperProps} [props.popperProps] - Additional properties for the Popper component.
* @param {React.ReactNode} [props.children] - The menu items to display within the dropdown.
* @returns {JSX.Element} A dropdown menu component that can be used for navigation or selection.
* @example
* <DropdownMenu
* name="Options"
* renderButtonContent={({ isOpen }) => (
* <span>{isOpen ? 'Close Menu' : 'Open Menu'}</span>
* )}
* buttonProps={{ color: 'primary' }}
* popperProps={{ placement: 'bottom-end' }}
* >
* <MenuItem onClick={() => globalThis.location.href = '/option1'}>
* Option 1
* </MenuItem>
* <MenuItem onClick={() => globalThis.location.href = '/option2'}>
* Option 2
* </MenuItem>
* </DropdownMenu>
* */
export const DropdownMenu = ({
name,
forNavigation,
renderButtonContent,
buttonProps,
children,
popperProps,
...props
}: {
name?: string;
forNavigation?: boolean;
renderButtonContent?: ({ isOpen }: { isOpen: boolean }) => React.ReactNode;
buttonProps?: ButtonBaseProps;
popperProps?: Partial<PopperProps>;
children?: React.ReactNode;
} & MenuListProps) => {
const [open, setOpen] = useState(false);
const buttonRef = useRef<HTMLButtonElement>(null);
const dropdownId = useId();
return (
<>
{/* Dropdown Button */}
<ButtonBase
ref={buttonRef}
aria-haspopup
aria-controls={open ? dropdownId : undefined}
aria-expanded={open}
aria-label={name || 'Dropdown Menu'}
onClick={() => {
setOpen(!open);
}}
{...buttonProps}
sx={{
...dropdownMenuStyles,
...buttonProps?.sx,
}}
>
{/* Pass the "open" state to the button contents in case they want to change based on dropdown state */}
<Unless condition={renderButtonContent === undefined}>{renderButtonContent?.({ isOpen: open })}</Unless>
{/* A basic button label if no custom content is provided */}
<When condition={renderButtonContent === undefined}>
<Grid container direction="row" alignItems="center" spacing={1}>
<Grid>
<BodyText sx={{ userSelect: 'none', textTransform: 'capitalize' }}>{name}</BodyText>
</Grid>
<Grid hidden={!children}>
<FontAwesomeIcon color="white" icon={faChevronDown} rotation={open ? 180 : undefined} />
</Grid>
</Grid>
</When>
</ButtonBase>
<ClickAwayListener
mouseEvent="onMouseUp"
onClickAway={(e) => {
if (e.target !== buttonRef.current && !buttonRef.current?.contains(e.target as Node))
setOpen(false);
}}
>
{/* Dropdown Contents */}
<Popper
onKeyDown={(e) => {
// listen for escape key to close menu
if (e.key === 'Escape') setOpen(false);
}}
id={dropdownId}
anchorEl={buttonRef.current}
placement="bottom-start"
modifiers={[{ name: 'offset', options: { offset: [0, 8] } }]}
open={open}
{...popperProps}
sx={{
zIndex: 10000,
boxShadow: elevations.default,
backgroundColor: colors.surface.blue[90],
padding: '2px',
borderRadius: '16px',
minWidth: 'fit-content',
width: buttonRef.current?.offsetWidth,
...popperProps?.sx,
}}
>
<ToggleNav isNav={forNavigation}>
<TrapFocus open={open}>
<MenuList
sx={{ minWidth: 'fit-content' }}
aria-label={name || 'Dropdown Menu'}
tabIndex={-1}
aria-expanded={open}
autoFocusItem
{...props}
>
{children}
</MenuList>
</TrapFocus>
</ToggleNav>
</Popper>
</ClickAwayListener>
</>
);
};
export default DropdownMenu;