---
meta:
title: Dialog
description: 'Dialogs, sometimes called "modals", appear above the page and require the user''s immediate attention.'
layout: component
---
```html:preview
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
CloseOpen Dialog
```
```jsx:react
import { useState } from 'react';
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlDialog from '@shoelace-style/shoelace/dist/react/dialog';
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
>
);
};
```
## 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
```
{% raw %}
```jsx:react
import { useState } from 'react';
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlDialog from '@shoelace-style/shoelace/dist/react/dialog';
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
>
);
};
```
{% endraw %}
### 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
Scroll down and give it a try! 👇
CloseOpen Dialog
```
{% raw %}
```jsx:react
import { useState } from 'react';
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlDialog from '@shoelace-style/shoelace/dist/react/dialog';
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
setOpen(false)}>
Scroll down and give it a try! 👇
setOpen(false)}>
Close
setOpen(true)}>Open Dialog
>
);
};
```
{% endraw %}
### Header Actions
The header shows a functional close button by default. You can use the `header-actions` slot to add additional [icon buttons](/components/icon-button) if needed.
```html:preview
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
CloseOpen Dialog
```
```jsx:react
import { useState } from 'react';
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlDialog from '@shoelace-style/shoelace/dist/react/dialog';
import SlIconButton from '@shoelace-style/shoelace/dist/react/icon-button';
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
setOpen(false)}>
window.open(location.href)}
/>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
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.
You can use `event.detail.source` to determine what triggered the request to close. This example prevents the dialog from closing when the overlay is clicked, but allows the close button or [[Escape]] to dismiss it.
```html:preview
This dialog will not close when you click on the overlay.
CloseOpen Dialog
```
```jsx:react
import { useState } from 'react';
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlDialog from '@shoelace-style/shoelace/dist/react/dialog';
const App = () => {
const [open, setOpen] = useState(false);
// Prevent the dialog from closing when the user clicks on the overlay
function handleRequestClose(event) {
if (event.detail.source === 'overlay') {
event.preventDefault();
}
}
return (
<>
setOpen(false)}>
This dialog will not close when you click on the overlay.
setOpen(false)}>
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 in the dialog. If you want a different element to have focus, add the `autofocus` attribute to it as shown below.
```html:preview
CloseOpen Dialog
```
```jsx:react
import { useState } from 'react';
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlDialog from '@shoelace-style/shoelace/dist/react/dialog';
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
setOpen(false)}>
setOpen(false)}>
Close
setOpen(true)}>Open Dialog
>
);
};
```
:::tip
You can further customize initial focus behavior by canceling the `sl-initial-focus` event and setting focus yourself inside the event handler.
:::