Skip to content

Should native elements use slots? #581

@LeaVerou

Description

@LeaVerou

This comes up periodically (what reminded me today was whatwg/html#6636 (comment) but this is just one instance of many) and it would be good to have definitive guidance.

Slots are an excellent mechanism for HTML elements to make simple things easy and complex things possible. The pattern is that the component includes UI for certain things, but wraps it in a <slot> so that authors can slot their own UI and replace that part of the component's UI. Since these can be nested, authors can override UI at various levels of granularity.

Native HTML elements need this for exactly the same reasons as web components. However, so far the web platform has refrained for making this functionality available to them, as there is a widespread idea that slots should perhaps be reserved to authorland components, just like parts (which is another discussion…).

So, when this need arises in a native element, we generally auto-slot by element name, and if no suitable element type exists, we invent a brand new one. An example is <summary> where if it's not present the <details> element will generate one. Perhaps <selectedcontent> is another example.

Now, I'm not saying that <summary> was a mistake and it should have been <div slot="summary">. Since it is meant to be an interactive element, this would have likely been the right call anyway, otherwise the interactive element would have needed to be a shadow DOM wrapper. But this is not always the case, and because there is no established pattern for how native elements should solve these use cases (besides inventing new HTML elements, which is a very high bar), the result is often to do nothing. But customization of the UI of native elements is a pain point that always comes up at the top in every State of HTML survey and not everything can be dealt with CSS and pseudo-elements.

I could be missing something, but it seems to me that there is no benefit to having two different mechanisms for slotting, one for native elements and one for custom elements. It does not seem to introduce any ambiguity, since slots are always direct children. Both mechanisms are useful to both and there is a definite learnability benefit to not having distinct mental models for the same concept. Auto-slotting certain element types is definitely better when it comes naturally (for WC as well!), but slots are great for when it doesn't.

If there is TAG consensus on this, it would be helpful to document this guidance, so that it can be referenced in such discussions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions