parts-in-scope
A component's parts are styled only inside its `@scope (root) to ([data-component])`, written after its root block, in the same file.
- Severity
- error, and a warning for the two placement checks
- Fixable
- no
Checks
Section titled “Checks”- A
[data-part]selector appears only inside its component’s scope. - A component lives in one kind of CSS in its file: scoped or global. A file may hold both, like a Vue file’s
<style scoped>and plain<style>, an Astro file’s<style>and<style is:global>, or a Svelte<style>’s scoped rules and its global CSS. In Svelte, global CSS is what ships without the file’s class: a:global { }block, a selector that starts with:globaland a space, and a selector that is:global(…)alone. In:global(body) [data-component="tabs"], Svelte adds the class to the root, so it stays scoped. One component styled in both (its root block in one, and its scope or another root block in the other) is reported where the second kind starts, naming the line of the first. The two merge in the browser, and which declaration wins depends on the selectors each framework ships, so it’s easy to get wrong. Two<style scoped>blocks are one kind, and a CSS Module’s classes are its own, so neither is reported. - The scope is in the file that holds the component’s root block. A scope for a component whose root block is in another file (
@scope ([data-component="tabs"]) to ([data-component])alone) is reported. What another file wants to style there is either a component of its own, placed in a slot, or something the component exposes through its component variables. A Vue file’s plain<style>and its<style scoped>are one file, and so are an Astro file’s<style>and<style is:global>. Syntax the rule can’t read outside any block (@include tabs-root;, or a rule whose selector it can’t read) may write the root block, so the scope isn’t reported then. An@includeinside the scope can’t, so it excuses nothing. - The root block and the scope name the component in the form the
componentRootsetting chooses:[data-component="name"]by default, or a class (.name) where the project chooses"class", as CSS Modules needs. A root written in the other form is reported once for each place it’s written, with the other form suggested from the component’s name: the@componenttag, else the attribute’s value, else the class in kebab-case. A class written beside the attribute with the component’s own name ([data-component="tabs"].tabs) is reported as the class to remove. A different class there ([data-component="card"].is-open) is a condition, so it’s left alone, and so is a class inside a function ([data-component="tabs"]:where(.tabs)), which can’t be removed on its own. Only an attribute at the compound’s own level counts: inside:not()it’s excluded, and inside:is()it’s one alternative. The root carriesdata-componentin the markup either way, because the scope’s limit stops at it. - A part selected with no component is reported too, on its own (
[data-part="title"]) or from a global element (main [data-part="title"],@scope (main) { [data-part="title"] { … } }): it reaches into every component that has a part of that name. - A scope with no component (
main { @scope (section) { … } }) is outside the rule, and so are a utility,:deep()and:global(). - The selector is checked, not the body: a rule that holds only an
@include, or nothing, is checked like any other. - Two exceptions, both in the root block:
- a placement path that steps from the root through parts, each with
>, and ends on a slot itself:& > [data-part="footer"] > [data-slot="action"]. A plain element or a sibling on the way, or the slot’s::after, takes it outside the exception. The path is reported once, where it first goes wrong; - a part named inside
:has(), to reach a state or a variant that lives on it:&:has(:where([data-part="input"]):disabled), also inside the guard’s:not().:has()reaches into the root, as a descendant or with>, never a sibling with+or~.
- a placement path that steps from the root through parts, each with
- A component’s scope has
to ([data-component])as its limit: no other limit, and not none. The limit is checked as it will ship:- in an Astro
<style>(withoutis:globaloris:inline), Astro adds the file’s scope to every selector, the limit included, soto ([data-component])would stop only at components written in that file. There, the limit is writtento (:global([data-component])), which Astro ships asto ([data-component]); - everywhere else (a
.cssfile, an Astro<style is:global>, a Vue<style>or<style scoped>, Svelte, CSS Modules), the compiler leaves the limit as written, so a:global()in it is reported.
- in an Astro
- A component’s scope is written after its root block, not before it and not inside it.
- Inside a scope,
:scopeappears only as:scope >:& >is written:scope >;&or:scopebefore a descendant (:scope [data-part="icon"]) is left out;:scope +and& ~select a sibling of the root, which is outside the scope. The message says so;:scopewith a condition (:scope:hover,:scope[data-size="small"] [data-part="icon"],&:not(.quiet) >) is a variant or a state of the root, even when it can’t be told which. It belongs in the root block. The message quotes the whole condition as written, with every bracket it opens closed (:hover:where(:not(:disabled)));:scopeon its own (:scope { … }), and declarations written directly in the scope, style the root. They belong in the root block. A pseudo-element of the root (:scope::after) moves into the root block as a whole rule,&::after.
- There’s no
--fix. Changing a root’s form without the markup drops the component’s styles. Removing:scopelowers a rule’s specificity, and turning& >into:scope >raises it, so either can change which rule wins against another in the scope. The author makes the change and checks it. - The same holds for the scope’s own root: a condition there (
@scope ([data-component="button"]:hover) to ([data-component])) is a variant or state selecting the parts. It belongs in the root block, setting part variables that the parts read. - A
[data-part]attribute on a component root’s own compound ([data-component="tab"][data-part="tab"]) is reported: an element is a component or a part, never both. - Two warnings about placing other components from the root block:
- in an Astro
<style>(withoutis:globaloris:inline), a slot written without:global():& > [data-slot="action"]. Astro adds the file’s scope to it, so a component from another file placed there won’t match. It works while both are in the same file, which is why it’s a warning.[data-slot]with no value counts too; - another component’s root selected by its class:
& > .sidebar, where this file styles.sidebaras a component. It stops working when Sidebar moves to its own file (Astro scopes the class) or under CSS Modules (the class is hashed). The slot is the selector that survives both. A class is reported only when the file shows it’s a component, by the same signs as a class root (below); a plain class (.story { & > .hint { … } } .hint { … }) isn’t. The suggested selector keeps the path and every condition on it, the root’s and the placed component’s included, and replaces only the class:& > [data-part="footer"] > [data-slot="pager"], with the slot in:global()in an Astro<style>. A part on that path isn’t reported as outside the scope too, since it becomes a placement path once the class is a slot. A path with a descendant step (& .pager) gets no selector, only the way to write one: from the root through parts, each with>, into the slot. A rule that styles inside the placed component (& > .pager > span,& > .pager::after) is told to move to that component’s own file.
- in an Astro
- One finding for each written thing: a selector list under two components (
[data-component="card"], [data-component="button"] { & [data-part="icon"] { … } }) gets one finding that names both scopes.
A part selected outside its component’s scope reaches into every nested component that has a part of the same name. The scope’s [data-component] limit keeps a component’s rules inside it, and it only works when every part rule is under it.
A scope nested in the root block changes its specificity when a build lowers nesting, and without (&) it applies nothing. Inside a scope, a build that lowers nesting writes & as :scope. :scope before a descendant adds (0,1,0) and matches what the descendant alone matches, so a component’s part rules end up at two specificities. A root state written in the scope beats the root block’s blocking states. A bare :scope beats the root block through scope proximity, even when the root block comes later.
A part outside its component’s scope
Section titled “A part outside its component’s scope”Reported:
[data-component="button"] [data-part="icon"] { inline-size: 1em;}Fixed:
[data-component="button"] { display: inline-flex;}@scope ([data-component="button"]) to ([data-component]) { [data-part="icon"] { inline-size: 1em; }}A root in the form the setting doesn’t allow
Section titled “A root in the form the setting doesn’t allow”Reported, with the default "componentRoot": "attribute":
.pager { display: flex;}@scope (.pager) to ([data-component]) { [data-part="link"] { padding: var(--t-space-2); }}Fixed, by hand, with data-component="pager" on the root’s markup:
[data-component="pager"] { display: flex;}@scope ([data-component="pager"]) to ([data-component]) { [data-part="link"] { padding: var(--t-space-2); }}With "componentRoot": "class", it’s the other way round: the attribute root is reported, and .pager is suggested. A class root can’t show a missing data-component, so the markup still needs it for the scope’s limit to work.
A class root is reported only when the file shows it’s a component: it has a scope, an @component tag, a part, a slot, a state, part or private variable on its root, or data-component with its own name beside the class. A lone class with plain declarations (.visually-hidden { … }) isn’t reported: it may be a utility that @import url("utilities.css") layer(utilities) puts in its layer, which its own file can’t show. A class written directly in @layer utilities is never a component.
Placing another component by its class (warning)
Section titled “Placing another component by its class (warning)”Reported:
[data-component="docs"] { display: grid; & > .sidebar { grid-area: sidebar; }}/** @component sidebar */.sidebar { display: block;}Fixed: the Sidebar’s root carries data-slot="sidebar".
[data-component="docs"] { display: grid; & > [data-slot="sidebar"] { grid-area: sidebar; }}A slot without :global() in an Astro <style> (warning)
Section titled “A slot without :global() in an Astro <style> (warning)”Reported:
<style> [data-component="card"] { & > [data-slot="action"] { justify-self: end; } }</style>Fixed:
<style> [data-component="card"] { & > :global([data-slot="action"]) { justify-self: end; } }</style>A scope with no root block in its file
Section titled “A scope with no root block in its file”Styling another file’s parts ties the two files together. When tabs renames or moves a part, the other file breaks, and nothing in tabs shows it.
Reported:
/* tab-panel.css: the root block of tabs is in tabs.css */@scope ([data-component="tabs"]) to ([data-component]) { [data-part="list"] { gap: 0.5rem; }}Fixed, in the file that holds the root block. To change it from another file, place that file’s component in a slot of tabs, or have tabs read a component variable the other file sets.
/* tabs.css */[data-component="tabs"] { display: grid;}@scope ([data-component="tabs"]) to ([data-component]) { [data-part="list"] { gap: 0.5rem; }}A component in both the scoped and the global CSS of its file
Section titled “A component in both the scoped and the global CSS of its file”The two merge in the browser, and which declaration wins depends on the selectors each framework ships. The rules can’t check the component whole either.
With componentRoot: "attribute", a plain class is no component, so .prose in the scoped CSS and .prose pre in the global CSS aren’t reported. A class counts once the file shows it’s a component: an @component tag, a scope, a part, or a state or part variable.
Reported, in an Astro file: the scope moved to the global block to write its limit without :global().
<style> [data-component="tabs"] { display: grid; }</style><style is:global> @scope ([data-component="tabs"]) to ([data-component]) { [data-part="list"] { gap: 0.5rem; } }</style>Fixed: both in the scoped <style>, with the limit Astro needs there.
<style> [data-component="tabs"] { display: grid; } @scope ([data-component="tabs"]) to (:global([data-component])) { [data-part="list"] { gap: 0.5rem; } }</style>Reported, in a Svelte file: the root block written in :global(), which Svelte ships without the file’s class.
<style> :global([data-component="tabs"]) { display: grid; } @scope ([data-component="tabs"]) to ([data-component]) { [data-part="list"] { gap: 0.5rem; } }</style>Fixed: both scoped. :global() around one piece of a selector, such as content from another file ([data-component="tabs"] :global(p)), stays an escape and isn’t reported.
A limit that stops at any class
Section titled “A limit that stops at any class”A part inside a utility-classed wrapper is missed.
Reported:
[data-component="card"] { display: grid;}@scope ([data-component="card"]) to ([class]) { [data-part="title"] { font-weight: 600; }}Fixed:
[data-component="card"] { display: grid;}@scope ([data-component="card"]) to ([data-component]) { [data-part="title"] { font-weight: 600; }}A scope nested in the root block
Section titled “A scope nested in the root block”Reported:
[data-component="button"] { @scope (&) to ([data-component]) { [data-part="icon"] { inline-size: 1em; } }}Fixed:
[data-component="button"] {}@scope ([data-component="button"]) to ([data-component]) { [data-part="icon"] { inline-size: 1em; }}A root state written inside the scope
Section titled “A root state written inside the scope”It beats the root block’s blocking states.
Reported:
[data-component="button"] { --s-opacity: 1; opacity: var(--s-opacity); &:disabled { --s-opacity: 0.5; }}@scope ([data-component="button"]) to ([data-component]) { :scope:hover { --s-opacity: 0.9; }}Fixed:
[data-component="button"] { --s-opacity: 1; opacity: var(--s-opacity); &:where(:not(:disabled, [aria-disabled="true"], [data-loading], [inert])) { &:hover { --s-opacity: 0.9; } } &:disabled { --s-opacity: 0.5; }}A state in the scope’s own root
Section titled “A state in the scope’s own root”It selects every part inside the scope.
Reported:
@scope ([data-component="button"]:hover) to ([data-component]) { [data-part="icon"] { opacity: 0.9; }}Fixed:
[data-component="button"] { --p-icon-opacity: 1; &:hover { --p-icon-opacity: 0.9; }}@scope ([data-component="button"]) to ([data-component]) { [data-part="icon"] { opacity: var(--p-icon-opacity); }}& and :scope before a part inside a scope
Section titled “& and :scope before a part inside a scope”Reported:
[data-component="button"] { display: inline-flex;}@scope ([data-component="button"]) to ([data-component]) { & > [data-part="icon"] { inline-size: 1em; } :scope [data-part="label"] { color: red; }}Fixed, by hand:
[data-component="button"] { display: inline-flex;}@scope ([data-component="button"]) to ([data-component]) { :scope > [data-part="icon"] { inline-size: 1em; } [data-part="label"] { color: red; }}A limit in an Astro <style>
Section titled “A limit in an Astro <style>”<!-- Reported: Astro ships the limit as `[data-component]` with the file's scope added, so it stops only at components written in this file --><style> [data-component="card"] { display: grid; } @scope ([data-component="card"]) to ([data-component]) { [data-part="title"] { font-weight: 600; } }</style><!-- Fixed --><style> [data-component="card"] { display: grid; } @scope ([data-component="card"]) to (:global([data-component])) { [data-part="title"] { font-weight: 600; } }</style>In a scoped <style>, what comes from another file is selected through the framework’s escape, so a reach across files is explicit: Markdown content and slotted children (:global(p)), a component placed in a slot (& > :global([data-slot="action"])), and a part the consumer writes inside a slot. In Vue, :global() makes the whole rule global, so the escape inside a component is :deep() or :slotted(). The rules skip what an escape selects.
Not reported
Section titled “Not reported”- Placement from the root block through parts into a slot:
[data-component="card"] { & > [data-part="footer"] > [data-slot="action"] { justify-self: end; } }. - A state on a part reached from the root block:
[data-component="field"] { &:has(:where([data-part="input"]):disabled) { --s-opacity: 0.6; } }, with>too, and the same state inside the guard’s:not(). - A scope with no component, whatever its body holds:
main { @scope (section) { p { … } } }. - A utility selecting a part:
@layer utilities { .stack [data-part="icon"] { @include paint; } }. - A scope in a file whose syntax the rule can’t read outside any block, which may write the root block:
@include tabs-root;before@scope ([data-component="tabs"]) to ([data-component]) { … }. - A lone class with plain declarations, under the default setting:
.visually-hidden { position: absolute; },.link:hover { text-decoration: underline; }. - A class on the document element:
:root.dark,html.dark. It’s a condition of the page, not a component. - Bare elements and other classes inside a scope (
[data-part="body"] p,.hljs-keyword): the scope stops at components, so they’re the author’s call.
Options
Section titled “Options”None of its own. It reads four statefulcss.config settings: componentRoot (attribute by default, or class), and componentAttribute, partAttribute and slotAttribute (data-component, data-part and data-slot by default).