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.
Styling from Outside: CSS Custom Properties
The only way to style Shadow DOM components from outside is using CSS custom properties (CSS variables):
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:hostselector, or::partCreating 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$hostscope 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 components that may be instantiated many times, use CSSStyleSheet objects instead of strings:
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:
Additional Resources
Slotted Content Documentation - Deep dive into slots,
@children, and@slotteddecoratorsWeb Components Documentation - Using Aurelia components as web components
CustomElement API Reference - Complete API documentation including Shadow DOM options
Last updated
Was this helpful?