Skip to content

Commit c101f7f

Browse files
committed
fix(material/dialog): make align attribute into an input of dialog actions directive instead of css
Fixes multiple issues, such as self-documentation of the align attribute, type checking, better bindings, and IDE support. Because align is not standard for HTMLDivElement, it doesn't make much sense to assume end users to know they can use the align attribute. Fixes #18479
1 parent 43081d9 commit c101f7f

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

src/dev-app/dialog/dialog-demo.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class DialogDemo {
2222
dialogRef: MatDialogRef<JazzDialog> | null;
2323
lastAfterClosedResult: string;
2424
lastBeforeCloseResult: string;
25-
actionsAlignment: string;
25+
actionsAlignment: 'end' | 'center' | undefined;
2626
config = {
2727
disableClose: false,
2828
panelClass: 'custom-overlay-pane-class',
@@ -110,20 +110,20 @@ export class JazzDialog {
110110
private _dimesionToggle = false;
111111

112112
constructor(
113-
public dialogRef: MatDialogRef<JazzDialog>,
114-
@Inject(MAT_DIALOG_DATA) public data: any) { }
113+
public dialogRef: MatDialogRef<JazzDialog>,
114+
@Inject(MAT_DIALOG_DATA) public data: any) { }
115115

116116
togglePosition(): void {
117117
this._dimesionToggle = !this._dimesionToggle;
118118

119119
if (this._dimesionToggle) {
120120
this.dialogRef
121-
.updateSize('500px', '500px')
122-
.updatePosition({ top: '25px', left: '25px' });
121+
.updateSize('500px', '500px')
122+
.updatePosition({ top: '25px', left: '25px' });
123123
} else {
124124
this.dialogRef
125-
.updateSize()
126-
.updatePosition();
125+
.updateSize()
126+
.updatePosition();
127127
}
128128
}
129129

@@ -160,7 +160,7 @@ export class JazzDialog {
160160
</p>
161161
</mat-dialog-content>
162162
163-
<mat-dialog-actions [attr.align]="actionsAlignment">
163+
<mat-dialog-actions [align]="actionsAlignment">
164164
<button
165165
mat-raised-button
166166
color="primary"
@@ -181,7 +181,7 @@ export class JazzDialog {
181181
`
182182
})
183183
export class ContentElementDialog {
184-
actionsAlignment: string;
184+
actionsAlignment: 'end' | 'center' | undefined;
185185

186186
constructor(public dialog: MatDialog) { }
187187

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class MatDialogClose implements OnInit, OnChanges {
7777
// dialog, and therefore clicking the button won't result in a focus change. This means that
7878
// the FocusMonitor won't detect any origin change, and will always output `program`.
7979
_closeDialogVia(this.dialogRef,
80-
event.screenX === 0 && event.screenY === 0 ? 'keyboard' : 'mouse', this.dialogResult);
80+
event.screenX === 0 && event.screenY === 0 ? 'keyboard' : 'mouse', this.dialogResult);
8181
}
8282
}
8383

@@ -96,11 +96,11 @@ export class MatDialogTitle implements OnInit {
9696
@Input() id: string = `mat-dialog-title-${dialogElementUid++}`;
9797

9898
constructor(
99-
// The dialog title directive is always used in combination with a `MatDialogRef`.
100-
// tslint:disable-next-line: lightweight-tokens
101-
@Optional() private _dialogRef: MatDialogRef<any>,
102-
private _elementRef: ElementRef<HTMLElement>,
103-
private _dialog: MatDialog) {}
99+
// The dialog title directive is always used in combination with a `MatDialogRef`.
100+
// tslint:disable-next-line: lightweight-tokens
101+
@Optional() private _dialogRef: MatDialogRef<any>,
102+
private _elementRef: ElementRef<HTMLElement>,
103+
private _dialog: MatDialog) {}
104104

105105
ngOnInit() {
106106
if (!this._dialogRef) {
@@ -136,9 +136,15 @@ export class MatDialogContent {}
136136
*/
137137
@Directive({
138138
selector: `[mat-dialog-actions], mat-dialog-actions, [matDialogActions]`,
139-
host: {'class': 'mat-dialog-actions'}
139+
host: {
140+
'class': 'mat-dialog-actions',
141+
'[class.mat-dialog-actions-align-center]': 'align === "center"',
142+
'[class.mat-dialog-actions-align-end]': 'align === "end"'
143+
}
140144
})
141-
export class MatDialogActions {}
145+
export class MatDialogActions {
146+
@Input() align?: 'center' | 'end' = undefined;
147+
}
142148

143149

144150
/**

src/material/dialog/dialog.scss

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ $mat-dialog-button-margin: 8px !default;
5757
// Pull the actions down to avoid their padding stacking with the dialog's padding.
5858
margin-bottom: -$mat-dialog-padding;
5959

60-
&[align='end'] {
61-
justify-content: flex-end;
62-
}
63-
64-
&[align='center'] {
60+
// .align-center and .align-end are set by directive input "align"
61+
// [align='center'] and [align='right'] are kept for backwards compability
62+
&.mat-dialog-actions-align-center, &[align='center'] {
6563
justify-content: center;
6664
}
65+
&.mat-dialog-actions-align-end, &[align='end'] {
66+
justify-content: flex-end;
67+
}
6768

6869
.mat-button-base + .mat-button-base,
6970
.mat-mdc-button-base + .mat-mdc-button-base {

src/material/dialog/dialog.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,14 @@ describe('MatDialog', () => {
14931493
expect(container.getAttribute('aria-labelledby'))
14941494
.toBe(title.id, 'Expected the aria-labelledby to match the title id.');
14951495
}));
1496+
1497+
it('should add align-* class according to given [align] input in [mat-dialog-actions]', () => {
1498+
let actions =
1499+
overlayContainerElement.querySelector('mat-dialog-actions')!;
1500+
1501+
expect(actions).not.toHaveClass('mat-dialog-actions-align-center', 'Expected action buttons to not have class align-center');
1502+
expect(actions).toHaveClass('mat-dialog-actions-align-end', 'Expected action buttons to have class align-end');
1503+
});
14961504
}
14971505
});
14981506

@@ -1876,7 +1884,7 @@ class PizzaMsg {
18761884
template: `
18771885
<h1 mat-dialog-title>This is the title</h1>
18781886
<mat-dialog-content>Lorem ipsum dolor sit amet.</mat-dialog-content>
1879-
<mat-dialog-actions>
1887+
<mat-dialog-actions align="end">
18801888
<button mat-dialog-close>Close</button>
18811889
<button class="close-with-true" [mat-dialog-close]="true">Close and return true</button>
18821890
<button
@@ -1895,7 +1903,7 @@ class ContentElementDialog {}
18951903
<ng-template>
18961904
<h1 mat-dialog-title>This is the title</h1>
18971905
<mat-dialog-content>Lorem ipsum dolor sit amet.</mat-dialog-content>
1898-
<mat-dialog-actions>
1906+
<mat-dialog-actions align="end">
18991907
<button mat-dialog-close>Close</button>
19001908
<button class="close-with-true" [mat-dialog-close]="true">Close and return true</button>
19011909
<button

0 commit comments

Comments
 (0)