Skip to content

Commit 8857fbf

Browse files
committed
Observe projected toolbar rows
1 parent 9927677 commit 8857fbf

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/lib/toolbar/toolbar.spec.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ describe('MdToolbar', () => {
4444
expect(toolbarElement.classList.contains('mat-warn')).toBe(true);
4545
});
4646

47-
it('should set the toolbar role on the host', () => {
48-
expect(toolbarElement.getAttribute('role')).toBe('toolbar');
49-
});
50-
5147
it('should not wrap the first row contents inside of a generated element', () => {
5248
expect(toolbarElement.firstElementChild!.tagName).toBe('SPAN',
5349
'Expected the <span> element of the first row to be a direct child of the toolbar');
@@ -70,6 +66,18 @@ describe('MdToolbar', () => {
7066
fixture.detectChanges();
7167
}).toThrowError(/attempting to combine different/i);
7268
});
69+
70+
it('should throw an error if a toolbar-row is added later', () => {
71+
const fixture = TestBed.createComponent(ToolbarMixedRowModes);
72+
73+
fixture.componentInstance.showToolbarRow = false;
74+
fixture.detectChanges();
75+
76+
expect(() => {
77+
fixture.componentInstance.showToolbarRow = true;
78+
fixture.detectChanges();
79+
}).toThrowError(/attempting to combine different/i);
80+
});
7381
});
7482

7583
});
@@ -100,8 +108,10 @@ class ToolbarMultipleRows {}
100108
template: `
101109
<md-toolbar>
102110
First Row
103-
<md-toolbar-row>Second Row</md-toolbar-row>
111+
<md-toolbar-row *ngIf="showToolbarRow">Second Row</md-toolbar-row>
104112
</md-toolbar>
105113
`
106114
})
107-
class ToolbarMixedRowModes {}
115+
class ToolbarMixedRowModes {
116+
showToolbarRow: boolean = true;
117+
}

src/lib/toolbar/toolbar.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,27 @@ export class MdToolbar extends _MdToolbarMixinBase implements CanColor, AfterVie
5858
}
5959

6060
ngAfterViewInit() {
61-
if (!isDevMode() || !this._platform.isBrowser || !this._toolbarRows.length) {
61+
if (!isDevMode() || !this._platform.isBrowser) {
6262
return;
6363
}
6464

65+
this._checkToolbarMixedModes();
66+
this._toolbarRows.changes.subscribe(() => this._checkToolbarMixedModes());
67+
}
68+
69+
/**
70+
* Throws an exception when developers are attempting to combine the different toolbar row modes.
71+
*/
72+
private _checkToolbarMixedModes() {
73+
if (!this._toolbarRows.length) {
74+
return;
75+
}
76+
77+
// Check if there are any other DOM nodes that can display content but aren't inside of
78+
// a <md-toolbar-row> element.
6579
const isCombinedUsage = [].slice.call(this._elementRef.nativeElement.childNodes)
6680
.filter(node => !(node.classList && node.classList.contains('mat-toolbar-row')))
81+
.filter(node => node.nodeType !== Node.COMMENT_NODE)
6782
.some(node => node.textContent.trim());
6883

6984
if (isCombinedUsage) {

0 commit comments

Comments
 (0)