Live Code
Storybook
Usage
Drawer
The Drawer component is a container that slides in from side of the screen. It can be used to display additional content or actions that are not part of the main view.
The Drawer is a composition of several subcomponents:
Accessibility Guidelines
👉 The animation effect of this component is dependent on the prefers-reduced-motion media query.
Drawer
const [isOpen, setOpen] = useState(false);
<Drawer id="drawer-dialog-example" isOpen={isOpen} onClose={() => setOpen(false)}>
{/* Drawer Panel goes here */}
</Drawer>;
Alignment
The Drawer component allows aligning the content panel horizontally to the left or right side of the screen using alignmentX prop. By default, the drawer content panel is aligned to the right.
<Drawer id="drawer-dialog-example" isOpen={isOpen} onClose={() => setOpen(false)} alignmentX="left">
{/* Drawer Panel goes here */}
</Drawer>
Close on Backdrop Click
By default, the drawer will close when the backdrop is clicked. You can disable this behavior by setting the closeOnBackdropClick prop to false.
<Drawer id="drawer-dialog-example" isOpen={isOpen} onClose={() => setOpen(false)} closeOnBackdropClick={false}>
{/* Drawer content goes here */}
</Drawer>
Close on Escape Key Down
By default, the drawer will close when the escape key is pressed. You can disable this behavior by setting the closeOnEscapeKeyDown prop to false.
<Drawer id="drawer-dialog-example" isOpen={isOpen} onClose={() => setOpen(false)} closeOnEscapeKeyDown={false}>
{/* Drawer content goes here */}
</Drawer>
API
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| alignmentX | left | right | right | ✕ | Drawer horizontal alignment |
| children | ReactNode | — | ✕ | Children node |
| closeOnBackdropClick | bool | true | ✕ | Whether the drawer will close when backdrop is clicked |
| closeOnEscapeKeyDown | bool | true | ✕ | Whether the drawer will close when escape key is pressed |
| id | string | — | ✓ | ID to be linked |
| isOpen | bool | false | ✓ | Open state |
| onClose | (event: ClickEvent or KeyboardEvent) => void | — | ✓ | Callback for drawer when closed |
The component further inherits properties from the <dialog> element.
On top of the API options, the components accept additional attributes. If you need more control over the styling of a component, you can use style props and escape hatches.
DrawerCloseButton
The DrawerCloseButton component is a button that closes the drawer when clicked.
<DrawerCloseButton />
Use color prop to change the color variant of the button. You can also change the size of the button using the size prop.
<DrawerCloseButton color="secondary" size="large" />
Size of the icon can be changed via iconBoxSize prop. This prop accepts a number or an object with responsive values.
<DrawerCloseButton iconBoxSize={20} />
<DrawerCloseButton iconBoxSize={{ mobile: 20, tablet: 30, desktop: 40 }} />
API
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| color | [Component Button Color dictionary | Emotion Color dictionary] | tertiary | ✕ | Color variant |
| iconBoxSize | [number | Responsive<number> ] | 24 | ✕ | Size of the icon, use object to set responsive values, e.g. { mobile: 20, tablet: 30, desktop: 40 } |
| label | string | Close | ✕ | Label of the drawer close button |
| size | Size dictionary | medium | ✕ | Size of the drawer close button |
The component further inherits properties from the <button> element.
On top of the API options, the components accept additional attributes. If you need more control over the styling of a component, you can use style props and escape hatches.
DrawerPanel
The DrawerPanel component is a container for the content that will be displayed in the drawer.
<DrawerPanel>{/* Drawer content goes here */}</DrawerPanel>
API
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| children | ReactNode | — | ✕ | Children node |
| elementType | ElementType | div | ✕ | Type of element used as drawer panel |
Full Example
import React, { useState } from 'react';
import { Button, Drawer, DrawerPanel, DrawerCloseButton } from '@alma-oss/spirit-web-react';
export const Example = () => {
const [isOpen, setIsOpen] = useState(false);
const handleOpen = () => setIsOpen(true);
const handleClose = () => setIsOpen(false);
return (
<>
<Button onClick={handleOpen} aria-controls="drawer-example">
Open Drawer
</Button>
<Drawer id="drawer-example" isOpen={isOpen} onClose={handleClose}>
<DrawerPanel>
<DrawerCloseButton />
<div>Drawer content</div>
</DrawerPanel>
</Drawer>
</>
);
};