Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit f8a3eb6

Browse files
authored
fix(app-bar): Execute prominent app-bar following timeout (#1229)
Closes #1226
1 parent ea09759 commit f8a3eb6

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

packages/app-bar/app-bar.ts

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { takeUntil, startWith } from 'rxjs/operators';
1919
import { Subject } from 'rxjs';
2020

21-
import { EventRegistry, isBrowser, toBoolean } from '@angular-mdc/web/common';
21+
import { isBrowser, toBoolean } from '@angular-mdc/web/common';
2222

2323
import {
2424
MdcAppBarActionItem,
@@ -41,7 +41,6 @@ export class MdcAppBarNavSelected {
4141
selector: '[mdc-app-bar], mdc-app-bar',
4242
template: '<ng-content></ng-content>',
4343
exportAs: 'mdcAppBar',
44-
providers: [EventRegistry],
4544
changeDetection: ChangeDetectionStrategy.OnPush,
4645
encapsulation: ViewEncapsulation.None
4746
})
@@ -58,7 +57,7 @@ export class MdcAppBar implements AfterContentInit, AfterViewInit, OnDestroy {
5857
this.setFixed(value);
5958
}
6059
}
61-
protected _fixed: boolean;
60+
private _fixed: boolean;
6261

6362
@Input()
6463
get prominent(): boolean { return this._prominent; }
@@ -67,7 +66,7 @@ export class MdcAppBar implements AfterContentInit, AfterViewInit, OnDestroy {
6766
this.setProminent(value);
6867
}
6968
}
70-
protected _prominent: boolean;
69+
private _prominent: boolean;
7170

7271
@Input()
7372
get short(): boolean { return this._short; }
@@ -76,7 +75,7 @@ export class MdcAppBar implements AfterContentInit, AfterViewInit, OnDestroy {
7675
this.setShort(value);
7776
}
7877
}
79-
protected _short: boolean;
78+
private _short: boolean;
8079

8180
@Input()
8281
get shortCollapsed(): boolean { return this._shortCollapsed; }
@@ -85,7 +84,7 @@ export class MdcAppBar implements AfterContentInit, AfterViewInit, OnDestroy {
8584
this.setShortCollapsed(value);
8685
}
8786
}
88-
protected _shortCollapsed: boolean;
87+
private _shortCollapsed: boolean;
8988

9089
@Input()
9190
get dense(): boolean { return this._dense; }
@@ -94,7 +93,7 @@ export class MdcAppBar implements AfterContentInit, AfterViewInit, OnDestroy {
9493
this.setDense(value);
9594
}
9695
}
97-
protected _dense: boolean;
96+
private _dense: boolean;
9897

9998
@Input()
10099
get fixedAdjustElement(): HTMLElement { return this._fixedAdjustElement; }
@@ -103,7 +102,7 @@ export class MdcAppBar implements AfterContentInit, AfterViewInit, OnDestroy {
103102
this.setFixedAdjustElement(element);
104103
}
105104
}
106-
protected _fixedAdjustElement: HTMLElement;
105+
private _fixedAdjustElement: HTMLElement;
107106

108107
/** Event emitted when the navigation icon is selected. */
109108
@Output() navigationSelected: EventEmitter<MdcAppBarNavSelected> = new EventEmitter<MdcAppBarNavSelected>();
@@ -140,22 +139,22 @@ export class MdcAppBar implements AfterContentInit, AfterViewInit, OnDestroy {
140139
registerScrollHandler: (handler: EventListener) => {
141140
if (!isBrowser()) { return; }
142141

143-
this._registry.listen('scroll', handler, window);
142+
window.addEventListener('scroll', handler);
144143
},
145144
deregisterScrollHandler: (handler: EventListener) => {
146145
if (!isBrowser()) { return; }
147146

148-
this._registry.unlisten('scroll', handler);
147+
window.removeEventListener('scroll', handler);
149148
},
150149
registerResizeHandler: (handler: EventListener) => {
151150
if (!isBrowser()) { return; }
152151

153-
this._registry.listen('resize', handler, window);
152+
window.addEventListener('resize', handler);
154153
},
155154
deregisterResizeHandler: (handler: EventListener) => {
156155
if (!isBrowser()) { return; }
157156

158-
this._registry.unlisten('resize', handler);
157+
window.removeEventListener('resize', handler);
159158
},
160159
getViewportScrollY: () => {
161160
if (!isBrowser()) { return 0; }
@@ -172,8 +171,7 @@ export class MdcAppBar implements AfterContentInit, AfterViewInit, OnDestroy {
172171

173172
constructor(
174173
private _changeDetectorRef: ChangeDetectorRef,
175-
public elementRef: ElementRef,
176-
private _registry: EventRegistry) { }
174+
public elementRef: ElementRef) { }
177175

178176
ngAfterContentInit(): void {
179177
this.actions.changes.pipe(startWith(null), takeUntil(this._destroy)).subscribe(() => {
@@ -194,7 +192,9 @@ export class MdcAppBar implements AfterContentInit, AfterViewInit, OnDestroy {
194192
this._destroy.next();
195193
this._destroy.complete();
196194

197-
this._foundation.destroy();
195+
if (this._foundation) {
196+
this._foundation.destroy();
197+
}
198198
}
199199

200200
setFixedAdjustElement(element: HTMLElement): void {
@@ -272,23 +272,27 @@ export class MdcAppBar implements AfterContentInit, AfterViewInit, OnDestroy {
272272
}
273273

274274
initializeFoundation(): void {
275-
this._foundation.destroy();
275+
setTimeout(() => {
276+
this._foundation.destroy();
276277

277-
this._getHostElement().style.top = '0px';
278-
this._resetFixedShort();
278+
this._getHostElement().style.top = '0px';
279+
this._resetFixedShort();
279280

280-
if (this.short) {
281-
this._foundation = new MDCShortTopAppBarFoundation(this._mdcAdapter);
282-
} else if (this.fixed) {
283-
this._foundation = new MDCFixedTopAppBarFoundation(this._mdcAdapter);
284-
} else {
285-
this._foundation = new MDCTopAppBarFoundation(this._mdcAdapter);
286-
}
281+
if (this.short) {
282+
this._foundation = new MDCShortTopAppBarFoundation(this._mdcAdapter);
283+
} else if (this.fixed) {
284+
this._foundation = new MDCFixedTopAppBarFoundation(this._mdcAdapter);
285+
} else {
286+
this._foundation = new MDCTopAppBarFoundation(this._mdcAdapter);
287+
}
287288

288-
this._foundation.init();
289-
this._isFoundationInit = true;
289+
this._foundation.init();
290+
this._isFoundationInit = true;
290291

291-
this._initAppBar();
292+
this._initAppBar();
293+
294+
this._changeDetectorRef.markForCheck();
295+
}, 20);
292296
}
293297

294298
private _resetFixedShort(): void {

test/app-bar/app-bar.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { By } from '@angular/platform-browser';
44

55
import {
66
MdcAppBarModule,
7-
MdcAppBar,
7+
MdcAppBar
88
} from '@angular-mdc/web';
99

1010
describe('MdcAppBar', () => {
@@ -59,25 +59,25 @@ describe('MdcAppBar', () => {
5959
expect(testDebugElement.nativeElement.classList.contains('mdc-top-app-bar--short')).toBe(true);
6060
});
6161

62-
it('#should apply mdc-top-app-bar--short-collapsed class based on property', () => {
63-
testComponent.shortCollapsed = true;
64-
fixture.detectChanges();
65-
expect(testDebugElement.nativeElement.classList.contains('mdc-top-app-bar--short-collapsed')).toBe(true);
66-
});
67-
6862
it('#should apply mdc-top-app-bar--prominent class based on property', () => {
63+
testComponent.short = true;
64+
fixture.detectChanges();
6965
testComponent.prominent = true;
7066
fixture.detectChanges();
7167
expect(testDebugElement.nativeElement.classList.contains('mdc-top-app-bar--prominent')).toBe(true);
7268
});
7369

7470
it('#should apply mdc-top-app-bar--dense class based on property', () => {
71+
testComponent.short = true;
72+
fixture.detectChanges();
7573
testComponent.dense = true;
7674
fixture.detectChanges();
7775
expect(testDebugElement.nativeElement.classList.contains('mdc-top-app-bar--dense')).toBe(true);
7876
});
7977

8078
it('#should apply mdc-top-app-bar--fixed class based on property', () => {
79+
testComponent.short = true;
80+
fixture.detectChanges();
8181
testComponent.fixed = true;
8282
fixture.detectChanges();
8383
expect(testDebugElement.nativeElement.classList.contains('mdc-top-app-bar--fixed')).toBe(true);
@@ -109,9 +109,9 @@ describe('MdcAppBar', () => {
109109
`,
110110
})
111111
class SimpleTest {
112-
fixed: boolean = false;
113-
short: boolean = true;
114-
shortCollapsed: boolean = false;
115-
prominent: boolean = false;
116-
dense: boolean = false;
112+
fixed: boolean;
113+
short: boolean;
114+
shortCollapsed: boolean;
115+
prominent: boolean;
116+
dense: boolean;
117117
}

0 commit comments

Comments
 (0)