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
[MenuUnstyled] Accept callbacks in componentsProps #32997
Conversation
@material-ui/unstyled: parsed: +1.07% , gzip: +1.02% |
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.
Generally, I believe we need to spread in this order:
- default values for props
- componentsProps.slot (resolved)
- props coming from hook (with merged event handlers)
- adding ref & className
I think we have mix & match at this point.
...getRootProps(other), | ||
className: clsx(classes.root, className, componentsProps.root?.className), | ||
...rootComponentProps, |
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.
hmm why was the order changed? Shouldn't the getRootProps()
comes last as they are adding some event handlers?
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.
This way devs are able to override whatever the hook is setting. But I see there's a problem with event handlers. I suppose the order should be as following:
- Props from the hook (with event handlers passed in to be chained)
- Additional props from the component (
...other
) with removed event handlers - only for the root slot componentsProps.<slot name>
with removed event handlersclassName
- merged fromclassName
prop,componentsProps.*.className
and slot-specific classesref
- if necessary
Developers would be able to override whatever prop they need and if they provide an event handler, it would be chained with the hook's handlers.
We could use a function that would encapsulate this logic and ensure this works consistently across our components.
What do you think?
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.
Yep, let's do this 👍
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.
I created a useSlotProps
function that takes care of everything needed to resolve, merge and supplement the props with ownerState. The preparation of the slots' props in the components is much cleaner now.
...componentsProps.listbox, | ||
...getListboxProps(), | ||
className: clsx(classes.listbox, componentsProps.listbox?.className), | ||
...propsFromHook, |
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.
I like that this was resolved previously it helps with readability :)
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.
Looks much better this way. Good job with the test @michaldudak :)
@siriwatknp you may find the |
Part of #32144 (covers both MenuUnstyled and MenuItemUnstyled).