Skip to content

Conversation

@kbangelov
Copy link
Contributor

@kbangelov kbangelov commented Dec 19, 2025

Aims to resolve UEPR-445, not before clarifying things.

https://scratchfoundation.atlassian.net/browse/UEPR-445

Proposed Changes

  1. Added tabIndex=0 to the elements in nav bar so they can be focused with tab
  2. Made a context (menu-ref-context) that takes care of tracking which menus and submenus are currently open via their refs. All of the logic with the "isOpen" type of props has been replaced for these menus.
  3. Made a base-menu to be extended by the menus that handles common key pressing, opening, closing logic and more.
  4. Brought out the menu logic in their respective files for the purpose of making them work via base-menu and also for code clarity.

To be discussed additionally

  • I remain uncertain whether the menus should collapse when the innermost items are clicked and which ones. For now they just keep their current behavior.
  • At a couple of places in code when I was making new react elements to replace the long lines of code in menu-bar, I was unsure whether it's a good idea to pass them as a prop or copy-paste the logic directly within the class.
  • The behavior of using screen reader and clicking with cursor leads to improper reads, as opposed to just navigating with keyboard.
  • I made the observation that mode menu seems to act like the other dropdown menus, but it doesn't have an expansion arrow like them. It is not a very active feature of the app, have to make isTotallyNormal=true in gui.jsx to test it.
  • I have also replaced isOpenMenu logic with custom such from the Context. Should I end up removing that entirely from code? I mean I think I have done this entirely in this PR the way it is now but should I? Seems like it will become obsolete.
  • Whatever the comments address.

Tip for reviewing this PR

  • Start from base-menu and menu-ref-context to understand the basic idea behind the code.
  • Test well with a screen reader, since it's likely you'll have one better than my chrome extension.
  • I have a feeling switching language and testing buttons with screen reader doesn't quite work as intended. Have to look into that

Bugs

  • When navigating with screen reader in the edit menu and "Turn on/off Screen Reader" button is triggered, the updated name of that button is not read correctly immediately (old value is read instead as long as we don't focus away - if we come back, then it's read correctly).
  • Bad behavior when combining screen reader usage and clicking with cursor.

Missing

  • aria-selected
  • aria-disabled
  • translations of aria-labels and such
  • The four remaining disabled buttons in the menu bar are untouched. I'm wondering if they're disabled because the changes on them belong to www repo or because they can't be tested locally, in which case I will have to finish them in this PR. Just want to clarify that before touching them.

@kbangelov kbangelov requested a review from a team as a code owner December 19, 2025 14:15
@KManolov3 KManolov3 requested a review from cwillisf December 19, 2025 14:26
@KManolov3 KManolov3 marked this pull request as draft December 19, 2025 14:26
@KManolov3 KManolov3 requested a review from Copilot December 19, 2025 14:27
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements keyboard navigation and accessibility improvements for the top navigation bar, enabling users to navigate menu items using Tab and arrow keys. The changes introduce a new context-based system for tracking open menus and managing focus states across nested menu hierarchies.

Key Changes:

  • Added tabIndex attributes and ARIA properties to make menu bar elements keyboard-navigable
  • Implemented arrow key navigation within dropdown menus (Settings, Language, Theme)
  • Created MenuRefContext to manage the state of open menus and focus tracking across the menu hierarchy

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
packages/scratch-gui/src/components/context-menu/menu-path-context.jsx New context provider for tracking open menu references and navigation state
packages/scratch-gui/src/components/menu-bar/settings-menu.jsx Converted to class component with keyboard navigation handlers
packages/scratch-gui/src/components/menu-bar/language-menu.jsx Added keyboard navigation with arrow keys and Enter/Escape handlers
packages/scratch-gui/src/components/menu-bar/theme-menu.jsx Added keyboard navigation similar to language menu
packages/scratch-gui/src/components/menu/menu.jsx Added accessibility props (focusedRef, aria attributes, keyboard handlers)
packages/scratch-gui/src/components/menu-bar/menu-bar.jsx Added tabIndex and ARIA attributes to menu bar items for keyboard access
packages/scratch-gui/src/components/gui/gui.jsx Wrapped MenuBar with MenuRefProvider context
packages/scratch-gui/src/containers/gui.jsx Contains commented test code

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

};

GUI.defaultProps = {
// isTotallyNormal: true, - for testing only
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented-out test code before merging. This appears to be temporary debugging code that should not be committed.

Suggested change
// isTotallyNormal: true, - for testing only
// Set to true only in specific testing scenarios.

Copilot uses AI. Check for mistakes.
expanded: PropTypes.bool,
onClick: PropTypes.func
onClick: PropTypes.func,
focusedRef: PropTypes.object,
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using PropTypes.object is discouraged. Consider using PropTypes.shape({ current: PropTypes.instanceOf(Element) }) for ref objects, or create a custom PropType validator if this will remain in the code.

Suggested change
focusedRef: PropTypes.object,
focusedRef: PropTypes.shape({ current: PropTypes.instanceOf(Element) }),

Copilot uses AI. Check for mistakes.
onClick: PropTypes.func,
theme: PropTypes.string
theme: PropTypes.string,
focusedRef: PropTypes.object,
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using PropTypes.object is discouraged. Consider using PropTypes.shape({ current: PropTypes.instanceOf(Element) }) for ref objects, or create a custom PropType validator.

Copilot uses AI. Check for mistakes.
this.setState({focusedIndex: -1}, () => {
this.setFocusedRef(this.props.focusedRef);
});
closeThemeMenu();
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to call closeThemeMenu() directly instead of dispatching it. Based on the import, this should be this.props.onRequestClose() or the function should be dispatched. The current implementation will not update the Redux state correctly.

Suggested change
closeThemeMenu();
this.props.onRequestClose();

Copilot uses AI. Check for mistakes.
this.setState({focusedIndex: -1}, () => {
this.setFocusedRef(this.props.focusedRef);
});
closeLanguageMenu();
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to call closeLanguageMenu() directly instead of dispatching it. This should likely use the Redux dispatch mechanism or call an appropriate prop function to properly update state.

Suggested change
closeLanguageMenu();
this.props.dispatch(closeLanguageMenu());

Copilot uses AI. Check for mistakes.

LanguageMenu.propTypes = {
currentLocale: PropTypes.string,
focusedRef: PropTypes.object,
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using PropTypes.object is discouraged. Consider using PropTypes.shape({ current: PropTypes.instanceOf(Element) }) for ref objects, or create a custom PropType validator.

Suggested change
focusedRef: PropTypes.object,
focusedRef: PropTypes.shape({current: PropTypes.instanceOf(Element)}),

Copilot uses AI. Check for mistakes.
this.state = {focusedIndex: -1};
this.languageRef = React.createRef();
this.themeRef = React.createRef();
// harcoded logic because of only two options
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected spelling of 'harcoded' to 'hardcoded'.

Suggested change
// harcoded logic because of only two options
// hardcoded logic because of only two options

Copilot uses AI. Check for mistakes.
Comment on lines 63 to 65
// printChain () {
// console.log(this.state.openRefs);
// }
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented-out debugging code. The printChain method and its reference in the context value should be deleted before merging.

Copilot uses AI. Check for mistakes.
<img
role="button"
aria-label="Go Home"
tabIndex="0"
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use numeric value instead of string for tabIndex. Change tabIndex=\"0\" to tabIndex={0} for consistency with React conventions.

Suggested change
tabIndex="0"
tabIndex={0}

Copilot uses AI. Check for mistakes.
this.context.print();
}

handleOnClose () {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uncertain whether the menus should collapse when they are clicked and which ones. For now they just keep their current behaviour.

]);

this.state = {focusedIndex: -1};
this.menuRef = props.menuRef;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to leave it like this for shorter lines on usage down below. Idk

accountMenuOptions={accountMenuOptions}
/>}
{!menuBarHidden &&
<MenuRefProvider>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I wasn't really sure where I should wrap this. It is only used in the menu bar so far, perhaps later it could easily be changed for entire editor. I'm not sure whether there will be conflicting contexts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not sure in which folder to put the file

ref={menuRef}
aria-label={ariaLabel}
aria-selected={isSelected ?? null}
aria-disabled={isDisabled ?? null}
Copy link
Contributor Author

@kbangelov kbangelov Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this applies for both aria-selected and aria-disabled.

They seem to require specific roles for both itself and its container, such as role="option" inside role="listbox", or role="tab" inside role="tablist". For some reason I couldn't really get it to work. But it might be due to my limited screen reader. I'll try to figure out why that is.

);

MenuItem.propTypes = {
menuRef: PropTypes.shape({current: PropTypes.instanceOf(Element)}),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repeats quite a few times. I should probably define it somewhere but I'm not really sure where and whether we have such a file.

id="gui.menuBar.saveAsCopy"
/>
);
const remixMessage = (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a duplicate definition so this can't be the final version of the PR. But I wonder if I should pass it down as a prop?

this.createRef = React.createRef();
this.remixRef = React.createRef();
this.loadFromComputerRef = React.createRef();
this.saveToComputerRef = React.createRef();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following question applies for the other constructors of custom menus as well.

Should the logic be this manual? Seems fine to me, since we're talking about menu items with specific behavior. Can't really think of a better alternative.

// If we are using hover rather than clicks for submenus, scroll the selected option into view
if (!this.props.menuOpen && this.selectedRef) {
this.selectedRef.scrollIntoView({block: 'center'});
}
Copy link
Contributor Author

@kbangelov kbangelov Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment is regarding the handleMouseOver method. Should I also focus on the selected language menu item when just hovering? I, personally, don't think that's a good idea.

@kbangelov kbangelov marked this pull request as ready for review December 29, 2025 13:16
this.colorRef = React.createRef();
this.itemRefs = [
...(this.props.canChangeLanguage ? [this.languageRef] : []),
...(this.props.canChangeTheme && this.props.availableThemesLength > 1 ? [this.themeRef] : []),
Copy link
Contributor Author

@kbangelov kbangelov Dec 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic repeats a lot with item refs, so it is typed twice in every such menu - once here and once in the render method. Perhaps it could be cleaned up with a sort of condition variable, because someone making a change might forget about accessibility.

onRequestOpen,
settingsMenuOpen
}) => {
const enabledColorModesMap = useMemo(() => Object.keys(colorModeMap).reduce((acc, colorMode) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the useMemo usage. It will probably rerender too often this way. It was just not allowed in a render() method, I was just not sure what workaround is available here. I thought it'd be easier to ask and resolve it quickly afterwards.

@kbangelov
Copy link
Contributor Author

Ok. Also the tests currently fail. I am not sure I understand why, even with Copilot's help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant