Skip to content

Commit d393d77

Browse files
committed
chore: addressing code mistakes - replaced needless state, brought back about menu icon style
1 parent 4aac3b7 commit d393d77

4 files changed

Lines changed: 22 additions & 19 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.about-icon {
2+
height: 1.25rem;
3+
margin: 0.5rem;
4+
vertical-align: middle;
5+
}

packages/scratch-gui/src/components/menu-bar/preference-menu.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const PreferenceItem = props => {
2626
onClick={props.onClick}
2727
onParentKeyDown={props.onParentKeyDown}
2828
isSelected={props.isSelected}
29-
{...props}
29+
isDataMenuItem={props.isDataMenuItem}
3030
>
3131
<div className={styles.option}>
3232
<img
@@ -49,7 +49,8 @@ PreferenceItem.propTypes = {
4949
icon: PropTypes.string,
5050
label: intlMessageShape.isRequired
5151
}),
52-
onParentKeyDown: PropTypes.func
52+
onParentKeyDown: PropTypes.func,
53+
isDataMenuItem: PropTypes.bool
5354
};
5455

5556
const PreferenceMenu = ({

packages/scratch-gui/src/components/menu-bar/settings-menu.jsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ const SettingsMenu = ({
7979
onClick={handleOnOpen}
8080
onKeyDown={handleKeyDown}
8181
ref={menuRef}
82-
data-menu-item
83-
data-menu-item-wrapper
8482
>
8583
<img src={settingsIcon} />
8684
<span className={styles.dropdownLabel}>
@@ -98,7 +96,7 @@ const SettingsMenu = ({
9896
onRequestClose={handleOnClose}
9997
>
10098
<MenuSection>
101-
{canChangeLanguage && <LanguageMenu depth={2} />}
99+
{canChangeLanguage && <LanguageMenu depth={depth + 1} />}
102100
{canChangeTheme &&
103101
// TODO: Consider always showing the theme menu, even if there is a single available theme
104102
availableThemesLength > 1 &&
@@ -113,7 +111,7 @@ const SettingsMenu = ({
113111
}}
114112
selectedItemKey={activeTheme}
115113
isRtl={isRtl}
116-
depth={2}
114+
depth={depth + 1}
117115
/>}
118116
{canChangeColorMode && <PreferenceMenu
119117
itemsMap={enabledColorModesMap}
@@ -125,7 +123,7 @@ const SettingsMenu = ({
125123
}}
126124
selectedItemKey={activeColorMode}
127125
isRtl={isRtl}
128-
depth={2}
126+
depth={depth + 1}
129127
/>}
130128
</MenuSection>
131129
</MenuBarMenu>

packages/scratch-gui/src/hooks/use-menu-navigation.jsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useCallback, useContext, useState, useRef} from 'react';
1+
import {useCallback, useContext, useRef} from 'react';
22
import {MenuRefContext} from '../contexts/menu-ref-context';
33
import {KEY} from '../lib/navigation-keys';
44

@@ -25,7 +25,7 @@ const MENU_ITEM_WRAPPER_SELECTOR = '[data-menu-item-wrapper="true"]';
2525
* - onClick={handleOnOpen}
2626
* - ref={menuRef}
2727
* - onKeyDown={handleKeyDown}
28-
* - tabIndex={0} or {-1} depending on the kind of focusability we want
28+
* - Make sure the element is focusable
2929
* - aria-expanded={isExpanded()} (and use it wherever else needed)
3030
* - for menu items pass onKeyDown={handleKeyDownOpenMenu}
3131
* 2. For the sake of consistent code structure, it is recommended for the core menus (depth 1)
@@ -56,7 +56,6 @@ export default function useMenuNavigation ({
5656
}) {
5757
const menuRef = useRef(null);
5858
const menuContext = useContext(MenuRefContext);
59-
const [focusedItem, setFocusedItem] = useState(null);
6059

6160
// BFS to find first children with attribute
6261
const findDirectSubitems = useCallback(() => {
@@ -70,8 +69,8 @@ export default function useMenuNavigation ({
7069
const element = children.shift();
7170
if (element.matches(MENU_ITEM_SELECTOR)) {
7271
// Skip original starting element if we went back to the wrapper
73-
if (!(menuRef.current.matches(MENU_ITEM_WRAPPER_SELECTOR) &&
74-
Array.from(menuRef.current.children).includes(element))) {
72+
if (!(root.matches(MENU_ITEM_WRAPPER_SELECTOR) &&
73+
Array.from(root.children).includes(element))) {
7574
directSubitems.push([element, element]);
7675
}
7776
continue;
@@ -109,7 +108,6 @@ export default function useMenuNavigation ({
109108
const focusItem = useCallback(item => {
110109
if (item) {
111110
item.focus();
112-
setFocusedItem(item);
113111
}
114112
}, []);
115113

@@ -126,7 +124,6 @@ export default function useMenuNavigation ({
126124
}, [menuContext, menuRef, depth, defaultIndexOnOpen]);
127125

128126
const handleOnClose = useCallback(() => {
129-
setFocusedItem(null);
130127
menuContext.closeMenuByRef(menuRef);
131128
menuRef?.current?.focus();
132129
}, [menuContext, menuRef]);
@@ -135,25 +132,28 @@ export default function useMenuNavigation ({
135132
const items = findDirectSubitemsFocusable();
136133
if (!items.length) return;
137134

138-
const currentIndex = items.indexOf(focusedItem);
135+
const currentIndex = items.indexOf(document.activeElement);
139136
const nextIndex = (currentIndex + direction + items.length) % items.length;
140137
focusItem(items[nextIndex]);
141-
}, [focusedItem, menuRef, focusItem]);
138+
}, [menuRef, focusItem]);
142139

143140
const handleKeyDownOpenMenu = useCallback(e => {
144141
// Logic for vertical menus, will need to change when implementing for horizontal
145142
switch (e.key) {
146143
case KEY.ARROW_DOWN:
147144
e.preventDefault();
145+
e.stopPropagation();
148146
handleMove(1);
149147
break;
150148
case KEY.ARROW_UP:
151149
e.preventDefault();
150+
e.stopPropagation();
152151
handleMove(-1);
153152
break;
154153
case KEY.ESCAPE:
155154
case KEY.ARROW_LEFT:
156155
e.preventDefault();
156+
e.stopPropagation();
157157
handleOnClose();
158158
break;
159159
case KEY.ENTER:
@@ -162,8 +162,8 @@ export default function useMenuNavigation ({
162162
{
163163
const focusableItems = findDirectSubitemsFocusable();
164164
const clickableItems = findDirectSubitemsClickable();
165-
166-
const index = focusableItems.indexOf(focusedItem);
165+
166+
const index = focusableItems.indexOf(document.activeElement);
167167
if (index >= 0 && clickableItems[index]) {
168168
clickableItems[index].click();
169169
}
@@ -199,7 +199,6 @@ export default function useMenuNavigation ({
199199

200200
return {
201201
menuRef,
202-
focusedItem,
203202
isExpanded,
204203
handleKeyDown,
205204
handleKeyDownOpenMenu,

0 commit comments

Comments
 (0)