-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[Joy][Menu] Menu stays open - onClose
not called
#38324
Comments
I also have the problem with a menu not closing. I have a custom button that has a loading state inside the menu. I want the menu to stay open until the user clicks somewhere else (not on the modal). I'm currently using the new version with Dropdown, MenuButton, etc. But I also had the issue inside the beta version. Also the disabled property does not seem to work on the MenuButton |
@Abhushit Yes, "uncontrolled" also works for me. I tried to control the open state here: https://codesandbox.io/s/eloquent-feather-wfsj7f?file=/Demo.tsx But the menu is never firing onClose when I click away. Is this use case not supported? Or am I setting the wrong properties here? |
Yes @juliushuck you are right. If you try to create a controlled function for onClose, it does not work with the Menu component. Please visit: https://codesandbox.io/s/2rc4wf?file=/Demo.js |
Opened PR which fixes disable issue. #38342 |
I see the requirement for Dropdown and MenuButton or ClickawayListener, but this is a large behavior change versus the former "onClose" approach. Are we expected to port the code of the application to this new pattern because onClose is present but not firing? Controllable menus are a requirement of real-world apps in my opinion, especially when you want the parent component to hold state, or an external Store to hold state (menus closeable by peer components, dynamic menu items, pluggable menus). Please let us know the expected behavior (and/or remove the onClose call to force downstream migration away from the former API). |
The With the latest changes, the Dropdown is responsible for opening and closing the popup, while the Menu is only concerned with list navigation, highlighted item, etc. This codesandbox shows how to implement a controlled menu. Does this help? |
Thanks @michaldudak, I'll port to the new pattern asap and let you know if I have feedback. My app has 4+ controllable menus, and some with complex controls (closeable by other parts of the UI as contents are pluggable). I'll wait for the official merge and deploy of your patch that removes the onClose, to see if my code breaks anywhere, but likely not, once I port it. |
To be sure: we don't need a MenuButton, and the Anchor can be set from outside, correct? |
Test results: The change in behavior is very deep. Issues introduced by the new menu paradigm1. Breaks different triggers to open the menuDeleting the 2. Breaks dynamic menus / non-sibling anchorsWhat if the Anchor is not a sibling of the Menu, but in a very different part of the UI. 3. Breaks conditional renderingReact conditional rendering is now not supported - e.g. "{condition && <Dropdown...>" - as it will not fire the onOpenChange callback. Bottom lineI ported my full app to Joy-5.beta0 in 1 day (which is great, and love the Beta), but I am stuck porting my 5 menus - they simply cannot be ported to the new framework. The requirement of using "Dropdown" and "MenuButton" while seems to be a simplification in the easiest use case, it makes things more complex and prevents flexibility, denies use cases, or requires very expensive workarounds. What used to be Apologies if this sounds critical of the change - I understand the spirit on delivering on an easy Dropdown experience - I read and followed the Migration Guide https://mui.com/joy-ui/migration/migrating-default-theme/, and was not expecting that after a day of work, my Joy App will be in an unfixable broken state - if I knew, I'd not have done the port. The Menu change goes deeper than style changes. |
@enricoros https://codesandbox.io/s/cold-firefly-qz9vst?file=/Demo.tsx I think demo in this sandbox covers all 3 use cases you mentioned. Does this work for you? @michaldudak what do you think about implementation? if you are fine, i can create demos. |
@sai6855 Hi, thanks for the code. The menu stays open (as per the original title of this issue); "clicking away" doesn't close it despite the code. Regarding whether the 3 use cases are covered, the main issue is that the Dropdown, MenuButton and Menu are co-located in this demo. With the former Imagine this use case: a menu that appears when you right-click. Before, it was possible with 1 line of code I don't want to keep this issue open; I'm sharing my experience in detail because this new API imposes a large limitation for Joy/Base IMO, and I love this product. |
Found a workaround, this issue can be closed. I wrote this component which is similar to the way the
|
@enricoros Thanks a lot for the feedback. cc @michaldudak I think it's worth taking #38324 (comment) into consideration. |
Previously, using controlled Button+Menu, we could leave the menu opened when a menu item was clicked. My use case is a dropdown menu, with items that I can enable/disable, and I want to leave the menu opened after a click on an item. |
I see 2 options:
cc @michaldudak |
I think the second solution offers more flexibility. It allows to mix items that closes the menu and items that don't. |
We could make it more explicit and have a prop on the MenuItem called I created a separate issue for this: mui/base-ui#46 |
@michaldudak Do you see any workaround to this? It sounds like a common use case to me and we should add it to the docs or demos. |
"Improworsement": is an improvement that makes things worse. The changes to the Menu component have transformed it from a React-controllable Menu, to a Dropdown. I don't write this message to get a reply, but just out of love for the product. The new Dropdown is limited in many ways compared to the old Menu -- but it's still called "Menu". For instance, how can I show a dynamic menu (with programmatic items) when right-clicking some selected text? Audience: you cater to react programmers, saving 1 anchorEl in the state is not hard for the audience (former behavior) I don't see why making menu state management opaque and non-controllable to the developers can improve the DX, if not for the basic use case of a dropdown on a button, but menus are used for much more than that. |
Here is my thought about the Menu:
cc @michaldudak If you agree with this, I can take this over. |
@enricoros, we've considered many different approaches when designing this solution. You can browse through #32088 for more details. I don't see a problem adding an explicit @siriwatknp I believe that more complex cases should also use the Dropdown, but not necessarily the MenuButton. |
FYI, I added the |
Duplicates
Latest version
Steps to reproduce 🕹
Link to live example:
https://codesandbox.io/s/bold-fog-dc2lz2?file=/Demo.tsx
Steps:
Current behavior 😯
Menu stays open even when clicking away - onClose is not called:
Expected behavior 🤔
onClose is called, and the menu closes
Context 🔦
I've ported my application to the newly released Joy UI
^5.0.0-beta.0
, and most menus in the application stay open.Selects close normally, but I cannot close menus. The repro shows the minimal code that used to work pre-beta.
Your environment 🌎
npx @mui/envinfo
The text was updated successfully, but these errors were encountered: