# Dialog
## Basic example
Dialogs are built using the `Dialog`, `DialogOverlay`, `DialogTitle`, and `DialogDescription` components.
When the dialog's `open` prop is `true`, the contents of the dialog will render. Focus will be moved inside the dialog and trapped there as the user cycles through the focusable elements. Scrolling is locked, the rest of your application UI is hidden from screen readers, and clicking outside the dialog or pressing the Escape key will fire the `close` event and close the dialog.
```svelte
```
## Showing and hiding your dialog
`Dialog` has no automatic management of its open/closed state. To show and hide your dialog, pass in a boolean value to the `open` prop. When `open` is true the dialog will render, and when it's false the dialog will unmount.
The `close` event is fired when an open dialog is dismissed, which happens when the user clicks outside of the contents of your dialog or presses the Escape key. You can use this callback to set `open` back to false and close your dialog.
```svelte
```
## Managing focus within your dialog
For accessibility reasons, your dialog should contain at least one focusable element. By default, the `Dialog` component will focus the first focusable element (by DOM order) when it is rendered, and pressing the Tab key will cycle through all additional focusable elements within the contents.
Focus is trapped within the dialog as long as it is rendered, so tabbing to the end will start cycling back through the beginning again. All other application elements outside of the dialog will be marked as inert and thus not focusable.
If you'd like something other than the first focusable element to receive initial focus when your dialog is initially rendered, you can use the `initialFocus` prop:
```svelte
```
## Styling
[See here](general-concepts#component-styling) for some general notes on styling the components in this library.
### Styling the overlay
Typically modal dialogs will be rendered on top of a transparent dark background. You can style the `DialogOverlay` component to achieve this look.
The overlay component accepts normal styling props like `style` and `class`, so you can style it using any technique you like. Be sure to place it before the rest of your dialog's contents in the DOM so that it doesn't obscure the dialog's interactive elements.
```svelte
```
### Styling the dialog
There's nothing special about the contents of your dialog--you can use whatever HTML and CSS you please. Typical dialogs will have a max width and be centered in the screen, but fullscreen treatments on smaller screens are also common.
## Using the Title and Description components
For accessibility reasons, you should use the `DialogTitle` and `DialogDescription` components when rendering content that labels and describes your dialog contents. They will be automatically linked to the root `Dialog` component via the `aria-labelledby` and `aria-describedby` attributes, and their contents will be announced to users using screenreaders.
```svelte
```
## Rendering to a Portal
If you've ever implemented a dialog before in another framework, you may have come across the concept of portals. Portals let you insert components from one place in the component tree (for instance, deep within your application UI), but have them actually render to another place in the DOM entirely.
Since dialogs and their overlays take up the full page, you typically want to render them as a sibling to the root node of the page. That way, you can rely on natural DOM ordering to ensure that their content is rendered on top of your existing application UI. This also makes it easy to apply scroll locking to the rest of your application, as well as ensure that your Dialog's contents and overlay are unobstructed to receive focus and click events.
Because of these accessibility concerns, Svelte Headless UI's `Dialog` actually uses a portal under the hood. This way we can provide features like unobstructed event handling and making the rest of your application inert. That means that when using this `Dialog`, there's no need to use a portal yourself--it's already taken care of. You can put the `