Skip to content

Drawer

A panel anchored to an edge of the screen, dragged between snap points and dismissed by swiping it towards that edge.

Installation

npm i @huin-core/react-drawer

API Reference

Drawer

PropTypeDescription
side"bottom" | "top" | "left" | "right"The edge the drawer is anchored to. This is also the direction you swipe towards in order to close it.
snapPoints"none" | "0.4,0.7,1" | "0.25,1"The points the component can rest at, as fractions of the container from least to most visible. Dragging settles on the nearest one instead of only opening or closing.
defaultOpenbooleanDetermines whether the component should be open by default when it is initially rendered.
openbooleanControls the open state of the component. When set to true, the component is displayed; when false, it is hidden.
modalbooleanThe modality of the dialog. When set to true, interaction with outside elements will be disabled and only dialog content will be visible to screen readers.
dismissiblebooleanWhen false, the component cannot be closed by swiping, pressing escape or interacting outside of it.
handleOnlybooleanWhen true, only the handle initiates a drag, so the rest of the content stays free for other pointer interactions.
closeThresholdnumberFraction (0–1) of the component's own extent a drag must exceed to close it.
velocityThresholdnumberSpeed in px/ms which, when exceeded on release, closes the component regardless of how far it was dragged.
scrollLockTimeoutnumberHow long, in milliseconds, a drag stays blocked after scrolling inside the component.
nestedDisplacementnumberHow far the component steps back, in pixels, for each one stacked on top of it — the strip of it that stays visible behind the one in front. Inherited by nested roots, so setting it on the outermost one covers the whole stack.
snapToSequentialPointbooleanA high velocity normally skips intermediate snap points. Set this when every point matters equally.
shouldScaleBackgroundbooleanScales the [data-huin-core-drawer-wrapper] element down while the drawer is open, the way an iOS sheet shrinks the page behind it.
setBackgroundColorOnScalebooleanWhen false, the body keeps its own background colour while the page behind the drawer is scaled down.
noBodyStylesbooleanWhen true, nothing is written to the body's styles.
fixedbooleanWhen true, an open keyboard only shortens the drawer instead of moving it up, so its content stays fully scrollable.
repositionInputsbooleanWhen true, inputs are repositioned above the keyboard rather than being scrolled into view by the browser.
preventScrollRestorationbooleanKeeps the page's scroll position from being restored after a navigation that happened while the drawer was open.
autoFocusbooleanWhen true, focus moves into the component on open. Left off, opening doesn't summon the keyboard on touch devices.
disableDefaultStylesbooleanSkips the stylesheet the package injects. You then own every animation, including the closed state exit animation, which must resolve to a different animation-name than the open state or the component will unmount without animating.
onOpenChangefunctionEvent handler called when the open state of the component changes.
onDragfunctionEvent handler called on every pointer move while dragging, with the fraction of the component's extent dragged so far.
onReleasefunctionEvent handler called when the drag ends. The second argument is whether the component will stay open.
onClosefunctionEvent handler called when the component starts closing, before the exit animation has finished.
onAnimationEndfunctionEvent handler called once the open or close animation has ended. It receives the open state as of that moment.

DrawerTrigger

PropTypeDescription
asChildbooleanWhen set to true, renders the component as its child element, inheriting the child's styles and behavior.

DrawerPortal

PropTypeDescription
forceMountbooleanUsed to force mounting when more control is needed. Useful when controlling animation with React animation libraries. It inherits from.

DrawerOverlay

PropTypeDescription
asChildbooleanWhen set to true, renders the component as its child element, inheriting the child's styles and behavior.
forceMountbooleanUsed to force mounting when more control is needed. Useful when controlling animation with React animation libraries. It inherits from.

DrawerContent

PropTypeDescription
asChildbooleanWhen set to true, renders the component as its child element, inheriting the child's styles and behavior.
forceMountbooleanUsed to force mounting when more control is needed. Useful when controlling animation with React animation libraries. It inherits from.
onOpenAutoFocusfunctionEvent handler called when focus moves into the component after opening. It can be prevented by calling event.preventDefault.
onCloseAutoFocusfunctionEvent handler called when focus moves to the trigger after closing. It can be prevented by calling event.preventDefault.
onEscapeKeyDownfunctionEvent handler called when the escape key is down. It can be prevented by calling event.preventDefault.
onPointerDownOutsidefunctionEvent handler called when a pointer event occurs outside the bounds of the component. It can be prevented by calling event.preventDefault
onInteractOutsidefunctionEvent handler called when an interaction (pointer or focus event) happens outside the bounds of the component. It can be prevented by calling event.preventDefault.

DrawerHandle

PropTypeDescription
preventCyclebooleanWhen true, tapping the handle does not move to the next snap point.
asChildbooleanWhen set to true, renders the component as its child element, inheriting the child's styles and behavior.

DrawerHeader

PropTypeDescription
asChildbooleanWhen set to true, renders the component as its child element, inheriting the child's styles and behavior.

DrawerTitle

PropTypeDescription
asChildbooleanWhen set to true, renders the component as its child element, inheriting the child's styles and behavior.

DrawerDescription

PropTypeDescription
asChildbooleanWhen set to true, renders the component as its child element, inheriting the child's styles and behavior.

DrawerFooter

PropTypeDescription
asChildbooleanWhen set to true, renders the component as its child element, inheriting the child's styles and behavior.

DrawerClose

PropTypeDescription
asChildbooleanWhen set to true, renders the component as its child element, inheriting the child's styles and behavior.

DrawerNestedRoot

PropTypeDescription
dismissiblebooleanWhen false, the component cannot be closed by swiping, pressing escape or interacting outside of it.
handleOnlybooleanWhen true, only the handle initiates a drag, so the rest of the content stays free for other pointer interactions.
nestedDisplacementnumberHow far the component steps back, in pixels, for each one stacked on top of it — the strip of it that stays visible behind the one in front. Inherited by nested roots, so setting it on the outermost one covers the whole stack.
onOpenChangefunctionEvent handler called when the open state of the component changes.

Keyboard Interactions

KeyDescription
Space
Enter
Opens the drawer from its trigger.
Tab
Moves focus to the next element, trapped inside the drawer.
Esc
Closes the drawer and returns focus to the trigger.