# Dialog
[component-header:sl-dialog]
Dialogs, sometimes called "modals", appear above the page and require the user's immediate attention.
```html preview
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
CloseOpen Dialog
```
```jsx react
import { useState } from 'react';
import { SlButton, SlDialog } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
setOpen(false)}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
setOpen(false)}>
Close
setOpen(true)}>Open Dialog
>
);
};
```
## UX Tips
- Use a dialog when you immediately require the user's attention, e.g. confirming a destructive action.
- Always provide an obvious way for the user to dismiss the dialog.
- Don't nest dialogs. It almost always leads to a poor experience for the user.
## Examples
### Custom Width
Use the `--width` custom property to set the dialog's width.
```html preview
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
CloseOpen Dialog
```
```jsx react
import { useState } from 'react';
import { SlButton, SlDialog } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
setOpen(false)}
>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
setOpen(false)}>
Close
setOpen(true)}>Open Dialog
>
);
};
```
### Scrolling
By design, a dialog's height will never exceed that of the viewport. As such, dialogs will not scroll with the page ensuring the header and footer are always accessible to the user.
```html preview
setOpen(false)}>
Close
setOpen(true)}>Open Dialog
>
);
};
```
### Preventing the Dialog from Closing
By default, dialogs will close when the user clicks the close button, clicks the overlay, or presses the Escape key. In most cases, the default behavior is the best behavior in terms of UX. However, there are situations where this may be undesirable, such as when data loss will occur.
To keep the dialog open in such cases, you can cancel the `sl-request-close` event. When canceled, the dialog will remain open and pulse briefly to draw the user's attention to it.
```html preview
This dialog will not close unless you use the button below.
Save & CloseOpen Dialog
```
```jsx react
import { useState } from 'react';
import { SlButton, SlDialog } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
event.preventDefault()}
onSlAfterHide={() => setOpen(false)}
>
This dialog will not close unless you use the button below.
setOpen(false)}>
Save & Close
setOpen(true)}>Open Dialog
>
);
};
```
### Customizing Initial Focus
By default, the dialog's panel will gain focus when opened. This allows a subsequent tab press to focus on the first tabbable element within the dialog. To set focus on a different element, listen for and cancel the `sl-initial-focus` event.
```html preview
CloseOpen Dialog
```
```jsx react
import { useRef, useState } from 'react';
import {
SlButton,
SlDialog,
SlInput
} from '@shoelace-style/shoelace/dist/react';
const App = () => {
const input = useRef(null);
const [open, setOpen] = useState(false);
function handleInitialFocus(event) {
event.preventDefault();
input.current.focus();
}
return (
<>
setOpen(false)}
>
setOpen(false)}>
Close
setOpen(true)}>Open Dialog
>
);
};
```
[component-metadata:sl-dialog]