Skip to main content Design tokens Components Style utilities
Skip to table of contents

    Drawer

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

    <pc-drawer label="Drawer" class="drawer-overview">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      <pc-button appearance="text" slot="footer">Close</pc-button>
    </pc-drawer>
    
    <pc-button>Open drawer</pc-button>
    
    <script>
      const drawer = document.querySelector(".drawer-overview");
      const openButton = drawer.nextElementSibling;
      const closeButton = drawer.querySelector('pc-button[slot="footer"]');
    
      openButton.addEventListener("click", () => drawer.show());
      closeButton.addEventListener("click", () => drawer.hide());
    </script>
    Code

    Demos

    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.

    Slide in from top

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

    Slide in from bottom

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

    Contained to an element

    By default, drawers slide out of their containing block, which is usually the viewport. To make a drawer slide out of a parent element, add the contained attribute to the drawer and apply position: relative to its parent.

    Unlike normal drawers, contained drawers are not modal. This means they do not show an overlay, they do not trap focus and they are not dismissible with the Esc key. This is intentional to allow users to interact with elements outside the 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.

    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.

    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.

    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-request-close 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 determine what triggered to request to close. This example prevents the drawer from closing when the overlay is clicked, but allows the close button or Esc to dismiss it.

    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.

    You can further customise initial focus behaviour by cancelling the pc-initial-focus event and setting focus yourself inside the event handler.

    Properties

    NameDescriptionReflectsDefault
    modalExposes the internal modal utility that controls focus trapping. To temporarily disable focus trapping and allow third‐party modals spawned from an active Placer Toolkit modal, call modal.activateExternal() when the third‐party modal opens. Upon closing, call modal.deactivateExternal() to restore Placer Toolkit’s focus trapping. new Modal(this)
    openIndicates whether or not the drawer is open. You can toggle this attribute to show and hide the drawer, or you can use the show() and hide() methods and this attribute will reflect the drawer’s open state. booleanfalse
    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. string""
    placementThe direction from which the drawer will open. "top" | "end" | "bottom" | "start""end"
    containedBy default, the drawer slides out of its containing block (usually the viewport). To make the drawer slide out of its parent element, set this attribute and add position: relative to the parent. booleanfalse
    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. booleanfalse
    updateComplete A read‐only promise that resolves when the component has finished updating. -

    Learn more about attributes and properties.

    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-icon-button>.
    footerThe drawer’s footer, usually one or more buttons representing various options.

    Learn more about using slots.

    Methods

    NameDescriptionArguments
    show()Shows the drawer.-
    hide()Hides the drawer.-

    Learn more about methods.

    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.-
    pc-after-hideEmitted after the drawer closes and all animations are complete.-
    pc-initial-focusEmitted when the drawer opens and is ready to receive focus. Calling event.preventDefault() will prevent focusing and allow you to set it on a different element, such as an input.-
    pc-request-closeEmitted when the user attempts to close the drawer by clicking the close button, clicking the overlay or pressing Esc. Calling event.preventDefault() will keep the drawer open. Avoid using this unless closing the drawer will result in destructive behavior such as data loss.{ source: "close-button" | "keyboard" | "overlay" }

    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.-
    --header-spacingThe amount of padding to use for the header.-
    --body-spacingThe amount of padding to use for the body.-
    --footer-spacingThe amount of padding to use for the footer.-

    Learn more about customising custom properties.

    Parts

    NameDescription
    baseThe component’s base wrapper.
    overlayThe overlay that covers the screen behind the drawer.
    panelThe drawer’s panel (where the drawer and its content are rendered).
    headerThe drawer’s header. This element wraps the title and header actions.
    header-actionsOptional actions to add to the header. Works best with <pc-icon-button>.
    titleThe drawer’s title.
    close-buttonThe close button, an <pc-icon-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.
    drawer.overlay.showThe animation to use when showing the drawer’s overlay.
    drawer.overlay.hideThe animation to use when hiding the drawer’s overlay.

    Learn more about customising animations.

    Importing

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

    ScriptImportBundler

    To import this component from the CDN with a script tag, copy this snippet and paste it in your HTML.

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

    To import this component from the CDN using a JavaScript import, copy this snippet and paste it in your JavaScript:

    import "https://cdn.jsdelivr.net/npm/placer-toolkit@0.5.1/dist/components/drawer/drawer.js";

    To import this component with a bundler using a JavaScript import, copy this snippet and paste it in your JavaScript:

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

    Dependencies

    This component automatically imports these components: