A web component that displays a notification bell icon with a dropdown panel for displaying notifications. The component supports both light and dark themes, and provides methods for managing notifications.
<!-- Load the component -->
<script type="module" src="assets/components/notification-tray/qds-notification-tray.js"></script>
<!-- Use the component -->
<qds-notification-tray></qds-notification-tray>
more-label (string): Text to display for the “more” link at the bottom of the notification panelmore-url (string): URL for the “more” link at the bottom of the notification paneladdNotification(notification): Adds a notification and returns its IDconst id = notificationTray.addNotification({
text: 'New notification',
time: 'Just now',
icon: '<i class="pi pi-info"></i>',
url: 'https://example.com'
});
removeNotification(id): Removes a notification by IDnotificationTray.removeNotification('notification-1');
clearNotifications(): Removes all notificationsnotificationTray.clearNotifications();
interface Notification {
id?: string; // Optional: Unique identifier. If not provided, one will be generated
text: string; // Required: The notification text content
time?: string; // Optional: Timestamp or relative time
icon?: string; // Optional: HTML string for an icon
url?: string; // Optional: URL associated with the notification
}
The component processes notification text in the following ways:
<, the entire string is treated as HTML and rendered as-is<, the HTML is escaped and displayed as plain textExamples:
// Plain text with URL
addNotification({
text: 'Check out https://example.com'
});
// HTML content
addNotification({
text: '<div class="custom">HTML content</div>'
});
// Text containing HTML (will be escaped)
addNotification({
text: 'This contains <b>HTML</b>'
});
The component uses CSS custom properties for theming:
:root {
--badge-bg-color: #e53935;
--badge-text-color: white;
--panel-bg-color: white;
--panel-border-color: #ddd;
--divider-color: #eee;
--text-color: #333;
--time-color: #666;
--link-color: #0078d4;
}
button: The button containericon: The notification bell iconbadge: The notification count badgepanel: The notification panel containernotification: Individual notification itemnotification-text: The notification text contentnotification-time: The notification timestampnotification-icon: The notification icondivider: The divider line between notificationsmore-link: The “more” link at the bottom of the panelThe component fires a click event when the notification button is clicked.
The component uses modern web features and is compatible with browsers that support: