-
Notifications
You must be signed in to change notification settings - Fork 113
[UEPR-445] Ensure topbar is navigable via tab navigation #403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import bindAll from 'lodash.bindall'; | ||
|
|
||
| export const MenuRefContext = React.createContext(null); | ||
|
|
||
| export class MenuRefProvider extends React.Component { | ||
| constructor (props) { | ||
| super(props); | ||
|
|
||
| this.state = { | ||
| openRefs: [] | ||
| }; | ||
|
|
||
| bindAll(this, [ | ||
| 'addInner', | ||
| 'isTopMenu', | ||
| 'isOpenMenu', | ||
| 'removeAll', | ||
| 'removeByRef', | ||
| 'removeInner' | ||
| ]); | ||
| } | ||
|
|
||
| isTopMenu (ref) { | ||
| const {openRefs} = this.state; | ||
| return openRefs.length > 0 && openRefs[openRefs.length - 1] === ref; | ||
| } | ||
|
|
||
| isOpenMenu (ref) { | ||
| return this.state.openRefs.includes(ref); | ||
| } | ||
|
|
||
| addInner (ref) { | ||
| this.setState(prev => ({ | ||
| openRefs: [...prev.openRefs, ref] | ||
| })); | ||
| } | ||
|
|
||
| removeByRef (ref) { | ||
| this.setState(prev => { | ||
| const refs = prev.openRefs; | ||
| const index = refs.indexOf(ref); | ||
|
|
||
| if (index === -1) return {openRefs: refs}; | ||
|
|
||
| return { | ||
| openRefs: refs.slice(0, index) | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| removeInner () { | ||
| this.setState(prev => ({ | ||
| openRefs: prev.openRefs.slice(0, prev.openRefs.length - 1) | ||
| })); | ||
| } | ||
|
|
||
| removeAll () { | ||
| this.setState({openRefs: []}); | ||
| } | ||
|
|
||
| // printChain () { | ||
| // console.log(this.state.openRefs); | ||
| // } | ||
|
|
||
| render () { | ||
| const value = { | ||
| openRefs: this.state.openRefs, | ||
| isTopMenu: this.isTopMenu, | ||
| isOpenMenu: this.isOpenMenu, | ||
| addInner: this.addInner, | ||
| removeInner: this.removeInner, | ||
| removeAll: this.removeAll, | ||
| removeByRef: this.removeByRef | ||
| // printChain: this.printChain | ||
| }; | ||
|
|
||
| return ( | ||
| <MenuRefContext.Provider value={value}> | ||
| {this.props.children} | ||
| </MenuRefContext.Provider> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| MenuRefProvider.propTypes = { | ||
| children: PropTypes.node | ||
| }; | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,8 +9,9 @@ import locales from 'scratch-l10n'; | |||||
| import check from './check.svg'; | ||||||
| import {MenuItem, Submenu} from '../menu/menu.jsx'; | ||||||
| import languageIcon from '../language-selector/language-icon.svg'; | ||||||
| import {languageMenuOpen, openLanguageMenu} from '../../reducers/menus.js'; | ||||||
| import {closeLanguageMenu, languageMenuOpen, openLanguageMenu} from '../../reducers/menus.js'; | ||||||
| import {selectLocale} from '../../reducers/locales.js'; | ||||||
| import {MenuRefContext} from '../context-menu/menu-path-context.jsx'; | ||||||
|
|
||||||
| import styles from './settings-menu.css'; | ||||||
|
|
||||||
|
|
@@ -20,9 +21,18 @@ class LanguageMenu extends React.PureComponent { | |||||
| constructor (props) { | ||||||
| super(props); | ||||||
| bindAll(this, [ | ||||||
| 'handleKeyPress', | ||||||
| 'handleKeyPressOpenMenu', | ||||||
| 'handleMove', | ||||||
| 'handleOnOpen', | ||||||
| 'handleOnClose', | ||||||
| 'setFocusedRef', | ||||||
| 'setRef', | ||||||
| 'handleMouseOver' | ||||||
| ]); | ||||||
|
|
||||||
| this.state = {focusedIndex: -1}; | ||||||
| this.itemRefs = Object.keys(locales).map(() => React.createRef()); | ||||||
| } | ||||||
|
|
||||||
| componentDidUpdate (prevProps) { | ||||||
|
|
@@ -32,26 +42,104 @@ class LanguageMenu extends React.PureComponent { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| static contextType = MenuRefContext; | ||||||
|
|
||||||
| setRef (component) { | ||||||
| this.selectedRef = component; | ||||||
| } | ||||||
|
|
||||||
| handleKeyPress (e) { | ||||||
| if (this.context.isTopMenu(this.props.focusedRef)) { | ||||||
| this.handleKeyPressOpenMenu(e); | ||||||
| } else if (!this.context.isOpenMenu(this.props.focusedRef) && (e.key === ' ' || e.key === 'ArrowRight')) { | ||||||
| e.preventDefault(); | ||||||
| this.handleOnOpen(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| handleKeyPressOpenMenu (e) { | ||||||
| if (e.key === 'ArrowDown') { | ||||||
| e.preventDefault(); | ||||||
| this.handleMove(1); | ||||||
| } | ||||||
| if (e.key === 'ArrowUp') { | ||||||
| e.preventDefault(); | ||||||
| this.handleMove(-1); | ||||||
| } | ||||||
|
|
||||||
| if (e.key === 'Enter') { | ||||||
| e.preventDefault(); | ||||||
| this.props.onChangeLanguage(Object.keys(locales)[this.state.focusedIndex]); | ||||||
| this.handleOnClose(); | ||||||
| } | ||||||
|
|
||||||
| if (e.key === 'ArrowLeft' || e.key === 'Escape') { | ||||||
| e.preventDefault(); | ||||||
| this.handleOnClose(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| handleMove (move) { | ||||||
| const newIndex = (this.state.focusedIndex + move + this.itemRefs.length) % this.itemRefs.length; | ||||||
| this.setState({focusedIndex: newIndex}, () => { | ||||||
| const ref = this.itemRefs[this.state.focusedIndex]; | ||||||
| if (ref && ref.current) ref.current.focus(); | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| handleMouseOver () { | ||||||
| // 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'}); | ||||||
| this.setFocusedRef(this.selectedRef); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| handleOnOpen () { | ||||||
| if (this.context.isOpenMenu(this.props.focusedRef)) return; | ||||||
|
|
||||||
| this.props.onRequestOpen(); | ||||||
| this.setState({focusedIndex: Object.keys(locales).indexOf(this.props.currentLocale)}, () => { | ||||||
| this.setFocusedRef(this.itemRefs[this.state.focusedIndex]); | ||||||
| }); | ||||||
|
|
||||||
| this.context.addInner(this.props.focusedRef); | ||||||
| } | ||||||
|
|
||||||
| handleOnClose () { | ||||||
| this.context.removeByRef(this.props.focusedRef); | ||||||
| this.setState({focusedIndex: -1}, () => { | ||||||
| this.setFocusedRef(this.props.focusedRef); | ||||||
| }); | ||||||
| closeLanguageMenu(); | ||||||
|
||||||
| closeLanguageMenu(); | |
| this.props.dispatch(closeLanguageMenu()); |
Outdated
Copilot
AI
Dec 19, 2025
There was a problem hiding this comment.
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.
| focusedRef: PropTypes.object, | |
| focusedRef: PropTypes.shape({current: PropTypes.instanceOf(Element)}), |
There was a problem hiding this comment.
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
printChainmethod and its reference in the context value should be deleted before merging.