shoelace/docs/components/alert.md

227 wiersze
7.0 KiB
Markdown
Czysty Zwykły widok Historia

2020-07-15 21:30:37 +00:00
# Alert
[component-header:sl-alert]
2020-09-16 21:00:48 +00:00
Alerts are used to display important messages either inline or as toast notifications.
2020-07-15 21:30:37 +00:00
```html preview
<sl-alert open>
<sl-icon slot="icon" name="info-circle"></sl-icon>
This is a standard alert. You can customize its content and even the icon.
</sl-alert>
```
## Examples
### Types
Set the `type` attribute to change the alert's type.
```html preview
<sl-alert type="primary" open>
<sl-icon slot="icon" name="info-circle"></sl-icon>
<strong>This is super informative</strong><br>
You can tell by how pretty the alert is.
</sl-alert>
<br>
<sl-alert type="success" open>
<sl-icon slot="icon" name="check2-circle"></sl-icon>
<strong>Your changes have been saved</strong><br>
You can safely exit the app now.
</sl-alert>
<br>
<sl-alert type="info" open>
<sl-icon slot="icon" name="gear"></sl-icon>
<strong>Your settings have been updated</strong><br>
2020-09-16 21:00:48 +00:00
Settings will take affect on next login.
2020-07-15 21:30:37 +00:00
</sl-alert>
<br>
<sl-alert type="warning" open>
<sl-icon slot="icon" name="exclamation-triangle"></sl-icon>
2020-09-16 13:34:54 +00:00
<strong>Your session has ended</strong><br>
Please login again to continue.
2020-07-15 21:30:37 +00:00
</sl-alert>
<br>
<sl-alert type="danger" open>
<sl-icon slot="icon" name="exclamation-octagon"></sl-icon>
2020-09-16 13:34:54 +00:00
<strong>Your account has been deleted</strong><br>
We're very sorry to see you go!
2020-07-15 21:30:37 +00:00
</sl-alert>
```
### Closable
Add the `closable` attribute to show a close button that will hide the alert.
```html preview
<sl-alert type="primary" open closable class="alert-closable">
<sl-icon slot="icon" name="info-circle"></sl-icon>
You can close this alert any time!
</sl-alert>
<script>
const alert = document.querySelector('.alert-closable');
alert.addEventListener('slAfterHide', () => {
setTimeout(() => alert.open = true, 2000);
});
</script>
```
### Without Icons
Icons are optional. Simply omit the `icon` slot if you don't want them.
```html preview
<sl-alert type="primary" open>
Nothing fancy here, just a simple alert.
</sl-alert>
```
2020-09-17 20:27:11 +00:00
### Duration
Set the `duration` prop to automatically hide an alert after a period of time. This is useful for alerts that don't require acknowledgement.
```html preview
<div class="alert-duration">
<sl-button type="primary">Show Alert</sl-button>
<sl-alert type="primary" duration="3000" closable style="margin-top: var(--sl-spacing-medium);">
<sl-icon slot="icon" name="info-circle"></sl-icon>
This alert will automatically hide itself after three seconds, unless you interact with it.
</sl-alert>
</div>
<script>
const container = document.querySelector('.alert-duration');
const button = container.querySelector('sl-button');
const alert = container.querySelector('sl-alert');
button.addEventListener('click', () => alert.show());
</script>
```
2020-09-16 21:00:48 +00:00
### Toast Notifications
2020-09-18 13:40:21 +00:00
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](#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.
2020-09-17 20:27:11 +00:00
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.
2020-09-16 21:00:48 +00:00
```html preview
<div class="alert-toast">
<sl-button type="primary">Primary</sl-button>
<sl-button type="success">Success</sl-button>
<sl-button type="info">Info</sl-button>
<sl-button type="warning">Warning</sl-button>
<sl-button type="danger">Danger</sl-button>
2020-09-17 20:27:11 +00:00
<sl-alert type="primary" duration="3000" closable>
2020-09-16 21:00:48 +00:00
<sl-icon slot="icon" name="info-circle"></sl-icon>
<strong>This is super informative</strong><br>
You can tell by how pretty the alert is.
</sl-alert>
2020-09-17 20:27:11 +00:00
<sl-alert type="success" duration="3000" closable>
2020-09-16 21:00:48 +00:00
<sl-icon slot="icon" name="check2-circle"></sl-icon>
<strong>Your changes have been saved</strong><br>
You can safely exit the app now.
</sl-alert>
2020-09-17 20:27:11 +00:00
<sl-alert type="info" duration="3000" closable>
2020-09-16 21:00:48 +00:00
<sl-icon slot="icon" name="gear"></sl-icon>
<strong>Your settings have been updated</strong><br>
Settings will take affect on next login.
</sl-alert>
2020-09-17 20:27:11 +00:00
<sl-alert type="warning" duration="3000" closable>
2020-09-16 21:00:48 +00:00
<sl-icon slot="icon" name="exclamation-triangle"></sl-icon>
<strong>Your session has ended</strong><br>
Please login again to continue.
</sl-alert>
2020-09-17 20:27:11 +00:00
<sl-alert type="danger" duration="3000" closable>
2020-09-16 21:00:48 +00:00
<sl-icon slot="icon" name="exclamation-octagon"></sl-icon>
<strong>Your account has been deleted</strong><br>
We're very sorry to see you go!
</sl-alert>
</div>
<script>
const container = document.querySelector('.alert-toast');
['primary', 'success', 'info', 'warning', 'danger'].map(type => {
const button = container.querySelector(`sl-button[type="${type}"]`);
const alert = container.querySelector(`sl-alert[type="${type}"]`);
2020-09-17 20:27:11 +00:00
button.addEventListener('click', () => alert.toast());
2020-09-16 21:00:48 +00:00
});
</script>
```
2020-09-18 13:40:21 +00:00
### Creating Toasts Imperatively
For convenience, you can create a 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.
2020-09-16 21:00:48 +00:00
```html preview
2020-09-17 20:27:11 +00:00
<div class="alert-toast-wrapper">
<sl-button type="primary">Create Toast</sl-button>
2020-09-16 21:00:48 +00:00
</div>
<script>
2020-09-17 20:27:11 +00:00
const container = document.querySelector('.alert-toast-wrapper');
2020-09-16 21:00:48 +00:00
const button = container.querySelector('sl-button');
let count = 0;
2020-09-18 13:40:21 +00:00
// Always escape HTML for text arguments!
2020-09-17 20:27:11 +00:00
function escapeHtml(html) {
2020-09-18 13:40:21 +00:00
const div = document.createElement('div');
div.textContent = html;
return div.innerHTML;
2020-09-17 20:27:11 +00:00
}
2020-09-18 13:40:21 +00:00
// Custom function to emit toast notifications
2020-09-17 20:27:11 +00:00
function notify(message, type = 'primary', icon = 'info-circle', duration = 3000) {
const alert = Object.assign(document.createElement('sl-alert'), {
type: type,
2020-09-16 21:00:48 +00:00
closable: true,
2020-09-17 20:27:11 +00:00
duration: duration,
2020-09-16 21:00:48 +00:00
innerHTML: `
2020-09-17 20:27:11 +00:00
<sl-icon name="${icon}" slot="icon"></sl-icon>
${escapeHtml(message)}
2020-09-16 21:00:48 +00:00
`
});
2020-09-17 20:27:11 +00:00
document.body.append(alert);
return alert.toast();
2020-09-16 21:00:48 +00:00
}
button.addEventListener('click', () => {
2020-09-17 20:27:11 +00:00
notify(`This is custom toast #${++count}`);
2020-09-16 21:00:48 +00:00
});
</script>
```
2020-09-18 13:40:21 +00:00
### The Toast Stack
2020-09-16 21:00:48 +00:00
2020-09-18 13:40:21 +00:00
The toast stack is a fixed position singleton element created and managed internally by the alert component. It will be added and removed from the DOM as needed when toasts are shown. When more than one toast is visible, they will stack vertically in the toast stack.
2020-09-16 21:00:48 +00:00
2020-09-18 13:40:21 +00:00
By default, the toast stack is positioned at the top-right of the viewport. You can change its position by targeting `.sl-toast-stack` in your stylesheet. To make toasts appear at the top-left of the viewport, for example, use the following styles.
2020-09-16 21:00:48 +00:00
2020-09-17 20:27:11 +00:00
```css
.sl-toast-stack {
left: 0;
right: auto;
}
2020-09-16 21:00:48 +00:00
```
2020-09-18 13:40:21 +00:00
?> By design, it is not possible to show toasts in more than one stack simultaneously. Such behavior is confusing and makes for a poor user experience.
2020-09-17 20:27:11 +00:00
2020-07-15 21:30:37 +00:00
[component-metadata:sl-alert]