Skip to content

Import#

import { useState } from 'react'
import { Drawer } from '@dnb/eufemia'

Description#

The Drawer component is a Modal variation that appears as a side panel at any chosen side of the page: top, bottom, left, or right (default right). A Drawer is typically used to show additional information. It can also be used for easy/quick tasks while staying in context.

Relevant links#

Parts in Drawer#

To provide custom content to parts of the Drawer, a set of component parts are provided:

  • <Drawer.Navigation>: The navigation field at the top of the component, default with a close button (Equal to the property navContent).
  • <Drawer.Header>: The header field of the component, where the title will appear (Equal to the property headerContent).
  • <Drawer.Body>: The body of the Drawer, provided with a section background color, default black-3 (Equal to the property modalContent).

More detailed information#

For more details regarding the component functionality, check out the Modal documentation.

Reveal the page#

Import the reveal driver separately to move the page to the right while keeping a left Drawer stationary. The driver also supports dragging right on the page to open the menu and dragging left inside the menu or on the exposed page to close it. Menu links and buttons can still be tapped. Form controls, horizontal scroll areas, and vertical scrolling keep their native interactions.

import { Drawer } from '@dnb/eufemia'
import { createDrawerReveal } from '@dnb/eufemia/components/drawer/DrawerReveal'
const reveal = createDrawerReveal({ target: '#app-page' })
function App() {
const [open, setOpen] = useState(false)
return (
<>
<main id="app-page">Page content</main>
<Drawer
open={open}
onOpen={() => setOpen(true)}
onClose={() => setOpen(false)}
renderDriver={reveal}
containerPlacement="left"
maxWidth="min(85%, 20rem)"
title="Menu"
triggerProps={{ text: 'Open menu' }}
>
Menu content
</Drawer>
</>
)
}

Create the driver once outside rendering, or memoize it when its options change. The target accepts a CSS selector for one or more page surfaces. Keep the Drawer and its portal outside these surfaces. The driver owns their transform and opacity while the menu is open and restores them on close.

When controlling open, synchronize both onOpen and onClose so a swipe updates your state too. This also lets the app close the menu with setOpen(false) after navigation.

The same renderDriver property works with SidebarMenu.ResponsiveDrawer, which enables it only on narrow screens. Keep a menu button available as an alternative to dragging. Modal focus handling, Escape, backdrop dismissal, and reduced motion remain available.

Ordinary Drawer and SidebarMenu imports do not import the reveal implementation or install its gesture listeners. The reveal JavaScript is included only through the separate import. Reveal styles are included in the Drawer stylesheet.

The mounted reveal driver registers a non-passive document touchmove listener to cancel native scrolling during horizontal dragging. Keep work on the main thread short so touch scrolling stays responsive, especially on heavy pages.

Root Element (React Portal)#

The Drawer component uses PortalRoot internally to render its content. See the PortalRoot documentation for information on how to control where the portal content appears in the DOM, and for the BrowserTranslate helper when browser translation tools such as Google Translate should not modify content rendered through PortalRoot.

Related components#

Drawer is part of the Feedback and dialogs category. More components in this category:

  • Badge — to highlight new, unread, or important information.
  • Dialog — when people need to make a choice or read an important message before continuing.
  • Form status — to show validation errors, warnings, or messages near a form.
  • Global error — to show a clear 404 or 500 error page.
  • Global status — to show important page-level messages or a summary of form errors.
  • Modal — when people must focus on one task or message before returning to the page.

See all in Feedback and dialogs →

Demos#

Basic Drawer#

<Drawer
  title="Drawer title"
  triggerProps={{
    text: 'Open drawer',
  }}
>
  <P top>Some informational content</P>
  <P top>
    Elementum eu suspendisse sit platea elit porttitor magna laoreet ad
    ultrices tempus urna curae parturient conubia quisque viverra eget
    vestibulum neque pulvinar semper vulputate id dis varius pellentesque
    nunc egestas risus amet mus aptent luctus imperdiet netus natoque
    cubilia mattis nostra proin ornare scelerisque sodales faucibus
    placerat sem bibendum pretium rutrum vitae sociis ligula inceptos morbi
    quam mi sed pharetra fermentum tortor ullamcorper ipsum tellus eros
    euismod volutpat nisl dui lectus fames suscipit phasellus praesent
    justo mollis montes velit taciti gravida
  </P>
</Drawer>

Basic Drawer left placement#

<Drawer title="Drawer title" containerPlacement="left">
  <P top>Some informational content</P>
  <P top>
    Elementum eu suspendisse sit platea elit porttitor magna laoreet ad
    ultrices tempus urna curae parturient conubia quisque viverra eget
    vestibulum neque pulvinar semper vulputate id dis varius pellentesque
    nunc egestas risus amet
  </P>
</Drawer>

Reveal a stationary menu#

The separately imported reveal driver moves the page to the right and reduces its opacity. The menu stays underneath. On touch screens, drag right on the page to open it and left inside the menu or on the exposed page to close it. The page cannot be interacted with while the menu is open.

Use the menu button as an alternative to dragging. Try Tab, Shift+Tab, and Escape to check keyboard navigation and focus return. The animation respects reduced motion.

My application

Open the menu below, or drag this page to the right on a touch screen.

See setup and bundle behavior.

Basic Drawer in fullscreen#

<Drawer
  fullscreen
  title="Drawer title"
  triggerProps={{
    text: 'Open drawer',
  }}
>
  <P top>Some informational content</P>
  <P top>
    Elementum eu suspendisse sit platea elit porttitor magna laoreet ad
    ultrices tempus urna curae parturient conubia quisque viverra eget
    vestibulum neque pulvinar semper vulputate id dis varius pellentesque
    nunc egestas risus amet
  </P>
</Drawer>

Drawer with custom content#

<Drawer title="Custom title">
  <Drawer.Navigation>
    <Breadcrumb onClick={handleBack} />
  </Drawer.Navigation>
  <Drawer.Header>
    <P bottom>This is a lorem ipsum dolor</P>
    <Button bottom size="large">
      Lorem ipsum
    </Button>
    <Button bottom size="large" variant="secondary">
      Dolor sit
    </Button>
    <FormStatus state="information">
      This is a lorem ipsum dolor
    </FormStatus>
    <Tabs
      id="unique-linked-id"
      data={[
        {
          title: 'One',
          key: 'one',
        },
        {
          title: 'Two',
          key: 'two',
        },
      ]}
    />
  </Drawer.Header>
  <Drawer.Body>
    <Tabs.Content id="unique-linked-id" key="contentKey">
      {({ title }) => {
        return (
          <>
            <H2>{title}</H2>
            <P top>This is a left aligned Drawer content.</P>
            <P top>
              Elementum eu suspendisse sit platea elit porttitor magna
              laoreet ad ultrices tempus urna curae parturient conubia
              quisque viverra eget vestibulum neque pulvinar semper
              vulputate id dis varius pellentesque nunc egestas risus amet
              mus aptent luctus imperdiet netus natoque cubilia mattis
              nostra proin ornare scelerisque sodales faucibus placerat sem
              bibendum pretium rutrum vitae sociis ligula inceptos morbi
              quam mi sed pharetra fermentum tortor ullamcorper ipsum
              tellus eros euismod volutpat nisl dui lectus fames suscipit
              phasellus praesent justo mollis montes velit taciti gravida
              lacus commodo senectus feugiat lorem etiam consequat
              penatibus cum hendrerit accumsan orci potenti purus nulla
            </P>
          </>
        )
      }}
    </Tabs.Content>
  </Drawer.Body>
</Drawer>

Customize trigger button#

<Drawer
  title="Drawer with custom trigger"
  triggerProps={{
    text: 'Custom trigger',
    variant: 'primary',
    size: 'large',
    icon: 'loupe',
    iconPosition: 'left',
  }}
>
  <Drawer.Body
    innerSpace={{
      block: 'large',
    }}
  >
    <P>Opened a Drawer with a custom trigger button!</P>
  </Drawer.Body>
</Drawer>

Close Drawer by callback method#

<Drawer
  title="Drawer title"
  triggerProps={{
    text: 'Open drawer',
  }}
  hideCloseButton
>
  {/* @ts-expect-error -- strictFunctionTypes */}
  {({ close }) => (
    <>
      <Button text="Close by callback" onClick={close} />
    </>
  )}
</Drawer>

Remove animation and spacing#

<Drawer
  title="No spacing or animation"
  noAnimation
  spacing={false}
  hideCloseButton
>
  <Drawer.Body>
    <P top bottom>
      This is a lorem ipsum dolor
    </P>
    <Button bottom size="large">
      Lorem ipsum
    </Button>
    <Button bottom size="large" variant="secondary">
      Dolor sit
    </Button>
    <FormStatus state="information">
      This is a lorem ipsum dolor
    </FormStatus>
  </Drawer.Body>
</Drawer>
Suggest an edit
Eufemia · /web/components/drawer/ · Version 11.18.1