Drawer
Display overlay area at any side of the screen
Import
Source
Docs
Package
Usage
Position
Drawer can be placed on left
(default), top
, right
and bottom
. Control drawer position with position
prop,
for example <Drawer position="top" />
.
Offset
Set offset
prop to change drawer offset from the edge of the viewport:
Customize overlay
Drawer
uses Overlay component, you can set any props that Overlay
supports with overlayProps
:
Sizes
You can change drawer width/height (depends on position
) by setting size
prop to predefined size or any valid width,
for example, size="55%"
or size={200}
:
Remove header
To remove header set withCloseButton={false}
Drawer with scroll
Usage with ScrollArea
Change transition
Drawer
is built with Transition component. Use transitionProps
prop to customize any Transition properties:
onExitTransitionEnd and onEnterTransitionEnd
onExitTransitionEnd
and onEnterTransitionEnd
props can be used to run code after
exit/enter transition is finished. For example, this is useful when you want to clear
data after drawer is closed:
Initial focus
Drawer
uses FocusTrap to trap focus. Add data-autofocus
attribute to the element that should receive initial focus.
If you do not want to focus any elements when the drawer is opened, use FocusTrap.InitialFocus
component to create a visually hidden element that will receive initial focus:
If you do not add data-autofocus
attribute and do not use FocusTrap.InitialFocus
,
drawer will focus the first focusable element inside it which is usually the close button.
Control behavior
The following props can be used to control Drawer
behavior.
In most cases it is not recommended to turn these features off –
it will make the component less accessible.
trapFocus
– determines whether focus should be trapped inside drawercloseOnEscape
– determines whether the drawer should be closed whenEscape
key is pressedcloseOnClickOutside
– determines whether the drawer should be closed when user clicks on the overlayreturnFocus
– determines whether focus should be returned to the element that was focused before the drawer was opened
react-remove-scroll settings
Drawer
uses react-remove-scroll
package to lock scroll. You can pass props down to the RemoveScroll
component
with removeScrollProps
:
Change close icon
Use closeButtonProps
to customize close button:
Compound components
You can use the following compound components to have full control over the Drawer
rendering:
Drawer.Root
– context providerDrawer.Overlay
– render OverlayDrawer.Content
– main drawer element, should include all drawer contentDrawer.Header
– sticky header, usually containsDrawer.Title
andDrawer.CloseButton
Drawer.Title
–h2
element,aria-labelledby
ofDrawer.Content
is pointing to this element, usually is rendered insideDrawer.Header
Drawer.CloseButton
– close button, usually rendered insideDrawer.Header
Drawer.Body
– a place for main content,aria-describedby
ofDrawer.Content
is pointing to this element
Drawer.Stack
Use Drawer.Stack
component to render multiple drawers at the same time.
Drawer.Stack
keeps track of opened drawers, manages z-index values, focus trapping
and closeOnEscape
behavior. Drawer.Stack
is designed to be used with useDrawersStack
hook.
Differences from using multiple Drawer
components:
Drawer.Stack
manages z-index values – drawers that are opened later will always have higher z-index value disregarding their order in the DOMDrawer.Stack
disables focus trap andEscape
key handling for all drawers except the one that is currently opened- Drawers that are not currently opened are present in the DOM but are hidden with
opacity: 0
andpointer-events: none
- Only one overlay is rendered at a time
Note that Drawer.Stack
can only be used with Drawer
component. Components built with Drawer.Root
and other compound components are not compatible with Drawer.Stack
.
useDrawersStack hook
useDrawersStack
hook provides an easy way to control multiple drawers at the same time.
It accepts an array of unique drawers ids and returns an object with the following properties:
Example of using useDrawersStack
with Drawer
component:
Fixed elements offset
Drawer
component uses react-remove-scroll
package to lock scroll. To properly size these elements
add a className
to them (documentation):
Accessibility
Drawer
component follows WAI-ARIA recommendations on accessibility.
Set title
props to make component accessible, will add aria-labelledby
to the content element:
To set close button aria-label
use closeButtonProps
: