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 style | Description |
|---|---|
primary | Used for most text (e.g., text-primary-on-light) |
secondary | Used for text which is lower in the visual hierarchy (e.g., text-secondary-on-light) |
hint | Used for text hints, such as those in text fields and labels (e.g., text-hint-on-light) |
disabled | Used for text in disabled components and content (e.g., text-disabled-on-light) |
icon | Used 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 property | Description |
|---|---|
--element-theme-primary | The theme primary color |
--element-theme-secondary | The theme secondary color |
--element-theme-background | The theme background color |
--element-theme-surface | The theme surface color |
--element-theme-on-primary | Text color on top of a primary background |
--element-theme-on-secondary | Text color on top of a secondary background |
--element-theme-on-surface | Text color on top of a surface background |
--element-theme-text-<TEXT_STYLE>-on-light | Text color for TEXT_STYLE on top of light background. Please see Text styles section. |
--element-theme-text-<TEXT_STYLE>-on-dark | Text color for TEXT_STYLE on top of dark background. Please see Text styles section. |
CSS Classes
| CSS Class | Description |
|---|---|
element-theme--primary | Sets the text color to the theme primary color |
element-theme--secondary | Sets the text color to the theme secondary color |
element-theme--background | Sets the background color to the theme background color |
element-theme--surface | Sets the surface color to the theme surface color |
element-theme--on-primary | Sets the text color to the theme on-primary color |
element-theme--on-secondary | Sets the text color to the theme on-secondary color |
element-theme--on-surface | Sets the text color to the theme on-surface color |
element-theme--primary-bg | Sets the background color to the theme primary color |
element-theme--secondary-bg | Sets the background color to the theme secondary color |
element-theme--text-<TEXT_STYLE>-on-light | Sets text to a suitable color for TEXT_STYLE on top of light background. Please see Text styles section. |
element-theme--text-<TEXT_STYLE>-on-dark | Sets text to a suitable color for TEXT_STYLE on top of dark background. Please see Text styles section. |
Sass Variables
Configurable variables
| Variable | Description |
|---|---|
$primary | The primary color used in nearly every component. |
$secondary | The secondary color |
$error | A color that represents an error for the user |
$surface | The theme surface color. A surface appears above the background |
$background | The theme background color. The background color of the page, perhaps on the body tag |
$text-colors | Specifies colors for all text styles on both light and dark backgrounds |
| Variable | Description |
|---|---|
$primary-light | A lighter version of the primary color used as an accent in places |
$success | A color that represents a successful action for the user |
$warning | A color that represents a warning for the user |
$gray-light | The lightest shade of gray used in Element |
$gray | A darker shade of gray |
$gray-dark | An even darker shade of gray |
$gray-darkest | The darkest shade of gray |
$spacing-unit | The basic unit of the Element grid. The Element design specs specify that everything be built on an 8px grid |
$ease-in-out-circ | A predefined, reusable easing curve as defined by easings.net |
$ease-out-back | A predefined, reusable easing curve as defined by easings.net |
$overlay-opacity | The opacity of all overlays (sometimes called "srcim"s) used to hide content behind the currently focused content |
$overlay-color | The color of all overlays (sometimes called "srcim"s) used to hide content behind the currently focused content |
$z-index | A 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
| Variable | Description |
|---|---|
$on-primary | Either black or white, depending if $primary is a light or dark color |
$on-secondary | Either black or white, depending if $secondary is a light or dark color |
$on-surface | Either black or white, depending if $surface is a light or dark color |
$on-error | Either black or white, depending if $error is a light or dark color |
Sass Mixins
| Mixin | Description |
|---|---|
↓ property($property, $value, $gss: (), $important: false) | Get a color for a particular scenario. See below for more details |
| Mixin | Description |
|---|---|
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 Name | Description |
|---|---|
primary | The theme primary color |
secondary | The theme secondary color |
background | The theme background color |
surface | The theme surface color |
text-<TEXT_STYLE>-on-background | TEXT_STYLE on top of a background of color $background. Please see Text styles section. |
text-<TEXT_STYLE>-on-surface | TEXT_STYLE on top of a background of color $surface. Please see Text styles section. |
text-<TEXT_STYLE>-on-light | TEXT_STYLE on top of a light background. Please see Text styles section. |
text-<TEXT_STYLE>-on-dark | TEXT_STYLE on top of a dark background. Please see Text styles section. |
on-primary | A text/iconography color that is usable on top of primary color |
on-secondary | A text/iconography color that is usable on top of secondary color |
on-surface | A 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
$valueargument 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:
| Fields | Description |
|---|---|
varname | The name of a CSS custom property |
fallback | A 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
| Function | Description |
|---|---|
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 |
| Function | Description |
|---|---|
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); // blueaccessible-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 astheme.prop-value$text-style: Value must be one ofprimary,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)