Shadow DOM

Learn how to use Shadow DOM in Aurelia components for style encapsulation and native web component features.

Shadow DOM provides native browser encapsulation for your components, isolating styles and DOM structure. Aurelia makes it easy to enable Shadow DOM for any custom element.

Enabling Shadow DOM

Using the @useShadowDOM Decorator

The simplest way to enable Shadow DOM is with the @useShadowDOM decorator:

import { customElement, useShadowDOM } from 'aurelia';

@customElement('my-card')
@useShadowDOM()
export class MyCard {
  message = 'Hello from Shadow DOM';
}

By default, this creates a shadow root with mode: 'open'.

Configuring Shadow DOM Mode

Shadow DOM supports two modes: open and closed.

Open mode (default) allows external JavaScript to access the shadow root:

@customElement('open-element')
@useShadowDOM({ mode: 'open' })
export class OpenElement {
  // External code can access: element.shadowRoot
}

Closed mode prevents external access to the shadow root:

Using the Configuration Object

You can also configure Shadow DOM using the @customElement decorator's configuration object:

Or using a static property:

Styling Shadow DOM Components

Shadow DOM provides complete CSS isolation. Styles defined outside the component won't affect elements inside, and styles inside won't leak out.

Component-Local Styles

Use the shadowCSS helper to register styles for your component:

Using Constructable Stylesheets

For better performance and reusability, you can pass CSSStyleSheet instances:

Global Shared Styles

Configure styles that apply to all Shadow DOM components in your application:

Global styles are applied first, followed by component-local styles.

Shadow DOM CSS Selectors

Shadow DOM provides special CSS selectors for enhanced styling control:

The :host Selector

Style the component's host element from within the shadow root:

The :host-context() Selector

Style the host based on an ancestor's context:

The ::slotted() Selector

Style content that has been projected into a slot:

The ::part() Selector

Expose specific elements for external styling using part attributes:

The ::part() selector is the recommended way to create styling hooks for consumers of your components. It provides explicit control over which internal elements can be styled externally.

Styling from Outside: CSS Custom Properties

The most flexible way to style Shadow DOM components from outside is using CSS custom properties (CSS variables):

Using CSS Modules with Shadow DOM

CSS Modules provide class name transformation for avoiding naming conflicts. You can combine cssModules() with Shadow DOM for both style encapsulation and class name scoping:

How it works: cssModules() transforms class names in your template at compile time, while shadowCSS() injects the actual CSS into the shadow root. When using CSS Modules with Shadow DOM, ensure your CSS rules use the transformed class names.

Shadow DOM and Slots

Native <slot> elements require Shadow DOM. Attempting to use <slot> without Shadow DOM will throw a compilation error.

Basic Slot Usage

Usage:

Named Slots

Usage:

Fallback Content

Slots can have default content when nothing is projected:

Listening to Slot Changes

React to changes in slotted content:

For more advanced slot usage, including the @children decorator and component view model retrieval, see the Slotted Content documentation.

Constraints and Limitations

Cannot Combine with @containerless

Shadow DOM requires a host element to attach to. You cannot use both @useShadowDOM and @containerless on the same component:

Error: Invalid combination: cannot combine the containerless custom element option with Shadow DOM.

Native Slots Require Shadow DOM

Using <slot> elements without enabling Shadow DOM will cause a compilation error:

Error: Template compilation error: detected a usage of "<slot>" element without specifying shadow DOM options in element: broken-component

Solution: Either enable Shadow DOM or use <au-slot> instead:

Choosing Between Shadow DOM and Light DOM

Use Shadow DOM When:

  • Style isolation is critical: You need to prevent external styles from affecting your component

  • Building reusable components: Your component will be used in different contexts and needs predictable styling

  • Using native web component features: You need features like <slot>, CSS :host selector, or ::part

  • Creating a design system: Components should maintain consistent appearance regardless of environment

Use Light DOM (no Shadow DOM) When:

  • Easy styling is important: Parent components or application styles should easily affect the component

  • Working with global styles: Your component should inherit application-wide styles

  • SEO is a concern: Search engines can more easily index light DOM content

  • Using <au-slot>: You need Aurelia's slot features like $host scope access

Practical Examples

Themed Button Component

Card with Multiple Slots

Component with Dynamic Styles

Best Practices

1. Use CSS Custom Properties for Theming

Allow users to customize your components through CSS variables with sensible defaults:

2. Provide Fallback Content for Slots

Give users a good default experience even when they don't provide slot content:

3. Namespace Your CSS Variables

Prevent naming conflicts by prefixing your component's CSS variables:

4. Consider Performance with Constructable Stylesheets

For optimal performance, Aurelia uses Constructable Stylesheets when supported by the browser, falling back to <style> elements otherwise.

Automatic caching: When you pass CSS strings to shadowCSS(), Aurelia automatically caches the compiled CSSStyleSheet instances. This means multiple instances of the same component share the same stylesheet object in memory.

For maximum control, you can create CSSStyleSheet objects directly:

Using pre-created CSSStyleSheet objects is slightly more efficient than CSS strings because it skips the string-to-stylesheet conversion step, though Aurelia's caching makes this difference minimal for most applications.

5. Use Open Mode Unless You Have a Reason Not To

Closed mode prevents useful debugging and testing. Use open mode by default:

6. Document Your CSS Custom Properties

If your component supports theming, document the available CSS variables:

7. Convention-Based CSS Does Not Auto-Inject into Shadow DOM

Aurelia's convention-based CSS loading (where my-component.css is auto-imported alongside my-component.ts) does not automatically inject styles into Shadow DOM. For Shadow DOM components, you must explicitly use shadowCSS():

Convention-based CSS loading works well for Light DOM components where styles are added to the document. For Shadow DOM components, always use shadowCSS() to ensure styles are properly scoped within the shadow root.

Additional Resources

Last updated

Was this helpful?