1. Introduction
Shadow DOM allows authors to separate their page into "components", subtrees of markup whose details are only relevant to the component itself, not the outside page. This reduces the chance of a style meant for one part of the page accidentally over-applying and making a different part of the page look wrong. However, this styling barrier also makes it harder for a page to interact with its components when it actually wants to do so.
This specification defines the ::part() pseudo-element, which allows an author to style specific, purposely exposed elements in a shadow tree from the outside page’s context. In combination with custom properties, which let the outside page pass particular values (such as theme colors) into the component for it to do with as it will, these pseudo-elements allow components and the outside page to interact in safe, powerful ways, maintaining encapsulation without surrendering all control.
1.1. Motivation
For custom elements to be fully useful and as capable as built-in elements it should be possible for parts of them to be styled from outside. Exactly what can be styled from outside should be controlled by the element author. Also, it should be possible for a custom element to present a stable "API" for styling. That is, the selector used to style a part of a custom element should not expose or require knowledge of the internal details of the element. The custom element author should be able to change the internal details of the element while leaving the selectors untouched.
The previous proposed method for styling inside the shadow tree, the >>> combinator, turned out to be too powerful for its own good; it exposed too much of a component’s internal structure to scrutiny, defeating some of the encapsulation benefits that using Shadow DOM brings. For this, and other performance-related reasons, the >>> combinator was eventually removed from the live profile.
This left us with using custom properties as the only way to style into a shadow tree: the component would advertise that it uses certain custom properties to style its internals, and the outer page could then set those properties as it wished on the shadow host, letting inheritance push the values down to where they were needed. This works very well for many simple theming use-cases.
However, there are some cases where this falls down. If a component wishes to allow arbitrary styling of something in its shadow tree, the only way to do so is to define hundreds of custom properties (one per CSS property they wish to allow control of), which is obviously ridiculous for both usability and performance reasons. The situation is compounded if authors wish to style the component differently based on pseudo-classes like :hover; the component needs to duplicate the custom properties used for each pseudo-class (and each combination, like :hover:focus, resulting in a combinatorial explosion). This makes the usability and performance problems even worse.
We introduce ::part() to handle this case much more elegantly and performantly. Rather than bundling everything into custom property names, the functionality lives in selectors and style rule syntax, like it’s meant to. This is far more usable for both component authors and component users, should have much better performance, and allows for better encapsulation/API surface.
It’s important to note that ::part() offers absolutely zero new theoretical power. It is not a rehash of the >>> combinator, it is simply a more convenient and consistent syntax for something authors can already do with custom properties. By separating out the explicitly "published" parts of an element (the part element map) from the sub-parts that it merely happens to contain, it also helps with encapsulation, as authors can use ::part() without fear of accidental over-styling.
2. Exposing a Shadow Element:
Elements in a shadow tree may be exported for styling by stylesheets outside the tree using the part and exportparts attributes.Each element has a part name list which is an ordered set of tokens.
Each element has a forwarded part name list which is a list of pairs containing a string for the inner part being forwarded and a string giving the name it will be exposed as.
Each shadow root can be thought of as having a part element map with keys that are strings and values that are ordered sets of elements.
The part element map is described only as part of the algorithm for calculating style in this spec. It is not exposed via the DOM, as calculating it may be expensive and exposing it could allow access to elements inside closed shadow roots.
Part element maps are affected by the addition and removal of elements and changes to the part name lists and forwarded part name lists of elements in the DOM.
-
For each descendant el within outerRoot:
-
For each name in el’s part name list, append el to outerRoot’s part element map[name].
-
If el is a shadow host itself then let innerRoot be its shadow root.
-
Calculate innerRoot’s part element map.
-
For each innerName/outerName in el’s forwarded part name list:
-
If innerName is an ident:
-
Let innerParts be innerRoot’s part element map[innerName]
-
Append the elements in innerParts to outerRoot’s part element map[outerName]
-
-
If innerName is a pseudo-element name:
-
Append innerRoot’s pseudo-element(s) with that name to outerRoot’s part element map[outerName].
-
-
-
2.1. Naming a Shadow Element: the part
attribute
Any element in a shadow tree can have a part
attribute.
This is used to expose the element outside of the shadow tree.
The part attribute is parsed as a space-separated list of tokens representing the part names of this element.
Note: It’s okay to give a part multiple names. The "part name" should be considered similar to a class, not an id or tagname.
<style> c-e::part(textspan) { color: red; } </style> <template id="c-e-template"> <span part="textspan">This text will be red</span> </template> <c-e></c-e> <script> // Add template as custom element c-e ... </script>
2.2. Forwarding a Shadow Element: the exportparts
attribute
Any element in a shadow tree can have a exportparts
attribute.
If the element is a shadow host,
this is used to allow styling of parts from hosts inside the shadow tree by rules outside this the shadow tree (as if they were elements in the same tree as the host,
named by a part attribute).
The exportparts attribute is parsed as a comma-separated list of part mappings. Each part mapping is one of:
innerIdent : outerIdent
-
Adds
innerIdent
/outerIdent
to el’s forwarded part name list. ident
-
Adds
ident
/ident
to el’s forwarded part name list.Note: This is shorthand for
ident : ident
. ::ident : outerIdent
-
If
::ident
is the name of a part-like pseudo-element, adds::ident
/outerIdent
to el’s forward part name list. Otherwise, does nothing. - anything else
-
Ignored for error-recovery / future compatibility.
Note: It’s okay to map a sub-part to several names.
<style> c-e::part(textspan) { color: red; } </style> <template id="c-e-outer-template"> <c-e-inner exportparts="innerspan: textspan"></c-e-inner> </template> <template id="c-e-inner-template"> <span part="innerspan"> This text will be red because the containing shadow host forwards innerspan to the document as "textspan" and the document style matches it. </span> <span part="textspan"> This text will not be red because textspan in the document style cannot match against the part inside the inner custom element if it is not forwarded. </span> </template> <c-e></c-e> <script> // Add template as custom elements c-e-inner, c-e-outer ... </script>
exportparts
attribute,
to masquerade as a ::part() for the component it’s in:
<template id=custom-element-template> <p exportparts="::before : preceding-text, ::after : following-text"> Main text. </template>
An element using that template can use a selector like x-component::part(preceding-text) to target the p::before pseudo-element in its shadow, so users of the component don’t need to know that the preceding text is implemented as a pseudo-element.
3. Selecting a Shadow Element: the ::part() pseudo-element
The ::part() pseudo-element
allows you to select elements that have been exposed via a part
attribute.
The syntax is:
::part() = ::part( <ident>+ )
The ::part() pseudo-element only matches anything when the originating element is a shadow host.
part="label"
),
you can select it with x-button::part(label). A tabstrip control might have multiple elements
with part="tab"
,
all of which are selected by ::part(tab).
If a single tab is active at a time,
it can be specially indicated with part="tab active"
and then selected by ::part(tab active) (or ::part(active tab), as order doesn’t matter).
The ::part() pseudo-element is a part-like pseudo-element. If the originating element’s shadow root’s part element map contains the specified <ident>, ::part() represents the elements keyed to that ident; if multiple idents are provided and the part element map contains them all, it represents the intersection of the elements keyed to each ident. Otherwise, it matches nothing.
::part() pseudo-elements inherit according to their position in the originating element’s shadow tree.
If the <x-panel>
’s internal confirm button had used something like part="label => confirm-label"
to forward the button’s internal parts up into the panel’s own part element map,
then a selector like x-panel::part(confirm-label) would select just the one button’s label,
ignoring any other labels.
4. Extensions to the Element
Interface
partial interface Element { [SameObject ,PutForwards =value ]readonly attribute DOMTokenList ; };
part
The part attribute’s getter must return a DOMTokenList object whose associated element is the context object and whose associated attribute’s local name is part. The token set of this particular DOMTokenList object are also known as the element’s parts.
Define this as a superglobal in the DOM spec. [Issue #w3c/csswg-drafts#3424]
5. Microsyntaxes for parsing
5.1. Rules for parsing part mappings
A valid part mapping is a pair of tokens separated by a U+003A COLON character and any number of space characters before or after the U+003A COLON The tokens must not contain U+003A COLON or U+002C COMMA characters.
The rules for parsing a part mapping are as follows:
-
Let input be the string being parsed.
-
Let position be a pointer into input, initially pointing at the start of the string.
-
Collect a sequence of code points that are space characters
-
Collect a sequence of code points that are not space characters or U+003A COLON characters, and let first token be the result.
-
If first token is empty then return error.
-
Collect a sequence of code points that are space characters.
-
If the end of the input has been reached, return the pair first token/first token
-
If character at position is not a U+003A COLON character, return error.
-
Consume the U+003A COLON character.
-
Collect a sequence of code points that are space characters.
-
Collect a sequence of code points that are not space characters or U+003A COLON characters. and let second token be the result.
-
If second token is empty then return error.
-
Collect a sequence of code points that are space characters.
-
If position is not past the end of input then return error.
-
Return the pair first token/second token.
5.2. Rules for parsing a list of part mappings
A valid list of part mappings is a number of valid part mappings separated by a U+002C COMMA character and any number of space characters before or after the U+002C COMMA
The rules for parsing a list of part mappings are as follow:
-
Let input be the string being parsed.
-
Split the string input on commas. Let unparsed mappings be the resulting list of strings.
-
Let mappings be an initially empty list of pairs of tokens. This list will be the result of this algorithm.
-
For each string unparsed mapping in unparsed mappings, run the following substeps:
-
If unparsed mapping is empty or contains only space characters, continue to the next iteration of the loop.
-
Let mapping be the result of parsing unparsed mapping using the rules for parsing part mappings.
-
If mapping is an error then continue to the next iteration of the loop. This allows clients to skip over new syntax that is not understood.
-
Append mapping to mappings.
-