Alerts are used to display important messages either inline or as toast notifications.
<six-alert open>
<six-icon slot="icon">info</six-icon>
This is a standard alert. You can customize its content and even the icon.
</six-alert>
warning
Alerts will not be visible if the open
attribute is not present.
Set the type
attribute to change the alert's type
<six-alert type="primary" open>
<six-icon slot="icon">info</six-icon>
<strong>This is super informative</strong><br>
You can tell by how pretty the alert is.
</six-alert>
<br>
<six-alert type="success" open>
<six-icon slot="icon">check_circle</six-icon>
<strong>Your changes have been saved</strong><br>
You can safely exit the app now.
</six-alert>
<br>
<six-alert type="info" open>
<six-icon slot="icon">info</six-icon>
<strong>Your settings have been updated</strong><br>
Settings will take affect on next login.
</six-alert>
<br>
<six-alert type="warning" open>
<six-icon slot="icon">warning_amber</six-icon>
<strong>Your session has ended</strong><br>
Please login again to continue.
</six-alert>
<br>
<six-alert type="danger" open>
<six-icon slot="icon">error_outline</six-icon>
<strong>Your account has been deleted</strong><br>
We're very sorry to see you go!
</six-alert>
Add the closable
attribute to show a close button that will hide the alert.
<six-alert type="primary" open closable class="alert-closable">
<six-icon slot="icon">info</six-icon>
You can close this alert any time!
</six-alert>
<script type="module">
(() => {
const alert = document.querySelector('.alert-closable');
alert.addEventListener('six-alert-after-hide', () => {
setTimeout(() => {
alert.open = true;
}, 2000);
});
})();
</script>
Icons are optional. Simply omit the icon
slot if you don't want them.
<six-alert type="primary" open> Nothing fancy here, just a simple alert. </six-alert>
Set the duration
prop to automatically hide an alert after a period of time. This is useful for alerts that don't require acknowledgement.
<div class="alert-duration">
<six-button type="primary">Show Alert</six-button>
<six-alert type="primary" duration="3000" closable>
<six-icon slot="icon">info</six-icon>
This alert will automatically hide itself after three seconds, unless you interact with it.
</six-alert>
</div>
<script type="module">
(() => {
const container = document.querySelector('.alert-duration');
const button = container.querySelector('six-button');
const disappearingAlert = container.querySelector('six-alert');
button.addEventListener('click', () => disappearingAlert.show());
})();
</script>
<style>
.alert-duration six-alert {
margin-top: var(--six-spacing-medium);
}
</style>
To display an alert as a toast notification, or "toast", create the alert and call its `toast()` method. This will move the alert out of its position in the DOM and into the toast stack where it will be shown. Once dismissed, it will be removed from the DOM completely. To reuse a toast, store a reference to it and call toast()
again later on.
You should always use the closable
prop so users can dismiss the notification. It's also common to set a reasonable duration
when the notification doesn't require acknowledgement.
<div class="alert-toast">
<six-button id="primary-button" type="primary">Primary</six-button>
<six-button id="success-button" type="success">Success</six-button>
<six-button id="secondary-button" type="secondary">Info</six-button>
<six-button id="warning-button" type="warning">Warning</six-button>
<six-button id="danger-button" type="danger">Danger</six-button>
<six-alert id="primary-alert" type="primary" duration="3000" closable>
<six-icon slot="icon">info</six-icon>
<strong>This is super informative</strong><br>
You can tell by how pretty the alert is.
</six-alert>
<six-alert id="success-alert" type="success" duration="3000" closable>
<six-icon slot="icon">check_circle</six-icon>
<strong>Your changes have been saved</strong><br>
You can safely exit the app now.
</six-alert>
<six-alert id="info-alert" type="info" duration="3000" closable>
<six-icon slot="icon">info</six-icon>
<strong>Your settings have been updated</strong><br>
Settings will take affect on next login.
</six-alert>
<six-alert id="warning-alert" type="warning" duration="3000" closable>
<six-icon slot="icon">warning_amber</six-icon>
<strong>Your session has ended</strong><br>
Please login again to continue.
</six-alert>
<six-alert id="danger-alert" type="danger" duration="3000" closable>
<six-icon slot="icon">error_outline</six-icon>
<strong>Your account has been deleted</strong><br>
We're very sorry to see you go!
</six-alert>
</div>
<script type="module">
(() => {
const alertToastContainer = document.querySelector('.alert-toast');
Object.entries({
primary: 'primary',
success: 'success',
info: 'secondary',
warning: 'warning',
danger: 'danger',
}).map(([alertType, buttonType]) => {
const button = alertToastContainer.querySelector(`#${buttonType}-button`);
const alert = alertToastContainer.querySelector(`#${alertType}-alert`);
button.addEventListener('click', () => alert.toast());
});
})();
</script>
The wrapper libraries for Angular and Vue offer a simpler way to create toast alerts:
- Angular: Inject the `alertService` and utilize the `showAlert` method
this.alertService.showAlert('The Alert Message');
- Vue: Use
showAlert
by importing it from'@six-group/ui-library'
and then invoke itshowAlert('The Alert Message');
You can also create your own utility that emits toast notifications with a function call rather than composing them in your HTML. To do this, generate the alert with JavaScript, append it to the body, and call the toast()
method as shown in the example below.
<div class="alert-toast-wrapper">
<six-button type="primary">Create Toast</six-button>
</div>
<script type="module">
(() => {
const alertToastImpContainer = document.querySelector('.alert-toast-wrapper');
const toastCreatorButton = alertToastImpContainer.querySelector('six-button');
let count = 0;
// Always escape HTML for text arguments!
function escapeHtml(html) {
const div = document.createElement('div');
div.textContent = html;
return div.innerHTML;
}
// Custom function to emit toast notifications
function notify(message, type = 'primary', icon = 'info', duration = 3000) {
const alert = Object.assign(document.createElement('six-alert'), {
type: type,
closable: true,
duration: duration,
innerHTML: `
<six-icon slot="icon">${icon}</six-icon>
${escapeHtml(message)}`,
});
document.body.append(alert);
return alert.toast();
}
toastCreatorButton.addEventListener('click', () => {
const possibleTypes = ['primary', 'success', 'info', 'warning', 'danger'];
const drawnNumber = Math.round(Math.random(0, 1) * 5) + 1;
notify(`This is custom toast #${++count}`, possibleTypes[drawnNumber]);
});
})();
</script>
The Toast Stack allows multiple toast notifications to be displayed in a clean, organized stack. Below is an example where 5 toast notifications are dynamically created and displayed with a short delay between each toast.
<div class="toast-stack-wrapper">
<six-button type="primary">Create 5 Toasts</six-button>
</div>
<script type="module">
(() => {
const toastStackWrapper = document.querySelector('.toast-stack-wrapper');
const createToastButton = toastStackWrapper.querySelector('six-button');
// Always escape HTML for text arguments!
function escapeHtml(html) {
const div = document.createElement('div');
div.textContent = html;
return div.innerHTML;
}
// Custom function to emit toast notifications
function createToast(message, type = 'primary', icon = 'info', duration = 4000) {
const toast = Object.assign(document.createElement('six-alert'), {
type: type,
closable: true,
duration: duration,
innerHTML: `
<six-icon slot="icon">${icon}</six-icon>
${escapeHtml(message)}`,
});
document.body.appendChild(toast);
toast.toast();
}
createToastButton.addEventListener('click', () => {
const toastTypes = ['primary', 'success', 'info', 'warning', 'danger'];
let delay = 0;
for (let i = 1; i <= 5; i++) {
const randomType = toastTypes[Math.floor(Math.random() * toastTypes.length)];
setTimeout(() => {
createToast(`Toast #${i}`, randomType, 'info');
}, delay);
delay += 200;
}
});
})();
</script>
Property | Attribute | Description | Type | Default |
---|---|---|---|---|
closable |
closable |
Set to true to make the alert closable. | boolean |
false |
duration |
duration |
The length of time, in milliseconds, the alert will show before closing itself. If the user hovers over the toast alert, the timer will pause. On leaving the element with the mouse, the timer resets. | number |
Infinity |
open |
open |
Indicates whether or not the alert is open. You can use this in lieu of the show/hide methods. | boolean |
false |
type |
type |
The type of alert. | "danger" | "info" | "primary" | "success" | "warning" |
'primary' |
Event | Description | Type |
---|---|---|
six-alert-after-hide |
Emitted after the alert closes and all transitions are complete. | CustomEvent<undefined> |
six-alert-after-show |
Emitted after the alert opens and all transitions are complete. | CustomEvent<undefined> |
six-alert-hide |
Emitted when the alert closes. Calling event.preventDefault() will prevent it from being closed. |
CustomEvent<undefined> |
six-alert-show |
Emitted when the alert opens. Calling event.preventDefault() will prevent it from being opened. |
CustomEvent<undefined> |
Hides the alert
Type: Promise<void>
Shows the alert.
Type: Promise<void>
Displays the alert as a toast notification. This will move the alert out of its position in the DOM and, when dismissed, it will be removed from the DOM completely. By storing a reference to the alert, you can reuse it by calling this method again. The returned promise will resolve after the alert is hidden.
Name | Type | Description |
---|---|---|
adjustPosition |
boolean |
If true, the top and right position of the toast stack is shifted according to the six-root header's height and the presence of a vertical scrollbar. |
Type: Promise<void>
Slot | Description |
---|---|
The alert's content. | |
"icon" |
An icon to show in the alert. |
Part | Description |
---|---|
"base" |
The component's base wrapper. |
"close-button" |
The close button. |
"icon" |
The container that wraps the alert icon. |
"message" |
The alert message. |
Name | Description |
---|---|
--box-shadow |
The alert's box shadow. |
graph TD;
six-alert --> six-icon-button
six-icon-button --> six-icon
style six-alert fill:#f9f,stroke:#333,stroke-width:4px
Copyright © 2021-present SIX-Group