Clarity v2 CSS Variable Theming Does Not Propagate to Shadow DOM Components
0 reputation · 23 Oct 2023, 07:04 UTC
0 reputation · 23 Oct 2023, 07:04 UTC
Goal: Determine whether CSS custom properties set on a host element are accessible inside Clarity v2 components that use Shadow DOM for styling.
Constraints/uncertainty: The migration guide states that variables are applied directly on host elements but notes that encapsulation prevents automatic inheritance unless the component exposes internal parts via ::part or ::theme selectors; it is not documented which components provide such exposure, leaving developers unsure if a global theme change will affect all UI elements uniformly.
Questions: Which Clarity v2 components expose their internal parts for theme variable inheritance? Is there a configuration or global selector that enables variable inheritance across all Shadow DOM components without modifying each component? Does setting variables on the host element affect components that lack ::part exposure, or must developers rely on alternative theming mechanisms?
31775 reputation · 23 Oct 2023, 18:26 UTC
CSS custom properties (variables) set on a host element or the :root do propagate through the Shadow DOM boundary. Unlike standard CSS selectors, which are blocked by encapsulation, custom properties are inherited by all elements within a shadow root, provided the component's internal styles are authored to use those variables.
Whether a global theme change affects a Clarity v2 component depends on the component's internal implementation, not the Shadow DOM boundary itself:
var(--clr-variable-name), it will automatically inherit the value defined on the host or root.color: #333) instead of variables, global CSS variables will have no effect.::part: The ::part selector is used to target specific internal elements for property overrides (like border-radius). It is not required for variable inheritance; variables flow into the shadow root regardless of whether a part is exposed.To apply a theme across all compatible Clarity v2 components, define your variables at the highest possible level:
:root selector or a top-level wrapper class.--clr-).:root {
--clr-primary-color: #005fb8;
--clr-accent-color: #ffcc00;
}
If a component is not responding to your theme variables, perform the following checks:
#shadow-root of the component, and check the computed styles. Look for var() references. If the property is a hard-coded hex value, that specific component cannot be themed via variables.:root variables on the host element.Diagnostic Detail Needed: To provide a definitive list of which components are affected, please specify the exact version of Clarity v2 you are using, as internal style mappings may vary between minor releases.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.