question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[RFC] The way to override components and slots

See original GitHub issue

What’s the problem? 🤔

Before reading this RFC, please go through #34333 first so that we are on the same page about the meaning of components and slots.

From #33416, and #21453 it seems like we are only talking about slot override (replacing the slot). However, for Material UI and Joy UI, we also need a component override (changing the HTML tag of the slot).

What are the requirements? ❓

  • How to override the HTML tag on each slot
  • How to replace the slot with a custom React component

Proposed solution 🟢

My proposed solution aims for the least breaking changes. All components (MUI Base, Material UI, and Joy UI) should follow this:

Components with a single slot (root)

  • component (existing prop): for changing the root slot’s HTML tag. (I think we should not drop this prop because it will be a huge breaking change)

~For this kind of components, it does not make sense to replace the root slot so having just component prop is cleaner.~ This is not true for MUI Base 🤔.

Components with more than one slot

  • component (existing prop): for changing the subcomponent of the root slot (could be an HTML tag or React element).
  • slots and slotsProps same as https://github.com/mui/material-ui/issues/33416
  • slots.{slot}.component: for changing the subcomponent of the target slot (could be an HTML tag or React element).

To replace the HTML of the slot, use slotsProps={{ listbox: { component: 'div' } }}.

Components that have nested components

See the problem and another example

Flatten the nested component slots with a new name. For example, TextField has Input as a nested component could look something like this:

<TextField
  slotsProps={{
    root: {...},
    inputRoot: { ... },
    inputInput: { ... },
    helperText: { ... }
  }}
/>

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
siriwatknpcommented, Sep 20, 2022

Marked this as RFC for Material UI and Joy UI only.

1reaction
siriwatknpcommented, Sep 20, 2022

so we should need only one component prop per component.

Yes, the component prop can be considered as a shortcut for slotsProps.component. If both are provided, slotsProps.component should have higher priority. cc @michaldudak

Btw, this is how it already works in Material UI, so I don’t expect big change anyway, unless I am misunderstanding the proposal.

I’d say that I expect big change for Material UI because most components are using another pattern. e.g. Accordion has TransitionComponent and TransitionProps. This will be breaking changes:

// current
<Accordion TransitionComponent={Slide} TransitionProps={{ delay: 100 }} />

// new
<Accordion slots={{ transition: Slide }} slotsProps={{ transition: { delay: 100 } }} />
Read more comments on GitHub >

github_iconTop Results From Across the Web

rfc7953 - IETF
This specification defines a new type of iCalendar component that can be used to publish user availability. CalDAV [RFC4791] provides a way for...
Read more >
RFC - 0114-dynamic-component-configuration - RFCs
When the lwc:bind contains an attribute that is already defined via the template, we overwrite that attribute with the bind value. For example,...
Read more >
How to override component on specific slot - Stack Overflow
The best way is to create a new CMS component only for that specific slot/section on homepage, because once you update a component, ......
Read more >
Building Component Slots in React | Articles - Sandro Roth
Slots allow us to pass elements to a component and tell the component where these elements should be rendered. A component may support...
Read more >
Slots - Vue.js
In 2.6.0, we introduced a new unified syntax (the v-slot directive) for named and scoped slots. It replaces the slot and slot-scope attributes, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found