Skip to content
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

Merged
merged 7 commits into from Jun 10, 2022

Conversation

michaldudak
Copy link
Member

Part of #32144 (covers both MenuUnstyled and MenuItemUnstyled).

@michaldudak michaldudak added new feature New feature or request component: menu This is the name of the generic UI component, not the React module! package: base-ui Specific to @mui/base labels Jun 2, 2022
@michaldudak michaldudak requested a review from a team June 2, 2022 14:12
@mui-bot
Copy link

mui-bot commented Jun 2, 2022

Details of bundle changes

@material-ui/unstyled: parsed: +1.07% , gzip: +1.02%

Generated by 🚫 dangerJS against a24c5b2

Copy link
Member

@mnajdova mnajdova left a 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,
Copy link
Member

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?

Copy link
Member Author

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:

  1. Props from the hook (with event handlers passed in to be chained)
  2. Additional props from the component (...other) with removed event handlers - only for the root slot
  3. componentsProps.<slot name> with removed event handlers
  4. className - merged from className prop, componentsProps.*.className and slot-specific classes
  5. ref - 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?

Copy link
Member

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 👍

Copy link
Member Author

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,
Copy link
Member

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 :)

Copy link
Member

@mnajdova mnajdova left a 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 :)

@michaldudak
Copy link
Member Author

@siriwatknp you may find the useSlotProps handy if you're creating Joy components using Base hooks.

@michaldudak michaldudak merged commit 3f95437 into mui:master Jun 10, 2022
@michaldudak michaldudak deleted the componentsProps/menu branch June 10, 2022 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: menu This is the name of the generic UI component, not the React module! new feature New feature or request package: base-ui Specific to @mui/base
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants