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] Meaning of components and slots

See original GitHub issue

What’s the problem? 🤔

From #33416 and #21453, I feel that we are mixing slots and components together even though they are not the same.

By having an agreement on the meaning, I believe that it will be easier to decide on the APIs (to override components and slots).

What are the requirements? ❓

There are real use cases where you want to replace the component (HTML tag) or the slot.

Slot override

When: developers want to take control of the logic, and styles of the default slot. Use case: GitHub label example, the popper’s logic has to be replaced to show options below the input.

Component override

When: developers want to change the HTML tag, normally for accessibility. (component override is more common than slot override). Alternatively, they want to add custom logic by preserving the default styles. Use case:

  • changing button to a
  • changing ul to div in the Autocomplete for grouped options
  • access ownerState and add some logic + styles.

Proposed solution 🟢

This can apply to MUI Base, Material UI and Joy UI.

Slots

All MUI components have at least 1 slot, aka root slot. The slot can be a string (DOM) or a styled component. In MUI Base, the slots are mostly strings because it is unstyled. However, for Material UI, Joy UI, the slots are mostly styled-components.

The name of the slot can be seen in the suffix of the className.

Components

Represents the HTML tag used in the slot. For example, Joy UI’s Input has 2 slots (root and input):

  • The component of the root slot is <div>.
  • The component of the input slot is <input>.,

Resources and benchmarks 🔗

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
michaldudakcommented, Sep 19, 2022

Consider the following simplified example - a Button component from a styled library (like Material UI or Joy UI). Its (greatly simplified) implementation could look like that:

const ButtonRoot = styled('button')(`
  // CSS styles go here
`);

const Button = (props) => {
  return <ButtonRoot />
}

Now, there are two things we can customize here:

  1. replace the ButtonRoot component with something else
  2. replace the button with something else

As I understand it, in this particular case, we’re referring to the ButtonRoot as the root slot and to the button as the root component.

This distinction only makes sense in the styled components. In MUI Base, in most cases, slots are HTML elements, so there’s just one thing to customize:

const ButtonRoot = 'button';

const ButtonUnstyled = (props) => {
  return <ButtonRoot />
}
1reaction
siriwatknpcommented, Oct 12, 2022

I think we are close to the conclusion here, so let’s pick one and use it across the documentation:

Available options:

  1. subcomponent
  2. leaf component
  3. rendered component
  4. as component
  5. forwarded component
Read more comments on GitHub >

github_iconTop Results From Across the Web

RFC 8141: Uniform Resource Names (URNs)
Code Components extracted from this document must include Simplified BSD License ... Locator: An identifier that provides a means of accessing a resource....
Read more >
Calendar Availability RFC 7953 - IETF Datatracker
Component Registrations This document defines the following new iCalendar components, which have been added to the registry defined in Section 8.3.1 of [RFC ......
Read more >
Blog - Layouts RFC | Next.js
A layout can be defined by default exporting a React component from a layout.js file. The component should accept a children prop which...
Read more >
The Ember Times - Issue No. 168
With the Yieldable Named Blocks RFC accepted and the ... sense to provide several "slots" that the user of the component can customize....
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