Skip to content

[ContextMenu][DropdownMenu] Fix issue introduced by new selector prop #404

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

Merged
merged 2 commits into from
Jan 25, 2021

Conversation

benoitgrelard
Copy link
Contributor

@benoitgrelard benoitgrelard commented Jan 25, 2021

Closes #402

After checking a reported issue #402, I realised we have introduced a bug in dependents of Menu (ContextMenu, DropdownMenu) when we added the selector prop.

This is because the expectation on what attributes are present changed, and we didn't account for it here.
Namely, before we were expecting the data-radix-menu-item to be present even in dependents of Menu.
This is no longer a safe assumption to make obviously, therefore we cannot rely on this selector.

Looking at other places we build selectors like this (roving focus behaviour, or typeahead behaviour) they both add their own data attribute to ensure they can safely select items. So it seems in this case we should be doing the same somehow. It feels a bit more annoying given that we know there is already a default selectors for items… but obviously now specific to each dependent of Menu).

I haven't made any change yet, wondering what your thoughts were.

@benoitgrelard benoitgrelard requested a review from jjenzz January 25, 2021 00:43
@benoitgrelard benoitgrelard changed the title Update Menu.tsx [ContextMenu][DropdownMenu] Fix issue introduced by new selector prop Jan 25, 2021
@jjenzz
Copy link
Contributor

jjenzz commented Jan 25, 2021

Ah, oops. It would be good to have a call about this one because I've had a look at the code and wondering if we could get rid of ENABLED_ITEM_SELECTOR altogether.

I commented out the following code and struggling to decipher the difference so would be good to run through it with you:

// focus the menu if the mouse is moved over anything else than an item
onMouseMove={composeEventHandlers(menuProps.onMouseMove, (event) => {
  if (!isItemOrInsideItem(event.target)) menuRef.current?.focus();
})}

Also, the onKeyDown handler could probably use a new itemsRef.current array that each MenuItem would append to in the context.

React.useEffect(() => {
  context.itemsRef.current = [...context.itemsRef.current, ref.current];
}, [context.itemsRef, ref]);

If all else fails, adding a new stable selector for this makes sense to me 🙂

@benoitgrelard benoitgrelard marked this pull request as ready for review January 25, 2021 15:21
@@ -304,10 +304,6 @@ const MenuImpl = React.forwardRef((props, forwardedRef) => {
}
}
})}
// focus the menu if the mouse is moved over anything else than an item
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Always nice to be able to remove unused code, well spotted @jjenzz!

@benoitgrelard benoitgrelard merged commit ca7745e into main Jan 25, 2021
@benoitgrelard benoitgrelard deleted the menu-arrow-focus-fix branch January 25, 2021 16:10
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.

[DropdownMenu] Opening focus and arrow keys does not work
2 participants