shoelace/docs/components/alert.md

434 wiersze
12 KiB
Markdown
Czysty Zwykły widok Historia

2020-07-15 21:30:37 +00:00
# Alert
[component-header:sl-alert]
2021-12-13 22:38:40 +00:00
Alerts are used to display important messages 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>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlAlert, SlIcon } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<SlAlert open>
<SlIcon slot="icon" name="info-circle" />
This is a standard alert. You can customize its content and even the icon.
</SlAlert>
);
```
2020-10-13 16:41:57 +00:00
?> Alerts will not be visible if the `open` attribute is not present.
2020-07-15 21:30:37 +00:00
## Examples
2021-12-13 22:38:40 +00:00
### Variants
2020-07-15 21:30:37 +00:00
2021-12-13 22:38:40 +00:00
Set the `variant` attribute to change the alert's variant.
2020-07-15 21:30:37 +00:00
```html preview
2021-12-13 22:38:40 +00:00
<sl-alert variant="primary" open>
2020-07-15 21:30:37 +00:00
<sl-icon slot="icon" name="info-circle"></sl-icon>
2022-03-02 15:10:41 +00:00
<strong>This is super informative</strong><br />
2020-07-15 21:30:37 +00:00
You can tell by how pretty the alert is.
</sl-alert>
2022-03-02 15:10:41 +00:00
<br />
2020-07-15 21:30:37 +00:00
2021-12-13 22:38:40 +00:00
<sl-alert variant="success" open>
2020-07-15 21:30:37 +00:00
<sl-icon slot="icon" name="check2-circle"></sl-icon>
2022-03-02 15:10:41 +00:00
<strong>Your changes have been saved</strong><br />
2020-07-15 21:30:37 +00:00
You can safely exit the app now.
</sl-alert>
2022-03-02 15:10:41 +00:00
<br />
2020-07-15 21:30:37 +00:00
2021-12-13 22:38:40 +00:00
<sl-alert variant="neutral" open>
2020-07-15 21:30:37 +00:00
<sl-icon slot="icon" name="gear"></sl-icon>
2022-03-02 15:10:41 +00:00
<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>
2022-03-02 15:10:41 +00:00
<br />
2020-07-15 21:30:37 +00:00
2021-12-13 22:38:40 +00:00
<sl-alert variant="warning" open>
2020-07-15 21:30:37 +00:00
<sl-icon slot="icon" name="exclamation-triangle"></sl-icon>
2022-03-02 15:10:41 +00:00
<strong>Your session has ended</strong><br />
2020-09-16 13:34:54 +00:00
Please login again to continue.
2020-07-15 21:30:37 +00:00
</sl-alert>
2022-03-02 15:10:41 +00:00
<br />
2020-07-15 21:30:37 +00:00
2021-12-13 22:38:40 +00:00
<sl-alert variant="danger" open>
2020-07-15 21:30:37 +00:00
<sl-icon slot="icon" name="exclamation-octagon"></sl-icon>
2022-03-02 15:10:41 +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>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlAlert, SlIcon } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2021-12-13 22:38:40 +00:00
<SlAlert variant="primary" open>
2021-11-04 22:12:47 +00:00
<SlIcon slot="icon" name="info-circle" />
2022-03-02 15:10:41 +00:00
<strong>This is super informative</strong>
<br />
2021-11-04 22:12:47 +00:00
You can tell by how pretty the alert is.
</SlAlert>
<br />
2021-12-13 22:38:40 +00:00
<SlAlert variant="success" open>
2021-11-04 22:12:47 +00:00
<SlIcon slot="icon" name="check2-circle" />
2022-03-02 15:10:41 +00:00
<strong>Your changes have been saved</strong>
<br />
2021-11-04 22:12:47 +00:00
You can safely exit the app now.
</SlAlert>
<br />
2021-12-13 22:38:40 +00:00
<SlAlert variant="neutral" open>
2021-11-04 22:12:47 +00:00
<SlIcon slot="icon" name="gear" />
2022-03-02 15:10:41 +00:00
<strong>Your settings have been updated</strong>
<br />
2021-11-04 22:12:47 +00:00
Settings will take affect on next login.
</SlAlert>
<br />
2021-12-13 22:38:40 +00:00
<SlAlert variant="warning" open>
2021-11-04 22:12:47 +00:00
<SlIcon slot="icon" name="exclamation-triangle" />
2022-03-02 15:10:41 +00:00
<strong>Your session has ended</strong>
<br />
2021-11-04 22:12:47 +00:00
Please login again to continue.
</SlAlert>
<br />
2021-12-13 22:38:40 +00:00
<SlAlert variant="danger" open>
2021-11-04 22:12:47 +00:00
<SlIcon slot="icon" name="exclamation-octagon" />
2022-03-02 15:10:41 +00:00
<strong>Your account has been deleted</strong>
<br />
We're very sorry to see you go!
2021-11-04 22:12:47 +00:00
</SlAlert>
</>
);
```
2020-07-15 21:30:37 +00:00
### Closable
Add the `closable` attribute to show a close button that will hide the alert.
```html preview
2021-12-13 22:38:40 +00:00
<sl-alert variant="primary" open closable class="alert-closable">
2020-07-15 21:30:37 +00:00
<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');
2020-10-09 21:45:16 +00:00
alert.addEventListener('sl-after-hide', () => {
2022-03-02 15:10:41 +00:00
setTimeout(() => (alert.open = true), 2000);
2020-07-15 21:30:37 +00:00
});
</script>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { useState } from 'react';
import { SlAlert, SlIcon } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(true);
function handleHide() {
setOpen(false);
setTimeout(() => setOpen(true), 2000);
}
return (
2022-03-02 15:10:41 +00:00
<SlAlert open={open} closable onSlAfterHide={handleHide}>
2021-11-04 22:12:47 +00:00
<SlIcon slot="icon" name="info-circle" />
You can close this alert any time!
</SlAlert>
);
};
```
2020-07-15 21:30:37 +00:00
### Without Icons
Icons are optional. Simply omit the `icon` slot if you don't want them.
```html preview
2022-03-02 15:10:41 +00:00
<sl-alert variant="primary" open> Nothing fancy here, just a simple alert. </sl-alert>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlAlert } from '@shoelace-style/shoelace/dist/react';
const App = () => (
2021-12-13 22:38:40 +00:00
<SlAlert variant="primary" open>
2021-11-04 22:12:47 +00:00
Nothing fancy here, just a simple alert.
</SlAlert>
);
```
2020-09-17 20:27:11 +00:00
### Duration
2021-07-08 21:41:10 +00:00
Set the `duration` attribute to automatically hide an alert after a period of time. This is useful for alerts that don't require acknowledgement.
2020-09-17 20:27:11 +00:00
```html preview
<div class="alert-duration">
2021-12-13 22:38:40 +00:00
<sl-button variant="primary">Show Alert</sl-button>
2020-09-17 20:27:11 +00:00
2021-12-13 22:38:40 +00:00
<sl-alert variant="primary" duration="3000" closable>
2020-09-17 20:27:11 +00:00
<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-10-13 16:41:57 +00:00
<style>
.alert-duration sl-alert {
margin-top: var(--sl-spacing-medium);
}
</style>
2020-09-17 20:27:11 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { useState } from 'react';
2022-03-02 15:10:41 +00:00
import { SlAlert, SlButton, SlIcon } from '@shoelace-style/shoelace/dist/react';
2021-11-04 22:12:47 +00:00
const css = `
.alert-duration sl-alert {
margin-top: var(--sl-spacing-medium);
}
`;
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
2021-11-05 14:06:06 +00:00
<div className="alert-duration">
2022-03-02 15:10:41 +00:00
<SlButton variant="primary" onClick={() => setOpen(true)}>
Show Alert
</SlButton>
<SlAlert variant="primary" duration="3000" open={open} closable onSlAfterHide={() => setOpen(false)}>
2021-11-04 22:12:47 +00:00
<SlIcon slot="icon" name="info-circle" />
This alert will automatically hide itself after three seconds, unless you interact with it.
</SlAlert>
</div>
<style>{css}</style>
</>
);
};
```
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
2021-07-08 21:41:10 +00:00
You should always use the `closable` attribute 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">
2021-12-13 22:38:40 +00:00
<sl-button variant="primary">Primary</sl-button>
<sl-button variant="success">Success</sl-button>
<sl-button variant="neutral">Neutral</sl-button>
<sl-button variant="warning">Warning</sl-button>
<sl-button variant="danger">Danger</sl-button>
2022-03-02 15:10:41 +00:00
2021-12-13 22:38:40 +00:00
<sl-alert variant="primary" duration="3000" closable>
2020-09-16 21:00:48 +00:00
<sl-icon slot="icon" name="info-circle"></sl-icon>
2022-03-02 15:10:41 +00:00
<strong>This is super informative</strong><br />
2020-09-16 21:00:48 +00:00
You can tell by how pretty the alert is.
</sl-alert>
2021-12-13 22:38:40 +00:00
<sl-alert variant="success" duration="3000" closable>
2020-09-16 21:00:48 +00:00
<sl-icon slot="icon" name="check2-circle"></sl-icon>
2022-03-02 15:10:41 +00:00
<strong>Your changes have been saved</strong><br />
2020-09-16 21:00:48 +00:00
You can safely exit the app now.
</sl-alert>
2021-12-13 22:38:40 +00:00
<sl-alert variant="neutral" duration="3000" closable>
2020-09-16 21:00:48 +00:00
<sl-icon slot="icon" name="gear"></sl-icon>
2022-03-02 15:10:41 +00:00
<strong>Your settings have been updated</strong><br />
2020-09-16 21:00:48 +00:00
Settings will take affect on next login.
</sl-alert>
2021-12-13 22:38:40 +00:00
<sl-alert variant="warning" duration="3000" closable>
2020-09-16 21:00:48 +00:00
<sl-icon slot="icon" name="exclamation-triangle"></sl-icon>
2022-03-02 15:10:41 +00:00
<strong>Your session has ended</strong><br />
2020-09-16 21:00:48 +00:00
Please login again to continue.
</sl-alert>
2021-12-13 22:38:40 +00:00
<sl-alert variant="danger" duration="3000" closable>
2020-09-16 21:00:48 +00:00
<sl-icon slot="icon" name="exclamation-octagon"></sl-icon>
2022-03-02 15:10:41 +00:00
<strong>Your account has been deleted</strong><br />
2020-09-16 21:00:48 +00:00
We're very sorry to see you go!
</sl-alert>
</div>
<script>
const container = document.querySelector('.alert-toast');
2021-12-13 22:38:40 +00:00
['primary', 'success', 'neutral', 'warning', 'danger'].map(variant => {
const button = container.querySelector(`sl-button[variant="${variant}"]`);
const alert = container.querySelector(`sl-alert[variant="${variant}"]`);
2020-09-16 21:00:48 +00:00
2020-09-17 20:27:11 +00:00
button.addEventListener('click', () => alert.toast());
2020-09-16 21:00:48 +00:00
});
</script>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { useRef } from 'react';
2022-03-02 15:10:41 +00:00
import { SlAlert, SlButton, SlIcon } from '@shoelace-style/shoelace/dist/react';
2021-11-04 22:12:47 +00:00
function showToast(alert) {
alert.toast();
}
const App = () => {
const primary = useRef(null);
const success = useRef(null);
const neutral = useRef(null);
const warning = useRef(null);
const danger = useRef(null);
return (
<>
2021-12-13 22:38:40 +00:00
<SlButton variant="primary" onClick={() => primary.current.toast()}>
2021-11-04 22:12:47 +00:00
Primary
</SlButton>
2021-12-13 22:38:40 +00:00
<SlButton variant="success" onClick={() => success.current.toast()}>
2021-11-04 22:12:47 +00:00
Success
</SlButton>
2021-12-13 22:38:40 +00:00
<SlButton variant="neutral" onClick={() => neutral.current.toast()}>
2021-11-04 22:12:47 +00:00
Neutral
</SlButton>
2021-12-13 22:38:40 +00:00
<SlButton variant="warning" onClick={() => warning.current.toast()}>
2021-11-04 22:12:47 +00:00
Warning
</SlButton>
2021-12-13 22:38:40 +00:00
<SlButton variant="danger" onClick={() => danger.current.toast()}>
2021-11-04 22:12:47 +00:00
Danger
</SlButton>
2021-12-13 22:38:40 +00:00
<SlAlert ref={primary} variant="primary" duration="3000" closable>
2021-11-04 22:12:47 +00:00
<SlIcon slot="icon" name="info-circle" />
2022-03-02 15:10:41 +00:00
<strong>This is super informative</strong>
<br />
2021-11-04 22:12:47 +00:00
You can tell by how pretty the alert is.
</SlAlert>
2021-12-13 22:38:40 +00:00
<SlAlert ref={success} variant="success" duration="3000" closable>
2021-11-04 22:12:47 +00:00
<SlIcon slot="icon" name="check2-circle" />
2022-03-02 15:10:41 +00:00
<strong>Your changes have been saved</strong>
<br />
2021-11-04 22:12:47 +00:00
You can safely exit the app now.
</SlAlert>
2021-12-13 22:38:40 +00:00
<SlAlert ref={neutral} variant="neutral" duration="3000" closable>
2021-11-04 22:12:47 +00:00
<SlIcon slot="icon" name="gear" />
2022-03-02 15:10:41 +00:00
<strong>Your settings have been updated</strong>
<br />
2021-11-04 22:12:47 +00:00
Settings will take affect on next login.
</SlAlert>
2021-12-13 22:38:40 +00:00
<SlAlert ref={warning} variant="warning" duration="3000" closable>
2021-11-04 22:12:47 +00:00
<SlIcon slot="icon" name="exclamation-triangle" />
2022-03-02 15:10:41 +00:00
<strong>Your session has ended</strong>
<br />
2021-11-04 22:12:47 +00:00
Please login again to continue.
</SlAlert>
2021-12-13 22:38:40 +00:00
<SlAlert ref={danger} variant="danger" duration="3000" closable>
2021-11-04 22:12:47 +00:00
<SlIcon slot="icon" name="exclamation-octagon" />
2022-03-02 15:10:41 +00:00
<strong>Your account has been deleted</strong>
<br />
2021-11-04 22:12:47 +00:00
We're very sorry to see you go!
</SlAlert>
</>
);
};
```
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">
2021-12-13 22:38:40 +00:00
<sl-button variant="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;
2022-03-02 15:10:41 +00:00
}
2020-09-17 20:27:11 +00:00
2020-09-18 13:40:21 +00:00
// Custom function to emit toast notifications
2021-12-13 22:38:40 +00:00
function notify(message, variant = 'primary', icon = 'info-circle', duration = 3000) {
2020-09-17 20:27:11 +00:00
const alert = Object.assign(document.createElement('sl-alert'), {
2021-12-13 22:38:40 +00:00
variant,
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]