-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathMenu.tsx
More file actions
160 lines (144 loc) · 4.56 KB
/
Menu.tsx
File metadata and controls
160 lines (144 loc) · 4.56 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
import { Popover } from '@hypothesis/frontend-shared';
import { MenuExpandIcon } from '@hypothesis/frontend-shared';
import CloseableContext from '@hypothesis/frontend-shared/lib/components/CloseableContext';
import classnames from 'classnames';
import type { ComponentChildren } from 'preact';
import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
import MenuKeyboardNavigation from './MenuKeyboardNavigation';
export type MenuProps = {
/**
* Whether the menu content is aligned with the left (default) or right edges
* of the toggle element.
*/
align?: 'left' | 'right';
/**
* Label element or string for the toggle button that hides and shows the menu
*/
label: ComponentChildren;
/** Menu content, typically `MenuSection` and `MenuItem` components */
children: ComponentChildren;
/** Additional CSS classes to apply to the Menu */
contentClass?: string;
/**
* Whether the menu is open when initially rendered. Ignored if `open` is
* present.
*/
defaultOpen?: boolean;
disabled?: boolean;
/** Whether to render an (arrow) indicator next to the Menu label */
menuIndicator?: boolean;
/** Callback when the Menu is opened or closed. */
onOpenChanged?: (open: boolean) => void;
/**
* Whether the Menu is currently open, when the Menu is being used as a
* controlled component. In these cases, an `onOpenChanged` handler should
* be provided to respond to the user opening or closing the menu.
*/
open?: boolean;
/**
* A title for the menu. This is important for accessibility if the menu's
* toggle button has only an icon as a label.
*/
title: string;
};
const noop = () => {};
/**
* A drop-down menu.
*
* Menus consist of a button which toggles whether the menu is open, an
* an arrow indicating the state of the menu and content when is shown when
* the menu is open. The children of the menu component are rendered as the
* content of the menu when open. Typically this consists of a list of
* `MenuSection` and/or `MenuItem` components.
*
* @example
* <Menu label="Preferences">
* <MenuItem label="View" onClick={showViewSettings}/>
* <MenuItem label="Theme" onClick={showThemeSettings}/>
* <MenuSection>
* <MenuItem label="Log out"/>
* </MenuSection>
* </Menu>
*/
export default function Menu({
align = 'left',
children,
contentClass,
defaultOpen = false,
disabled = false,
label,
open,
onOpenChanged,
menuIndicator = true,
title,
}: MenuProps) {
let [isOpen, setOpen]: [boolean, (open: boolean) => void] =
useState(defaultOpen);
if (typeof open === 'boolean') {
isOpen = open;
setOpen = onOpenChanged || noop;
}
// Notify parent when menu is opened or closed.
const wasOpen = useRef(isOpen);
useEffect(() => {
if (typeof onOpenChanged === 'function' && wasOpen.current !== isOpen) {
wasOpen.current = isOpen;
onOpenChanged(isOpen);
}
}, [isOpen, onOpenChanged]);
const toggleMenu = () => setOpen(!isOpen);
const closeMenu = useCallback(() => setOpen(false), [setOpen]);
const buttonRef = useRef<HTMLButtonElement | null>(null);
return (
<>
<button
aria-expanded={isOpen ? 'true' : 'false'}
aria-haspopup={true}
className={classnames(
'focus-visible-ring',
'flex items-center justify-center rounded transition-colors',
{
'text-grey-7 hover:text-grey-9': !isOpen,
'text-brand': isOpen,
},
)}
data-testid="menu-toggle-button"
disabled={disabled}
onClick={toggleMenu}
aria-label={title}
title={title}
ref={buttonRef}
>
<span
// wrapper is needed to serve as the flex layout for the label and indicator content.
className="flex items-center gap-x-1"
>
{label}
{menuIndicator && (
<span
className={classnames({
'rotate-180 text-color-text': isOpen,
})}
>
<MenuExpandIcon className="w-2.5 h-2.5" />
</span>
)}
</span>
</button>
<Popover
open={isOpen}
onClose={closeMenu}
anchorElementRef={buttonRef}
align={align}
classes={classnames(
'!max-h-full text-md !shadow-intense !rounded-lg',
contentClass,
)}
>
<CloseableContext.Provider value={{ onClose: closeMenu }}>
<MenuKeyboardNavigation visible>{children}</MenuKeyboardNavigation>
</CloseableContext.Provider>
</Popover>
</>
);
}