UNPKG

8.24 kBMarkdownView Raw
1<!--docs:
2title: "Theme"
3layout: detail
4section: components
5excerpt: "Color theming for MDC Web components."
6iconId: theme
7path: /catalog/theme/
8-->
9
10# Theme
11
12The Material Design color system can be used to create a color scheme that reflects your brand or style.
13
14## Design & API Documentation
15
16<ul class="icon-list">
17 <li class="icon-list-item icon-list-item--spec">
18 <a href="https://material.io/go/design-theming">Material Design guidelines: Color</a>
19 </li>
20</ul>
21
22## Installation
23
24```
25npm install @material/theme
26```
27
28## Usage
29
30### Color Scheme
31
32You can define the theme color variables before importing any MDC Web components:
33
34```scss
35@use "@material/theme" with (
36 $primary: #fcb8ab,
37 $secondary: #feeae6,
38 $on-primary: #442b2d,
39 $on-secondary: #442b2d,
40);
41@use "@material/button/mdc-button";
42```
43
44We suggest you follow the Web Content Accessibility Guidelines 2.0 when picking the values for on-primary, on-secondary, etc. These values should be accessible on top of the corresponding value, e.g. primary and secondary.
45
46https://www.w3.org/TR/WCAG20
47
48### Advanced customization
49
50Color scheme will only get you 80% of the way to a well-designed app. Inevitably there will be some components that do not work "out of the box". To fix problems with accessibility and design, we suggest you use our Sass mixins, such as `button.filled-accessible()`. For more information, consult the documentation for each component.
51
52### Text styles
53
54The text styles (referred to as `<TEXT_STYLE>` below) used in the color system:
55
56Text style | Description
57--- | ---
58`primary` | Used for most text (e.g., `text-primary-on-light`)
59`secondary` | Used for text which is lower in the visual hierarchy (e.g., `text-secondary-on-light`)
60`hint` | Used for text hints, such as those in text fields and labels (e.g., `text-hint-on-light`)
61`disabled` | Used for text in disabled components and content (e.g., `text-disabled-on-light`)
62`icon` | Used for icons (e.g., `text-icon-on-light`)
63
64Here are the example usages of `primary` text style:
65
66 * CSS Custom property: `--mdc-theme-text-primary-on-light`
67 * Class name: `mdc-theme--text-primary-on-light`
68 * Property name used in Sass: `text-primary-on-light`
69
70### Non-Sass customization
71
72Only 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.
73
74#### CSS Custom Properties
75
76CSS Custom property | Description
77--- | ---
78`--mdc-theme-primary` | The theme primary color
79`--mdc-theme-secondary` | The theme secondary color
80`--mdc-theme-background` | The theme background color
81`--mdc-theme-surface` | The theme surface color
82`--mdc-theme-on-primary` | Text color on top of a primary background
83`--mdc-theme-on-secondary` | Text color on top of a secondary background
84`--mdc-theme-on-surface` | Text color on top of a surface background
85`--mdc-theme-text-<TEXT_STYLE>-on-light` | Text color for TEXT_STYLE on top of light background. Please see [Text styles section](#text-styles).
86`--mdc-theme-text-<TEXT_STYLE>-on-dark` | Text color for TEXT_STYLE on top of dark background. Please see [Text styles section](#text-styles).
87
88#### CSS Classes
89
90CSS Class | Description
91--- | ---
92`mdc-theme--primary` | Sets the text color to the theme primary color
93`mdc-theme--secondary` | Sets the text color to the theme secondary color
94`mdc-theme--background` | Sets the background color to the theme background color
95`mdc-theme--surface` | Sets the surface color to the theme surface color
96`mdc-theme--on-primary` | Sets the text color to the theme on-primary color
97`mdc-theme--on-secondary` | Sets the text color to the theme on-secondary color
98`mdc-theme--on-surface` | Sets the text color to the theme on-surface color
99`mdc-theme--primary-bg` | Sets the background color to the theme primary color
100`mdc-theme--secondary-bg` | Sets the background color to the theme secondary color
101`mdc-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](#text-styles).
102`mdc-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](#text-styles).
103
104### Sass Mixins, Variables, and Functions
105
106Mixin | Description
107--- | ---
108`property($property, $value, $gss, $important)` | Applies a dynamic value to the specified property. The value may be a standard CSS value, a custom property Map, or a Material theme key.
109
110#### Material theme keys with `theme.property()`
111
112Material theme key names below can be used as the `$value` argument for the `theme.property()` mixin. Some keys are dynamic, and change context depending on other key values. Keys may also translate to custom properties for dynamic runtime theming.
113
114Key Name | Description
115--- | ---
116`primary` | The theme primary color
117`secondary` | The theme secondary color
118`background` | The theme background color
119`surface` | The theme surface color
120`text-<TEXT_STYLE>-on-light` | TEXT_STYLE on top of a light background. Please see [Text styles section](#text-styles).
121`text-<TEXT_STYLE>-on-dark` | TEXT_STYLE on top of a dark background. Please see [Text styles section](#text-styles).
122`on-primary` | A text/iconography color that is usable on top of primary color
123`on-secondary` | A text/iconography color that is usable on top of secondary color
124`on-surface` | A text/iconography color that is usable on top of surface color
125
126#### Custom properties with `theme.property()`
127
128The `theme.property()` mixin also accepts a custom property Map for the `$value` argument. The map must contain a `varname` key with the name of the custom property, and an optional `fallback` key with the value of the custom property.
129
130Use the `@material/theme/custom-properties` module to create custom property Maps.
131
132For example, the following Sass...
133
134```scss
135@use "@material/theme";
136@use "@material/theme/custom-properties";
137
138.foo {
139 @include theme.property(color, custom-properties.create(--foo-color, red));
140}
141```
142
143...will produce the following CSS...
144
145```
146.foo {
147 color: red;
148 color: var(--foo-color, red);
149}
150```
151
152The 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.
153
154#### `theme.luminance($color)`
155
156Calculates the luminance value (0 - 1) of a given color.
157
158```scss
159@debug theme.luminance(#9c27b0); // 0.11654
160```
161
162#### `theme.contrast($back, $front)`
163
164Calculates the contrast ratio between two colors.
165
166```scss
167@debug theme.contrast(#9c27b0, #000); // 3.33071
168```
169
170#### `theme.tone($color)`
171
172Determines whether the given color is "light" or "dark".
173
174If the input color is a string literal equal to `"light"` or `"dark"`, it will be returned verbatim.
175
176```scss
177@debug theme.tone(#9c27b0); // dark
178@debug theme.tone(light); // light
179```
180
181#### `theme.contrast-tone($color)`
182
183Determines whether to use light or dark text on top of a given color.
184
185```scss
186@debug theme.contrast-tone(#9c27b0); // light
187```
188
189#### `theme.accessible-ink-color($fill-color, $text-style: primary)`
190
191Returns an accessible ink color that has sufficient contrast against the given fill color.
192
193Params:
194
195- `$fill-color`: Supports the same values as `theme.prop-value`
196- `$text-style`: Value must be one of `primary`, `secondary`, `hint`, `disabled`, `icon` (see `$text-colors`)
197
198> NOTE: This function is defined in `_variables.scss` instead of `_functions.scss` to avoid circular imports.
199
200```scss
201@debug theme.accessible-ink-color(secondary); // rgba(0, 0, 0, .87) (text-primary-on-light)
202@debug theme.accessible-ink-color(blue); // white (text-primary-on-dark)
203```
204#### `theme.text-emphasis($emphasis)`
205
206Returns opacity value for given emphasis.
207
208Params:
209
210- `$emphasis`: Type of emphasis such as `high`, `medium` & `disabled`.
211
212```scss
213@debug theme.text-emphasis(high); // .87
214@debug theme.text-emphasis(disabled); // .38
215```