# Drawer
[component-header:sl-drawer]
Drawers slide in from a container to expose additional options and information.
```html preview
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Close
Open Drawer
```
```jsx react
import { useState } from 'react';
import { SlButton, SlDrawer } 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 Drawer
>
);
};
```
## Examples
### Slide in From Start
By default, drawers slide in from the end. To make the drawer slide in from the start, set the `placement` attribute to `start`.
```html preview
This drawer slides in from the start.
Close
Open Drawer
```
```jsx react
import { useState } from 'react';
import { SlButton, SlDrawer } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
setOpen(false)}>
This drawer slides in from the start.
setOpen(false)}>
Close
setOpen(true)}>Open Drawer
>
);
};
```
### Slide in From Top
To make the drawer slide in from the top, set the `placement` attribute to `top`.
```html preview
This drawer slides in from the top.
Close
Open Drawer
```
```jsx react
import { useState } from 'react';
import { SlButton, SlDrawer } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
setOpen(false)}>
This drawer slides in from the top.
setOpen(false)}>
Close
setOpen(true)}>Open Drawer
>
);
};
```
### Slide in From Bottom
To make the drawer slide in from the bottom, set the `placement` attribute to `bottom`.
```html preview
This drawer slides in from the bottom.
Close
Open Drawer
```
```jsx react
import { useState } from 'react';
import { SlButton, SlDrawer } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
setOpen(false)}>
This drawer slides in from the bottom.
setOpen(false)}>
Close
setOpen(true)}>Open Drawer
>
);
};
```
### Contained to an Element
By default, the drawer slides out of its [containing block](https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#Identifying_the_containing_block), which is usually the viewport. To make the drawer slide out of its parent element, add the `contained` attribute and `position: relative` to the parent.
```html preview
The drawer will be contained to this box. This content won't shift or be affected in any way when the drawer opens.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Close
Open Drawer
```
```jsx react
import { useState } from 'react';
import { SlButton, SlDrawer } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
The drawer will be contained to this box. This content won't shift or be affected in any way when the drawer
opens.
setOpen(false)} style={{ '--size': '50%' }}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
setOpen(false)}>
Close
setOpen(true)}>Open Drawer
>
);
};
```
### Custom Size
Use the `--size` custom property to set the drawer's size. This will be applied to the drawer's width or height depending on its `placement`.
```html preview
This drawer is always 50% of the viewport.
Close
Open Drawer
```
```jsx react
import { useState } from 'react';
import { SlButton, SlDrawer } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
setOpen(false)} style={{ '--size': '50vw' }}>
This drawer is always 50% of the viewport.
setOpen(false)}>
Close
setOpen(true)}>Open Drawer
>
);
};
```
### Scrolling
By design, a drawer's height will never exceed 100% of its container. As such, drawers will not scroll with the page to ensure the header and footer are always accessible to the user.
```html preview
Scroll down and give it a try! 👇
Close
Open Drawer
```
```jsx react
import { useState } from 'react';
import { SlButton, SlDrawer } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
setOpen(false)}>
Scroll down and give it a try! 👇
setOpen(false)}>
Close
setOpen(true)}>Open Drawer
>
);
};
```
### Preventing the Drawer from Closing
By default, drawers 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 drawer open in such cases, you can cancel the `sl-request-close` event. When canceled, the drawer 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 drawer from closing when the overlay is clicked, but allows the close button or Escape to dismiss it.
```html preview
This drawer will not close when you click on the overlay.
Close
Open Drawer
```
```jsx react
import { useState } from 'react';
import { SlButton, SlDrawer } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(false);
// Prevent the drawer from closing when the user clicks on the overlay
function handleRequestClose(event) {
if (event.detail.source === 'overlay') {
event.preventDefault();
}
}
return (
<>
setOpen(false)}>
This drawer will not close when you click on the overlay.
setOpen(false)}>
Save & Close
setOpen(true)}>Open Drawer
>
);
};
```
### Customizing Initial Focus
By default, the drawer's panel will gain focus when opened. This allows a subsequent tab press to focus on the first tabbable element in the drawer. If you want a different element to have focus, add the `autofocus` attribute to it as shown below.
```html preview
Close
Open Drawer
```
```jsx react
import { useState } from 'react';
import { SlButton, SlDrawer, SlInput } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [open, setOpen] = useState(false);
return (
<>
setOpen(false)}>
setOpen(false)}>
Close
setOpen(true)}>Open Drawer
>
);
};
```
?> You can further customize initial focus behavior by canceling the `sl-initial-focus` event and setting focus yourself inside the event handler.
[component-metadata:sl-drawer]