Sharing Styles

Immutable styles provides a couple of ways to share common styles among similar rulesets:

Mixins are used to share styles among rulesets with the same element type. Whilst detached rulesets are used to share styles among rulesets with different element types.

This guide will feature an example of using each approach.

Sharing Styles with Immutable Mixins

For mixins we will extend the button example first introduced in The Basics guide. The designer has returned to the style-guide and added a few different button variations. Additionally each variation now has four UI states: a default, hover, focussed, and disabled state.

Here is our fictitious style-guide:

From the style-guide above we can identify common styles shared among all buttons:

  • padding

  • border-width

  • border-style

  • border-radius

  • font-size

  • font-family

  • cursor

In contrast we can also identify styles unique to each button type:

  • background

  • border-color

  • color

The common styles for the default state can be put into a mixin:

Each button variation can use the button.default mixin to apply the common styles shared among all buttons, and then apply its own styles unique to the button type:

The same approach can be applied to the different UI states. Common styles for each UI state can be assigned to its own mixin:

Sharing Styles with Detached Rulesets

Detached rulesets allow groups of styles to be shared across different rulesets. For example:

Grouped CSS Selectors

Detached rulesets offer an alternative to grouped CSS selectors. In the Typed Selectors guide the btn class could be used by elements of types a and button:

The equivalent rulesets in immutable styles would be:

Last updated