[Modal] Refactor tests to remove internal accesses#16262
Conversation
|
@material-ui/lab: parsed: -0.42% 😍, gzip: -0.31% 😍 Details of bundle changes.Comparing: 93f5b33...44976a0
|
| } | ||
| }, [mountNode, onRendered]); | ||
|
|
||
| React.useImperativeHandle(ref, () => mountNode || childRef.current, [mountNode]); |
There was a problem hiding this comment.
Change the order so ref triggers before onRendered.
There was a problem hiding this comment.
Wait refs should never trigger before layout effects. Those are two different phases. Do you have a test for that?
There was a problem hiding this comment.
useImperativeHandle triggers at the same time than synchronous layout effects from what I can log (not layout effects!). I'm adding a test case.
There was a problem hiding this comment.
Huh, need to play around with this. Did not know that.
There was a problem hiding this comment.
I believe that reason is the following: you need to have access to the DOM element in the synchronous layout effects. If the ref triggers after, you don't have it.
There was a problem hiding this comment.
Did some experimenting and it is essentially react/react#7769 i.e. within a render block useImperativeHandle runs in the same phase as layout effects but any layout effect queued outside (e.g. in a parent) will run after useImperativeHandle of children.
There was a problem hiding this comment.
I think onRendered is actually an Anti-Pattern. We already have callback refs to handle when a host component is committed. What should happen is
- const portalRef = React.useRef();
- <Portal container={container} onRendered={() => doSomethingWith(portalRef.current)} ref={portalRef} />
+ <Portal container={container} ref={doSomethingWith} />There was a problem hiding this comment.
Oh, interesting. I have never thought about it
There was a problem hiding this comment.
I think we can re-evaluate dropping onRendered for callback refs in v5. For now this fix works.
There was a problem hiding this comment.
I'm in favor of dropping the prop in v5. I will update the Modal implementation to stop using it in the next Modal pull request. Thanks for raising. I agree that it doesn't provide anything since the forwardRef effort was completed. 👌
| import ownerDocument from '../utils/ownerDocument'; | ||
| import ownerWindow from '../utils/ownerWindow'; | ||
|
|
||
| export function isBody(node) { |
| const onClose = spy(); | ||
| wrapper.setProps({ onClose }); | ||
|
|
||
| const handler = wrapper.find('Modal').instance().handleBackdropClick; |
There was a problem hiding this comment.
Remove usages of .instance().
| }), | ||
| ); | ||
|
|
||
| export default function SimpleModal() { |
There was a problem hiding this comment.
Add a TypeScript demo 👌
| import Dialog from 'packages/material-ui/src/Dialog'; | ||
| import MenuItem from '@material-ui/core/MenuItem'; | ||
| import Select from '@material-ui/core/Select'; | ||
| import Dialog from '@material-ui/core/Dialog'; |
There was a problem hiding this comment.
I have removed all the usages of imports from packages/. Better have fewer ways of doing the same thing!
| }; | ||
|
|
||
| export default withTheme(SwipeableDrawer); | ||
| export default SwipeableDrawer; |
There was a problem hiding this comment.
Remove most of the usages of withTheme(). It should help with perf, bundle size and debug noise.
| onRendered: PropTypes.func, | ||
| }; | ||
|
|
||
| Portal.defaultProps = { |
There was a problem hiding this comment.
Move some of the .defaultProps to prop destructuring. I haven't migrated everything. I have left the complex objects and the one used in the custom prop-types.
| } | ||
| }, [mountNode, onRendered]); | ||
|
|
||
| React.useImperativeHandle(ref, () => mountNode || childRef.current, [mountNode]); |
There was a problem hiding this comment.
Wait refs should never trigger before layout effects. Those are two different phases. Do you have a test for that?
4756d1f to
89dde00
Compare
cf0664f to
6ebe7f6
Compare
6ebe7f6 to
44976a0
Compare
Prepare #16254.
withThemetouseTheme..defaultProps.refandonRenderedinversed call order.isEnabledis not "stable".packages/material-ui/src/imports with@material-ui/core/.