Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/notification/notification-content.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export interface NotificationContent {
[key: string]: any;
type: NotificationType;
title: string;
titleHtml?: string;
target?: string;
duration?: number;
smart?: boolean;
closeLabel?: any;
message?: string;
messageHtml?: string;
showClose?: boolean;
lowContrast?: boolean;
template?: TemplateRef<any>;
Expand Down
26 changes: 23 additions & 3 deletions src/notification/notification.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { I18n } from "carbon-components-angular/i18n";
import { NotificationDisplayService } from "./notification-display.service";
import { isObservable, of } from "rxjs";
import { BaseNotification } from "./base-notification.component";
import { DomSanitizer, SafeHtml } from "@angular/platform-browser";

/**
* Notification messages are displayed toward the top of the UI and do not interrupt user’s work.
Expand All @@ -29,11 +30,21 @@ import { BaseNotification } from "./base-notification.component";
<div
cdsNotificationTitle
*ngIf="!notificationObj.template"
[innerHTML]="notificationObj.title"
[id]="notificationID">
<ng-container *ngIf="notificationObj.titleHtml; else plainTitle">
<div [innerHTML]="safeNotificationTitleHtml"></div>
</ng-container>
<ng-template #plainTitle>
{{ notificationObj.title }}
</ng-template>
</div>
<div *ngIf="!notificationObj.template" cdsNotificationSubtitle>
<span [innerHTML]="notificationObj.message"></span>
<ng-container *ngIf="notificationObj.messageHtml; else plainMessage">
<span [innerHTML]="safeNotificationMessageHtml"></span>
</ng-container>
<ng-template #plainMessage>
<span>{{ notificationObj.message }}</span>
</ng-template>
</div>
<ng-container *ngTemplateOutlet="notificationObj.template; context: { $implicit: notificationObj}"></ng-container>
</div>
Expand Down Expand Up @@ -65,6 +76,12 @@ export class Notification extends BaseNotification {
obj.closeLabel = of(obj.closeLabel);
}
this._notificationObj = Object.assign({}, this.defaultNotificationObj, obj);
if (this._notificationObj.titleHtml) {
this.safeNotificationTitleHtml = this.sanitizer.bypassSecurityTrustHtml(this._notificationObj.titleHtml);
}
if (this._notificationObj.messageHtml) {
this.safeNotificationMessageHtml = this.sanitizer.bypassSecurityTrustHtml(this._notificationObj.messageHtml);
}
}

notificationID = `notification-${Notification.notificationCount++}`;
Expand All @@ -79,7 +96,10 @@ export class Notification extends BaseNotification {
@HostBinding("class.cds--inline-notification--low-contrast") get isLowContrast() { return this.notificationObj.lowContrast; }
@HostBinding("class.cds--inline-notification--hide-close-button") get isCloseHidden() { return !this.notificationObj.showClose; }

constructor(protected notificationDisplayService: NotificationDisplayService, protected i18n: I18n) {
protected safeNotificationTitleHtml: SafeHtml;
protected safeNotificationMessageHtml: SafeHtml;

constructor(protected notificationDisplayService: NotificationDisplayService, protected i18n: I18n, protected sanitizer: DomSanitizer) {
super(notificationDisplayService, i18n);
}
}
19 changes: 19 additions & 0 deletions src/notification/notification.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,22 @@ export const CustomContent = CustomTemplate.bind({});
CustomContent.args = {
showClose: true
};

const HtmlTemplate = (args) => ({
props: args,
template: `
<!-- Notification with HTML title and message -->
<cds-notification [notificationObj]="{
type: 'success',
titleHtml: '<b>Success!</b> Operation completed.',
messageHtml: 'Your request has been <u>processed successfully</u>.',
showClose: showClose
}">
</cds-notification>
`
});

export const HtmlContent = HtmlTemplate.bind({});
HtmlContent.args = {
showClose: true
};
Loading