Valo

Customization

One of the main benefits of the Valo theme is that it allows you to customize it to your needs. Because it’s based on Web Components and Polymer, the styling is done a bit differently than for a traditional web page.

Depending on your needs, there are different options how to customize any element’s styles and theme.


Common use cases

Built-in theme variations

For common use cases, Valo offers many built-in theme variations you can apply to individual element instances. For example, there is a “primary” theme for buttons, and a ”small” theme text-fields.

The built-in variations are applied using the theme attribute, and they work in any style scope:

Example: making a button more prominent

The build-in variations of each component are documented in each element’s documentation page, where you can also view the source code how to apply them. See Button’s color variations for example.


Small adjustments

CSS custom properties

Valo defines global and element specific CSS custom properties that you can use to make smaller adjustments to the theme.

The pages under the “Style guide” section (Colors for example) list the custom properties that affect all elements. Element specific custom properties are listed at the end of each element’s documentation page. See Button’s custom properties for example.

Global changes

As an example, if you wish to adjust the border radius for all elements that are affected by it, you can place the following code in the top level scope of you application (e.g. index.html):

Use the <custom-style> element for cross-browser compatility.

Example: changing the border radius of all elements it affects

Scoped changes

If you wish to adjust the border radius for vaadin-button specifically, you can do the following:

Example: Changing the border radius of one element using a common custom property

But, there’s a catch – you need to place this <style> element in the same style scope (i.e. shadow DOM) as the button you are trying to style. Normal CSS selectors, like the vaadin-button tag selector here, will not affect elements inside other style scopes (i.e. they are not able to style elements across shadow DOM boundaries).

To work around this limitation, we can target the button specific --valo-button-border-radius custom property instead, and we can apply that in the top level scope as well:

Example: Changing the border radius of one element using an element specific custom property

Advanced styling

Theme modules

When you can’t accomplish the styling you want using the CSS custom properties provided by the theme, you can create new theme modules to add low level CSS for any element.

Defining a theme module

A theme module is basically the same as a Polymer style module, with an additional theme-for attribute.

The theme-for attribute specifies the element (read: Web Component) that the styles in the theme module will affect. The value can be a space separated list of element names, and can contain wildcard element names as well.

Examples of valid theme-for attribute values:

Writing styles in a theme module

The styles written inside a theme module are included in the elements shadow DOM. That means, that in order to style the main element (a.k.a. the host element) you need to use the :host selector:

Supported selectors

In addition to the :host selector, the only other supported selectors inside a theme module are ones that target the documented “shadow parts” of the element. You can still write more complex selectors, which rely on any of the state attributes applied on the host or parts.

Example: Styling text field’s focused and invalid states

You can find links to the shadow parts documentation at the end of each element’s styling API documentation. See vaadin-button for example.

Theme modules are global

When creating a theme module for an element, the styles in that theme module will apply to all instances of that element. The styles are always “global” in that sense and can’t be scoped by default with anything.

Scoping styles in a theme module

There are two ways to scope the styles that you write in a theme module.

  1. Expose new custom properties
    This is the recommened first option for simple situations. If you end up exposing more than a handful of properties, you should consider the second option.
  2. Use scoping selectors
    This approach is actually what is used by the built-in theme variations (i.e. theme attribute). The downside of this approach is that you end up adding the styles to all instances, even though only some of them need those styles.
Example: Exposing new custom properties
Example: Use scoping selectors

Define theme modules before importing elements

As with the built-in element themes, theme modules should be defined before the corresponding elements are imported to make sure they are loaded by the elements.