Skip to content

fix(badge): AoT and server-side rendering errors #9935

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

Merged
merged 1 commit into from
Feb 18, 2018
Merged
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
19 changes: 12 additions & 7 deletions src/lib/badge/badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type MatBadgeSize = 'small' | 'medium' | 'large';
selector: '[matBadge]',
host: {
'class': 'mat-badge',
'[class.mat-badge-overlap]': '_overlap',
'[class.mat-badge-overlap]': 'overlap',
'[class.mat-badge-above]': 'isAbove()',
'[class.mat-badge-below]': '!isAbove()',
'[class.mat-badge-before]': '!isAfter()',
Expand Down Expand Up @@ -123,6 +123,8 @@ export class MatBadge {
/** Creates the badge element */
private _createBadgeElement(): HTMLElement {
const badgeElement = this._document.createElement('span');
const activeClass = 'mat-badge-active';

badgeElement.setAttribute('id', `mat-badge-content-${this._id}`);
badgeElement.classList.add('mat-badge-content');
badgeElement.textContent = this.content;
Expand All @@ -134,12 +136,15 @@ export class MatBadge {
this._elementRef.nativeElement.appendChild(badgeElement);

// animate in after insertion
this._ngZone.runOutsideAngular(() => requestAnimationFrame(() => {
// ensure content available
if (badgeElement) {
badgeElement.classList.add('mat-badge-active');
}
}));
if (typeof requestAnimationFrame === 'function') {
this._ngZone.runOutsideAngular(() => {
requestAnimationFrame(() => {
badgeElement.classList.add(activeClass);
});
});
} else {
badgeElement.classList.add(activeClass);
}

return badgeElement;
}
Expand Down
6 changes: 6 additions & 0 deletions src/universal-app/kitchen-sink/kitchen-sink.html
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,9 @@ <h2>Focus trap</h2>
<div cdkTrapFocus cdkTrapFocusAutoCapture>
<button>Oh no, I'm trapped!</button>
</div>

<h2>Badge</h2>

<button mat-raised-button matBadge="Clicked 1337 times">
Clicky thing
</button>
2 changes: 2 additions & 0 deletions src/universal-app/kitchen-sink/kitchen-sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ServerModule} from '@angular/platform-server';
import {BrowserModule} from '@angular/platform-browser';
import {
MatAutocompleteModule,
MatBadgeModule,
MatButtonModule,
MatButtonToggleModule,
MatCardModule,
Expand Down Expand Up @@ -93,6 +94,7 @@ export class KitchenSink {
imports: [
BrowserModule.withServerTransition({appId: 'kitchen-sink'}),
MatAutocompleteModule,
MatBadgeModule,
MatButtonModule,
MatButtonToggleModule,
MatCardModule,
Expand Down