Drawers slide in from the edge of a container to expose additional options and information without
navigating away. Useful for navigation menus, filters, and secondary content.
Drawers are great for showing additional content without leaving the current page.
CloseOpen Drawer
<cs-drawerlabel="Drawer"id="drawer-overview">
Drawers are great for showing additional content without leaving the current page.
<cs-buttonslot="footer"variant="brand"data-drawer="close">Close</cs-button></cs-drawer><cs-buttonappearance="filled">Open Drawer</cs-button><scripttype="module">const drawer = document.querySelector('#drawer-overview');const openButton = drawer.nextElementSibling;
openButton.addEventListener('click',()=>(drawer.open =true));</script>
Examples
Without a Header
Headers are enabled by default. To render a drawer without a header, add the without-header attribute.
Footers can be used to display titles and more. Use the footer slot to add a footer to the drawer.
This drawer has a footer where you can put actions and other controls.
CloseOpen Drawer
<cs-drawerlabel="Drawer"class="drawer-footer">
This drawer has a footer where you can put actions and other controls.
<cs-buttonslot="footer"variant="brand"data-drawer="close">Close</cs-button></cs-drawer><cs-buttonappearance="filled">Open Drawer</cs-button><scripttype="module">const drawer = document.querySelector('.drawer-footer');const openButton = drawer.nextElementSibling;
openButton.addEventListener('click',()=>(drawer.open =true));</script>
Opening & Closing Declaratively
You can open and close drawers with JavaScript by toggling the open attribute, but you can also do it declaratively. Add the data-drawer="open id" to any button on the page, where id is the ID of the drawer you want to open.
This drawer was opened declaratively using a data attribute on the button.
CloseOpen Drawer
<cs-drawerlabel="Drawer"id="drawer-opening">
This drawer was opened declaratively using a data attribute on the button.
<cs-buttonslot="footer"variant="brand"data-drawer="close">Close</cs-button></cs-drawer><cs-buttonappearance="filled"data-drawer="open drawer-opening">Open Drawer</cs-button>
Similarly, you can add data-drawer="close" to a button inside of a drawer to tell it to close.
Click the button below to close this drawer — no JavaScript required!
CloseOpen Drawer
<cs-drawerlabel="Drawer"id="drawer-dismiss">
Click the button below to close this drawer — no JavaScript required!
<cs-buttonslot="footer"variant="brand"data-drawer="close">Close</cs-button></cs-drawer><cs-buttonappearance="filled"data-drawer="open drawer-dismiss">Open Drawer</cs-button>
Placement
Drawers slide in from the end by default. Set the placement attribute to slide in from a different edge.
Placement
Slides in from
enddefault
The end (right, in LTR)
start
The start (left, in LTR)
top
The top
bottom
The bottom
This drawer slides in from the start.
CloseOpen from Start
<cs-drawerlabel="Drawer"placement="start"class="drawer-placement-start">
This drawer slides in from the start.
<cs-buttonslot="footer"variant="brand"data-drawer="close">Close</cs-button></cs-drawer><cs-buttonappearance="filled">Open from Start</cs-button><scripttype="module">const drawer = document.querySelector('.drawer-placement-start');const openButton = drawer.nextElementSibling;
openButton.addEventListener('click',()=>(drawer.open =true));</script>
This drawer slides in from the bottom.
CloseOpen from Bottom
<cs-drawerlabel="Drawer"placement="bottom"class="drawer-placement-bottom">
This drawer slides in from the bottom.
<cs-buttonslot="footer"variant="brand"data-drawer="close">Close</cs-button></cs-drawer><cs-buttonappearance="filled">Open from Bottom</cs-button><scripttype="module">const drawer = document.querySelector('.drawer-placement-bottom');const openButton = drawer.nextElementSibling;
openButton.addEventListener('click',()=>(drawer.open =true));</script>
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.
This drawer is always 50% of the viewport.
CloseOpen Drawer
<cs-drawerlabel="Drawer"class="drawer-custom-size"style="--size: 50vw;">
This drawer is always 50% of the viewport.
<cs-buttonslot="footer"variant="brand"data-drawer="close">Close</cs-button></cs-drawer><cs-buttonappearance="filled">Open Drawer</cs-button><scripttype="module">const drawer = document.querySelector('.drawer-custom-size');const openButton = drawer.nextElementSibling;
openButton.addEventListener('click',()=>(drawer.open =true));</script>
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.
Scroll down and give it a try!
CloseOpen Drawer
<cs-drawerlabel="Drawer"class="drawer-scrolling"><divstyle="height: 150vh;border: dashed 2px var(--cs-color-surface-border);padding: 0 1rem;"><pclass="cs-cluster cs-gap-2xs">Scroll down and give it a try! <cs-iconname="arrow_downward"></cs-icon></p></div><cs-buttonslot="footer"variant="brand"data-drawer="close">Close</cs-button></cs-drawer><cs-buttonappearance="filled">Open Drawer</cs-button><scripttype="module">const drawer = document.querySelector('.drawer-scrolling');const openButton = drawer.nextElementSibling;
openButton.addEventListener('click',()=>(drawer.open =true));</script>
Header Actions
The header shows a functional close button by default. You can use the header-actions slot to add additional buttons if needed.
You can add custom actions to the header, like the button up there to open in a new window.
CloseOpen Drawer
<cs-drawerlabel="Drawer"class="drawer-header-actions"><cs-buttonclass="new-window"slot="header-actions"appearance="plain"><cs-iconname="open_in_new"label="Open in new window"></cs-icon></cs-button>
You can add custom actions to the header, like the button up there to open in a new window.
<cs-buttonslot="footer"variant="brand"data-drawer="close">Close</cs-button></cs-drawer><cs-buttonappearance="filled">Open Drawer</cs-button><scripttype="module">const drawer = document.querySelector('.drawer-header-actions');const openButton = drawer.nextElementSibling;const newWindowButton = drawer.querySelector('.new-window');
openButton.addEventListener('click',()=>(drawer.open =true));
newWindowButton.addEventListener('click',()=> window.open(location.href));</script>
Light Dismissal
If you want the drawer to close when the user clicks on the overlay, add the light-dismiss attribute.
This drawer will close when you click on the overlay.
CloseOpen Drawer
<cs-drawerlabel="Drawer"light-dismissclass="drawer-light-dismiss">
This drawer will close when you click on the overlay.
<cs-buttonslot="footer"variant="brand"data-drawer="close">Close</cs-button></cs-drawer><cs-buttonappearance="filled">Open Drawer</cs-button><scripttype="module">const drawer = document.querySelector('.drawer-light-dismiss');const openButton = drawer.nextElementSibling;
openButton.addEventListener('click',()=>(drawer.open =true));</script>
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 cs-hide 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.
This drawer will only close when you click the button below.
CloseOpen Drawer
<cs-drawerlabel="Drawer"class="drawer-deny-close">
This drawer will only close when you click the button below.
<cs-buttonslot="footer"variant="brand"data-drawer="close">Close</cs-button></cs-drawer><cs-buttonappearance="filled">Open Drawer</cs-button><scripttype="module">const drawer = document.querySelector('.drawer-deny-close');const openButton = drawer.nextElementSibling;const closeButton = drawer.querySelector('cs-button[slot="footer"]');
openButton.addEventListener('click',()=>(drawer.open =true));// Prevent the drawer from closing unless the close button is clicked
drawer.addEventListener('cs-hide',event=>{if(event.detail.source !== closeButton){
event.preventDefault();}});</script>
Initial Focus
To give focus to a specific element when the drawer opens, use the autofocus attribute.
CloseOpen Drawer
<cs-drawerlabel="Drawer"class="drawer-focus"><cs-inputautofocusplaceholder="I will have focus when the drawer is opened"></cs-input><cs-buttonslot="footer"variant="brand"data-drawer="close">Close</cs-button></cs-drawer><cs-buttonappearance="filled">Open Drawer</cs-button><scripttype="module">const drawer = document.querySelector('.drawer-focus');const input = drawer.querySelector('cs-input');const openButton = drawer.nextElementSibling;
openButton.addEventListener('click',()=>(drawer.open =true));</script>
API
Importing
If you're using the autoloader or a hosted project, components load on demand — no manual import
needed. To cherry-pick this component, use one of the following snippets.
The drawer's label as displayed in the header. You should always include a relevant label, as it is required for proper accessibility. If you need to display HTML, use the label slot instead.
string
''
Yes
lightDismiss
light-dismiss
When enabled, the drawer will be closed when the user clicks outside of it.
boolean
false
—
modal
—
Exposes the internal modal utility that controls focus trapping. To temporarily disable focus trapping and allow third-party modals spawned from an active Shoelace modal, call modal.activateExternal() when the third-party modal opens. Upon closing, call modal.deactivateExternal() to restore Shoelace's focus trapping.
—
—
—
open
open
Indicates whether or not the drawer is open. Toggle this attribute to show and hide the drawer.
boolean
false
Yes
placement
placement
The direction from which the drawer will open.
'top' | 'end' | 'bottom' | 'start'
'end'
Yes
ssrFooter
ssr-footer
Only required for SSR. Set to true if you're slotting in a footer element so the server-rendered markup includes the footer before the component hydrates on the client.
boolean
false
—
withoutHeader
without-header
Disables the header. This will also remove the default close button.
Emitted after the drawer closes and all animations are complete.
cs-after-show
Emitted after the drawer opens and all animations are complete.
cs-hide
Emitted when the drawer is requesting to close. Calling event.preventDefault() will prevent the drawer from closing. You can inspect event.detail.source to see which element caused the drawer to close. If the source is the drawer element itself, the user has pressed Escape or the drawer has been closed programmatically. Avoid using this unless closing the drawer will result in destructive behavior such as data loss.
A filter to apply to the backdrop behind the drawer.
none
--hide-duration
The animation duration when hiding the drawer.
var(--cs-transition-normal)
--show-duration
The animation duration when showing the drawer.
var(--cs-transition-normal)
--size
The preferred size of the drawer. This will be applied to the drawer's width or height depending on its placement. Note that the drawer will shrink to accommodate smaller screens.
—
--spacing
The amount of space around and between the drawer's content.