Element Theme

Element Global - Theme

Theme is a foundational, SASS only component. It contains the colors and other basic units used throughout Element.

Basic usage

@use '@element/global/theme';
@use '@element/global/theme/mixins' as theme-mixins;
@use '@element/global/theme/functions' as theme-functions;

Getting a text color for a particular surface

It's a common task to need to fetch the proper text color for a given background surface type.

If you just want either white or black depending on which shows up better on $primary, $secondary, $background, $surface, or $error, you can just use the appropriate variable from:

  • $on-primary
  • $on-secondary
  • $on-background
  • $on-surface
  • $on-error

Getting a text color for a particular surface AND text style

If you want to also specify a particular text style (e.g. primary, hint, disabled), you should use the prop() mixin.

The first argument is the CSS property that you would like to set. The second argument is a string in the form text-<TEXT_STYLE>-on-<SURFACE>.

For more information on the available text styles, please see the Text Styles section below.

The options for SURFACE are:

  • background
  • surface
  • light
  • dark
@use '@element/global/theme/mixins' as theme-mixins;

@include theme-mixins.prop(color, text-hint-on-background);
@include theme-mixins.prop(color, text-primary-on-surface);
@include theme-mixins.prop(color, text-disabled-on-light);
@include theme-mixins.prop(color, text-icon-on-dark);

Getting a text color for a non-standard background color

If you for some reason don't want to use one of the provided background colors, it is also possible to calculate a text color for a particular text style on any given background color using the theme-functions.ink-color-for-fill($text-style, $fill-color) function.

The first argument is kind of text you're trying to get the color for (e.g. primary, hint, disabled). See the Text Styles section below for a list of all possible options.

The second argument is the fill color that you want to display the text on.

@use '@element/global/theme';

color: theme.ink-color-for-fill(hint, #2b2b2b);
color: theme.ink-color-for-fill(disabled, $my-color-variable);

Note: Using the prop() mixin is preferred when possible because it has other logic around error checking and setting of CSS variables as well.

Text Styles

The text styles (referred to as <TEXT_STYLE> below) used in the color system:

Text styleDescription
primaryUsed for most text (e.g., text-primary-on-light)
secondaryUsed for text which is lower in the visual hierarchy (e.g., text-secondary-on-light)
hintUsed for text hints, such as those in text fields and labels (e.g., text-hint-on-light)
disabledUsed for text in disabled components and content (e.g., text-disabled-on-light)
iconUsed for icons (e.g., text-icon-on-light)

Here are the example usages of primary text style:

  • CSS Custom property: --element-theme-text-primary-on-light
  • Class name: element-theme--text-primary-on-light
  • Property name used in Sass: text-primary-on-light

Style Customization

Non-Sass customization

Only a very limited number of Material Design color customization features are supported for non-Sass clients. They are a set of CSS custom properties, and a set of CSS classes.

CSS Custom Properties

CSS Custom propertyDescription
--element-theme-primaryThe theme primary color
--element-theme-secondaryThe theme secondary color
--element-theme-backgroundThe theme background color
--element-theme-surfaceThe theme surface color
--element-theme-on-primaryText color on top of a primary background
--element-theme-on-secondaryText color on top of a secondary background
--element-theme-on-surfaceText color on top of a surface background
--element-theme-text-<TEXT_STYLE>-on-lightText color for TEXT_STYLE on top of light background. Please see Text styles section.
--element-theme-text-<TEXT_STYLE>-on-darkText color for TEXT_STYLE on top of dark background. Please see Text styles section.

CSS Classes

CSS ClassDescription
element-theme--primarySets the text color to the theme primary color
element-theme--secondarySets the text color to the theme secondary color
element-theme--backgroundSets the background color to the theme background color
element-theme--surfaceSets the surface color to the theme surface color
element-theme--on-primarySets the text color to the theme on-primary color
element-theme--on-secondarySets the text color to the theme on-secondary color
element-theme--on-surfaceSets the text color to the theme on-surface color
element-theme--primary-bgSets the background color to the theme primary color
element-theme--secondary-bgSets the background color to the theme secondary color
element-theme--text-<TEXT_STYLE>-on-lightSets text to a suitable color for TEXT_STYLE on top of light background. Please see Text styles section.
element-theme--text-<TEXT_STYLE>-on-darkSets text to a suitable color for TEXT_STYLE on top of dark background. Please see Text styles section.

Sass Variables

Configurable variables

VariableDescription
$primaryThe primary color used in nearly every component.
$secondaryThe secondary color
$errorA color that represents an error for the user
$surfaceThe theme surface color. A surface appears above the background
$backgroundThe theme background color. The background color of the page, perhaps on the body tag
$text-colorsSpecifies colors for all text styles on both light and dark backgrounds
VariableDescription
$primary-lightA lighter version of the primary color used as an accent in places
$successA color that represents a successful action for the user
$warningA color that represents a warning for the user
$gray-lightThe lightest shade of gray used in Element
$grayA darker shade of gray
$gray-darkAn even darker shade of gray
$gray-darkestThe darkest shade of gray
$spacing-unitThe basic unit of the Element grid. The Element design specs specify that everything be built on an 8px grid
$ease-in-out-circA predefined, reusable easing curve as defined by easings.net
$ease-out-backA predefined, reusable easing curve as defined by easings.net
$overlay-opacityThe opacity of all overlays (sometimes called "srcim"s) used to hide content behind the currently focused content
$overlay-colorThe color of all overlays (sometimes called "srcim"s) used to hide content behind the currently focused content
$z-indexA single map that defined the relative Z-Index for all Element components. If this is changed, it is recommended to keep the absolute order of values the same to prevent undesirable layering

Calculated variables

VariableDescription
$on-primaryEither black or white, depending if $primary is a light or dark color
$on-secondaryEither black or white, depending if $secondary is a light or dark color
$on-surfaceEither black or white, depending if $surface is a light or dark color
$on-errorEither black or white, depending if $error is a light or dark color

Sass Mixins

MixinDescription
property($property, $value, $gss: (), $important: false)Get a color for a particular scenario. See below for more details
MixinDescription
style-scroll-bar($thumb-color: rgba($element-gray-dark, 0.6), $width-type: thin)Style a scrollbar. The defaults provide a simple, thin gray scrollbar
padding-x($size)Set the left and right padding to $size
padding-y($size)Set the left and right padding to $size
margin-x($size)Set the left and right margin to $size
margin-y($size)Set the left and right margin to $size

prop mixin

The properties below can be used as the $value argument for the prop mixin. Literal color values (e.g., rgba(0, 0, 0, .75)) may also be used.

Property NameDescription
primaryThe theme primary color
secondaryThe theme secondary color
backgroundThe theme background color
surfaceThe theme surface color
text-<TEXT_STYLE>-on-backgroundTEXT_STYLE on top of a background of color $background. Please see Text styles section.
text-<TEXT_STYLE>-on-surfaceTEXT_STYLE on top of a background of color $surface. Please see Text styles section.
text-<TEXT_STYLE>-on-lightTEXT_STYLE on top of a light background. Please see Text styles section.
text-<TEXT_STYLE>-on-darkTEXT_STYLE on top of a dark background. Please see Text styles section.
on-primaryA text/iconography color that is usable on top of primary color
on-secondaryA text/iconography color that is usable on top of secondary color
on-surfaceA text/iconography color that is usable on top of surface color
@use "@element/global/theme";

.foo {
  // Set the `color` property to the theme's primary color
  @include theme.property(color, primary);
}

property mixin with CSS Custom Properties

Note The Sass map $value argument is intended only for use with color mixins.

The property mixin also accepts a Sass map for the $value argument. The map must contain the following fields:

FieldsDescription
varnameThe name of a CSS custom property
fallbackA fallback value for the CSS custom property

For example, the following Sass...

@use "@element/global/theme";

.foo {
  @include theme.property(
    color,
    (
      varname: --foo-color,
      fallback: red,
    )
  );
}

...will produce the following CSS...

.foo {
  color: red;
  color: var(--foo-color, red);
}

The above output CSS will apply the fallback field's value for all supported browsers (including IE11) while allowing for CSS custom property use as a progressive enhancement. Browsers like IE11 that do not support CSS custom properties will apply the color: red; and ignore the color: var(--foo-color, red);. This argument type is intended for clients who need custom color application outside of the existing theme properties.

Sass Functions

FunctionDescription
luminance($color)Calculates the luminance value (0 - 1) of a given color
contrast($back, $front)Calculates the contrast ratio between two colors
tone($color)Determines whether the given color is "light" or "dark". If the input color is a string literal equal to "light" or "dark", it will be returned verbatim
contrast-tone($color)Determines whether to use light or dark text on top of a given color
color-hash($color)Returns hash in string format that uniquely represents any given color format. Useful for generating unique keyframe names
prop-value($style)Fetch a color for a particular scenario. See dedicated section below.
accessible-ink-color($fill-color, $text-style: primary)Returns an accessible ink color that has sufficient contrast against the given fill color. See dedicated section below.
text-emphasis($emphasis)Returns opacity value for given emphasis. $emphasis is the type of emphasis such as high, medium & disabled
FunctionDescription
ink-color-for-fill($text-style, $fill-color)Determine the text color for a particular background color. $text-style must be one of primary, secondary, hint, disabled, or icon. $fill-color is the background color that the text will be displayed on
space($multiple)Convenience function to create a multiple of $element-spacing-unit

prop-value($style)

This is an alternative to the prop mixin. This is mainly useful in situations where prop cannot be used directly (e.g., box-shadow).

If $style is a color (a literal color value, currentColor, or a CSS custom property), it is returned verbatim. Otherwise, $style is treated as a theme property name, and the corresponding value from $property-values is returned. If this also fails, an error is thrown.

Unlike the prop mixin, this function does not support CSS custom properties. It only returns the raw color value of the specified theme property.

@debug theme.prop-value(primary); // #3f51b5
@debug theme.prop-value(blue); // blue

accessible-ink-color($fill-color, $text-style: primary)

Returns an accessible ink color that has sufficient contrast against the given fill color.

Params:

  • $fill-color: Supports the same values as theme.prop-value
  • $text-style: Value must be one of primary, secondary, hint, disabled, icon (see $text-colors)
@debug theme.accessible-ink-color(
  secondary
); // rgba(0, 0, 0, .87) (text-primary-on-light)
@debug theme.accessible-ink-color(
  blue
); // white              (text-primary-on-dark)