Skip to main content Sidebar
On this page
Skip to table of contents

Drawer

<pc-drawer> 0.5.1 experimental

Drawers slide in from a container to expose additional options and information.

<pc-drawer label="Drawer" class="drawer-overview">
    This drawer contains supplementary content that is not required immediately, such as settings, contextual actions or secondary information related to the current view.

    <pc-button variant="plain" slot="footer" data-drawer="close">
        Close
    </pc-button>
</pc-drawer>

<pc-button>Open drawer</pc-button>

<script>
    const drawer = document.querySelector(".drawer-overview");
    const openButton = drawer.nextElementSibling;

    openButton.addEventListener("click", () => (drawer.open = true));
</script>
Code Edit

Demos

Opening and closing drawers 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 dialog you want to open.

Edit

Similarly, you can add data-drawer="close" to a button inside of the drawer to tell it to close it.

Edit

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.

Edit

Slide in from top

To make the drawer slide in from the top, set the placement attribute to top.

Edit

Slide in from bottom

To make the drawer slide in from the bottom, set the placement attribute to bottom.

Edit

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.

Edit

Scrolling

By design, a drawer’s height will never exceed that of the viewport. As such, drawers will not scroll with the page to ensure the header and footer are always accessible to the user.

Edit

Header actions

The header shows a functional close button by default. You can use the header-actions slot to add additional icon buttons if needed.

Edit

Prevent light dismissal

If you want to prevent the drawer from closing if the user clicks on the overlay, add the no-light-dismiss attribute.

Edit

Prevent drawer from closing

By default, drawers will close when the user clicks the close button, clicks the overlay or presses the Esc key. In most cases, the default behaviour is the best behaviour 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 pc-hide event. When cancelled, the drawer will remain open and pulse briefly to draw the user’s attention to it.

You can use event.detail.source to check what triggered the close request. In this example, the drawer will only close when a custom close button inside the footer is clicked. Clicking the overlay, pressing Esc or using the built‐in header close button will all be prevented.

Edit

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

Edit

Properties

NameDescriptionReflectsDefault
openIndicates whether or not the drawer is open. Toggle this attribute to show and hide the drawer.
Type: boolean
false
labelThe drawer’s label as displayed in the header. You should always include a relevant label even when using no-header, as it is required for proper accessibility. If you need to display HTML, use the label slot instead.
Type: string
""
placementThe direction from which the drawer will open.
Type: "top" | "end" | "bottom" | "start"
"end"
noHeader
no-header
This removes the header. This will also remove the default close button, so please ensure you provide an easy, accessible way for users to dismiss the drawer.
Type: boolean
false
noLightDismiss
no-light-dismiss
Prevents clicks on the backdrop causing the drawer to close.
Type: boolean
false
updateComplete A read‐only promise that resolves when the component has finished updating.

Learn more about customising animations.

Slots

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

Learn more about using slots.

Events

NameDescriptionEvent detail
pc-showEmitted when the drawer opens.
pc-after-showEmitted after the drawer opens and all animations are complete.
pc-hideEmitted when the drawer closes.{ source: Element }
pc-after-hideEmitted after the drawer closes and all animations are complete.

Learn more about events.

Custom properties

NameDescriptionDefault
--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.25rem
--header-spacingThe amount of padding to use for the header.var(--pc-spacing-xl)
--body-spacingThe amount of padding to use for the body.var(--pc-spacing-s) var(--pc-spacing-xl)
--footer-spacingThe amount of padding to use for the footer.var(--pc-spacing-s) var(--pc-spacing-xl) var(--pc-spacing-xl)

Learn more about customising custom properties.

Parts

NameDescription
drawerThe drawer’s <dialog> element.
headerThe drawer’s header. This element wraps the title and header actions.
header-actionsOptional actions to add to the header. Works best with <pc-button>.
titleThe drawer’s title.
close-buttonThe close button, a <pc-button>.
close-button-baseThe close button’s exported base part.
bodyThe drawer’s body.
footerThe drawer’s footer.

Learn more about customising CSS parts.

Animations

NameDescription
drawer.showTopThe animation to use when showing a drawer with top placement.
drawer.showEndThe animation to use when showing a drawer with end placement.
drawer.showBottomThe animation to use when showing a drawer with bottom placement.
drawer.showStartThe animation to use when showing a drawer with start placement.
drawer.hideTopThe animation to use when hiding a drawer with top placement.
drawer.hideEndThe animation to use when hiding a drawer with end placement.
drawer.hideBottomThe animation to use when hiding a drawer with bottom placement.
drawer.hideStartThe animation to use when hiding a drawer with start placement.
drawer.denyCloseThe animation to use when a request to close the drawer is denied.

Learn more about customising animations.

Importing

If you’re using the autoloader or the standard loader, you can skip this section. But if you’re cherry picking, you can use any of the following snippets to import this component.

CDN (script tag)CDN (import)npm (import)

To manually import this component from the CDN, copy this code snippet and paste it in your HTML.

<script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/drawer/drawer.js"></script>

To manually import this component from the CDN, copy this code snippet and paste it in your JavaScript file.

import "https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/drawer/drawer.js";

To manually import this component from npm, copy this code snippet and paste it in your JavaScript file.

import "placer-toolkit/dist/components/drawer/drawer.js";

Dependencies

This component automatically imports these components:

We’d love to hear from you. Please reach out to us with any questions or enquiries you may have.

You can contact us via e‐mail at placer.coc.reports+contact@gmail.com.

We look forward to hearing from you!

Got it!
Dangerous lands

Whoa! You’ve wandered into the dangerous lands of Placer Toolkit. Version 0 is out of date and doesn’t meet EU privacy standards, including GDPR.

Want the latest powers, security and compliance? Stick with the current version of Placer Toolkit!

Yikes! Power up!

Our site is 100 % cookie‐free! We value your privacy, which is why we don’t store any cookies or personal information related to you.

Your browser history is safe from crumbs, and your data is protected, aligning with modern privacy standards like the GDPR. Enjoy your visit without a single digital cookie in sight! 🍪🚫

View our Privacy Policy for more information.

Got it!