UNPKG

8.69 kBSCSSView Raw
1//
2// Copyright 2021 Google Inc.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20// THE SOFTWARE.
21//
22
23// stylelint-disable selector-class-pattern --
24// Selector '.mdc-*' should only be used in this project.
25
26@use 'sass:map';
27@use 'sass:math';
28@use '@material/feature-targeting/feature-targeting';
29@use '@material/theme/custom-properties';
30@use '@material/theme/keys';
31@use '@material/theme/state';
32@use '@material/theme/theme';
33@use '@material/theme/theme-color';
34@use '@material/tokens/resolvers';
35@use '@material/ripple/ripple-theme';
36@use './button-base';
37@use './button-shared-theme';
38@use './button-ripple';
39
40$outlined-border-width: 1px !default;
41$outline-color: rgba(theme-color.prop-value(on-surface), 0.12) !default;
42
43$_custom-property-prefix: 'outlined-button';
44
45$light-theme: (
46 container-height: button-shared-theme.$height,
47 container-shape: button-shared-theme.$shape-radius,
48 disabled-label-text-color: button-shared-theme.$disabled-ink-color,
49 disabled-outline-color: button-shared-theme.$disabled-container-color,
50 focus-label-text-color: null,
51 focus-outline-color: null,
52 focus-state-layer-color: primary,
53 focus-state-layer-opacity: 0.12,
54 hover-label-text-color: null,
55 hover-outline-color: null,
56 hover-state-layer-color: primary,
57 hover-state-layer-opacity: 0.04,
58 label-text-color: primary,
59 label-text-font: button-font-family,
60 label-text-size: button-font-size,
61 label-text-tracking: button-letter-spacing,
62 label-text-transform: button-text-transform,
63 label-text-weight: button-font-weight,
64 outline-color: $outline-color,
65 outline-width: $outlined-border-width,
66 pressed-label-text-color: null,
67 pressed-state-layer-color: primary,
68 pressed-state-layer-opacity: 0.12,
69 with-icon-icon-color: null,
70 with-icon-icon-size: 18px,
71 with-icon-hover-icon-color: null,
72 with-icon-focus-icon-color: null,
73 with-icon-pressed-icon-color: null,
74 with-icon-disabled-icon-color: null,
75);
76
77/// Sets theme based on provided theme configuration.
78/// Only emits theme related styles.
79/// @param {Map} $theme - Theme configuration to use.
80@mixin theme($theme, $resolver: resolvers.$material) {
81 @include theme.validate-theme($light-theme, $theme);
82 $theme: button-shared-theme.resolve-theme-elevation-keys(
83 $theme,
84 $resolver: $resolver
85 );
86 @include keys.declare-custom-properties($theme, $_custom-property-prefix);
87}
88
89@mixin theme-styles(
90 $theme,
91 $resolver: resolvers.$material,
92 $query: feature-targeting.all()
93) {
94 @include theme.validate-theme-keys($light-theme, $theme);
95 $theme: keys.create-theme-properties(
96 $theme,
97 $prefix: $_custom-property-prefix
98 );
99 @include _theme($theme, $resolver, $query: $query);
100}
101
102@mixin _theme($theme, $resolver, $query) {
103 @include button-shared-theme.theme($theme, $resolver, $query: $query);
104 @include outline-color(
105 (
106 default: map.get($theme, outline-color),
107 disabled: map.get($theme, disabled-outline-color),
108 focus: map.get($theme, focus-outline-color),
109 hover: map.get($theme, hover-outline-color),
110 ),
111 $query: $query
112 );
113 @include outline-width(map.get($theme, outline-width), $query: $query);
114}
115
116@mixin deprecated-theme-styles($query: feature-targeting.all()) {
117 .mdc-button--outlined {
118 $theme: map.merge(
119 $light-theme,
120 (
121 focus-state-layer-color: null,
122 focus-state-layer-opacity: null,
123 hover-state-layer-color: null,
124 hover-state-layer-opacity: null,
125 pressed-state-layer-color: null,
126 pressed-state-layer-opacity: null,
127 label-text-font: null,
128 label-text-size: null,
129 label-text-tracking: null,
130 label-text-transform: null,
131 label-text-weight: null,
132 )
133 );
134 @include _theme($theme, resolvers.$material, $query: $query);
135 }
136}
137
138///
139/// Sets the outline color to the given color for an enabled button.
140/// @param {Color} $color-or-map - The desired outline color, specified either
141/// as a flat value or a map of colors with states
142/// {default, hover, focused, pressed, disabled} as keys.
143///
144@mixin outline-color($color-or-map, $query: feature-targeting.all()) {
145 &:not(:disabled) {
146 @include _outline-color(
147 state.get-default-state($color-or-map),
148 $query: $query
149 );
150
151 &:hover {
152 @include _outline-color(
153 state.get-hover-state($color-or-map),
154 $query: $query
155 );
156 }
157
158 @include ripple-theme.focus() {
159 @include _outline-color(
160 state.get-focus-state($color-or-map),
161 $query: $query
162 );
163 }
164
165 // Increase active state specificity due to ripple-theme.focus().
166 &:active,
167 &:focus:active {
168 @include _outline-color(
169 state.get-pressed-state($color-or-map),
170 $query: $query
171 );
172 }
173 }
174
175 &:disabled {
176 @include _outline-color(
177 state.get-disabled-state($color-or-map),
178 $query: $query
179 );
180 }
181}
182
183///
184/// Sets the outline color to the given color for a disabled button.
185/// @param {Color} $color - The desired outline color.
186/// @deprecated - call `outline-color` instead with `disabled` as a map key.
187///
188@mixin disabled-outline-color($color, $query: feature-targeting.all()) {
189 @include outline-color(
190 (
191 disabled: $color,
192 ),
193 $query: $query
194 );
195}
196
197@mixin outline-width(
198 $outline-width,
199 $padding: button-shared-theme.$contained-horizontal-padding,
200 // For a button with an icon, the padding on the side of the button with the
201 // icon.
202 $padding-icon: button-shared-theme.$contained-horizontal-padding-icon,
203 $query: feature-targeting.all()
204) {
205 $feat-structure: feature-targeting.create-target($query, structure);
206
207 @if $outline-width != null {
208 $outline-width-value: if(
209 custom-properties.is-custom-prop($outline-width),
210 custom-properties.get-fallback($outline-width),
211 $outline-width
212 );
213 // TODO(b/194792044): uncouple padding from outline-width
214 // Note: Adjust padding to maintain consistent width with non-outlined buttons
215 $padding-value: math.max($padding - $outline-width-value, 0);
216 $padding-icon-value: math.max($padding-icon - $outline-width-value, 0);
217
218 @include button-shared-theme.horizontal-padding(
219 $padding: $padding-value,
220 $padding-icon: $padding-icon-value,
221 $query: $query
222 );
223
224 @include feature-targeting.targets($feat-structure) {
225 @include theme.property(border-width, $outline-width);
226 }
227
228 #{button-ripple.$ripple-target} {
229 @include feature-targeting.targets($feat-structure) {
230 @include theme.property(top, -1 * $outline-width-value);
231 @include theme.property(left, -1 * $outline-width-value);
232 @include theme.property(bottom, -1 * $outline-width-value);
233 @include theme.property(right, -1 * $outline-width-value);
234 @include theme.property(border-width, $outline-width);
235 }
236 }
237 .mdc-button__touch {
238 @include feature-targeting.targets($feat-structure) {
239 @include theme.property(
240 left,
241 'calc(-1 * outline-width)',
242 $replace: (outline-width: $outline-width)
243 );
244 @include theme.property(
245 width,
246 'calc(100% + 2 * outline-width)',
247 $replace: (outline-width: $outline-width)
248 );
249 }
250 }
251 }
252}
253
254///
255/// Sets the outline color to the given color. This mixin should be
256/// wrapped in a selector that qualifies button state.
257/// @access private
258///
259@mixin _outline-color($color, $query: feature-targeting.all()) {
260 $feat-color: feature-targeting.create-target($query, color);
261
262 @if $color {
263 @include feature-targeting.targets($feat-color) {
264 @include theme.property(border-color, $color);
265 }
266 }
267}