Skip to content

Commit 64ed7ca

Browse files
authored
feat(material/core): add experimental theme demo (angular#29636)
1 parent ac324fb commit 64ed7ca

12 files changed

+683
-18
lines changed

.stylelintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181

8282
"unit-case": "lower",
8383
"unit-no-unknown": true,
84-
"unit-allowed-list": ["px", "%", "deg", "s", "ms", "em", "rem", "vh", "vw", "vmin"],
84+
"unit-allowed-list": ["px", "%", "deg", "s", "ms", "em", "rem", "vh", "vw", "vmin", "fr"],
8585

8686
"value-list-comma-space-after": "always-single-line",
8787
"value-list-comma-space-before": "never",

src/dev-app/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ ng_module(
7171
"//src/dev-app/table",
7272
"//src/dev-app/table-scroll-container",
7373
"//src/dev-app/tabs",
74+
"//src/dev-app/theme",
7475
"//src/dev-app/toolbar",
7576
"//src/dev-app/tooltip",
7677
"//src/dev-app/tree",

src/dev-app/dev-app/dev-app-layout.html

+11
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ <h1>Angular Material Demos</h1>
9797
<mat-icon>dark_mode</mat-icon>
9898
}
9999
</button>
100+
<button
101+
mat-icon-button
102+
(click)="toggleSystemTheme()"
103+
[matTooltip]="state.systemTheme ? 'Switch to standard theme' : 'Switch to system theme'"
104+
>
105+
@if (state.systemTheme) {
106+
<mat-icon>public_off</mat-icon>
107+
} @else {
108+
<mat-icon>public</mat-icon>
109+
}
110+
</button>
100111
<button
101112
mat-icon-button
102113
(click)="toggleRippleDisabled()"

src/dev-app/dev-app/dev-app-layout.ts

+8
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export class DevAppLayout {
110110
{name: 'Table Scroll Container', route: '/table-scroll-container'},
111111
{name: 'Table', route: '/table'},
112112
{name: 'Tabs', route: '/tabs'},
113+
{name: 'Theme', route: '/theme'},
113114
{name: 'Toolbar', route: '/toolbar'},
114115
{name: 'Tooltip', route: '/tooltip'},
115116
{name: 'Tree', route: '/tree'},
@@ -127,6 +128,7 @@ export class DevAppLayout {
127128

128129
constructor() {
129130
this.toggleTheme(this.state.darkTheme);
131+
this.toggleSystemTheme(this.state.systemTheme);
130132
this.toggleStrongFocus(this.state.strongFocusEnabled);
131133
this.toggleDensity(Math.max(this._densityScales.indexOf(this.state.density), 0));
132134
this.toggleRippleDisabled(this.state.rippleDisabled);
@@ -141,6 +143,12 @@ export class DevAppLayout {
141143
setAppState(this.state);
142144
}
143145

146+
toggleSystemTheme(value = !this.state.systemTheme) {
147+
this.state.systemTheme = value;
148+
this._document.body.classList.toggle('demo-experimental-theme', value);
149+
setAppState(this.state);
150+
}
151+
144152
toggleFullscreen() {
145153
this._element.nativeElement.querySelector('.demo-content')?.requestFullscreen();
146154
}

src/dev-app/dev-app/dev-app-state.ts

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface DevAppState {
1616
animations: boolean;
1717
zoneless: boolean;
1818
darkTheme: boolean;
19+
systemTheme: boolean;
1920
rippleDisabled: boolean;
2021
strongFocusEnabled: boolean;
2122
m3Enabled: boolean;
@@ -42,6 +43,7 @@ export function getAppState(): DevAppState {
4243
animations: true,
4344
zoneless: false,
4445
darkTheme: false,
46+
systemTheme: false,
4547
rippleDisabled: false,
4648
strongFocusEnabled: false,
4749
m3Enabled: false,

src/dev-app/routes.ts

+4
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ export const DEV_APP_ROUTES: Routes = [
221221
path: 'tabs',
222222
loadComponent: () => import('./tabs/tabs-demo').then(m => m.TabsDemo),
223223
},
224+
{
225+
path: 'theme',
226+
loadComponent: () => import('./theme/theme-demo').then(m => m.ThemeDemo),
227+
},
224228
{
225229
path: 'toolbar',
226230
loadComponent: () => import('./toolbar/toolbar-demo').then(m => m.ToolbarDemo),

src/dev-app/theme-m3.scss

+52-17
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
// Disable legacy API compatibility, since dev-app is fully migrated to theme inspection API.
66
mat.$theme-legacy-inspection-api-compatibility: false;
77

8+
$primary: mat.$azure-palette;
9+
$tertiary: mat.$blue-palette;
10+
811
// Create a theme with the specified color type and density.
912
@function create-theme($type: light, $density: 0) {
1013
@return mat.define-theme((
1114
color: (
1215
theme-type: $type,
13-
primary: mat.$azure-palette,
14-
tertiary: mat.$blue-palette,
16+
primary: $primary,
17+
tertiary: $tertiary,
1518
use-system-variables: true,
1619
),
1720
typography: (use-system-variables: true),
@@ -34,12 +37,26 @@ $dark-theme: create-theme($type: dark);
3437

3538
// Include the default theme styles.
3639
html {
37-
@include mat.all-component-themes($light-theme);
38-
@include mat.system-level-colors($light-theme);
39-
@include mat.system-level-typography($light-theme);
40-
// TODO(mmalerba): Support M3 for experimental components.
41-
// @include matx.column-resize-theme($light-theme);
42-
// @include matx.popover-edit-theme($light-theme);
40+
body:not(.demo-experimental-theme) {
41+
@include mat.all-component-themes($light-theme);
42+
@include mat.system-level-colors($light-theme);
43+
@include mat.system-level-typography($light-theme);
44+
// TODO(mmalerba): Support M3 for experimental components.
45+
// @include matx.column-resize-theme($light-theme);
46+
// @include matx.popover-edit-theme($light-theme);
47+
}
48+
49+
body.demo-experimental-theme {
50+
@include mat.private-experimental-theme((
51+
color: (
52+
theme-type: light,
53+
primary: $primary,
54+
tertiary: $tertiary,
55+
),
56+
typography: Roboto,
57+
density: 0,
58+
));
59+
}
4360
}
4461

4562
@include mat.typography-hierarchy($light-theme);
@@ -55,13 +72,25 @@ html {
5572
// CSS class whatever you want. In this example, any component inside of an element with
5673
// `.demo-unicorn-dark-theme` will be affected by this alternate dark theme instead of the
5774
// default theme.
58-
.demo-unicorn-dark-theme {
59-
// Include the dark theme color styles.
60-
@include mat.all-component-colors($dark-theme);
61-
@include mat.system-level-colors($dark-theme);
62-
// TODO(mmalerba): Support M3 for experimental components.
63-
// @include matx.column-resize-color($dark-theme);
64-
// @include matx.popover-edit-color($dark-theme);
75+
body.demo-unicorn-dark-theme {
76+
&:not(.demo-experimental-theme) {
77+
// Include the dark theme color styles.
78+
@include mat.all-component-colors($dark-theme);
79+
@include mat.system-level-colors($dark-theme);
80+
// TODO(mmalerba): Support M3 for experimental components.
81+
// @include matx.column-resize-color($dark-theme);
82+
// @include matx.popover-edit-color($dark-theme);
83+
}
84+
85+
&.demo-experimental-theme {
86+
@include mat.private-experimental-theme((
87+
color: (
88+
theme-type: dark,
89+
primary: $primary,
90+
tertiary: $tertiary,
91+
),
92+
));
93+
}
6594

6695
// Include the dark theme colors for focus indicators.
6796
&.demo-strong-focus {
@@ -75,8 +104,14 @@ html {
75104
$density-scales: (-1, -2, -3, -4, minimum, maximum);
76105
@each $scale in $density-scales {
77106
.demo-density-#{$scale} {
78-
$density-theme: create-theme($density: $scale);
79-
@include mat.all-component-densities($density-theme);
107+
body:not(.demo-experimental-theme) & {
108+
$density-theme: create-theme($density: $scale);
109+
@include mat.all-component-densities($density-theme);
110+
}
111+
112+
body.demo-experimental-theme & {
113+
@include mat.private-experimental-theme((density: $scale));
114+
}
80115
}
81116
}
82117

src/dev-app/theme/BUILD.bazel

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
load("//tools:defaults.bzl", "ng_module", "sass_binary")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
ng_module(
6+
name = "theme",
7+
srcs = glob(["**/*.ts"]),
8+
assets = [
9+
"theme-demo.html",
10+
":theme_demo_scss",
11+
],
12+
deps = [
13+
"//src/dev-app/dev-app",
14+
"//src/material/card",
15+
"//src/material/expansion",
16+
"//src/material/icon",
17+
"@npm//@angular/forms",
18+
],
19+
)
20+
21+
sass_binary(
22+
name = "theme_demo_scss",
23+
src = "theme-demo.scss",
24+
deps = [
25+
"//src/material:sass_lib",
26+
],
27+
)

0 commit comments

Comments
 (0)