Drawer
A panel that slides in from the edge of the screen.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Drawer;
use function Phem\Ui\{DrawerClose, DrawerContent, DrawerDescription, DrawerPopup, DrawerTitle, DrawerTrigger};
#[Component]
function DrawerHeroDemo(): Node
{
return (
<Drawer swipeDirection="right">
<DrawerTrigger>Open drawer</DrawerTrigger>
<DrawerPopup>
<DrawerContent>
<DrawerTitle>Drawer</DrawerTitle>
<DrawerDescription>This is a drawer that slides in from the side. You can swipe to dismiss it.</DrawerDescription>
<div className="flex justify-end gap-3">
<DrawerClose>Close</DrawerClose>
</div>
</DrawerContent>
</DrawerPopup>
</Drawer>
);
}
Examples#
State#
By default, Drawer is an uncontrolled component: the browser keeps its state. The defaultOpen prop is the state that the server renders: a render with a new value opens or closes the drawer, and the controller keeps the value of the browser in data-phem-dialog-open-value.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Drawer;
use function Phem\Ui\{DrawerClose, DrawerContent, DrawerDescription, DrawerPopup, DrawerTitle, DrawerTrigger};
#[Component]
function DrawerStateDemo(): Node
{
return (
<Drawer>
<DrawerTrigger>Open</DrawerTrigger>
<DrawerPopup>
<DrawerContent>
<DrawerTitle>Example drawer</DrawerTitle>
<DrawerDescription>The browser keeps the state of this drawer.</DrawerDescription>
<div className="flex justify-end gap-3">
<DrawerClose>Close</DrawerClose>
</div>
</DrawerContent>
</DrawerPopup>
</Drawer>
);
}
Position#
The swipeDirection prop places the drawer, and gives the direction of the swipe that closes it. It defaults to "down" for bottom sheets. Use "up", "left", or "right" for other drawer positions.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Drawer, DrawerViewport};
use function Phem\Ui\{DrawerClose, DrawerContent, DrawerDescription, DrawerPopup, DrawerTitle, DrawerTrigger};
#[Component]
function DrawerPositionDemo(): Node
{
return (
<Drawer>
<DrawerTrigger>Open bottom drawer</DrawerTrigger>
<DrawerViewport>
<DrawerPopup className="max-h-[calc(80vh+3rem)] pt-4">
<div className="mx-auto mb-4 h-1 w-12 bg-neutral-300 dark:bg-neutral-700" />
<DrawerContent>
<DrawerTitle className="text-center">Notifications</DrawerTitle>
<DrawerDescription className="text-center">You are all caught up. Good job!</DrawerDescription>
<div className="flex justify-center gap-3">
<DrawerClose>Close</DrawerClose>
</div>
</DrawerContent>
</DrawerPopup>
</DrawerViewport>
</Drawer>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Drawer, DrawerViewport};
use function Phem\Ui\{DrawerClose, DrawerContent, DrawerDescription, DrawerPopup, DrawerTitle, DrawerTrigger};
#[Component]
function DrawerNestedDemo(): Node
{
return (
<Drawer>
<DrawerTrigger>Open drawer stack</DrawerTrigger>
<DrawerViewport>
<DrawerPopup className="[--bleed:3rem] [--peek:1rem] [--stack-progress:clamp(0,var(--drawer-swipe-progress,0),1)] [--stack-step:0.05] [--stack-peek-offset:max(0px,calc((var(--nested-drawers,0)-var(--stack-progress))*var(--peek)))] [--scale-base:calc(max(0,1-(var(--nested-drawers,0)*var(--stack-step))))] [--scale:clamp(0,calc(var(--scale-base)+(var(--stack-step)*var(--stack-progress))),1)] [--shrink:calc(1-var(--scale))] [--height:max(0px,calc(var(--drawer-frontmost-height,var(--drawer-height,0px))-var(--bleed)))] relative -mx-px w-[calc(100%+2px)] max-h-[calc(80%+3rem)] supports-[-webkit-touch-callout:none]:max-h-[calc(80vh+3rem)] h-[var(--drawer-height,auto)] border border-b-0 px-6 pt-4 [transform-origin:50%_calc(100%-var(--bleed))] [transform:translateY(calc(var(--drawer-swipe-movement-y,0px)-var(--stack-peek-offset)-(var(--shrink)*var(--height))))_scale(var(--scale))] after:pointer-events-none after:absolute after:inset-0 after:bg-transparent after:content-[''] after:transition-[background-color] after:duration-[450ms] after:ease-[cubic-bezier(0.32,0.72,0,1)] data-nested-drawer-swiping:duration-0 data-ending-style:opacity-[0.9999] data-nested-drawer-open:h-[calc(var(--height)+var(--bleed))] data-nested-drawer-open:overflow-hidden data-nested-drawer-open:after:bg-black/5 dark:border-b-0 [transition:transform_450ms_cubic-bezier(0.32,0.72,0,1),height_450ms_cubic-bezier(0.32,0.72,0,1),opacity_450ms_cubic-bezier(0.32,0.72,0,1)]">
<div className="mx-auto mb-4 h-1 w-12 bg-neutral-300 transition-opacity duration-[200ms] [[data-nested-drawer-open]:not([data-nested-drawer-swiping])>&]:opacity-0 dark:bg-neutral-700" />
<DrawerContent className="transition-opacity duration-[300ms] ease-[cubic-bezier(0.45,1.005,0,1.005)] [[data-nested-drawer-open]:not([data-nested-drawer-swiping])>&]:opacity-0">
<DrawerTitle className="text-center">Account</DrawerTitle>
<DrawerDescription className="text-center">Nested drawers can be styled to stack, while each drawer remains independently focus managed.</DrawerDescription>
<div className="flex items-center justify-end gap-4">
<div className="mr-auto">
<Drawer>
<DrawerTrigger>Security settings</DrawerTrigger>
<DrawerViewport className="backdrop:bg-transparent">
<DrawerPopup className="[--bleed:3rem] [--peek:1rem] [--stack-progress:clamp(0,var(--drawer-swipe-progress,0),1)] [--stack-step:0.05] [--stack-peek-offset:max(0px,calc((var(--nested-drawers,0)-var(--stack-progress))*var(--peek)))] [--scale-base:calc(max(0,1-(var(--nested-drawers,0)*var(--stack-step))))] [--scale:clamp(0,calc(var(--scale-base)+(var(--stack-step)*var(--stack-progress))),1)] [--shrink:calc(1-var(--scale))] [--height:max(0px,calc(var(--drawer-frontmost-height,var(--drawer-height,0px))-var(--bleed)))] relative -mx-px w-[calc(100%+2px)] max-h-[calc(80%+3rem)] supports-[-webkit-touch-callout:none]:max-h-[calc(80vh+3rem)] h-[var(--drawer-height,auto)] border border-b-0 px-6 pt-4 [transform-origin:50%_calc(100%-var(--bleed))] [transform:translateY(calc(var(--drawer-swipe-movement-y,0px)-var(--stack-peek-offset)-(var(--shrink)*var(--height))))_scale(var(--scale))] after:pointer-events-none after:absolute after:inset-0 after:bg-transparent after:content-[''] after:transition-[background-color] after:duration-[450ms] after:ease-[cubic-bezier(0.32,0.72,0,1)] data-nested-drawer-swiping:duration-0 data-ending-style:opacity-[0.9999] data-nested-drawer-open:h-[calc(var(--height)+var(--bleed))] data-nested-drawer-open:overflow-hidden data-nested-drawer-open:after:bg-black/5 dark:border-b-0 [transition:transform_450ms_cubic-bezier(0.32,0.72,0,1),height_450ms_cubic-bezier(0.32,0.72,0,1),opacity_450ms_cubic-bezier(0.32,0.72,0,1)]">
<div className="mx-auto mb-4 h-1 w-12 bg-neutral-300 transition-opacity duration-[200ms] [[data-nested-drawer-open]:not([data-nested-drawer-swiping])>&]:opacity-0 dark:bg-neutral-700" />
<DrawerContent className="transition-opacity duration-[300ms] ease-[cubic-bezier(0.45,1.005,0,1.005)] [[data-nested-drawer-open]:not([data-nested-drawer-swiping])>&]:opacity-0">
<DrawerTitle className="text-center">Security</DrawerTitle>
<DrawerDescription className="text-center">Review sign-in activity and update your security preferences.</DrawerDescription>
<ul className="mb-6 list-disc pl-5 text-neutral-700 dark:text-neutral-300">
<li>Passkeys enabled</li>
<li>2FA via authenticator app</li>
<li>3 signed-in devices</li>
</ul>
<div className="flex items-center justify-end gap-4">
<div className="mr-auto">
<Drawer>
<DrawerTrigger>Advanced options</DrawerTrigger>
<DrawerViewport className="backdrop:bg-transparent">
<DrawerPopup className="[--bleed:3rem] [--peek:1rem] [--stack-progress:clamp(0,var(--drawer-swipe-progress,0),1)] [--stack-step:0.05] [--stack-peek-offset:max(0px,calc((var(--nested-drawers,0)-var(--stack-progress))*var(--peek)))] [--scale-base:calc(max(0,1-(var(--nested-drawers,0)*var(--stack-step))))] [--scale:clamp(0,calc(var(--scale-base)+(var(--stack-step)*var(--stack-progress))),1)] [--shrink:calc(1-var(--scale))] [--height:max(0px,calc(var(--drawer-frontmost-height,var(--drawer-height,0px))-var(--bleed)))] relative -mx-px w-[calc(100%+2px)] max-h-[calc(80%+3rem)] supports-[-webkit-touch-callout:none]:max-h-[calc(80vh+3rem)] h-[var(--drawer-height,auto)] border border-b-0 px-6 pt-4 [transform-origin:50%_calc(100%-var(--bleed))] [transform:translateY(calc(var(--drawer-swipe-movement-y,0px)-var(--stack-peek-offset)-(var(--shrink)*var(--height))))_scale(var(--scale))] after:pointer-events-none after:absolute after:inset-0 after:bg-transparent after:content-[''] after:transition-[background-color] after:duration-[450ms] after:ease-[cubic-bezier(0.32,0.72,0,1)] data-nested-drawer-swiping:duration-0 data-ending-style:opacity-[0.9999] data-nested-drawer-open:h-[calc(var(--height)+var(--bleed))] data-nested-drawer-open:overflow-hidden data-nested-drawer-open:after:bg-black/5 dark:border-b-0 [transition:transform_450ms_cubic-bezier(0.32,0.72,0,1),height_450ms_cubic-bezier(0.32,0.72,0,1),opacity_450ms_cubic-bezier(0.32,0.72,0,1)]">
<div className="mx-auto mb-4 h-1 w-12 bg-neutral-300 transition-opacity duration-[200ms] [[data-nested-drawer-open]:not([data-nested-drawer-swiping])>&]:opacity-0 dark:bg-neutral-700" />
<DrawerContent className="transition-opacity duration-[300ms] ease-[cubic-bezier(0.45,1.005,0,1.005)] [[data-nested-drawer-open]:not([data-nested-drawer-swiping])>&]:opacity-0">
<DrawerTitle className="text-center">Advanced</DrawerTitle>
<DrawerDescription className="text-center">This drawer is taller to demonstrate variable-height stacking.</DrawerDescription>
<div className="mb-4 grid gap-2">
<label className="text-sm font-bold text-neutral-700 dark:text-neutral-300" htmlFor="device-name">Device name</label>
<input id="device-name" className="h-control w-full border border-neutral-950 bg-white px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:bg-neutral-950 dark:text-white dark:placeholder:text-neutral-400" value="Personal laptop" />
</div>
<div className="mb-4 grid gap-2">
<label className="text-sm font-bold text-neutral-700 dark:text-neutral-300" htmlFor="device-notes">Notes</label>
<textarea id="device-notes" rows="3" className="h-control w-full border border-neutral-950 bg-white px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:bg-neutral-950 dark:text-white dark:placeholder:text-neutral-400 h-auto min-h-32 resize-y p-2">Rotate recovery codes and revoke older sessions.</textarea>
</div>
<div className="flex justify-end">
<DrawerClose>Done</DrawerClose>
</div>
</DrawerContent>
</DrawerPopup>
</DrawerViewport>
</Drawer>
</div>
<DrawerClose>Close</DrawerClose>
</div>
</DrawerContent>
</DrawerPopup>
</DrawerViewport>
</Drawer>
</div>
<DrawerClose>Close</DrawerClose>
</div>
</DrawerContent>
</DrawerPopup>
</DrawerViewport>
</Drawer>
);
}
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'). Apply the snap point offset in your styles: 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).
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Drawer, DrawerViewport};
use function Phem\Ui\{DrawerClose, DrawerContent, DrawerDescription, DrawerPopup, DrawerTitle, DrawerTrigger};
#[Component]
function DrawerSnapPointsDemo(): Node
{
$snapPoints = ['31rem', 1];
return (
<Drawer snapPoints={{ snapPoints }}>
<DrawerTrigger>Open snap drawer</DrawerTrigger>
<DrawerViewport className="touch-none [--bleed:3rem] after:pointer-events-none after:fixed after:inset-x-0 after:bottom-0 after:h-[var(--bleed)] after:bg-white after:content-[''] data-closed:after:opacity-0 dark:after:bg-neutral-950">
<DrawerPopup className="relative z-1 flex max-h-[calc(100dvh-1rem)] min-h-0 flex-col overflow-visible p-0 touch-none [padding-bottom:max(0px,calc(var(--drawer-snap-point-offset,0px)+var(--drawer-swipe-movement-y,0px)))] after:pointer-events-none after:absolute after:inset-x-0 after:top-full after:h-[var(--bleed)] after:bg-[inherit] after:content-[''] data-starting-style:[padding-bottom:0] data-ending-style:[padding-bottom:0]">
<div className="shrink-0 border-b border-neutral-300 px-6 pt-3.5 pb-4 touch-none select-none dark:border-neutral-700">
<div className="mx-auto mb-2.5 h-1 w-12 shrink-0 bg-neutral-300 dark:bg-neutral-700" />
<DrawerTitle className="mb-0 cursor-default text-center">Snap points</DrawerTitle>
</div>
<DrawerContent className="max-w-none min-h-0 flex-1 overflow-y-auto overscroll-contain touch-auto px-6 pt-4 pb-[calc(1.5rem+env(safe-area-inset-bottom,0px))]">
<div className="mx-auto w-full max-w-90">
<DrawerDescription className="mb-4 text-center">Drag the sheet to snap between a compact peek and a near full-height view.</DrawerDescription>
<div className="mb-6 grid gap-3" aria-hidden="true">
{% for i in 1..20 %}
<div key={{ i }} className="h-12 bg-neutral-200 dark:bg-neutral-700" />
{% endfor %}
</div>
<div className="flex items-center justify-end gap-3">
<DrawerClose>Close</DrawerClose>
</div>
</div>
</DrawerContent>
</DrawerPopup>
</DrawerViewport>
</Drawer>
);
}
Virtual keyboard aware#
Set virtualKeyboard on a bottom sheet to make it react to software keyboards when it contains form controls. When the keyboard opens, the field with the focus scrolls into view, and the popup gets the height of the keyboard in --drawer-keyboard-inset.
Keep the popup frame stable: place header and footer content outside a plain scrollable body. If a footer contains its own input, reserve a footer slot below the scroll area and offset it by var(--drawer-keyboard-inset, 0px). Always include the 0px fallback: the variable is only set while the keyboard is open.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Drawer, DrawerViewport};
use function Phem\Ui\{DrawerClose, DrawerContent, DrawerPopup, DrawerTitle, DrawerTrigger};
const DRAWER_DELIVERY = [
['Name', 'Ada Lovelace'],
['Phone', '+1 (555) 123-4567'],
['Street address', '12 Computing Way'],
['Apartment', 'Unit 4B'],
['City', 'San Francisco'],
['Postal code', '94107'],
['Delivery window', 'After 6 PM'],
['Backup contact', 'Grace Hopper'],
];
#[Component]
function DrawerKeyboardDemo(): Node
{
$fields = DRAWER_DELIVERY;
return (
<Drawer virtualKeyboard>
<DrawerTrigger>Open keyboard-aware drawer</DrawerTrigger>
<DrawerViewport className="touch-none [--bleed:3rem] after:pointer-events-none after:fixed after:inset-x-0 after:bottom-0 after:h-[var(--bleed)] after:bg-white after:content-[''] data-closed:after:opacity-0 has-[[data-swiping]]:after:opacity-0 dark:after:bg-neutral-950">
<DrawerPopup className="relative z-1 -mb-[var(--bleed)] flex h-[calc(100%-1rem+var(--bleed))] max-h-[calc(100%-1rem+var(--bleed))] w-full flex-col overflow-visible p-0 touch-none [--footer-reserved-height:calc(5.25rem+var(--bleed))] will-change-transform">
<div className="relative shrink-0 border-b border-neutral-300 px-6 py-4 touch-none select-none dark:border-neutral-700">
<div className="absolute top-2 left-1/2 h-1 w-12 shrink-0 -translate-x-1/2 bg-neutral-300 dark:bg-neutral-700" />
<div className="grid grid-cols-[minmax(0,1fr)_auto_minmax(0,1fr)] items-center gap-3">
<DrawerClose className="min-w-0 justify-self-start">Cancel</DrawerClose>
<DrawerTitle className="mb-0 cursor-default text-center">Delivery details</DrawerTitle>
<DrawerClose className="min-w-0 justify-self-end">Save</DrawerClose>
</div>
</div>
<DrawerContent className="max-w-none min-h-0 flex-1 overflow-y-auto overscroll-contain touch-auto px-6 pt-4 pb-6">
<div className="mx-auto grid w-full max-w-90 gap-3">
{% for item in fields %}
<label key={{ item[0] }} className="flex w-full flex-col items-start gap-1">
<span className="text-sm font-bold text-neutral-950 dark:text-white">{{ item[0] }}</span>
<input type="text" placeholder={{ item[1] }} className="h-control w-full border border-neutral-950 bg-white px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:bg-neutral-950 dark:text-white dark:placeholder:text-neutral-400" />
</label>
{% endfor %}
<label className="flex w-full flex-col items-start gap-1">
<span className="text-sm font-bold text-neutral-950 dark:text-white">Instructions</span>
<textarea placeholder="Gate code, drop-off spot, or anything else the driver should know" className="h-control w-full border border-neutral-950 bg-white px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:bg-neutral-950 dark:text-white dark:placeholder:text-neutral-400 h-auto min-h-22 resize-y py-2"></textarea>
</label>
</div>
</DrawerContent>
<div className="relative min-h-[var(--footer-reserved-height)] shrink-0 transition-[min-height] duration-[260ms] ease-[cubic-bezier(0.32,0.72,0,1)] focus-within:min-h-[calc(var(--footer-reserved-height)+var(--drawer-keyboard-inset,0px))] motion-reduce:transition-none">
<div className="absolute right-0 bottom-0 left-0 z-1 border-t border-neutral-950 bg-white px-6 pt-2.5 pb-[calc(0.625rem+env(safe-area-inset-bottom,0px)+var(--bleed))] transition-[bottom,padding-bottom] duration-[260ms] ease-[cubic-bezier(0.32,0.72,0,1)] focus-within:fixed focus-within:z-3 focus-within:bottom-0 focus-within:pb-[calc(0.625rem+env(safe-area-inset-bottom,0px)+var(--bleed)+var(--drawer-keyboard-inset,0px))] focus-within:[transform:translate3d(0,0,0)] motion-reduce:transition-none dark:border-white dark:bg-neutral-950">
<label className="mx-auto flex w-full max-w-90 flex-col gap-1">
<span className="text-sm font-bold text-neutral-950 dark:text-white">Delivery note</span>
<input type="text" placeholder="Add a note for the driver" className="h-control w-full border border-neutral-950 bg-white px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:bg-neutral-950 dark:text-white dark:placeholder:text-neutral-400 h-10 px-2.5" />
</label>
</div>
</div>
</DrawerPopup>
</DrawerViewport>
</Drawer>
);
}
Indent effect#
Scale the background down when any drawer opens by wrapping your app in DrawerProvider and using DrawerIndentBackground and DrawerIndent at the top of your tree. Any Drawer within the provider activates the indent parts while it is open: they get the [data-active] attribute, and --drawer-swipe-progress during a swipe.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Drawer, DrawerViewport};
use function Phem\Ui\{DrawerBackdrop, DrawerClose, DrawerContent, DrawerDescription, DrawerIndent, DrawerIndentBackground, DrawerPopup, DrawerProvider, DrawerTitle, DrawerTrigger};
#[Component]
function DrawerIndentDemo(): Node
{
return (
<DrawerProvider className="relative w-full overflow-hidden [--bleed:3rem]">
<DrawerIndentBackground className="absolute inset-0 bg-black dark:bg-neutral-300" />
<Drawer modal={{ false }}>
<DrawerIndent className="[--indent-radius:calc(1rem*(1-var(--drawer-swipe-progress)))] [--indent-transition:calc(1-clamp(0,calc(var(--drawer-swipe-progress)*100000),1))] relative min-h-80 border border-neutral-950 bg-white p-4 text-neutral-950 [transition:transform_0.4s_cubic-bezier(0.32,0.72,0,1),border-radius_0.25s_cubic-bezier(0.32,0.72,0,1)] origin-[center_top] will-change-transform [transform:scale(1)_translateY(0)] [transition-duration:calc(400ms*var(--indent-transition)),calc(250ms*var(--indent-transition))] data-active:transform-[scale(calc(0.98+(0.02*var(--drawer-swipe-progress))))_translateY(calc(0.5rem*(1-var(--drawer-swipe-progress))))] data-active:rounded-tl-(--indent-radius) data-active:rounded-tr-(--indent-radius) dark:border-white dark:bg-neutral-950 dark:text-white">
<div className="flex min-h-80 items-center justify-center">
<DrawerTrigger>Open drawer</DrawerTrigger>
</div>
</DrawerIndent>
<DrawerBackdrop className="absolute" />
<DrawerViewport className="absolute h-auto w-auto">
<DrawerPopup className="max-h-[calc(80vh+var(--bleed))] pt-4">
<div className="mx-auto mb-4 h-1 w-12 bg-neutral-300 dark:bg-neutral-700" />
<DrawerContent>
<DrawerTitle className="text-center">Notifications</DrawerTitle>
<DrawerDescription className="text-center">You are all caught up. Good job!</DrawerDescription>
<div className="flex justify-center gap-3">
<DrawerClose>Close</DrawerClose>
</div>
</DrawerContent>
</DrawerPopup>
</DrawerViewport>
</Drawer>
</DrawerProvider>
);
}
Non-modal#
Set modal={{ false }} to opt out of focus trapping and disablePointerDismissal to keep the drawer open on outside clicks.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Drawer, DrawerViewport};
use function Phem\Ui\{DrawerClose, DrawerContent, DrawerDescription, DrawerPopup, DrawerTitle, DrawerTrigger};
#[Component]
function DrawerNonModalDemo(): Node
{
return (
<Drawer swipeDirection="right" modal={{ false }} disablePointerDismissal>
<DrawerTrigger>Open non-modal drawer</DrawerTrigger>
<DrawerViewport className="pointer-events-none">
<DrawerPopup className="pointer-events-auto">
<DrawerContent>
<DrawerTitle>Non-modal drawer</DrawerTitle>
<DrawerDescription>This drawer does not trap focus and ignores outside clicks. Use the close button or swipe to dismiss it.</DrawerDescription>
<div className="flex justify-end gap-3">
<DrawerClose>Close</DrawerClose>
</div>
</DrawerContent>
</DrawerPopup>
</DrawerViewport>
</Drawer>
);
}
#
You can build a full-screen mobile navigation sheet using Drawer parts, including a flick-to-dismiss from the top gesture.
Swipe to open#
Place DrawerSwipeArea along the edge of the viewport to enable swipe-to-open gestures.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Drawer, DrawerViewport};
use function Phem\Ui\{DrawerBackdrop, DrawerClose, DrawerContent, DrawerDescription, DrawerPopup, DrawerSwipeArea, DrawerTitle};
#[Component]
function DrawerSwipeAreaDemo(): Node
{
return (
<div className="relative min-h-80 w-full overflow-hidden border border-neutral-950 bg-white text-neutral-950 dark:border-white dark:bg-neutral-950 dark:text-white">
<Drawer swipeDirection="right" modal={{ false }}>
<DrawerSwipeArea className="absolute inset-y-0 right-0 z-[1] w-10 border-l-2 border-dashed border-blue-800 bg-blue-800/10 dark:border-blue-500 dark:bg-blue-500/10">
<span className="pointer-events-none absolute top-1/2 right-0 z-0 mr-2 origin-center -translate-y-1/2 -rotate-90 text-xs font-bold tracking-[0.12em] whitespace-nowrap text-blue-800 uppercase dark:text-blue-500">Swipe here</span>
</DrawerSwipeArea>
<div className="flex min-h-80 flex-col items-center justify-center gap-3 p-4 text-center">
<p className="pr-12 text-center text-sm text-neutral-600 dark:text-neutral-400">Swipe from the right edge to open the drawer.</p>
</div>
<DrawerBackdrop className="absolute min-h-0" />
<DrawerViewport className="absolute z-20 h-auto w-auto">
<DrawerPopup className="h-full">
<DrawerContent>
<DrawerTitle>Library</DrawerTitle>
<DrawerDescription>Swipe from the edge whenever you want to jump back into your playlists.</DrawerDescription>
<div className="flex justify-end gap-3">
<DrawerClose>Close</DrawerClose>
</div>
</DrawerContent>
</DrawerPopup>
</DrawerViewport>
</Drawer>
</div>
);
}
Close confirmation#
This example shows a nested confirmation dialog that opens if the text entered in the drawer is going to be discarded.
To implement this, give the confirmClose prop of the Drawer the handle of an AlertDialog in its popup. When the user changed a field, a request to close opens the alert dialog: the confirmation is automatically shown when the user clicks the backdrop, presses the Esc key, clicks a close button, or dismisses the drawer with a swipe gesture. Its DialogDiscard button closes the two and resets the text.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{AlertDialog, Drawer};
use function Phem\Ui\{AlertDialogClose, AlertDialogDescription, AlertDialogPopup, AlertDialogTitle, DialogDiscard, DrawerClose, DrawerContent, DrawerPopup, DrawerTitle, DrawerTrigger};
#[Component]
function DrawerCloseConfirmationDemo(): Node
{
return (
<Drawer swipeDirection="right" confirmClose="discard-drawer-tweet">
<DrawerTrigger>Tweet</DrawerTrigger>
<DrawerPopup className="after:pointer-events-none after:absolute after:inset-0 after:bg-black/5 after:opacity-0 after:transition-opacity after:duration-100 after:ease-out data-nested-dialog-open:after:opacity-100">
<DrawerContent className="flex flex-col gap-4">
<DrawerTitle id="drawer-tweet-title" className="mb-0">New tweet</DrawerTitle>
<form method="dialog" className="flex flex-col gap-4">
<textarea aria-labelledby="drawer-tweet-title" required placeholder="What’s on your mind?" className="w-full border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 dark:text-white placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white min-h-32 p-2"></textarea>
<div className="flex justify-end gap-3">
<DrawerClose>Cancel</DrawerClose>
<button type="submit" className="flex h-control items-center justify-center border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 px-3 text-sm text-neutral-950 dark:text-white hover:bg-neutral-100 dark:hover:bg-neutral-800 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white">Tweet</button>
</div>
</form>
</DrawerContent>
<AlertDialog handle="discard-drawer-tweet">
<AlertDialogPopup className="top-[calc(50%+1.25rem*var(--nested-dialogs,0))] scale-[calc(1-0.1*var(--nested-dialogs,0))] transition-[top,scale,opacity] after:absolute after:inset-0 after:bg-black/5 after:opacity-0 after:transition-opacity after:duration-100 after:ease-out after:pointer-events-none data-ending-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs,0))] data-ending-style:scale-[0.96] data-nested-dialog-open:after:opacity-100 data-starting-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs,0))] data-starting-style:scale-[0.96] data-nested:backdrop:bg-transparent">
<div className="flex flex-col gap-1">
<AlertDialogTitle>Discard tweet?</AlertDialogTitle>
<AlertDialogDescription>Your tweet will be lost.</AlertDialogDescription>
</div>
<div className="flex justify-end gap-3">
<AlertDialogClose>Go back</AlertDialogClose>
<DialogDiscard>Discard</DialogDiscard>
</div>
</AlertDialogPopup>
</AlertDialog>
</DrawerPopup>
</Drawer>
);
}
Action sheet#
This demo builds an action sheet with a grouped list of actions plus a separate destructive action button.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Drawer, DrawerViewport};
use function Phem\Ui\{DrawerClose, DrawerContent, DrawerDescription, DrawerPopup, DrawerTitle, DrawerTrigger};
const DRAWER_ACTIONS = [
'Unfollow',
'Mute',
'Add to Favourites',
'Add to Close Friends',
'Restrict',
];
#[Component]
function DrawerActionSheetDemo(): Node
{
$actions = DRAWER_ACTIONS;
return (
<Drawer>
<DrawerTrigger>Open action sheet</DrawerTrigger>
<DrawerViewport>
<DrawerPopup className="pointer-events-none mb-0 flex w-full max-w-[20rem] flex-col gap-3 overflow-visible border-0 bg-transparent px-4 pt-0 pb-[calc(1rem+env(safe-area-inset-bottom,0px))] shadow-none data-starting-style:[transform:translateY(calc(100%+1rem+2px))] data-ending-style:[transform:translateY(calc(100%+1rem+2px))]">
<DrawerContent className="pointer-events-auto max-w-none overflow-hidden border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none">
<DrawerTitle className="sr-only">Profile actions</DrawerTitle>
<DrawerDescription className="sr-only">Choose an action for this user.</DrawerDescription>
<ul aria-label="Profile actions" className="m-0 list-none divide-y divide-neutral-950 p-0 dark:divide-white">
{% for item in actions %}
<li key={{ item }}>
<DrawerClose className="h-10 w-full border-0 bg-transparent px-5 text-center text-sm text-neutral-950 select-none hover:bg-neutral-100 active:bg-neutral-200 focus-visible:bg-neutral-100 focus-visible:outline-none dark:text-white dark:hover:bg-neutral-800 dark:active:bg-neutral-700 dark:focus-visible:bg-neutral-800">{{ item }}</DrawerClose>
</li>
{% endfor %}
</ul>
</DrawerContent>
<div className="pointer-events-auto overflow-hidden border border-neutral-950 bg-white shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:shadow-none">
<DrawerClose className="h-10 w-full border-0 bg-transparent px-5 text-center text-sm text-neutral-950 select-none hover:bg-neutral-100 active:bg-neutral-200 focus-visible:bg-neutral-100 focus-visible:outline-none dark:text-white dark:hover:bg-neutral-800 dark:active:bg-neutral-700 dark:focus-visible:bg-neutral-800 text-red-700 dark:text-red-400">Block User</DrawerClose>
</div>
</DrawerPopup>
</DrawerViewport>
</Drawer>
);
}
Detached triggers#
A drawer can be controlled by a trigger located either inside or outside the Drawer component. For simple, one-off interactions, place the DrawerTrigger inside Drawer.
However, if defining the drawer's content next to its trigger is not practical, you can use a detached trigger: give the same handle to the DrawerTrigger and to the Drawer. The drawer can render different content depending on which trigger opened it: give each trigger a payload, and the DrawerPayload part shows it.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Drawer;
use function Phem\Ui\{DrawerClose, DrawerContent, DrawerDescription, DrawerPayload, DrawerPopup, DrawerTitle, DrawerTrigger};
#[Component]
function DrawerDetachedDemo(): Node
{
return (
<div className="flex gap-2">
<DrawerTrigger handle="detached-drawer" payload="Profile">Profile</DrawerTrigger>
<DrawerTrigger handle="detached-drawer" payload="Settings">Settings</DrawerTrigger>
<Drawer handle="detached-drawer" swipeDirection="right">
<DrawerPopup>
<DrawerContent>
<DrawerTitle>
<DrawerPayload />
</DrawerTitle>
<DrawerDescription>The trigger that opened this drawer gave its title.</DrawerDescription>
<div className="flex justify-end gap-3">
<DrawerClose>Close</DrawerClose>
</div>
</DrawerContent>
</DrawerPopup>
</Drawer>
</div>
);
}
API reference#
Import the components of the family:
use Phem\Ui\Drawer;
use Phem\Ui\DrawerViewport;
use function Phem\Ui\{DrawerTrigger, DrawerPopup, DrawerContent, DrawerTitle, DrawerDescription, DrawerClose, DrawerPayload, DrawerBackdrop, DrawerSwipeArea, DrawerProvider, DrawerIndentBackground, DrawerIndent};Drawer#
A panel that slides in from an edge of the viewport, and closes with a swipe: Base UI Drawer.Root, on the phem-dialog controller with a swipe-direction.
The props of Dialog work the same: handle links detached triggers (a DrawerTrigger with the same handle), triggerId is the id of the trigger that opened it, confirmClose is the handle of an AlertDialog that asks before the close, and finalFocus is the selector of the element that gets the focus after the close. With modal={{ false }}, the page stays active and the focus is not trapped. With disablePointerDismissal, a press outside does not close it. With virtualKeyboard (Base UI Drawer.VirtualKeyboardProvider), the popup gets --drawer-keyboard-inset while the keyboard of the device is open.
Animate the open, the close, the swipes and the stack with CSS: the popup gets data-starting-style when it starts to open and data-ending-style when it starts to close. --nested-drawers is the depth in the stack (0 for the frontmost drawer), and --drawer-height and --drawer-frontmost-height align the drawers behind it when the heights are not the same. A drawer behind the frontmost one gets data-nested-drawer-open, and data-nested-drawer-swiping while the frontmost one moves. --drawer-swipe-movement-x, --drawer-swipe-movement-y and --drawer-snap-point-offset give the drag and snap offsets, data-swipe-direction with data-ending-style gives the direction of the close, --drawer-swipe-progress fades the backdrop, and --drawer-swipe-strength scales the duration of the release.
| Prop | Type | Default |
|---|---|---|
swipeDirection | 'up' | 'down' | 'left' | 'right' | SwipeDirection::Down |
handle | ?string | null |
defaultOpen | bool | false |
triggerId | ?string | null |
dismissible | bool | true |
disablePointerDismissal | bool | false |
modal | bool | true |
confirmClose | ?string | null |
finalFocus | ?string | null |
virtualKeyboard | bool | false |
snapPoints | array | [] |
snapToSequentialPoints | bool | false |
children | ?Html | null |
...attrs | array | [] |
DrawerPopup#
The panel. Its ::backdrop is the Backdrop of Base UI: it fades with the swipe. In a DrawerViewport, it is a div, and the viewport has the ::backdrop. A drawer that is not modal is not in the top layer: z-50 puts it above the page, as the Portal of Base UI.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
DrawerViewport#
The container that places the popup, as the Viewport of Base UI: a <dialog> that fills the screen, with the popup on the edge of the swipeDirection. Its ::backdrop is the Backdrop: it fades with the swipe. A tap on it outside the popup closes the drawer. With z-50, a drawer that is not modal (not in the top layer) is above the page.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...attrs | array | [] |
DrawerTrigger#
The button that opens the drawer. With a handle, it is a detached trigger of the Drawer with the same handle, and its payload is the text of the DrawerPayload.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
DrawerContent#
The content of the panel: a centered column. A mouse press in it selects its text, and does not start a swipe.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
DrawerTitle#
The heading of the drawer. It names the drawer (aria-labelledby).
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
DrawerDescription#
The text that describes the drawer (aria-describedby).
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
DrawerClose#
A button that closes the drawer.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
DrawerPayload#
The text of the payload of the trigger that opened the drawer, or its children.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
DrawerBackdrop#
The Backdrop of Base UI, as an element: for a drawer that is not modal (it has no ::backdrop), or in a container of the page. It fades with the swipe.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
DrawerSwipeArea#
An area at the edge of the screen: a swipe away from the edge opens the drawer.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
DrawerProvider#
The part of the page that the drawers indent, as the Provider of Base UI: put the DrawerIndentBackground and the DrawerIndent in it. While a drawer in it is open, the two parts get data-active, and --drawer-swipe-progress during its swipe.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
DrawerIndentBackground#
The background behind the DrawerIndent: it shows while the indent is smaller.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
DrawerIndent#
The page that gets smaller while a drawer is open: style it with data-active.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
The source of the examples: examples/ui/Drawer.phem.