Skip to main content
Default Gray Amethyst

Drawer

<mo-drawer> | MODrawer
Since 1.0 stable

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

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Close Open Drawer
<mo-drawer noHeader label="Drawer" class="drawer-overview">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  <mo-button slot="footer" variant="primary">Close</mo-button>
</mo-drawer>

<mo-button>Open Drawer</mo-button>

<script>
  const drawer = document.querySelector('.drawer-overview');
  const openButton = drawer.nextElementSibling;
  const closeButton = drawer.querySelector('mo-button[variant="primary"]');

  openButton.addEventListener('click', () => drawer.show());
  closeButton.addEventListener('click', () => drawer.hide());
</script>
import { useState } from 'react';
import { MOButton, MODrawer } from '@metsooutotec/modes-web-components/dist/react';

const App = () => {
  const [open, setOpen] = useState(false);

  return (
    <>
      <MODrawer label="Drawer" open={open} onMoAfterHide={() => setOpen(false)}>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        <MOButton slot="footer" variant="primary" onClick={() => setOpen(false)}>
          Close
        </MOButton>
      </MODrawer>

      <MOButton onClick={() => setOpen(true)}>Open Drawer</MOButton>
    </>
  );
};

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.

This drawer slides in from the start. Close Open Drawer
<mo-drawer noHeader label="Drawer" placement="start" class="drawer-placement-start">
  This drawer slides in from the start.
  <mo-button slot="footer" variant="primary">Close</mo-button>
</mo-drawer>

<mo-button>Open Drawer</mo-button>

<script>
  const drawer = document.querySelector('.drawer-placement-start');
  const openButton = drawer.nextElementSibling;
  const closeButton = drawer.querySelector('mo-button[variant="primary"]');

  openButton.addEventListener('click', () => drawer.show());
  closeButton.addEventListener('click', () => drawer.hide());
</script>
import { useState } from 'react';
import { MOButton, MODrawer } from '@metsooutotec/modes-web-components/dist/react';

const App = () => {
  const [open, setOpen] = useState(false);

  return (
    <>
      <MODrawer label="Drawer" placement="start" open={open} onMoAfterHide={() => setOpen(false)}>
        This drawer slides in from the start.
        <MOButton slot="footer" variant="primary" onClick={() => setOpen(false)}>
          Close
        </MOButton>
      </MODrawer>

      <MOButton onClick={() => setOpen(true)}>Open Drawer</MOButton>
    </>
  );
};

Slide in from top

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

This drawer slides in from the top. Close Open Drawer
<mo-drawer label="Drawer" placement="top" class="drawer-placement-top">
  This drawer slides in from the top.
  <mo-button slot="footer" variant="primary">Close</mo-button>
</mo-drawer>

<mo-button>Open Drawer</mo-button>

<script>
  const drawer = document.querySelector('.drawer-placement-top');
  const openButton = drawer.nextElementSibling;
  const closeButton = drawer.querySelector('mo-button[variant="primary"]');

  openButton.addEventListener('click', () => drawer.show());
  closeButton.addEventListener('click', () => drawer.hide());
</script>
import { useState } from 'react';
import { MOButton, MODrawer } from '@metsooutotec/modes-web-components/dist/react';

const App = () => {
  const [open, setOpen] = useState(false);

  return (
    <>
      <MODrawer label="Drawer" placement="top" open={open} onMoAfterHide={() => setOpen(false)}>
        This drawer slides in from the top.
        <MOButton slot="footer" variant="primary" onClick={() => setOpen(false)}>
          Close
        </MOButton>
      </MODrawer>

      <MOButton onClick={() => setOpen(true)}>Open Drawer</MOButton>
    </>
  );
};

Slide in from bottom

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

This drawer slides in from the bottom. Close Open Drawer
<mo-drawer label="Drawer" placement="bottom" class="drawer-placement-bottom">
  This drawer slides in from the bottom.
  <mo-button slot="footer" variant="primary">Close</mo-button>
</mo-drawer>

<mo-button>Open Drawer</mo-button>

<script>
  const drawer = document.querySelector('.drawer-placement-bottom');
  const openButton = drawer.nextElementSibling;
  const closeButton = drawer.querySelector('mo-button[variant="primary"]');

  openButton.addEventListener('click', () => drawer.show());
  closeButton.addEventListener('click', () => drawer.hide());
</script>
import { useState } from 'react';
import { MOButton, MODrawer } from '@metsooutotec/modes-web-components/dist/react';

const App = () => {
  const [open, setOpen] = useState(false);

  return (
    <>
      <MODrawer label="Drawer" placement="bottom" open={open} onMoAfterHide={() => setOpen(false)}>
        This drawer slides in from the bottom.
        <MOButton slot="footer" variant="primary" onClick={() => setOpen(false)}>
          Close
        </MOButton>
      </MODrawer>

      <MOButton onClick={() => setOpen(true)}>Open Drawer</MOButton>
    </>
  );
};

Contained to an element

By default, the drawer slides out of its 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.

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
<div
  style="position: relative; border: solid 2px var(--mo-panel-border-color); height: 300px; padding: 1rem; margin-bottom: 1rem;"
>
  The drawer will be contained to this box. This content won't shift or be affected in any way when the drawer opens.

  <mo-drawer label="Drawer" contained class="drawer-contained" style="--size: 50%;">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    <mo-button slot="footer" variant="primary">Close</mo-button>
  </mo-drawer>
</div>

<mo-button>Open Drawer</mo-button>

<script>
  const drawer = document.querySelector('.drawer-contained');
  const openButton = drawer.parentElement.nextElementSibling;
  const closeButton = drawer.querySelector('mo-button[variant="primary"]');

  openButton.addEventListener('click', () => drawer.show());
  closeButton.addEventListener('click', () => drawer.hide());
</script>
import { useState } from 'react';
import { MOButton, MODrawer } from '@metsooutotec/modes-web-components/dist/react';

const App = () => {
  const [open, setOpen] = useState(false);

  return (
    <>
      <div
        style={{
          position: 'relative',
          border: 'solid 2px var(--mo-panel-border-color)',
          height: '300px',
          padding: '1rem',
          marginBottom: '1rem'
        }}
      >
        The drawer will be contained to this box. This content won't shift or be affected in any way when the drawer
        opens.
        <MODrawer label="Drawer" contained open={open} onMoAfterHide={() => setOpen(false)} style={{ '--size': '50%' }}>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
          <MOButton slot="footer" variant="primary" onClick={() => setOpen(false)}>
            Close
          </MOButton>
        </MODrawer>
      </div>

      <MOButton onClick={() => setOpen(true)}>Open Drawer</MOButton>
    </>
  );
};

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.

This drawer is always 50% of the viewport. Close Open Drawer
<mo-drawer label="Drawer" class="drawer-custom-size" style="--size: 50vw;">
  This drawer is always 50% of the viewport.
  <mo-button slot="footer" variant="primary">Close</mo-button>
</mo-drawer>

<mo-button>Open Drawer</mo-button>

<script>
  const drawer = document.querySelector('.drawer-custom-size');
  const openButton = drawer.nextElementSibling;
  const closeButton = drawer.querySelector('mo-button[variant="primary"]');

  openButton.addEventListener('click', () => drawer.show());
  closeButton.addEventListener('click', () => drawer.hide());
</script>
import { useState } from 'react';
import { MOButton, MODrawer } from '@metsooutotec/modes-web-components/dist/react';

const App = () => {
  const [open, setOpen] = useState(false);

  return (
    <>
      <MODrawer label="Drawer" open={open} onMoAfterHide={() => setOpen(false)} style={{ '--size': '50vw' }}>
        This drawer is always 50% of the viewport.
        <MOButton slot="footer" variant="primary" onClick={() => setOpen(false)}>
          Close
        </MOButton>
      </MODrawer>

      <MOButton onClick={() => setOpen(true)}>Open Drawer</MOButton>
    </>
  );
};

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
<mo-drawer label="Drawer" class="drawer-scrolling">
  <div style="height: 150vh; border: dashed 2px var(--color-neutral-200); padding: 0 1rem;">
    <p>Scroll down and give it a try! 👇</p>
  </div>
  <mo-button slot="footer" variant="primary">Close</mo-button>
</mo-drawer>

<mo-button>Open Drawer</mo-button>

<script>
  const drawer = document.querySelector('.drawer-scrolling');
  const openButton = drawer.nextElementSibling;
  const closeButton = drawer.querySelector('mo-button[variant="primary"]');

  openButton.addEventListener('click', () => drawer.show());
  closeButton.addEventListener('click', () => drawer.hide());
</script>
import { useState } from 'react';
import { MOButton, MODrawer } from '@metsooutotec/modes-web-components/dist/react';

const App = () => {
  const [open, setOpen] = useState(false);

  return (
    <>
      <MODrawer label="Drawer" open={open} onMoAfterHide={() => setOpen(false)}>
        <div
          style={{
            height: '150vh',
            border: 'dashed 2px var(--color-neutral-200)',
            padding: '0 1rem'
          }}
        >
          <p>Scroll down and give it a try! 👇</p>
        </div>
        <MOButton slot="footer" variant="primary" onClick={() => setOpen(false)}>
          Close
        </MOButton>
      </MODrawer>

      <MOButton onClick={() => setOpen(true)}>Open Drawer</MOButton>
    </>
  );
};

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

This drawer will not close when you click on the overlay. Close Open Drawer
<mo-drawer label="Drawer" class="drawer-deny-close">
  This drawer will not close when you click on the overlay.
  <mo-button slot="footer" variant="primary">Close</mo-button>
</mo-drawer>

<mo-button>Open Drawer</mo-button>

<script>
  const drawer = document.querySelector('.drawer-deny-close');
  const openButton = drawer.nextElementSibling;
  const closeButton = drawer.querySelector('mo-button[variant="primary"]');

  openButton.addEventListener('click', () => drawer.show());
  closeButton.addEventListener('click', () => drawer.hide());

  // Prevent the drawer from closing when the user clicks on the overlay
  drawer.addEventListener('mo-request-close', event => {
    if (event.detail.source === 'overlay') {
      event.preventDefault();
    }
  });
</script>
import { useState } from 'react';
import { MOButton, MODrawer } from '@metsooutotec/modes-web-components/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 (
    <>
      <MODrawer label="Drawer" open={open} onMoRequestClose={handleRequestClose} onMoAfterHide={() => setOpen(false)}>
        This drawer will not close when you click on the overlay.
        <MOButton slot="footer" variant="primary" onClick={() => setOpen(false)}>
          Save &amp; Close
        </MOButton>
      </MODrawer>

      <MOButton onClick={() => setOpen(true)}>Open Drawer</MOButton>
    </>
  );
};

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.

Close Open Drawer
<mo-drawer label="Drawer" class="drawer-focus">
  <mo-input autofocus placeholder="I will have focus when the drawer is opened"></mo-input>
  <mo-button slot="footer" variant="primary">Close</mo-button>
</mo-drawer>

<mo-button>Open Drawer</mo-button>

<script>
  const drawer = document.querySelector('.drawer-focus');
  const input = drawer.querySelector('mo-input');
  const openButton = drawer.nextElementSibling;
  const closeButton = drawer.querySelector('mo-button[variant="primary"]');

  openButton.addEventListener('click', () => drawer.show());
  closeButton.addEventListener('click', () => drawer.hide());
</script>
import { useState } from 'react';
import { MOButton, MODrawer, MOInput } from '@metsooutotec/modes-web-components/dist/react';

const App = () => {
  const [open, setOpen] = useState(false);

  return (
    <>
      <MODrawer label="Drawer" open={open} onMoAfterHide={() => setOpen(false)}>
        <MOInput autofocus placeholder="I will have focus when the drawer is opened" />
        <MOButton slot="footer" variant="primary" onClick={() => setOpen(false)}>
          Close
        </MOButton>
      </MODrawer>

      <MOButton onClick={() => setOpen(true)}>Open Drawer</MOButton>
    </>
  );
};

Importing

If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.

Bundler React Script

To import this component using a bundler:

import '@metsooutotec/modes-web-components/dist/components/drawer/drawer.js';

To import this component as a React component:

import MODrawer from '@metsooutotec/modes-web-components/dist/react/drawer/';

To import this component using a script tag:

<script type="module" src="https://modes-web.metso.com/dist/components/cdn/components/drawer/drawer.js"></script>

Slots

Name Description
(default) The drawer’s content.
label The drawer’s label. Alternatively, you can use the label prop.
footer The drawer’s footer, usually one or more buttons representing various options.

Learn more about using slots.

Properties

Name Description Reflects Type Default
open Indicates whether or not the drawer is open. You can use this in lieu of the show/hide methods. boolean false
label The 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. string ''
placement The direction from which the drawer will open. 'top' | 'end' | 'bottom' | 'start' 'end'
contained By default, the drawer slides out of its containing block (usually the viewport). To make the drawer slide out of its parent element, set this prop and add position: relative to the parent. boolean false
spanning Will make the drawer 100% in width. boolean false
noHeader 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. boolean false
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Events

Name React Event Description Event Detail
mo-show onMoShow Emitted when the drawer opens. -
mo-after-show onMoAfterShow Emitted after the drawer opens and all animations are complete. -
mo-hide onMoHide Emitted when the drawer closes. -
mo-after-hide onMoAfterHide Emitted after the drawer closes and all animations are complete. -
mo-initial-focus onMoInitialFocus Emitted 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. -
mo-request-close onMoRequestClose Emitted when the user attempts to close the drawer by clicking the close button, clicking the overlay, or pressing escape. 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.

Methods

Name Description Arguments
show() Shows the drawer. -
hide() Hides the drawer -

Learn more about methods.

Custom Properties

Name Description Default
--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.
--header-spacing The amount of padding to use for the header.
--body-spacing The amount of padding to use for the body.
--footer-spacing The amount of padding to use for the footer.

Learn more about customizing CSS custom properties.

Parts

Name Description
base The component’s internal wrapper.
overlay The overlay.
panel The drawer panel (where the drawer and its content is rendered).
header The drawer header.
title The drawer title.
close-button The close button.
close-button__base The close button’s base part.
body The drawer body.
footer The drawer footer.

Learn more about customizing CSS parts.

Animations

Name Description
drawer.showTop The animation to use when showing a drawer with top placement.
drawer.showEnd The animation to use when showing a drawer with end placement.
drawer.showBottom The animation to use when showing a drawer with bottom placement.
drawer.showStart The animation to use when showing a drawer with start placement.
drawer.hideTop The animation to use when hiding a drawer with top placement.
drawer.hideEnd The animation to use when hiding a drawer with end placement.
drawer.hideBottom The animation to use when hiding a drawer with bottom placement.
drawer.hideStart The animation to use when hiding a drawer with start placement.
drawer.denyClose The animation to use when a request to close the drawer is denied.
drawer.overlay.show The animation to use when showing the drawer’s overlay.
drawer.overlay.hide The animation to use when hiding the drawer’s overlay.

Learn more about customizing animations.

Dependencies

This component automatically imports the following dependencies.