Skip to content

fix(dialog) make align attribute into an input of dialog actions directive instead of css #21439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/dev-app/dialog/dialog-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class DialogDemo {
dialogRef: MatDialogRef<JazzDialog> | null;
lastAfterClosedResult: string;
lastBeforeCloseResult: string;
actionsAlignment: string;
actionsAlignment: 'end' | 'center' | undefined;
config = {
disableClose: false,
panelClass: 'custom-overlay-pane-class',
Expand Down Expand Up @@ -156,7 +156,7 @@ export class JazzDialog {
</p>
</mat-dialog-content>

<mat-dialog-actions [attr.align]="actionsAlignment">
<mat-dialog-actions [align]="actionsAlignment">
<button
mat-raised-button
color="primary"
Expand All @@ -177,7 +177,7 @@ export class JazzDialog {
`,
})
export class ContentElementDialog {
actionsAlignment: string;
actionsAlignment: 'end' | 'center' | undefined;

constructor(public dialog: MatDialog) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,15 @@ export class MatDialogContent {}
*/
@Directive({
selector: `[mat-dialog-actions], mat-dialog-actions, [matDialogActions]`,
host: {'class': 'mat-mdc-dialog-actions mdc-dialog__actions'},
host: {
'class': 'mat-mdc-dialog-actions mdc-dialog__actions',
'[class.mat-mdc-dialog-actions-align-center]': 'align === "center"',
'[class.mat-mdc-dialog-actions-align-end]': 'align === "end"',
},
})
export class MatDialogActions {}
export class MatDialogActions {
@Input() align?: 'center' | 'end' = undefined;
}

/**
* Finds the closest MatDialogRef to an element by looking at the DOM.
Expand Down
10 changes: 5 additions & 5 deletions src/material-experimental/mdc-dialog/dialog.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ $mat-dialog-button-horizontal-margin: 8px !default;
// aligns actions at the end of the container.
justify-content: start;

&[align='end'] {
justify-content: flex-end;
}

&[align='center'] {
// .align-center and .align-end are set by directive input "align"
&.mat-mdc-dialog-actions-align-center, &[align='center'] {
justify-content: center;
}
&.mat-mdc-dialog-actions-align-end, &[align='end'] {
justify-content: flex-end;
}

// MDC applies horizontal margin to buttons that have an explicit `mdc-dialog__button`
// class applied. We can't set this class for projected buttons that consumers of the
Expand Down
16 changes: 14 additions & 2 deletions src/material-experimental/mdc-dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {By} from '@angular/platform-browser';
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
import {numbers} from '@material/dialog';
import {Subject} from 'rxjs';

import {
MatDialog,
MatDialogModule,
Expand Down Expand Up @@ -1670,6 +1671,17 @@ describe('MDC-based MatDialog', () => {
.withContext('Expected the aria-labelledby to match the title id.')
.toBe(title.id);
}));

it('should add align-* class according to given [align] input in [mat-dialog-actions]', () => {
let actions = overlayContainerElement.querySelector('mat-dialog-actions')!;

expect(actions)
.withContext('Expected action buttons to not have class align-center')
.not.toHaveClass('mat-mdc-dialog-actions-align-center');
expect(actions)
.withContext('Expected action buttons to have class align-end')
.not.toHaveClass('mat-mdc-dialog-actions-align-end');
});
}
});

Expand Down Expand Up @@ -2072,7 +2084,7 @@ class PizzaMsg {
template: `
<h1 mat-dialog-title>This is the title</h1>
<mat-dialog-content>Lorem ipsum dolor sit amet.</mat-dialog-content>
<mat-dialog-actions>
<mat-dialog-actions align="end">
<button mat-dialog-close>Close</button>
<button class="close-with-true" [mat-dialog-close]="true">Close and return true</button>
<button
Expand All @@ -2091,7 +2103,7 @@ class ContentElementDialog {}
<ng-template>
<h1 mat-dialog-title>This is the title</h1>
<mat-dialog-content>Lorem ipsum dolor sit amet.</mat-dialog-content>
<mat-dialog-actions>
<mat-dialog-actions align="end">
<button mat-dialog-close>Close</button>
<button class="close-with-true" [mat-dialog-close]="true">Close and return true</button>
<button
Expand Down
10 changes: 8 additions & 2 deletions src/material/dialog/dialog-content-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,15 @@ export class MatDialogContent {}
*/
@Directive({
selector: `[mat-dialog-actions], mat-dialog-actions, [matDialogActions]`,
host: {'class': 'mat-dialog-actions'},
host: {
'class': 'mat-dialog-actions',
'[class.mat-dialog-actions-align-center]': 'align === "center"',
'[class.mat-dialog-actions-align-end]': 'align === "end"',
},
})
export class MatDialogActions {}
export class MatDialogActions {
@Input() align?: 'center' | 'end' = undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than undefined, should it support start which doesn't apply any styling?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will resubmit a new PR.

Yes, that might be better for API consistency. I will do that change in my next PR

}

/**
* Finds the closest MatDialogRef to an element by looking at the DOM.
Expand Down
11 changes: 6 additions & 5 deletions src/material/dialog/dialog.scss
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ $button-margin: 8px !default;
// Pull the actions down to avoid their padding stacking with the dialog's padding.
margin-bottom: -$padding;

&[align='end'] {
justify-content: flex-end;
}

&[align='center'] {
// .align-center and .align-end are set by directive input "align"
// [align='center'] and [align='right'] are kept for backwards compability
&.mat-dialog-actions-align-center, &[align='center'] {
justify-content: center;
}
&.mat-dialog-actions-align-end, &[align='end'] {
justify-content: flex-end;
}

.mat-button-base + .mat-button-base,
.mat-mdc-button-base + .mat-mdc-button-base {
Expand Down
60 changes: 45 additions & 15 deletions src/material/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
import {Directionality} from '@angular/cdk/bidi';
import {A, ESCAPE} from '@angular/cdk/keycodes';
import {Overlay, OverlayContainer, ScrollStrategy} from '@angular/cdk/overlay';
import {ScrollDispatcher} from '@angular/cdk/scrolling';
import {
ComponentFixture,
fakeAsync,
flushMicrotasks,
inject,
TestBed,
tick,
flush,
} from '@angular/core/testing';
createKeyboardEvent,
dispatchEvent,
dispatchKeyboardEvent,
dispatchMouseEvent,
patchElementFocus,
} from '@angular/cdk/testing/private';
import {Location} from '@angular/common';
import {SpyLocation} from '@angular/common/testing';
import {
ChangeDetectionStrategy,
Component,
ComponentFactoryResolver,
Directive,
Inject,
Injector,
Expand All @@ -21,6 +26,15 @@ import {
NgZone,
ViewEncapsulation,
} from '@angular/core';
import {
ComponentFixture,
fakeAsync,
flush,
flushMicrotasks,
inject,
TestBed,
tick,
} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
import {Location} from '@angular/common';
Expand All @@ -40,12 +54,13 @@ import {
} from '../../cdk/testing/private';
import {
MAT_DIALOG_DATA,
MAT_DIALOG_DEFAULT_OPTIONS,
MatDialog,
MatDialogModule,
MatDialogRef,
MAT_DIALOG_DEFAULT_OPTIONS,
MatDialogState,
} from './index';

import {Subject} from 'rxjs';

describe('MatDialog', () => {
Expand Down Expand Up @@ -1381,7 +1396,8 @@ describe('MatDialog', () => {
button.focus();

// Patch the element focus after the initial and real focus, because otherwise the
// `activeElement` won't be set, and the dialog won't be able to restore focus to an element.
// `activeElement` won't be set, and the dialog won't be able to restore focus to an
// element.
patchElementFocus(button);

dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
Expand Down Expand Up @@ -1414,7 +1430,8 @@ describe('MatDialog', () => {
button.focus();

// Patch the element focus after the initial and real focus, because otherwise the
// `activeElement` won't be set, and the dialog won't be able to restore focus to an element.
// `activeElement` won't be set, and the dialog won't be able to restore focus to an
// element.
patchElementFocus(button);

dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
Expand Down Expand Up @@ -1623,9 +1640,10 @@ describe('MatDialog', () => {
'Expected the focus to change when dialog was opened.',
);

// Start the closing sequence and move focus out of dialog.
dialogRef.close();
otherButton.focus();
flushMicrotasks();
viewContainerFixture.detectChanges();
tick(500);

expect(document.activeElement!.id)
.withContext('Expected focus to be on the alternate button.')
Expand Down Expand Up @@ -1733,6 +1751,18 @@ describe('MatDialog', () => {
.withContext('Expected the aria-labelledby to match the title id.')
.toBe(title.id);
}));

it('should add align-* class according to given [align] input in [mat-dialog-actions]', () => {
let actions = overlayContainerElement.querySelector('mat-dialog-actions')!;

expect(actions)
.withContext('Expected action buttons to not have class mat-dialog-actions-align-center')
.not.toHaveClass('mat-dialog-actions-align-center');

expect(actions)
.withContext('Expected action buttons to have class mat-dialog-actions-align-end')
.toHaveClass('mat-dialog-actions-align-end');
});
}
});

Expand Down Expand Up @@ -2116,7 +2146,7 @@ class PizzaMsg {
template: `
<h1 mat-dialog-title>This is the title</h1>
<mat-dialog-content>Lorem ipsum dolor sit amet.</mat-dialog-content>
<mat-dialog-actions>
<mat-dialog-actions align="end">
<button mat-dialog-close>Close</button>
<button class="close-with-true" [mat-dialog-close]="true">Close and return true</button>
<button
Expand All @@ -2135,7 +2165,7 @@ class ContentElementDialog {}
<ng-template>
<h1 mat-dialog-title>This is the title</h1>
<mat-dialog-content>Lorem ipsum dolor sit amet.</mat-dialog-content>
<mat-dialog-actions>
<mat-dialog-actions align="end">
<button mat-dialog-close>Close</button>
<button class="close-with-true" [mat-dialog-close]="true">Close and return true</button>
<button
Expand Down