Skip to content

Commit 44c092d

Browse files
committed
chore: cleaning up docs and comments
1 parent d1d1e02 commit 44c092d

File tree

3 files changed

+27
-107
lines changed

3 files changed

+27
-107
lines changed

client/components/Menubar/Menubar.jsx

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,12 @@ import { MenuOpenContext, MenubarContext } from './contexts';
1111
import usePrevious from '../../common/usePrevious';
1212

1313
/**
14-
* @component
15-
* @param {object} props
14+
* Menubar manages a collection of menu items and their submenus. It provides keyboard navigation,
15+
* focus and state management, and other accessibility features for the menu items and submenus.
16+
*
1617
* @param {React.ReactNode} props.children - Menu items that will be rendered in the menubar
1718
* @param {string} [props.className='nav__menubar'] - CSS class name to apply to the menubar
1819
* @returns {JSX.Element}
19-
*/
20-
21-
/**
22-
* Menubar manages a collection of menu items and their submenus. It provides keyboard navigation,
23-
* focus and state management, and other accessibility features for the menu items and submenus.
2420
*
2521
* @example
2622
* <Menubar>
@@ -31,20 +27,16 @@ import usePrevious from '../../common/usePrevious';
3127
*/
3228

3329
function Menubar({ children, className }) {
34-
// core state for menu management
3530
const [menuOpen, setMenuOpen] = useState('none');
3631
const [activeIndex, setActiveIndex] = useState(0);
3732
const prevIndex = usePrevious(activeIndex);
3833
const [hasFocus, setHasFocus] = useState(false);
3934

40-
// refs for menu items and their ids
4135
const menuItems = useRef(new Set()).current;
4236
const menuItemToId = useRef(new Map()).current;
4337

44-
// ref for hiding submenus
4538
const timerRef = useRef(null);
4639

47-
// get the id of a menu item by its index
4840
const getMenuId = useCallback(
4941
(index) => {
5042
const items = Array.from(menuItems);
@@ -54,9 +46,6 @@ function Menubar({ children, className }) {
5446
[menuItems, menuItemToId, activeIndex]
5547
);
5648

57-
/**
58-
* navigation functions
59-
*/
6049
const prev = useCallback(() => {
6150
const newIndex = (activeIndex - 1 + menuItems.size) % menuItems.size;
6251
setActiveIndex(newIndex);
@@ -85,8 +74,6 @@ function Menubar({ children, className }) {
8574
setActiveIndex(menuItems.size - 1);
8675
}, []);
8776

88-
// closes the menu and returns focus to the active menu item
89-
// is called on Escape key press
9077
const close = useCallback(() => {
9178
if (menuOpen === 'none') return;
9279

@@ -96,27 +83,17 @@ function Menubar({ children, className }) {
9683
activeNode.focus();
9784
}, [activeIndex, menuItems, menuOpen]);
9885

99-
// toggle the open state of a submenu
10086
const toggleMenuOpen = useCallback((id) => {
10187
setMenuOpen((prevState) => (prevState === id ? 'none' : id));
10288
});
10389

104-
/**
105-
* Register top level menu items. Stores both the DOM node and the id of the submenu.
106-
* Access to the DOM node is needed for focus management and tabindex control,
107-
* while the id is needed to toggle the submenu open and closed.
108-
*
109-
* @param {React.RefObject} ref - a ref to the DOM node of the menu item
110-
* @param {string} submenuId - the id of the submenu that the menu item opens
111-
*
112-
*/
11390
const registerTopLevelItem = useCallback(
11491
(ref, submenuId) => {
11592
const menuItemNode = ref.current;
11693

11794
if (menuItemNode) {
11895
menuItems.add(menuItemNode);
119-
menuItemToId.set(menuItemNode, submenuId); // store the id of the submenu
96+
menuItemToId.set(menuItemNode, submenuId);
12097
}
12198

12299
return () => {
@@ -127,9 +104,6 @@ function Menubar({ children, className }) {
127104
[menuItems, menuItemToId]
128105
);
129106

130-
/**
131-
* focus and blur management
132-
*/
133107
const clearHideTimeout = useCallback(() => {
134108
if (timerRef.current) {
135109
clearTimeout(timerRef.current);
@@ -164,7 +138,6 @@ function Menubar({ children, className }) {
164138
[nodeRef]
165139
);
166140

167-
// keyboard navigation
168141
const keyHandlers = {
169142
ArrowLeft: (e) => {
170143
e.preventDefault();
@@ -183,7 +156,6 @@ function Menubar({ children, className }) {
183156
},
184157
Tab: (e) => {
185158
e.stopPropagation();
186-
// close
187159
},
188160
Home: (e) => {
189161
e.preventDefault();
@@ -195,17 +167,15 @@ function Menubar({ children, className }) {
195167
e.stopPropagation();
196168
last();
197169
}
198-
// to do: support direct access keys
170+
// TO DO: support direct access keys
199171
};
200172

201-
// focus the active menu item and set its tabindex
202173
useEffect(() => {
203174
if (activeIndex !== prevIndex) {
204175
const items = Array.from(menuItems);
205176
const activeNode = items[activeIndex];
206177
const prevNode = items[prevIndex];
207178

208-
// roving tabindex
209179
prevNode?.setAttribute('tabindex', '-1');
210180
activeNode?.setAttribute('tabindex', '0');
211181

@@ -219,7 +189,6 @@ function Menubar({ children, className }) {
219189
clearHideTimeout();
220190
}, [clearHideTimeout]);
221191

222-
// context value for dropdowns and menu items
223192
const contextValue = useMemo(
224193
() => ({
225194
createMenuHandlers: (menu) => ({

client/components/Menubar/MenubarItem.jsx

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,26 @@ import { MenubarContext, SubmenuContext, ParentMenuContext } from './contexts';
44
import ButtonOrLink from '../../common/ButtonOrLink';
55

66
/**
7+
* MenubarItem wraps a button or link in an accessible list item that
8+
* integrates with keyboard navigation and other submenu behaviors.
9+
*
10+
* TO DO: how to document props passed through spread operator?
711
* @component
812
* @param {object} props
913
* @param {string} [props.className='nav__dropdown-item'] - CSS class name to apply to the list item
1014
* @param {string} props.id - The id of the list item
1115
* @param {string} [props.role='menuitem'] - The role of the list item
12-
* @param {boolean} [props.isDisabled=false] - Whether to hide the item
16+
* @param {boolean} [props.isDisabled=false] - Whether to disable the item
1317
* @param {boolean} [props.selected=false] - Whether the item is selected
1418
* @returns {JSX.Element}
15-
*/
16-
17-
/**
18-
* MenubarItem wraps a button or link in an accessible list item that
19-
* integrates with keyboard navigation and other submenu behaviors.
2019
*
21-
* @example
22-
* ```jsx
23-
* // basic MenubarItem with click handler and keyboard shortcut
20+
* @example <caption>Basic MenubarItem with click handler and keyboard shortcut</caption>
2421
* <MenubarItem id="sketch-run" onClick={() => dispatch(startSketch())}>
2522
* Run
2623
* <span className="nav__keyboard-shortcut">{metaKeyName}+Enter</span>
2724
* </MenubarItem>
2825
*
29-
* // as an option in a listbox
26+
* @example <caption>as an option in a listbox</caption>
3027
* <MenubarItem
3128
* id={key}
3229
* key={key}
@@ -37,7 +34,6 @@ import ButtonOrLink from '../../common/ButtonOrLink';
3734
* >
3835
* {languageKeyToLabel(key)}
3936
* </MenubarItem>
40-
* ```
4137
*/
4238

4339
function MenubarItem({
@@ -48,7 +44,6 @@ function MenubarItem({
4844
selected,
4945
...rest
5046
}) {
51-
// core context and state management
5247
const { createMenuItemHandlers, hasFocus } = useContext(MenubarContext);
5348
const {
5449
setSubmenuActiveIndex,
@@ -57,17 +52,13 @@ function MenubarItem({
5752
} = useContext(SubmenuContext);
5853
const parent = useContext(ParentMenuContext);
5954

60-
// ref for the list item
6155
const menuItemRef = useRef(null);
6256

63-
// handlers from parent menu
6457
const handlers = createMenuItemHandlers(parent);
6558

66-
// role and aria-selected
6759
const role = customRole || 'menuitem';
6860
const ariaSelected = role === 'option' ? { 'aria-selected': selected } : {};
6961

70-
// focus submenu item on mouse enter
7162
const handleMouseEnter = () => {
7263
if (hasFocus) {
7364
const items = Array.from(submenuItems);
@@ -78,7 +69,6 @@ function MenubarItem({
7869
}
7970
};
8071

81-
// register with parent submenu for keyboard navigation
8272
useEffect(() => {
8373
const unregister = registerSubmenuItem(menuItemRef);
8474
return unregister;
@@ -107,25 +97,21 @@ function MenubarItem({
10797

10898
MenubarItem.propTypes = {
10999
...ButtonOrLink.propTypes,
100+
className: PropTypes.string,
110101
id: PropTypes.string,
111-
onClick: PropTypes.func,
112-
value: PropTypes.string,
113102
/**
114103
* Provides a way to deal with optional items.
115104
*/
116-
isDisabled: PropTypes.bool,
117-
className: PropTypes.string,
118105
role: PropTypes.oneOf(['menuitem', 'option']),
106+
isDisabled: PropTypes.bool,
119107
selected: PropTypes.bool
120108
};
121109

122110
MenubarItem.defaultProps = {
123-
onClick: null,
124-
value: null,
125-
isDisabled: false,
126111
className: 'nav__dropdown-item',
127-
role: 'menuitem',
128112
id: undefined,
113+
role: 'menuitem',
114+
isDisabled: false,
129115
selected: false
130116
};
131117

0 commit comments

Comments
 (0)