Components with open/close animations should have cancelable opening/closing start events
See original GitHub issueThis is more of a generalized issue and feedback regarding how MDC handles components that have an implied stateful lifecycle (opening, opened, closing, closed). Like most of my issues, this one is related to React, but is not React specific.
Because of the uni-directional data flow of React, interfacing with the components through RMWC is controlled via props. You don’t execute an openMenu callback, you simply pass open=true. Again, following traditional React patterns, you would pass a callback to something like an onClose prop whenever the state is about to change to closed. This gives you the opportunity to respond to onClose and decided whether or not you want to close the component, or keep it opened. This a relatively standard pattern with what are called “controlled components”.
A reductive example
class MyComponent extends React.Component {
state = {
open: false
}
render() {
return (
<Menu
open={this.state.open}
// maybe I want to stay open...
onClose={() => this.setState({open: true})}
/>
)
}
}
The heart of where this becomes the problem is in the following code in the menu-surface foundation.
Basically, the MenuSurface has already started its un-cancellable close cycle before it notifies the consumers that it’s going to close. This prevents consumers from doing something as simple as leaving the Menu open. The best I’ve been able to do in the upcoming RMWC release is to “re-open” the menu every time which gives you a nice little blip of the menu on the screen, as well as re-animate and re-call any logic when it opens (like focusing the first menu item).
This example is for the menu, but impacts anything that has similar opening / closing behavior (Menus, Dialogs, Drawers, and Snackbars at a quick glance).
Proposed Solution:
- Implement a standard pattern for components with an open / close lifecycle
- Keep an
open: booleanflag in the foundation (Menu already has isOpen_) - Wait a frame when trying to execute the close cycle and cancel if the
openflag has been set back to true - Dialog seems to be the closest fit to this pattern right now.
My ideal events
- open: executed immediately when the
openchanges from false to true - opened: executed when all animations are finished and the component is resting. This may not execute if the component has been switched back to close.
- close: executed immediately when
openchanges from true to false - closed: executed when all animations are finished and the component is resting. This may not be executed if the component has been switched back to open.
Sorry for the book. Thoughts?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:9 (6 by maintainers)

Top Related StackOverflow Question
On the footgun thing. Agreed. It’s hard to factor how someone needs to use the components. A lot of it comes down to implementation details on the consumers end. No matter what, people can can use this library and still violate the material spec all day long (and I’m sure they will).
@lucasecdb I read what @dawsonc623 wrote, and I see what both of your are saying. We’re looking into solutions. 😃