Skip to content

Commit 6cf4628

Browse files
committed
refactor(material/dialog): introduce tokens to help with M3 implementation
Adds the following tokens to the dialog which are required to implement M3: * `container-max-width` - sets the default `max-width` of the dialog. Previously this was done through JS but was too wide. * `container-small-max-width` - sets the default `max-width` on a small screen. * `container-min-width` - minimum width of the dialog. * `actions-alignment` - default alignment of the actions. Previously this was handled through JS, but we need to align the buttons to the end to match the spec. * `content-padding` - padding of `mat-dialog-content`. * `actions-padding` - padding of `mat-dialog-actions`. * `headline-padding` - padding of `mat-dialog-title`.
1 parent cd754d4 commit 6cf4628

File tree

9 files changed

+116
-9
lines changed

9 files changed

+116
-9
lines changed

src/material-experimental/theming/_m3-density.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ $_density-tokens: (
7373
(mat, checkbox): (
7474
touch-target-display: (block, block, none, none),
7575
),
76+
(mat, dialog): (),
7677
(mat, datepicker): (),
7778
(mat, divider): (),
7879
(mat, expansion): (

src/material/core/tokens/m2/_index.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@use './mat/protected-button' as tokens-mat-protected-button;
99
@use './mat/filled-button' as tokens-mat-filled-button;
1010
@use './mat/outlined-button' as tokens-mat-outlined-button;
11+
@use './mat/dialog' as tokens-mat-dialog;
1112
@use './mat/bottom-sheet' as tokens-mat-bottom-sheet;
1213
@use './mat/card' as tokens-mat-card;
1314
@use './mat/datepicker' as tokens-mat-datepicker;
@@ -117,6 +118,7 @@
117118
_get-tokens-for-module($theme, tokens-mat-button-toggle),
118119
_get-tokens-for-module($theme, tokens-mat-card),
119120
_get-tokens-for-module($theme, tokens-mat-datepicker),
121+
_get-tokens-for-module($theme, tokens-mat-dialog),
120122
_get-tokens-for-module($theme, tokens-mat-divider),
121123
_get-tokens-for-module($theme, tokens-mat-expansion),
122124
_get-tokens-for-module($theme, tokens-mat-fab),
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@use '../../token-utils';
2+
@use '../../../style/sass-utils';
3+
4+
// The prefix used to generate the fully qualified name for tokens in this file.
5+
$prefix: (mat, dialog);
6+
7+
// Tokens that can't be configured through Angular Material's current theming API,
8+
// but may be in a future version of the theming API.
9+
@function get-unthemable-tokens() {
10+
@return (
11+
container-max-width: 80vw,
12+
container-small-max-width: 80vw,
13+
container-min-width: 0,
14+
actions-alignment: start,
15+
actions-padding: 8px,
16+
content-padding: 20px 24px,
17+
with-actions-content-padding: 20px 24px,
18+
19+
// Note: the CSS class is `title` while the M2 tokens call it `subhead`.
20+
// M3 calls it `headline` which is what we're aligning with.
21+
headline-padding: 0 24px 9px,
22+
);
23+
}
24+
25+
// Tokens that can be configured through Angular Material's color theming API.
26+
@function get-color-tokens($theme) {
27+
@return ();
28+
}
29+
30+
// Tokens that can be configured through Angular Material's typography theming API.
31+
@function get-typography-tokens($theme) {
32+
@return ();
33+
}
34+
35+
// Tokens that can be configured through Angular Material's density theming API.
36+
@function get-density-tokens($theme) {
37+
@return ();
38+
}
39+
40+
// Combines the tokens generated by the above functions into a single map with placeholder values.
41+
// This is used to create token slots.
42+
@function get-token-slots() {
43+
@return sass-utils.deep-merge-all(
44+
get-unthemable-tokens(),
45+
get-color-tokens(token-utils.$placeholder-color-config),
46+
get-typography-tokens(token-utils.$placeholder-typography-config),
47+
get-density-tokens(token-utils.$placeholder-density-config)
48+
);
49+
}

src/material/dialog/_dialog-theme.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
@use '@material/dialog/dialog-theme' as mdc-dialog-theme;
33
@use '../core/style/sass-utils';
44
@use '../core/tokens/m2/mdc/dialog' as tokens-mdc-dialog;
5+
@use '../core/tokens/m2/mat/dialog' as tokens-mat-dialog;
6+
@use '../core/tokens/token-utils';
57
@use '../core/theming/theming';
68
@use '../core/theming/inspection';
79
@use '../core/typography/typography';
@@ -14,6 +16,8 @@
1416
// Add default values for tokens not related to color, typography, or density.
1517
@include sass-utils.current-selector-or-root() {
1618
@include mdc-dialog-theme.theme(tokens-mdc-dialog.get-unthemable-tokens());
19+
@include token-utils.create-token-values(
20+
tokens-mat-dialog.$prefix, tokens-mat-dialog.get-unthemable-tokens());
1721
}
1822
}
1923
}
@@ -25,6 +29,8 @@
2529
@else {
2630
@include sass-utils.current-selector-or-root() {
2731
@include mdc-dialog-theme.theme(tokens-mdc-dialog.get-color-tokens($theme));
32+
@include token-utils.create-token-values(
33+
tokens-mat-dialog.$prefix, tokens-mat-dialog.get-color-tokens($theme));
2834
}
2935
}
3036
}
@@ -36,6 +42,8 @@
3642
@else {
3743
@include sass-utils.current-selector-or-root() {
3844
@include mdc-dialog-theme.theme(tokens-mdc-dialog.get-typography-tokens($theme));
45+
@include token-utils.create-token-values(
46+
tokens-mat-dialog.$prefix, tokens-mat-dialog.get-typography-tokens($theme));
3947
}
4048
}
4149
}
@@ -70,5 +78,7 @@
7078
@mixin _theme-from-tokens($tokens) {
7179
@if ($tokens != ()) {
7280
@include mdc-dialog-theme.theme(map.get($tokens, tokens-mdc-dialog.$prefix));
81+
@include token-utils.create-token-values(
82+
tokens-mat-dialog.$prefix, map.get($tokens, tokens-mat-dialog.$prefix));
7383
}
7484
}

src/material/dialog/dialog-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class MatDialogConfig<D = any> {
8181
minHeight?: number | string;
8282

8383
/** Max-width of the dialog. If a number is provided, assumes pixel units. Defaults to 80vw. */
84-
maxWidth?: number | string = '80vw';
84+
maxWidth?: number | string;
8585

8686
/** Max-height of the dialog. If a number is provided, assumes pixel units. */
8787
maxHeight?: number | string;

src/material/dialog/dialog-content-directives.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export class MatDialogContent {}
157157
standalone: true,
158158
host: {
159159
'class': 'mat-mdc-dialog-actions mdc-dialog__actions',
160+
'[class.mat-mdc-dialog-actions-align-start]': 'align === "start"',
160161
'[class.mat-mdc-dialog-actions-align-center]': 'align === "center"',
161162
'[class.mat-mdc-dialog-actions-align-end]': 'align === "end"',
162163
},
@@ -165,7 +166,7 @@ export class MatDialogActions {
165166
/**
166167
* Horizontal alignment of action buttons.
167168
*/
168-
@Input() align?: 'start' | 'center' | 'end' = 'start';
169+
@Input() align?: 'start' | 'center' | 'end';
169170
}
170171

171172
/**

src/material/dialog/dialog-ref.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ export class MatDialogRef<T, R = any> {
7272
this.disableClose = config.disableClose;
7373
this.id = _ref.id;
7474

75+
// Used to target panels specifically tied to dialogs.
76+
_ref.addPanelClass('mat-mdc-dialog-panel');
77+
7578
// Emit when opening animation completes
7679
_containerInstance._animationStateChanged
7780
.pipe(

src/material/dialog/dialog.scss

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
@use '@material/animation/animation' as mdc-animation;
33
@use '@material/dialog/dialog-theme' as mdc-dialog-theme;
44
@use '../core/tokens/m2/mdc/dialog' as tokens-mdc-dialog;
5+
@use '../core/tokens/m2/mat/dialog' as tokens-mat-dialog;
56
@use '../core/mdc-helpers/mdc-helpers';
7+
@use '../core/tokens/token-utils';
8+
@use '../core/style/variables';
69
@use './mdc-dialog-structure-overrides';
710

811
// Dialog content max height. This has been copied from the standard dialog
@@ -52,22 +55,63 @@ $mat-dialog-button-horizontal-margin: 8px !default;
5255
}
5356
}
5457

58+
// This needs extra specificity so it doesn't get overwritten by the CDK structural styles.
59+
.cdk-overlay-pane.mat-mdc-dialog-panel {
60+
@include token-utils.use-tokens(tokens-mat-dialog.$prefix, tokens-mat-dialog.get-token-slots()) {
61+
@include token-utils.create-token-slot(max-width, container-max-width);
62+
@include token-utils.create-token-slot(min-width, container-min-width);
63+
64+
@media (variables.$xsmall) {
65+
@include token-utils.create-token-slot(max-width, container-small-max-width);
66+
}
67+
}
68+
}
69+
70+
.mat-mdc-dialog-title {
71+
@include token-utils.use-tokens(tokens-mat-dialog.$prefix, tokens-mat-dialog.get-token-slots()) {
72+
@include token-utils.create-token-slot(padding, headline-padding);
73+
}
74+
}
75+
5576
// MDC sets the display behavior for title and actions, but not for content. Since we support
5677
// using the `mdc-dialog__content` as custom element, we need to set the element to `block`.
5778
.mat-mdc-dialog-content {
5879
display: block;
80+
81+
@include token-utils.use-tokens(tokens-mat-dialog.$prefix, tokens-mat-dialog.get-token-slots()) {
82+
// These styles need a bit more specificity.
83+
.mat-mdc-dialog-container & {
84+
@include token-utils.create-token-slot(padding, content-padding);
85+
}
86+
87+
.mat-mdc-dialog-container:has(.mat-mdc-dialog-actions) & {
88+
@include token-utils.create-token-slot(padding, with-actions-content-padding);
89+
}
90+
}
91+
92+
.mat-mdc-dialog-container .mat-mdc-dialog-title + & {
93+
padding-top: 0;
94+
}
5995
}
6096

6197
.mat-mdc-dialog-actions {
6298
// For backwards compatibility, actions align at start by default. MDC usually
6399
// aligns actions at the end of the container.
64-
justify-content: start;
100+
@include token-utils.use-tokens(tokens-mat-dialog.$prefix, tokens-mat-dialog.get-token-slots()) {
101+
@include token-utils.create-token-slot(padding, actions-padding);
102+
@include token-utils.create-token-slot(justify-content, actions-alignment);
103+
}
104+
105+
// .mat-mdc-dialog-actions-align-{start|center|end} are set by directive input "align"
106+
// [align='start'], [align='center'] and [align='right'] are kept for backwards compability
107+
&.mat-mdc-dialog-actions-align-start, &[align='start'] {
108+
justify-content: start;
109+
}
65110

66-
// .mat-mdc-dialog-actions-align-{center|end} are set by directive input "align"
67-
// [align='center'] and [align='right'] are kept for backwards compability
68111
&.mat-mdc-dialog-actions-align-center, &[align='center'] {
69112
justify-content: center;
70113
}
114+
71115
&.mat-mdc-dialog-actions-align-end, &[align='end'] {
72116
justify-content: flex-end;
73117
}

src/material/dialog/dialog.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,7 @@ describe('MDC-based MatDialog', () => {
474474

475475
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
476476

477-
expect(overlayPane.style.maxWidth)
478-
.withContext('Expected dialog to set a default max-width on overlay pane')
479-
.toBe('80vw');
480-
477+
expect(overlayPane.style.maxWidth).toBe('');
481478
dialogRef.close();
482479

483480
tick(500);

0 commit comments

Comments
 (0)