<cs-drawer>
StableLayoutSince 0.1

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. Close Open Drawer

Examples

Without a Header

Headers are enabled by default. To render a drawer without a header, add the without-header attribute.

Look ma, no header! Close Open Drawer

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. Close Open Drawer

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. Close Open Drawer

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! Close Open Drawer

Placement

Drawers slide in from the end by default. Set the placement attribute to slide in from a different edge.

PlacementSlides in from
end defaultThe end (right, in LTR)
startThe start (left, in LTR)
topThe top
bottomThe bottom
This drawer slides in from the start. Close Open from Start
This drawer slides in from the bottom. Close Open from Bottom

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. Close 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.

Scroll down and give it a try!

Close
Open Drawer

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. Close Open Drawer

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. Close 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 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. Close Open Drawer

Initial Focus

To give focus to a specific element when the drawer opens, use the autofocus attribute.

Close Open Drawer

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.

npm Self-Hosted React
import '@cruglobal/cornerstone-components/components/drawer/drawer.js';
import './cornerstone/components/drawer/drawer.js';
import CsDrawer from '@cruglobal/cornerstone-components/react/drawer/index.js';

Slots

Learn more about using slots.

NameDescription
(default)The drawer's main content.
footerThe drawer's footer, usually one or more buttons representing various options.
header-actionsOptional actions to add to the header. Works best with <cs-button>.
labelThe drawer's label. Alternatively, you can use the label attribute.

Attributes & Properties

Learn more about attributes and properties.

PropertyAttributeDescriptionTypeDefaultReflects
labellabelThe 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
lightDismisslight-dismissWhen enabled, the drawer will be closed when the user clicks outside of it.booleanfalse
modalExposes 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.
openopenIndicates whether or not the drawer is open. Toggle this attribute to show and hide the drawer.booleanfalseYes
placementplacementThe direction from which the drawer will open.'top' | 'end' | 'bottom' | 'start''end'Yes
ssrFooterssr-footerOnly 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.booleanfalse
withoutHeaderwithout-headerDisables the header. This will also remove the default close button.booleanfalseYes

Events

Learn more about events.

NameDescription
cs-after-hideEmitted after the drawer closes and all animations are complete.
cs-after-showEmitted after the drawer opens and all animations are complete.
cs-hideEmitted 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.
cs-showEmitted when the drawer opens.

CSS Custom Properties

Learn more about CSS custom properties.

NameDescriptionDefault
--backdrop-filterA filter to apply to the backdrop behind the drawer.none
--hide-durationThe animation duration when hiding the drawer.var(--cs-transition-normal)
--show-durationThe animation duration when showing the drawer.var(--cs-transition-normal)
--sizeThe 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.
--spacingThe amount of space around and between the drawer's content.

CSS Parts

Learn more about CSS parts.

NameDescriptionCSS selector
bodyThe drawer's body.::part(body)
close-buttonThe close button, a <cs-button>.::part(close-button)
close-button__buttonThe close button's exported button part.::part(close-button__button)
dialogThe drawer's internal <dialog> element.::part(dialog)
footerThe drawer's footer.::part(footer)
headerThe drawer's header. This element wraps the title and header actions.::part(header)
header-actionsOptional actions to add to the header. Works best with <cs-button>.::part(header-actions)
titleThe drawer's title.::part(title)

Dependencies

This component automatically imports the following elements. Sub-dependencies, if any exist, are included in this list.