Skip to content
GitHub11 stars

A panel that slides in from the edge of the screen and can be swiped away.

@inject ToastManager Toasts

<Blazix.Primitives.MediaQuery Query="(max-width: 767px)" MatchesChanged="@(matches => _isMobile = matches)" />

Installation

dnx Blazix.Cli add drawer

Add the following to your global styles. On iOS Safari, the drawer overlay is absolutely positioned and requires a positioned body to cover the viewport after the page is scrolled.

body {
  position: relative;
}

Usage

<Drawer>
    <DrawerTrigger>
        <Render>
            <Button @attributes="context.Attributes" Variant="ButtonVariant.Outline">Open</Button>
        </Render>
    </DrawerTrigger>
    <DrawerContent>
        <DrawerHeader>
            <DrawerTitle>Are you absolutely sure?</DrawerTitle>
            <DrawerDescription>This action cannot be undone.</DrawerDescription>
        </DrawerHeader>
        <div class="p-4">...</div>
        <DrawerFooter>
            <Button>Submit</Button>
            <DrawerClose>
                <Render>
                    <Button @attributes="context.Attributes" Variant="ButtonVariant.Outline">Cancel</Button>
                </Render>
            </DrawerClose>
        </DrawerFooter>
    </DrawerContent>
</Drawer>

Composition

Use the following composition to build a Drawer:

Drawer
├── DrawerTrigger
└── DrawerContent
    ├── DrawerHeader
    │   ├── DrawerTitle
    │   └── DrawerDescription
    └── DrawerFooter

DrawerContent composes the portal, overlay, viewport, and popup from Blazix.Primitives. For lower-level control, DrawerPortal, DrawerOverlay, and DrawerSwipeHandle are also exported.

Custom Sizes

A vertical drawer sizes itself to its content and is capped at calc(100dvh - 6rem) by default. A side drawer spans 75% of the viewport width, or 24rem on larger screens.

To customize the height of a vertical drawer, use the h-* and max-h-* utilities on DrawerContent.

<DrawerContent Class="h-[50vh]">...</DrawerContent>

To customize the width of a side drawer, use the w-* and max-w-* utilities on DrawerContent.

<DrawerContent Class="w-96">...</DrawerContent>

When the same component renders in multiple directions, scope an override to one axis using the data-[swipe-axis=*] variants.

<DrawerContent Class="data-[swipe-axis=y]:max-h-[50vh] data-[swipe-axis=x]:w-96">...</DrawerContent>

To make a region of the drawer scrollable, make the scroll container a flex item. Avoid h-full, which does not resolve inside a content-sized drawer.

<DrawerContent>
    <DrawerHeader>...</DrawerHeader>
    <div class="flex-1 overflow-y-auto p-4">...</div>
    <DrawerFooter>...</DrawerFooter>
</DrawerContent>

Styling

The drawer exposes CSS variables for style-level customization. Set the sizing variables on DrawerContent. Set the overlay variable on [data-slot=drawer-overlay] in your CSS.

VariableDefaultDescription
--drawer-inset0pxFloats the drawer from the viewport edges.
--drawer-bleed-backgroundvar(--color-popover)Fills the gap behind the drawer on swipe overshoot.
--drawer-overlay-min-opacity0Minimum overlay opacity. Defaults to 0.5 when snap points are active.

The drawer also sets data attributes you can target with variants such as data-[swipe-direction=down]: on DrawerContent, or group-data-[swipe-axis=y]/drawer-popup: on its descendants.

AttributeValuesSet when
data-swipe-directionup, right, down, leftAlways.
data-swipe-axisx, yAlways.
data-snap-pointsPresentThe drawer has snap points.
data-expandedPresentThe drawer is at the full snap point.
data-swipingPresentA swipe is in progress.
data-nested-drawer-openPresentA nested drawer is open on top.

Position

Use the SwipeDirection parameter to set the side of the drawer.

Available options are DrawerSwipeDirection.Up, Right, Down (the default), and Left.

<Drawer SwipeDirection="DrawerSwipeDirection.Left">
    <DrawerTrigger>
        <Render>

Swipe Handle

Use ShowSwipeHandle on Drawer to render a swipe handle.

<Drawer ShowSwipeHandle="true">
    <DrawerTrigger>
        <Render>

Nested

Open drawers from inside another drawer. Parent drawers stay mounted and stack behind the frontmost drawer.

<Blazix.Primitives.MediaQuery Query="(max-width: 767px)" MatchesChanged="@(matches => _isMobile = matches)" />
<Drawer ShowSwipeHandle="_isMobile" SwipeDirection="Direction">
    <DrawerTrigger>

Non Modal

Set Modal="false" to allow interaction with the rest of the page while the drawer is open. Combine with DisablePointerDismissal to prevent the drawer from closing on outside presses.

<Drawer Modal="false" DisablePointerDismissal="true" SwipeDirection="DrawerSwipeDirection.Right">
    <DrawerTrigger>
        <Render>

Snap Points

Use SnapPoints to snap a drawer to preset heights. Numbers between 0 and 1 represent fractions of the viewport. Numbers greater than 1 are treated as pixel values. String values support px and rem units. Snap points apply to vertical drawers.

Track the active snap point with @bind-SnapPoint. At the full snap point, the drawer gets a data-expanded attribute you can style with the data-expanded: variant.

<Drawer SnapPoints="SnapPoints" ShowSwipeHandle="true">
    <DrawerTrigger>
        <Render Context="trigger">

Responsive

You can combine the Dialog and Drawer components to create a responsive dialog. This renders a Dialog component on desktop and a Drawer on mobile.

<Blazix.Primitives.MediaQuery Query="(min-width: 768px)" MatchesChanged="@(matches => _isDesktop = matches)" />
@if (_isDesktop)
{

RTL

To enable RTL support in Blazix, see the RTL configuration guide.

Translated with AI for the demo, so it may have mistakes.

@* The swipe direction is physical: slide in from the end of the reading direction, the left in RTL. *@
<Drawer @bind-Open="_open" SwipeDirection="@(Language.Direction == TextDirection.Rtl ? DrawerSwipeDirection.Left : DrawerSwipeDirection.Right)">
    <DrawerTrigger>

API Reference

Drawer

ParameterTypeDefault
Open bool false
OpenChanged EventCallback<bool> -
OnOpenChange EventCallback<OpenChangeEventArgs> -
OnOpenChangeComplete EventCallback<bool> -
Modal bool true
ShowSwipeHandle bool false
SwipeDirection DrawerSwipeDirection DrawerSwipeDirection.Down
SnapPoints IReadOnlyList<DrawerSnapPoint>? -
SnapPoint DrawerSnapPoint? -
SnapPointChanged EventCallback<DrawerSnapPoint?> -
OnSnapPointChange EventCallback<ValueChangeEventArgs<DrawerSnapPoint?>> -
DefaultSnapPoint DrawerSnapPoint? -
SnapToSequentialPoints bool false
DisablePointerDismissal bool false
ChildContent RenderFragment? -

DrawerClose

ParameterTypeDefault
Render RenderFragment<PopupTriggerRenderProps>? -
NativeButton bool? -
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

DrawerContent

ParameterTypeDefault
Render RenderFragment<DialogPopupRenderProps>? -
InitialFocus string? -
FinalFocus string? -
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

DrawerDescription

ParameterTypeDefault
Render RenderFragment<PopupRenderProps>? -
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

DrawerFooter

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

DrawerHeader

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

DrawerOverlay

ParameterTypeDefault
Class string? -

Other attributes are passed to the element it renders.

DrawerPortal

ParameterTypeDefault
KeepMounted bool false
ChildContent RenderFragment? -

DrawerSwipeHandle

ParameterTypeDefault
Class string? -

Other attributes are passed to the element it renders.

DrawerTitle

ParameterTypeDefault
Render RenderFragment<PopupRenderProps>? -
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

DrawerTrigger

ParameterTypeDefault
Render RenderFragment<PopupTriggerRenderProps>? -
NativeButton bool? -
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.