:host {
--border: ...
--padding: ...
--background: ...
}:host {
--border-color: ...
--border-width: ...
--padding-left: ...
--padding-right: ...
--background-color: ...
}Three primary strategies control or adjust the look and feel of the system, each with distinct trade-offs.
Automatic (system): can adjust look/feel based on browser conditions with high confidence. For example, CSS container or style queries can report accurate states of the current UI.
Explicit (consumer dev/designer): low confidence the system can predict precise usage but high confidence by the consumer developer/designer. These cases are context specific. For example, when to use a small button in a toolbar vs a large button for a call to action.
Theme (user): system nor the consumer dev/designer can predict users preferences. For example, user has system OS defined preferences or overrides for light and dark themes or zoom/typography sizing.
Elements expose custom CSS properties that typically correspond to their native CSS properties, as shown in the table below. Some custom properties have no direct mappings to their native counterparts and the table indicates them by the absence of MDN documentation references.
Prefer exposing shorthand values. border: var(--border) rather than border-color: var(--border-color) as this gives more flexibility to the user without expanding the public API surface. When possible keep CSS Custom Property names simple and map them 1:1 to the native CSS property. This lowers the learning curve of the API and makes it consistent between other components within the library.
The internal host element is a pattern that provides an API guard on the element. When styling a custom element, avoid applying styles other than basic display properties and custom properties to the host element. The more styles applied to the host, the easier a consumer can override and change the styles in unexpected ways.
In this example, the app may style the element in unsupported or unpredictable ways. Custom style leaks like this make visual changes or migrations difficult for both the library authors and consumers.
To prevent internal style leakage, leverage a internal host element to apply styles and expose only the styles as needed to the public facing API.
By scoping the background and color to the internal-host element, only the public API of the exposed CSS Custom Properties can change the look and feel of the element.
Use host selectors to customize an element's visual state. In this component stylesheet, each state updates the same custom properties.
By modifying only a single set of Custom Properties the CSS specificity stays low and ensures that developers can theme any combination of visual states within the predictable public API.
With custom elements, the element tag is the scope for the design tokens. Element-specific design tokens are unnecessary and increase the surface area of the API and number of tokens to maintain.
Visual customizations and theming are now fully compatible throughout the public API of the CSS custom properties.
In this example the subtle change of not assigning to the default host properties not only increase the CSS specificity but also make certain customizations/theming impossible without exposing extra CSS custom properties.
CSS Custom Properties defined on the host should use Design Tokens that appropriately describe the system's intent. CSS Custom Properties should not be explicitly named with the element name as this encourages inconsistency between elements rather than using the system's intent/purpose tokens. Leveraging State Properties for CSS custom properties, element specific design tokens are unnecessary and increase the surface area of the API and number of tokens to maintain. (see State Properties for more information)
Style components using their supported attributes and custom states. Use status="error" for field validation, such as on nve-input or nve-control-message. Use status="danger" on nve-alert for critical messages.
nve-accordion exposes the expanded state for selectors such as nve-accordion:state(expanded). Add the expanded attribute to start open and behavior-expand to let users toggle the accordion.
Elements should not have any external margins or whitespace outside the bounds of the host element. Margins on a host element make assumptions about the layout that is external to their responsibility. A design token/layout system allows designers and developers to layout elements/utilities consistently and with explicit intent and constraint.
Use CSS Logical Properties when applying styles to text content that may invert with reading style. By using logical properties the styles follow the reading direction of the element. i18n support requires this when the user preferences in the browser reverse the reading order.
CSS Parts let consumers style specific elements inside a component's shadow DOM. For example, nve-dialog exposes the close-button part when the dialog has the closable attribute.
CSS Parts give full control to the application developer, but this comes with a significant tradeoff. As the element exposes more internal elements they become part of the public API of the element. Over time increases the difficulty of maintaining the API and making visual changes of the element without causing unexpected visual breaking changes to the consumer.
You can more safely expose elements that are part of the library's public API since each has its own well defined and versioned API. This safely gives more control to the application developer while preventing the element API surface from growing.
When naming a part use the name of the custom element without the nve- prefix for API consistency. When internal parts share a type, use both the general element name and a semantic name. This allows generalized selectors for theming as well as precise selections when needed.
Use the exportparts attribute to expose parts across nested shadow roots. The nve-week component exposes its calendar button as the icon-button part.
The calendar button exposes an icon part. In its internal template, nve-week uses exportparts="icon:icon-button-icon" on the icon button. Consumers can then select the icon by its exported name.
Use CSS export parts for advanced theming of nested shadow root elements.
Elements should be responsive to support a wide variety of use cases. While not all apps are responsive, elements should be responsive to their parent element. This ensures that the element properly renders in any context, regardless if the element is in a datagrid, dashboard, or article content. Leverage APIs like ResizeObserver and in the future Container Queries to achieve this.
You can extract and import component specific styles from a separate CSS file.
Components should not import theme files or text/layout utilities. These stylesheets target global application level styles. Imports cause the styles to be inlined at runtime and create severe performance penalties to consumers of the component.