Skip to content

Add shadowrootslotassignment attribute#43753

Open
keithamus wants to merge 5 commits intomdn:mainfrom
keithamus:add-shadowrootslotassignment-attribute
Open

Add shadowrootslotassignment attribute#43753
keithamus wants to merge 5 commits intomdn:mainfrom
keithamus:add-shadowrootslotassignment-attribute

Conversation

@keithamus
Copy link
Copy Markdown
Contributor

@keithamus keithamus commented Apr 12, 2026

Description

This adds docs for the shadowrootslotassignment attribute.

Motivation

Trying to add the docs so that whatwg/html#12267 can be merged.

Additional details

I've largely cribbed the docs style from the other shadowroot* attributes.

Related issues and pull requests

@keithamus keithamus requested review from a team as code owners April 12, 2026 09:38
@keithamus keithamus requested review from estelle and pepelsbey and removed request for a team April 12, 2026 09:38
@github-actions github-actions Bot added Content:HTML Hypertext Markup Language docs Content:WebAPI Web API docs labels Apr 12, 2026
@github-actions github-actions Bot added the size/s [PR only] 6-50 LoC changed label Apr 12, 2026
@pepelsbey
Copy link
Copy Markdown
Member

Thank you. Is there an existing browser implementation? It seems to be missing from BCD for now.

@pepelsbey
Copy link
Copy Markdown
Member

It seems like it's not implemented yet. So I would keep this PR open until the first experimental implementation lands, as we only document features supported by browsers. Thank you again for doing this in advance!

@keithamus keithamus force-pushed the add-shadowrootslotassignment-attribute branch from c4c9d81 to 01414b1 Compare April 12, 2026 09:58
Comment thread files/en-us/web/html/reference/elements/template/index.md Outdated
Comment thread files/en-us/web/html/reference/elements/template/index.md Outdated
Comment thread files/en-us/web/html/reference/elements/template/index.md
@keithamus
Copy link
Copy Markdown
Contributor Author

It seems like it's not implemented yet. So I would keep this PR open until the first experimental implementation lands, as we only document features supported by browsers. Thank you again for doing this in advance!

Right. https://groups.google.com/a/mozilla.org/g/dev-platform/c/8qxAHE9-Xdo is my intent to ship this in Firefox. I'll work on adding BCD definitions.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 12, 2026

@keithamus keithamus force-pushed the add-shadowrootslotassignment-attribute branch 4 times, most recently from ce2b217 to 24afef4 Compare April 12, 2026 10:11
Comment thread files/en-us/web/api/htmltemplateelement/shadowrootslotassignment/index.md Outdated
Comment on lines +11 to +15
The **`shadowRootSlotAssignment`** property of the {{domxref("HTMLTemplateElement")}} interface reflects the value of the [`shadowrootslotassignment`](/en-US/docs/Web/HTML/Reference/Elements/template#shadowrootslotassignment) attribute of the associated [`<template>`](/en-US/docs/Web/HTML/Reference/Elements/template) element.

Note that this property is not useful for developers.
If a `<template>` element is used to declaratively create a [`ShadowRoot`](/en-US/docs/Web/API/ShadowRoot), then this object and property do not exist.
Otherwise, if an `HTMLTemplateElement` is created, the value of this property is irrelevant because the object is not a shadow root and cannot subsequently be changed to a shadow root. It can be used to detect support for the declarative attribute.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR was was quite helpful for me while trying to figure how to assign shadowrootslotassignment into web-features. Thank you! But after reading this, it sounds like it does have some utility to developers and it might be better to start with that information. Something along these lines:

The shadowRootSlotAssignment property of the HTMLTemplateElement interface reflects the value of the shadowrootslotassignment attribute of the associated <template> element. You can use its existence to detect support for the shadowrootslotassignment attribute.

You can't use this to use this to change…

(This is a drive-by suggestion from someone who knows very, very little about the shadow DOM. Take it with a grain of salt. 😅)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UPdated in 2f3331a to show the only use - feature checking

If set, the shadow root may be serialized by calling the {{DOMxRef('Element.getHTML()')}} or {{DOMxRef('ShadowRoot.getHTML()')}} methods with the `options.serializableShadowRoots` parameter set `true`.
The value defaults to `false`.

- `shadowrootslotassignment`
Copy link
Copy Markdown
Collaborator

@hamishwillee hamishwillee Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't originally assigned to this PR, so I wasn't going to comment.

My problem with this PR is that after reading it I really don't understand what this feature actually does. I was hoping to find the information at this point. It looks like I would have to go to Element.attachShadow() for more detail. Perhaps just a little more detail here would be useful. If not, then all the other links to here need to also point to attachShadow() (including the JavaScript interfaces) to reduce the steps to finding examples etc.

On that note, and example below in this doc would be great.

Copy link
Copy Markdown
Collaborator

@hamishwillee hamishwillee Apr 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two ways to do this now right? Something like ...

Suggested change
The read-only **`slotAssignment`** property of the {{domxref("ShadowRoot")}} interface returns the _slot assignment mode_ for the shadow DOM tree. Nodes are either automatically assigned (`named`) or manually assigned (`manual`).
The value of this property is defined using the `slotAssignment` option when calling {{domxref("Element.attachShadow()")}}, or using the [`shadowrootslotassignment` attribute](/en-US/docs/Web/HTML/Reference/Elements/template#shadowrootslotassignment) on {{htmleelement("template")}} when declaratively creating a shadow root.

@hamishwillee hamishwillee force-pushed the add-shadowrootslotassignment-attribute branch from 4893b1c to dab8147 Compare April 24, 2026 01:59
@hamishwillee
Copy link
Copy Markdown
Collaborator

@keithamus THanks for this. I'm supported to sort out docs for the FF151 release - have updated BCD and release note. Let me know what help you need to push this over the line.

@keithamus
Copy link
Copy Markdown
Contributor Author

@hamishwillee happy to have you take over this PR if you have time. I won’t be able to get to it until late next week or so.

@hamishwillee hamishwillee force-pushed the add-shadowrootslotassignment-attribute branch from dab8147 to 9bee4a2 Compare April 28, 2026 01:39
@github-actions github-actions Bot added size/m [PR only] 51-500 LoC changed and removed size/s [PR only] 6-50 LoC changed labels Apr 28, 2026
@hamishwillee
Copy link
Copy Markdown
Collaborator

@hamishwillee happy to have you take over this PR if you have time. I won’t be able to get to it until late next week or so.

Hi @keithamus

I've updated it "a bit" to what I consider a reasonable minimum - i.e. the point where I understand what is going on here:-)

Specifically I've added examples to the template doc on how this is used, and also on attachShadow() - but only for the named slot assignment case. I know that doesn't actually "say" anything extra, but live examples are like a picture that tells many words.

This needs your review, and then perhaps we should get an independent review from Vadim.

What is missing though is some explanation of why you would ever use manual assignment. There must be good reasons, and it would be great to explain them - and when you would use/not use the feature.

It might make sense to include this in https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements - that could then be the master copy we link to for this kind of stuff.

@pepelsbey pepelsbey removed their request for review May 6, 2026 14:26
@hamishwillee hamishwillee force-pushed the add-shadowrootslotassignment-attribute branch from 9875422 to 292c737 Compare May 8, 2026 04:23
@hamishwillee
Copy link
Copy Markdown
Collaborator

@keithamus Did you see my comment above? ^^^

@pepelsbey Do you think you could review? I could just approve this, but as per #43753 (comment) I did some edits and might be a bit "too close" to the text to be objective now.

@keithamus
Copy link
Copy Markdown
Contributor Author

keithamus commented May 8, 2026

What is missing though is some explanation of why you would ever use manual assignment. There must be good reasons, and it would be great to explain them - and when you would use/not use the feature.

Put simply: if you want different heuristics for slot assignment, rather than the stock slot= attribute. There are two main benefits to deviating from this:

  1. More ergonomic slot assignment, using co-operative elements or heuristics like the element name or a query selector. This means the light dom doesn't need slot= attributes everywhere.
  2. Dynamic slot assignment, where the HTML structure remains static but slots get swapped in and out based on interaction or some other state.

For example you might have a set of tab components which co-operate with one another, such as:

<tab-container>
	<button>One</button>
	<button>Two</button>
	<tab-panel>One's content</tab-panel>
	<tab-panel>Two's content</tab-panel>
</tab-container>

Without manual assignment you'd you'd need to sprinkle slot=tabs, slot=panels everywhere, and the <tab-container> would need to mutate the <tab-panel> elements to add/remove slot attributes, which would cause other side effects such as MutationObservers running, etc.

Another example to demonstrate dynamic slot assignment could be something like a movie picker, where you can filter movies by genre, where movies have multiple dramas:

<movie-picker>
	<label>Genre:
		<select>
			<option>Comedy</option>
			<option>Drama</option>
			<option>Action</option>
			<option>Romance</option>
		</select>
	</label>
	<div data-genre="comedy romance"><h2>Hungover on Valentine's Day</h2></div>
	<div data-genre="drama romance"><h2>Us Two, plus Three</h2></div>
	<div data-genre="action drama"><h2>The Hitman 2: Can't die twice</h2></div>
	<div data-genre="action comedy"><h2>Tinkerbell, the last action hero</h2></div>
</movie-picker>

The element could use manual slot assignment to detect a change event in the select, and slot only the elements who's data-genre matches, thereby not rendering elements that don't match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:HTML Hypertext Markup Language docs Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants