Skip to content

Commit a2d38eb

Browse files
committed
refactor: merge light/dark color config definition
1 parent 2438454 commit a2d38eb

File tree

1 file changed

+39
-95
lines changed

1 file changed

+39
-95
lines changed

src/material/core/m2/_theming.scss

Lines changed: 39 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -113,52 +113,30 @@
113113
@return $theme;
114114
}
115115

116-
// Creates a light-themed color configuration from the specified
117-
// primary, accent and warn palettes.
118-
@function _mat-create-light-color-config($primary, $accent, $warn: null) {
116+
// Creates a color configuration from the specified primary, accent and warn palettes.
117+
@function _mat-create-color-config($primary, $accent, $warn, $is-dark) {
119118
@return (
120119
primary: $primary,
121120
accent: $accent,
122-
warn: if($warn != null, $warn, define-palette(palette.$red-palette)),
123-
is-dark: false,
124-
foreground: palette.$light-theme-foreground-palette,
125-
background: palette.$light-theme-background-palette,
121+
warn: $warn,
122+
is-dark: $is-dark,
123+
foreground: if($is-dark,
124+
palette.$dark-theme-foreground-palette,
125+
palette.$light-theme-foreground-palette),
126+
background: if($is-dark,
127+
palette.$dark-theme-background-palette,
128+
palette.$light-theme-background-palette),
126129
system: (
127-
surface: white,
128-
on-surface: rgba(black, 0.87),
129-
on-surface-variant: rgba(black, 0.54),
130-
background: map.get(palette.$grey-palette, 50),
131-
inverse-surface: map.get(palette.$grey-palette, 800),
132-
inverse-on-surface: white,
133-
outline: rgba(black, 0.12),
130+
surface: if($is-dark, map.get(palette.$grey-palette, 800), white),
131+
on-surface: if($is-dark, white, rgba(black, 0.87)),
132+
on-surface-variant: if($is-dark, rgba(white, 0.7), rgba(black, 0.54)),
133+
background: if($is-dark, #303030, map.get(palette.$grey-palette, 50)),
134+
inverse-surface: if($is-dark, white, map.get(palette.$grey-palette, 800)),
135+
inverse-on-surface: if($is-dark, rgba(black, 0.87), white),
136+
outline: if($is-dark, rgba(white, 0.12), rgba(black, 0.12)),
134137
hover-state-layer-opacity: 0.04,
135138
focus-state-layer-opacity: 0.12,
136-
pressed-state-layer-opacity: 0.12,
137-
),
138-
);
139-
}
140-
141-
// Creates a dark-themed color configuration from the specified
142-
// primary, accent and warn palettes.
143-
@function _mat-create-dark-color-config($primary, $accent, $warn: null) {
144-
@return (
145-
primary: $primary,
146-
accent: $accent,
147-
warn: if($warn != null, $warn, define-palette(palette.$red-palette)),
148-
is-dark: true,
149-
foreground: palette.$dark-theme-foreground-palette,
150-
background: palette.$dark-theme-background-palette,
151-
system: (
152-
surface: map.get(palette.$grey-palette, 800),
153-
on-surface: white,
154-
on-surface-variant: rgba(white, 0.7),
155-
background: #303030,
156-
inverse-surface: white,
157-
inverse-on-surface: rgba(black, 0.87),
158-
outline: rgba(white, 0.12),
159-
hover-state-layer-opacity: 0.04,
160-
focus-state-layer-opacity: 0.12,
161-
pressed-state-layer-opacity: 0.12,
139+
pressed-state-layer-opacity: 0.12
162140
),
163141
);
164142
}
@@ -170,49 +148,7 @@
170148
/// @param {Map} $primary The theme configuration object.
171149
/// @returns {Map} A complete Angular Material theme map.
172150
@function define-light-theme($primary, $accent: null, $warn: define-palette(palette.$red-palette)) {
173-
// This function creates a container object for the individual component theme mixins. Consumers
174-
// can construct such an object by calling this function, or by building the object manually.
175-
// There are two possible ways to invoke this function in order to create such an object:
176-
//
177-
// (1) Passing in a map that holds optional configurations for individual parts of the
178-
// theming system. For `color` configurations, the function only expects the palettes
179-
// for `primary` and `accent` (and optionally `warn`). The function will expand the
180-
// shorthand into an actual configuration that can be consumed in `-color` mixins.
181-
// (2) Legacy pattern: Passing in the palettes as parameters. This is not as flexible
182-
// as passing in a configuration map because only the `color` system can be configured.
183-
//
184-
// If the legacy pattern is used, we generate a container object only with a light-themed
185-
// configuration for the `color` theming part.
186-
@if $accent != null {
187-
@warn theming.$private-legacy-theme-warning;
188-
$theme: _mat-validate-theme(
189-
(
190-
_is-legacy-theme: true,
191-
color: _mat-create-light-color-config($primary, $accent, $warn),
192-
)
193-
);
194-
195-
@return _internalize-theme(theming.private-create-backwards-compatibility-theme($theme));
196-
}
197-
// If the map pattern is used (1), we just pass-through the configurations for individual
198-
// parts of the theming system, but update the `color` configuration if set. As explained
199-
// above, the color shorthand will be expanded to an actual light-themed color configuration.
200-
$result: $primary;
201-
@if map.get($primary, color) {
202-
$color-settings: map.get($primary, color);
203-
$primary: map.get($color-settings, primary);
204-
$accent: map.get($color-settings, accent);
205-
$warn: map.get($color-settings, warn);
206-
$result: map.merge(
207-
$result,
208-
(
209-
color: _mat-create-light-color-config($primary, $accent, $warn),
210-
)
211-
);
212-
}
213-
@return _internalize-theme(
214-
theming.private-create-backwards-compatibility-theme(_mat-validate-theme($result))
215-
);
151+
@return _define-theme($primary, $accent, $warn, false);
216152
}
217153

218154
// TODO: Remove legacy API and rename below `$primary` to `$config`. Currently it cannot be renamed
@@ -222,6 +158,13 @@
222158
/// @param {Map} $primary The theme configuration object.
223159
/// @returns {Map} A complete Angular Material theme map.
224160
@function define-dark-theme($primary, $accent: null, $warn: define-palette(palette.$red-palette)) {
161+
@return _define-theme($primary, $accent, $warn, true);
162+
}
163+
164+
/// Creates a container object for a theme to be given to individual component theme mixins.
165+
/// @param {Map} $primary The theme configuration object.
166+
/// @returns {Map} A complete Angular Material theme map.
167+
@function _define-theme($primary, $accent, $warn, $is-dark) {
225168
// This function creates a container object for the individual component theme mixins. Consumers
226169
// can construct such an object by calling this function, or by building the object manually.
227170
// There are two possible ways to invoke this function in order to create such an object:
@@ -233,36 +176,37 @@
233176
// (2) Legacy pattern: Passing in the palettes as parameters. This is not as flexible
234177
// as passing in a configuration map because only the `color` system can be configured.
235178
//
236-
// If the legacy pattern is used, we generate a container object only with a dark-themed
179+
// If the legacy pattern is used, we generate a container object only with a themed
237180
// configuration for the `color` theming part.
238181
@if $accent != null {
239182
@warn theming.$private-legacy-theme-warning;
240183
$theme: _mat-validate-theme(
241-
(
242-
_is-legacy-theme: true,
243-
color: _mat-create-dark-color-config($primary, $accent, $warn),
244-
)
184+
(
185+
_is-legacy-theme: true,
186+
color: _mat-create-color-config($primary, $accent, $warn, $is-dark),
187+
)
245188
);
189+
246190
@return _internalize-theme(theming.private-create-backwards-compatibility-theme($theme));
247191
}
248192
// If the map pattern is used (1), we just pass-through the configurations for individual
249193
// parts of the theming system, but update the `color` configuration if set. As explained
250-
// above, the color shorthand will be expanded to an actual dark-themed color configuration.
194+
// above, the color shorthand will be expanded to an actual light-themed color configuration.
251195
$result: $primary;
252196
@if map.get($primary, color) {
253197
$color-settings: map.get($primary, color);
254198
$primary: map.get($color-settings, primary);
255199
$accent: map.get($color-settings, accent);
256-
$warn: map.get($color-settings, warn);
200+
$warn: map.get($color-settings, warn) or define-palette(palette.$red-palette);
257201
$result: map.merge(
258-
$result,
259-
(
260-
color: _mat-create-dark-color-config($primary, $accent, $warn),
261-
)
202+
$result,
203+
(
204+
color: _mat-create-color-config($primary, $accent, $warn, $is-dark),
205+
)
262206
);
263207
}
264208
@return _internalize-theme(
265-
theming.private-create-backwards-compatibility-theme(_mat-validate-theme($result))
209+
theming.private-create-backwards-compatibility-theme(_mat-validate-theme($result))
266210
);
267211
}
268212

0 commit comments

Comments
 (0)