# Alert [component-header:sl-alert] Alerts are used to display important messages inline or as toast notifications. ```html preview This is a standard alert. You can customize its content and even the icon. ``` ```jsx react import { SlAlert, SlIcon } from '@shoelace-style/shoelace/dist/react'; const App = () => ( This is a standard alert. You can customize its content and even the icon. ); ``` ?> Alerts will not be visible if the `open` attribute is not present. ## Examples ### Variants Set the `variant` attribute to change the alert's variant. ```html preview This is super informative
You can tell by how pretty the alert is.

Your changes have been saved
You can safely exit the app now.

Your settings have been updated
Settings will take affect on next login.

Your session has ended
Please login again to continue.

Your account has been deleted
We're very sorry to see you go!
``` ```jsx react import { SlAlert, SlIcon } from '@shoelace-style/shoelace/dist/react'; const App = () => ( <> This is super informative
You can tell by how pretty the alert is.

Your changes have been saved
You can safely exit the app now.

Your settings have been updated
Settings will take affect on next login.

Your session has ended
Please login again to continue.

Your account has been deleted
We're very sorry to see you go!
); ``` ### Closable Add the `closable` attribute to show a close button that will hide the alert. ```html preview You can close this alert any time! ``` ```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 ( You can close this alert any time! ); }; ``` ### Without Icons Icons are optional. Simply omit the `icon` slot if you don't want them. ```html preview Nothing fancy here, just a simple alert. ``` ```jsx react import { SlAlert } from '@shoelace-style/shoelace/dist/react'; const App = () => ( Nothing fancy here, just a simple alert. ); ``` ### Duration Set the `duration` attribute to automatically hide an alert after a period of time. This is useful for alerts that don't require acknowledgement. ```html preview
Show Alert This alert will automatically hide itself after three seconds, unless you interact with it.
``` ```jsx react import { useState } from 'react'; import { SlAlert, SlButton, SlIcon } from '@shoelace-style/shoelace/dist/react'; const css = ` .alert-duration sl-alert { margin-top: var(--sl-spacing-medium); } `; const App = () => { const [open, setOpen] = useState(false); return ( <>
setOpen(true)}> Show Alert setOpen(false)}> This alert will automatically hide itself after three seconds, unless you interact with it.
); }; ``` ### Toast Notifications 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. 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. ```html preview
Primary Success Neutral Warning Danger This is super informative
You can tell by how pretty the alert is.
Your changes have been saved
You can safely exit the app now.
Your settings have been updated
Settings will take affect on next login.
Your session has ended
Please login again to continue.
Your account has been deleted
We're very sorry to see you go!
``` ```jsx react import { useRef } from 'react'; import { SlAlert, SlButton, SlIcon } from '@shoelace-style/shoelace/dist/react'; 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 ( <> primary.current.toast()}> Primary success.current.toast()}> Success neutral.current.toast()}> Neutral warning.current.toast()}> Warning danger.current.toast()}> Danger This is super informative
You can tell by how pretty the alert is.
Your changes have been saved
You can safely exit the app now.
Your settings have been updated
Settings will take affect on next login.
Your session has ended
Please login again to continue.
Your account has been deleted
We're very sorry to see you go!
); }; ``` ### 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. ```html preview
Create Toast
``` ### The Toast Stack 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. 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. ```css .sl-toast-stack { left: 0; right: auto; } ``` ?> 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. [component-metadata:sl-alert]