Components
Drawer
A panel that slides in from the edge of the screen.
Usage guidelines#
- Drawer extends Dialog: It adds gesture support, snap points, and indent effects. If you don't need these, use Dialog instead. A panel that slides in from the edge of the screen and doesn't need gesture support is a positioned Dialog.
Anatomy#
Import the component and assemble its parts:
import { Drawer } from '@egoist/solid-base/drawer';
<Drawer.Provider>
<Drawer.IndentBackground />
<Drawer.Indent>
<Drawer.Root>
<Drawer.Trigger />
<Drawer.SwipeArea />
<Drawer.Portal>
<Drawer.Backdrop />
<Drawer.Viewport>
<Drawer.Popup>
<Drawer.Content>
<Drawer.Title />
<Drawer.Description />
<Drawer.Close />
</Drawer.Content>
</Drawer.Popup>
</Drawer.Viewport>
</Drawer.Portal>
</Drawer.Root>
</Drawer.Indent>
</Drawer.Provider>;Drawer supports swipe gestures to dismiss. Set swipeDirection to control which direction
dismisses the drawer. <Drawer.Content> allows text selection of its children without swipe
interference when using a mouse pointer.
Add data-base-ui-swipe-ignore to a descendant to opt it out of swipe dismissal for all input
types. If the element only handles touch drags along one axis, such as a JavaScript carousel, set
the value to x or y so touch drags along the other axis still swipe the drawer.
Use <Drawer.VirtualKeyboardProvider> when a bottom sheet contains form fields and you want the
drawer to manage keyboard-aware focus and scroll handling for software keyboards. Drawers without
this provider are unaffected.
Examples#
State#
By default, Drawer is an uncontrolled component that manages its own state.
<Drawer.Root>
<Drawer.Trigger>Open</Drawer.Trigger>
<Drawer.Portal>
<Drawer.Viewport>
<Drawer.Popup>
<Drawer.Content>
<Drawer.Title>Example drawer</Drawer.Title>
<Drawer.Close>Close</Drawer.Close>
</Drawer.Content>
</Drawer.Popup>
</Drawer.Viewport>
</Drawer.Portal>
</Drawer.Root>Use open and onOpenChange props if you need to access or control the state of the drawer.
const [open, setOpen] = createSignal(false);
<Drawer.Root open={open()} onOpenChange={(next) => setOpen(next)}>
<Drawer.Trigger>Open</Drawer.Trigger>
<Drawer.Portal>
<Drawer.Viewport>
<Drawer.Popup>
<Drawer.Content>
<Drawer.Title>Example drawer</Drawer.Title>
<Drawer.Close>Close</Drawer.Close>
</Drawer.Content>
</Drawer.Popup>
</Drawer.Viewport>
</Drawer.Portal>
</Drawer.Root>;Position#
Positioning is handled by your styles. swipeDirection defaults to "down" for bottom sheets.
Use "up", "left", or "right" for other drawer positions.
<Drawer.Root swipeDirection="right">Nested drawers#
Use the [data-nested-drawer-open] selector and the --nested-drawers CSS variable to style
drawers when a nested drawer is open.
This demo stacks nested drawers using a constant peek so the frontmost drawer stays anchored to the
bottom while the ones behind it are scaled down and lifted. It also uses the --drawer-height and
--drawer-frontmost-height CSS variables to handle varying drawer heights.
Snap points#
Use snapPoints to snap a bottom sheet drawer to preset heights. Numbers between 0 and 1
represent fractions of the viewport height, and numbers greater than 1 are treated as pixel
values. String values support px and rem units (for example, '148px' or '30rem').
const snapPoints = ['148px', 1];
const [snapPoint, setSnapPoint] = createSignal<Drawer.Root.SnapPoint | null>(snapPoints[0]);
<Drawer.Root
snapPoints={snapPoints}
snapPoint={snapPoint()}
onSnapPointChange={(next) => setSnapPoint(next)}
>
{/* ... */}
</Drawer.Root>;Apply the snap point offset in your styles when using vertical drawers:
.DrawerPopup {
transform: translateY(calc(var(--drawer-snap-point-offset) + var(--drawer-swipe-movement-y)));
}By default, the drawer can skip snap points when swiping quickly. Specify the
snapToSequentialPoints prop to disable velocity-based skipping so the snap target is determined
by drag distance (you can still drag past multiple points).
Virtual keyboard aware#
Wrap a bottom sheet in <Drawer.VirtualKeyboardProvider> to make it react to software keyboards
when it contains form controls. When the keyboard opens, the provider scrolls the body to keep the
focused field visible.
- Keep the popup frame stable: place header and footer content outside a plain scrollable body.
- Lift a pinned footer input: if a footer contains its own input, reserve a footer slot below
the scroll area and offset it by
var(--drawer-keyboard-inset, 0px). The demo switches the focused footer toposition: fixed— positioned against the popup, since itstransformcontains fixed descendants — and adds the inset to its bottom padding. - Always include the
0pxfallback: the provider only sets--drawer-keyboard-insetwhile the keyboard is aligned, so a barevar(--drawer-keyboard-inset)is invalid before the first alignment and after cleanup.
<Drawer.Root>
<Drawer.VirtualKeyboardProvider>{/* ... */}</Drawer.VirtualKeyboardProvider>
</Drawer.Root>Indent effect#
Scale the background down when any drawer opens by wrapping your app in <Drawer.Provider> and
use <Drawer.IndentBackground> + <Drawer.Indent> at the top of your tree. Any <Drawer.Root>
within the provider notifies it when it mounts, which activates the indent parts (they receive
[data-active] state attributes).
The demo renders the drawer inside its own frame by passing the frame element to the portal's
container prop. In Solid, store the element in a signal and pass the accessor itself:
const [container, setContainer] = createSignal<HTMLDivElement>();
<div ref={setContainer}>
<Drawer.Portal container={container}>{/* ... */}</Drawer.Portal>
</div>;Non-modal#
Set modal={false} to opt out of focus trapping and disablePointerDismissal to keep the drawer
open on outside clicks.
Swipe to open#
Place <Drawer.SwipeArea> along the edge of the viewport to enable swipe-to-open gestures.
Swipe from the right edge to open the drawer.
Detached triggers#
A drawer can be controlled by a trigger located either inside or outside the <Drawer.Root>
component. For simple, one-off interactions, place the <Drawer.Trigger> inside <Drawer.Root>.
However, if defining the drawer's content next to its trigger is not practical, you can use a
detached trigger. This involves placing the <Drawer.Trigger> outside of <Drawer.Root> and
linking them with a handle created by the Drawer.createHandle() function.
The imperative methods on the handle, such as open() and openWithPayload(), require a
<Drawer.Root> using the same handle to be mounted. Calls made while no root is attached to the
handle — before one mounts, or after it unmounts — are ignored. Each time a root mounts, it starts
from fresh state: a call made while no root was attached is not replayed, and no open state carries
over from a previous mount.
const demoDrawer = Drawer.createHandle();
<Drawer.Trigger handle={demoDrawer}>Open</Drawer.Trigger>
<Drawer.Root handle={demoDrawer}>
...
</Drawer.Root>The drawer can render different content depending on which trigger opened it. This is achieved
by passing a payload to the <Drawer.Trigger> and using the function-as-a-child pattern in
<Drawer.Root>.
const demoDrawer = Drawer.createHandle<{ title: string }>();
<Drawer.Trigger handle={demoDrawer} payload={{ title: 'Profile' }}>
Profile
</Drawer.Trigger>
<Drawer.Trigger handle={demoDrawer} payload={{ title: 'Settings' }}>
Settings
</Drawer.Trigger>
<Drawer.Root handle={demoDrawer}>
{(state) => (
<Drawer.Portal>
<Drawer.Popup>
<Drawer.Content>
<Drawer.Title>{state.payload?.title}</Drawer.Title>
</Drawer.Content>
</Drawer.Popup>
</Drawer.Portal>
)}
</Drawer.Root>Stacking and animations#
Use CSS transitions or animations to animate drawer opening, closing, swipe interactions, and
nested stacking. The data-starting-style attribute is applied when a drawer starts to open, and
data-ending-style is applied when it starts to close.
The --nested-drawers CSS variable can be used to determine stack depth. The frontmost drawer has
index 0.
.DrawerPopup {
--stack-step: 0.05;
--stack-scale: calc(1 - (var(--nested-drawers) * var(--stack-step)));
transform: translateY(var(--drawer-swipe-movement-y)) scale(var(--stack-scale));
}When stacked drawers have varying heights, use the --drawer-height and
--drawer-frontmost-height variables to keep collapsed drawers aligned with the frontmost one.
.DrawerPopup {
--bleed: 3rem;
--stack-height: max(
0px,
calc(var(--drawer-frontmost-height, var(--drawer-height)) - var(--bleed))
);
height: var(--drawer-height, auto);
}
.DrawerPopup[data-nested-drawer-open] {
height: calc(var(--stack-height) + var(--bleed));
overflow: hidden;
}The data-nested-drawer-open attribute marks drawers behind the frontmost drawer. Use it with
data-nested-drawer-swiping to dim or hide parent drawer content while keeping it visible during
nested swipe interactions.
.DrawerContent {
transition: opacity 300ms;
}
.DrawerPopup[data-nested-drawer-open] .DrawerContent {
opacity: 0;
}
.DrawerPopup[data-nested-drawer-open][data-nested-drawer-swiping] .DrawerContent {
opacity: 1;
}The --drawer-swipe-movement-x, --drawer-swipe-movement-y, and --drawer-snap-point-offset CSS
variables can be used to create smooth drag and snap offsets:
.DrawerPopup[data-swipe-direction='right'] {
transform: translateX(var(--drawer-swipe-movement-x));
}
.DrawerPopup[data-swipe-direction='down'] {
transform: translateY(calc(var(--drawer-snap-point-offset) + var(--drawer-swipe-movement-y)));
}The data-swipe-direction attribute can be used with data-ending-style to animate directional
dismissal:
.DrawerPopup[data-ending-style][data-swipe-direction='right'] {
transform: translateX(100%);
}
.DrawerPopup[data-ending-style][data-swipe-direction='down'] {
transform: translateY(100%);
}Use --drawer-swipe-progress to fade the backdrop as the drawer is swiped, and
--drawer-swipe-strength to scale release transition durations based on swipe velocity.
.DrawerBackdrop {
--backdrop-opacity: 0.2;
opacity: calc(var(--backdrop-opacity) * (1 - var(--drawer-swipe-progress)));
}
.DrawerPopup[data-ending-style],
.DrawerBackdrop[data-ending-style] {
transition-duration: calc(var(--drawer-swipe-strength) * 400ms);
}
.DrawerPopup[data-swiping],
.DrawerBackdrop[data-swiping] {
transition-duration: 0ms;
}API reference#
<Drawer.Provider>
Provides a shared context for coordinating global Drawer UI, such as indent/background effects based on whether any Drawer is open. Doesn't render its own HTML element.
<Drawer.Root>
Groups all parts of the drawer. Doesn't render its own HTML element.
openboolean—Whether the drawer is currently open.
defaultOpenbooleanfalseWhether the drawer is initially open.
To render a controlled drawer, use the open prop instead.
modalboolean | 'trap-focus'trueDetermines if the drawer enters a modal state when open. - true: user interaction is limited to just the drawer: focus is trapped, document page scroll is locked, and pointer interactions on outside elements are disabled. - false: user interaction with the rest of the document is allowed. - 'trap-focus': focus is trapped inside the drawer, but document page scroll is not locked and pointer interactions outside of it remain enabled.
onOpenChange((open: boolean, eventDetails: DrawerRoot.ChangeEventDetails) => void)—Event handler called when the drawer is opened or closed.
onOpenChangeComplete((open: boolean) => void)—Event handler called after any animations complete when the drawer is opened or closed.
disablePointerDismissalbooleanfalseWhether to prevent the drawer from closing on outside presses. For non-modal drawers, this also prevents the drawer from closing when focus moves outside of it.
actionsRefActionsRef<DrawerRoot.Actions>—A ref to imperative actions: a ref object ({ current: null }) or a callback receiving them (and null on unmount). - unmount: Ends the closing phase of the drawer after an externally controlled closing animation finishes. Call preventUnmountOnClose() in onOpenChange first, otherwise the drawer completes closing on its own. Whether it leaves the DOM is decided by keepMounted on the portal. - close: Closes the drawer imperatively when called.
handleDrawerHandle<Payload>—A handle to associate the drawer with a trigger. If specified, allows detached triggers to control the drawer's open state. Can be created with the Drawer.createHandle() method.
triggerIdstring | null—ID of the trigger that the drawer is associated with. This is useful in conjunction with the open prop to create a controlled drawer. There's no need to specify this prop when the drawer is uncontrolled (that is, when the open prop is not set).
defaultTriggerIdstring | null—ID of the trigger that the drawer is associated with. This is useful in conjunction with the defaultOpen prop to create an initially open drawer.
swipeDirectionDrawerSwipeDirection'down'The swipe direction used to dismiss the drawer.
snapPointsDrawerSnapPoint[]—Snap points used to position the drawer. Use numbers between 0 and 1 to represent fractions of the viewport height, numbers greater than 1 as pixel values, or strings in px/rem units (for example, '148px' or '30rem').
snapToSequentialPointsbooleanfalseDisables velocity-based snap skipping so drag distance determines the next snap point.
snapPointDrawerSnapPoint | null—The currently active snap point. Use with onSnapPointChange to control the snap point.
defaultSnapPointDrawerSnapPoint | null—The initial snap point value when uncontrolled.
onSnapPointChange| (( snapPoint: DrawerSnapPoint | null, eventDetails: DrawerRoot.SnapPointChangeEventDetails, ) => void)—Callback fired when the snap point changes.
<Drawer.Trigger>
A button that opens the drawer. Renders a <button> element.
handleDrawerHandle<Payload>—A handle to associate the trigger with a drawer. Can be created with the Drawer.createHandle() method.
idstring—ID of the trigger. In addition to being forwarded to the rendered element, it is also used to specify the active trigger for the dialog in controlled mode (with the DialogRoot triggerId prop).
disabledbooleanfalseWhether the component ignores user interaction.
payloadPayload—A payload to pass to the dialog when it is opened.
nativeButtonbooleantrueWhether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Drawer.Backdrop>
An overlay displayed beneath the popup. Renders a <div> element.
forceRenderbooleanfalseWhether the backdrop is forced to render even when nested.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
--drawer-swipe-progressThe swipe progress of the drawer gesture.
<Drawer.Close>
A button that closes the drawer. Renders a <button> element.
disabledbooleanfalseWhether the component ignores user interaction.
nativeButtonbooleantrueWhether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Drawer.Content>
A container for the drawer contents. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Drawer.Description>
A paragraph with additional information about the drawer. Renders a <p> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Drawer.Indent>
A wrapper element intended to contain your app's main UI. Applies data-active when any drawer within the nearest <Drawer.Provider> is open. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Drawer.IndentBackground>
An element placed before <Drawer.Indent> to render a background layer that can be styled based on whether any drawer is open. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Drawer.Popup>
A container for the drawer contents. Renders a <div> element.
initialFocusFocusTargetProp—Determines the element to focus when the drawer is opened. By default, the popup itself is focused.
- false: Do not move focus. - true: Move focus based on the default behavior (first tabbable element or popup). - An element or a ref object ({ current }): Move focus to that element. - function: Called with the interaction type (mouse, touch, pen, or keyboard). Return an element to focus, true to use the default behavior, or false/undefined to do nothing.
finalFocusFocusTargetProp—Determines the element to focus when the drawer is closed.
- false: Do not move focus. - true: Move focus based on the default behavior (trigger or previously focused element). - An element or a ref object ({ current }): Move focus to that element. - function: Called with the interaction type (mouse, touch, pen, or keyboard). Return an element to focus, true to use the default behavior, or false/undefined to do nothing.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-expandedPresent when the drawer is at the expanded (full-height) snap point.
data-nested-drawer-openPresent when a nested drawer is open.
data-nested-drawer-swipingPresent when a nested drawer is being swiped.
data-swipe-dismissPresent when the drawer is dismissed by swiping.
data-swipe-directionIndicates the swipe direction.
data-swipingPresent when the drawer is being swiped.
--nested-drawersThe number of nested drawers that are currently open.
--drawer-heightThe height of the drawer popup.
--drawer-frontmost-heightThe height of the frontmost open drawer in the current nested drawer stack.
--drawer-swipe-movement-xThe swipe movement on the X axis.
--drawer-swipe-movement-yThe swipe movement on the Y axis.
--drawer-snap-point-offsetThe snap point offset used for translating the drawer.
--drawer-swipe-strengthA scalar (0.1-1) used to scale the swipe release transition duration in CSS.
<Drawer.Portal>
A portal element that moves the popup to a different part of the DOM. By default, the portal element is appended to <body>. Renders a <div> element.
keepMountedbooleanfalseWhether to keep the portal mounted in the DOM while the popup is hidden.
containerPortalContainer—A parent element to render the portal element into.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Drawer.SwipeArea>
An invisible area that listens for swipe gestures to open the drawer. Renders a <div> element.
disabledbooleanfalseWhether the swipe area is disabled.
swipeDirectionDrawerSwipeDirection—The swipe direction that opens the drawer. Defaults to the opposite of Drawer.Root swipeDirection.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-disabledPresent when the swipe area is disabled.
data-swipe-directionIndicates the swipe direction.
data-swipingPresent when the drawer is being swiped.
<Drawer.Title>
A heading that labels the drawer. Renders an <h2> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Drawer.Viewport>
A positioning container for the drawer popup that can be made scrollable. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-nestedPresent when the drawer is nested within another drawer.
--drawer-keyboard-insetThe software keyboard inset, measured from the bottom edge of the layout viewport. Present only when the drawer is wrapped in Drawer.VirtualKeyboardProvider.
<Drawer.VirtualKeyboardProvider>
Provides keyboard-aware focus and scroll handling for bottom-sheet drawers with form fields.