-
Notifications
You must be signed in to change notification settings - Fork 22.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document ShadowRoot.slotAssignment (#25649)
* Document ShadowRoot.slotAssignment * Update files/en-us/web/api/shadowroot/slotassignment/index.md Co-authored-by: Ruth John <Rumyra@users.noreply.github.com> --------- Co-authored-by: Ruth John <Rumyra@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
86 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
title: ShadowRoot.slotAssignment | ||
slug: Web/API/ShadowRoot/slotAssignment | ||
page-type: web-api-instance-property | ||
browser-compat: api.ShadowRoot.slotAssignment | ||
--- | ||
|
||
{{APIRef("Shadow DOM")}} | ||
|
||
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 defined using the `slotAssignment` option when calling {{domxref("Element.attachShadow()")}}. | ||
|
||
## Value | ||
|
||
A string that can be one of: | ||
|
||
- `named` | ||
- : Elements are automatically assigned to {{HTMLElement("slot")}} elements within this shadow root. Any descendents of the host with a `slot` attribute which matches the `name` attribute of a `<slot>` within this shadow root will be assigned to that slot. Any top-level children of the host with no `slot` attribute will be assigned to a `<slot>` with no `name` attribute (the "default slot") if one is present. | ||
- `manual` | ||
- : Elements are not automatically assigned to {{HTMLElement("slot")}} elements. Instead, they must be manually assigned with {{domxref("HTMLSlotElement.assign()")}}. | ||
|
||
## Examples | ||
|
||
In the example below, the `assign()` method is used to display the correct tab in a tabbed application. The function is called and passed the panel to show, which is then assigned to the slot. We test if the `slotAssignment` is `named` to prevent an exception to be raised when {{domxref("HTMLSlotElement.assign()")}} is called. | ||
|
||
```js | ||
function UpdateDisplayTab(elem, tabIdx) { | ||
const shadow = elem.shadowRoot; | ||
|
||
// This test is usually not needed, but can be useful when debugging | ||
if (shadow.slotAssignment === "named") { | ||
console.error( | ||
"Trying to manually assign a slot on an automatically-assigned (named) slot" | ||
); | ||
} | ||
const slot = shadow.querySelector("slot"); | ||
const panels = elem.querySelectorAll("tab-panel"); | ||
if (panels.length && tabIdx && tabIdx <= panels.length) { | ||
slot.assign(panels[tabIdx - 1]); | ||
} else { | ||
slot.assign(); | ||
} | ||
} | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("Element.attachShadow()")}} | ||
- {{domxref("HTMLSlotElement.assign()")}} |