-
-
Notifications
You must be signed in to change notification settings - Fork 88
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
[Menu] Fixed Menu item not toggling highlighted prop when submenu popup is opened using keyboard #1310
Open
onehanddev
wants to merge
12
commits into
mui:master
Choose a base branch
from
onehanddev:menu/submenu-highlight-keyboard-focus-updated
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[Menu] Fixed Menu item not toggling highlighted prop when submenu popup is opened using keyboard #1310
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0419c12
Added setActiveIndex to the MenuRoot and MenuSubmenuTrigger component…
onehanddev eb47bc2
fix: ts errors
onehanddev c4aa02c
Handled click event as well for setting active item for menu, added m…
onehanddev 1700a03
addressed PR comments
onehanddev 03c6581
added keyboard support for rtl direction for submenu toggling for Men…
onehanddev 661c502
Merge branch 'master' into menu/submenu-highlight-keyboard-focus-updated
onehanddev eecafbd
Merge branch 'master' into menu/submenu-highlight-keyboard-focus-updated
onehanddev 9ac8981
Added test cases to verify that the parent menu item remains inactive…
onehanddev 08a8d3e
Merge branch 'master' into menu/submenu-highlight-keyboard-focus-updated
onehanddev 7f69c60
fix: replaced tabindex with data-highlighted for <Menu.SubmenuTrigger…
onehanddev 6281f1b
Merge branch 'menu/submenu-highlight-keyboard-focus-updated' of https…
onehanddev 79cc42c
fix: retained tabindex of the submenutrigger once the submenu opens
onehanddev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/react/src/menu/submenu-trigger/MenuSubmenuTrigger.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import { fireEvent, waitFor, screen } from '@mui/internal-test-utils'; | ||
import { createRenderer } from '#test-utils'; | ||
import { DirectionProvider } from '@base-ui-components/react/direction-provider'; | ||
import { Menu } from '@base-ui-components/react/menu'; | ||
|
||
type TextDirection = 'ltr' | 'rtl'; | ||
|
||
describe('<Menu.SubmenuTrigger />', () => { | ||
const { render } = createRenderer(); | ||
|
||
function TestComponent({ direction = 'ltr' }: { direction: TextDirection }) { | ||
return ( | ||
<DirectionProvider direction={direction}> | ||
<Menu.Root open> | ||
<Menu.Portal> | ||
<Menu.Positioner> | ||
<Menu.Popup> | ||
<Menu.Item>1</Menu.Item> | ||
<Menu.Root> | ||
<Menu.SubmenuTrigger>2</Menu.SubmenuTrigger> | ||
<Menu.Portal> | ||
<Menu.Positioner> | ||
<Menu.Popup> | ||
<Menu.Item>2.1</Menu.Item> | ||
<Menu.Item>2.2</Menu.Item> | ||
</Menu.Popup> | ||
</Menu.Positioner> | ||
</Menu.Portal> | ||
</Menu.Root> | ||
</Menu.Popup> | ||
</Menu.Positioner> | ||
</Menu.Portal> | ||
</Menu.Root> | ||
</DirectionProvider> | ||
); | ||
} | ||
|
||
const testCases = [ | ||
{ direction: 'ltr', openKey: 'ArrowRight', closeKey: 'ArrowLeft' }, | ||
{ direction: 'rtl', openKey: 'ArrowLeft', closeKey: 'ArrowRight' }, | ||
]; | ||
|
||
testCases.forEach(({ direction, openKey }) => { | ||
it(`opens the submenu with ${openKey} and highlights a single item in ${direction.toUpperCase()} direction`, async () => { | ||
await render(<TestComponent direction={direction as TextDirection} />); | ||
const submenuTrigger = screen.getByText('2'); | ||
|
||
fireEvent.focus(submenuTrigger); | ||
fireEvent.keyDown(submenuTrigger, { key: openKey }); | ||
|
||
const submenuItems = await screen.findAllByRole('menuitem'); | ||
const submenuItem1 = submenuItems.find((item) => item.textContent === '2.1'); | ||
|
||
await waitFor(() => { | ||
expect(submenuItem1).toHaveFocus(); | ||
}); | ||
|
||
submenuItems.forEach((item) => { | ||
if (item === submenuItem1) { | ||
expect(item).to.have.attribute('tabindex', '0'); | ||
} else { | ||
expect(item).to.have.attribute('tabindex', '-1'); | ||
} | ||
}); | ||
|
||
// Check that parent menu items are not active | ||
const parentMenuItems = screen | ||
.getAllByRole('menuitem') | ||
.filter((item) => item.textContent !== '2.1' && item.textContent !== '2.2'); | ||
parentMenuItems.forEach((item) => { | ||
expect(item).not.to.have.attribute('tabindex', '0'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,12 @@ import { FloatingEvents } from '@floating-ui/react'; | |
import { useMenuItem } from '../item/useMenuItem'; | ||
import { useForkRef } from '../../utils/useForkRef'; | ||
import { GenericHTMLProps } from '../../utils/types'; | ||
import { mergeReactProps } from '../../utils/mergeReactProps'; | ||
import { useDirection } from '../../direction-provider/DirectionContext'; | ||
|
||
type MenuKeyboardEvent = { | ||
key: 'ArrowRight' | 'ArrowLeft' | 'ArrowUp' | 'ArrowDown'; | ||
} & React.KeyboardEvent; | ||
|
||
export function useMenuSubmenuTrigger( | ||
parameters: useSubmenuTrigger.Parameters, | ||
|
@@ -17,6 +23,7 @@ export function useMenuSubmenuTrigger( | |
setTriggerElement, | ||
allowMouseUpTriggerRef, | ||
typingRef, | ||
setActiveIndex, | ||
} = parameters; | ||
|
||
const { getRootProps: getMenuItemProps, rootRef: menuItemRef } = useMenuItem({ | ||
|
@@ -32,17 +39,28 @@ export function useMenuSubmenuTrigger( | |
|
||
const menuTriggerRef = useForkRef(menuItemRef, setTriggerElement); | ||
|
||
const direction = useDirection(); | ||
|
||
const getRootProps = React.useCallback( | ||
(externalProps?: GenericHTMLProps) => { | ||
return { | ||
const openKey = direction === 'rtl' ? 'ArrowLeft' : 'ArrowRight'; | ||
|
||
return mergeReactProps(externalProps, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
...getMenuItemProps({ | ||
'aria-haspopup': 'menu' as const, | ||
...externalProps, | ||
onKeyDown: (event: MenuKeyboardEvent) => { | ||
if (event.key === openKey && highlighted) { | ||
// Clear parent menu's highlight state when entering submenu | ||
// This prevents multiple highlighted items across menu levels | ||
setActiveIndex(null); | ||
} | ||
}, | ||
onClick: () => highlighted && setActiveIndex(null), | ||
}), | ||
ref: menuTriggerRef, | ||
}; | ||
}); | ||
}, | ||
[getMenuItemProps, menuTriggerRef], | ||
[getMenuItemProps, menuTriggerRef, highlighted, setActiveIndex, direction], | ||
); | ||
|
||
return React.useMemo( | ||
|
@@ -82,6 +100,11 @@ export namespace useSubmenuTrigger { | |
* A ref that is set to `true` when the user is using the typeahead feature. | ||
*/ | ||
typingRef: React.RefObject<boolean>; | ||
/** | ||
* Callback to update the active (highlighted) item index. | ||
* Set to null to remove highlighting from all items. | ||
*/ | ||
setActiveIndex: (index: number | null) => void; | ||
} | ||
|
||
export interface ReturnValue { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Small nit:
data-highlighted
is the attribute used for styling so it'd be better to test that, even if they're functionally the sameThere 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.
@atomiks Done 👍