Fragiola

API: components

Drawer

A modal panel at an edge of the screen that the user swipes away, with snap points and nested drawers, in each framework.

A panel that slides in from an edge of the screen: a bottom sheet, a side panel. It is a Dialog: it takes focus, traps it, makes the rest of the page inert, closes on Escape and on a press outside, and returns focus when it closes (the WAI-ARIA APG modal dialog pattern). It adds a swipe to dismiss with the mouse, a pen or a touch, snap points (a bottom sheet resting at preset heights), the CSS variables your styles move it with, and nested drawers. The APG has no drawer pattern: the gestures, the snap points and the names follow Base UI's Drawer.

Drawer has its own entry and is built on Dialog's parts, whose part attribute names the drawer (drawer-popup), plus its own Content. The swipe is the toolkit's @fragiola/primitives/swipe entry, which only Drawer imports: an app with Dialog alone never bundles it.

Share sheet
Gallery
Cart panel
Gallery

Behaviour

  • Open and close: as a Dialog's. Focus moves to the first tabbable element, Tab wraps inside, Escape (escape-key), a press outside (outside-press), a Close part (close-press) or the Trigger again (trigger-press) close it, focus returns to the trigger.
  • Swipe: a press on the Popup followed by a move of 6 px along the axis of swipeDirection starts a swipe (data-swiping); a move along the other axis does nothing. Towards the edge the popup follows the pointer; away from it, it moves less and less (damped). On release it dismisses (reason swipe) when it moved more than half its size, or 10 px or more at a flick's velocity (0.5 px per ms); otherwise it settles back.
  • Where a swipe starts: never on a text control, a range input or an element marked data-swipe-ignore; with the mouse, never on a button, a link or a label, nor inside the Content (where the mouse selects text). A touch swipes from anywhere else.
  • Scroll containment: a scrollable element under the pointer that can still scroll in the direction of the move scrolls instead; the drawer moves once it reached its edge.
  • Snap points: with snapPoints, a bottom sheet rests at heights you give (fractions of the viewport, px or rem); a release settles on the closest snap point to where it was let go (a flick carries it further), or dismisses when closed is closer. onSnapPointChange reports the new snap point with reason swipe.
  • Nested drawers: a Drawer inside a Drawer's popup is nested: data-nested on it, data-nested-drawer-open and --nested-drawers on the parent; a swipe or Escape closes the nested one alone (nested overlays).
  • Detached triggers and lifecycle: as a Dialog's, with a Drawer handle (Drawer.createHandle()).

Anatomy

partelementdata-primitives-partrole
Rootnoneholds the state, the swipe direction and the snap points
Triggerbuttondrawer-triggeropens the drawer
Portalnonemoves the Backdrop, the Viewport and the Popup to the end of body
Backdropdivdrawer-backdropcovers the page (optional)
Viewportdivdrawer-viewporta full-page container you place the popup at its edge in (optional)
Popupdivdrawer-popupthe dialog; the swipe starts on it
Contentdivdrawer-contentwhere the mouse selects text instead of swiping (optional)
Titleh2drawer-titlenames it
Descriptionpdrawer-descriptiondescribes it
Closebuttondrawer-closecloses it

Usage

import { Drawer } from "@fragiola/primitives-react/drawer";

function Filters() {
    return (
        <Drawer.Root swipeDirection="down" snapPoints={["240px", 1]}>
            <Drawer.Trigger>Filters</Drawer.Trigger>
            <Drawer.Portal>
                <Drawer.Backdrop />
                <Drawer.Popup>
                    <div aria-hidden="true" className="handle" />
                    <Drawer.Content>
                        <Drawer.Title>Filters</Drawer.Title>
                        <Drawer.Description>Narrow the results.</Drawer.Description>
                        <Drawer.Close>Done</Drawer.Close>
                    </Drawer.Content>
                </Drawer.Popup>
            </Drawer.Portal>
        </Drawer.Root>
    );
}

Move the popup with the variables in your styles:

[data-primitives-part="drawer-popup"][data-swipe-direction="down"] {
    transform: translateY(calc(var(--drawer-snap-point-offset) + var(--drawer-swipe-movement-y)));
    transition: transform 300ms;
}
[data-primitives-part="drawer-popup"][data-swiping] { transition: none; }
[data-primitives-part="drawer-popup"][data-starting-style],
[data-primitives-part="drawer-popup"][data-ending-style] { transform: translateY(100%); }

Props

Every part also takes what the primitive contract gives. The names below are React's; Vue binds them the same way (v-model:open, v-model:snap-point, @open-change), and Angular as inputs and outputs ([(open)], [(snapPoint)], (openChangeDetails)). A drawer is always modal.

Root

proptypedefaultdescription
openbooleanthe open state, controlled
defaultOpenbooleanfalsethe initial open state, uncontrolled
onOpenChange(open: boolean, details: OpenChangeDetails) => voidevery change, with its reason (swipe for a swipe, and Dialog's); called before the change, details.cancel() stops it
onOpenChangeComplete(open: boolean) => voidonce an opening or a closing has finished its transitions
actionsRef (react){ current: OverlayActions | null }receives close() (reason imperative-action) and unmount(); Vue exposes them on the Root's template ref, Angular as the root directive's methods
handleDrawerHandle<Payload>connects Triggers rendered outside the Root (detached triggers)
triggerIdstring | nullthe active trigger's id, controlled together with open
defaultTriggerIdstring | nullnullthe initial active trigger, uncontrolled
swipeDirection"up" | "down" | "left" | "right""down"the edge a swipe dismisses it towards
snapPointsSnapPoint[]the heights it rests at: a fraction of the viewport (0 to 1), px (a number above 1, or "148px"), or "30rem"; vertical directions only
snapPointSnapPoint | nullthe active snap point, controlled
defaultSnapPointSnapPoint | nullthe first snap pointthe snap point it opens on, uncontrolled
onSnapPointChange(snapPoint: SnapPoint | null, details: ChangeDetails) => voida swipe settled on another snap point (swipe), or an opening went back to the default one (none)

Trigger

proptypedefaultdescription
disabledbooleanfalsea disabled trigger opens nothing
handleDrawerHandle<Payload>the handle of a Root this Trigger opens from anywhere, outside it
payloadPayloadwhat the Root's content receives while this Trigger is the active one
idstringgeneratedthe Trigger's id: triggerId and the handle's open(id) name it

Portal

proptypedefaultdescription
container (react, vue)Elementdocument.bodywhere the Backdrop, the Viewport and the Popup go
keepMounted (react, vue)booleanfalsekeep the content rendered while closed, hidden and data-closed
options (angular)Element | { container?, keepMounted? }{}the same two options: *drawerPortal="{ keepMounted: true }"

Backdrop

proptypedefaultdescription
forceRenderbooleanfalserender the Backdrop of a nested drawer too

Viewport

No props of its own.

proptypedefaultdescription
initialFocusan element, a ref, or falsethe first tabbable elementfocused on open (false: the popup itself); Vue and Angular also take a getter
finalFocusan element, a ref, or falsethe triggerfocused on close (false: nothing moves)

Content

No props of its own.

Title

proptypedefaultdescription
id (angular)stringgeneratedthe id the popup's aria-labelledby points at; React and Vue take it as a plain attribute

Description

proptypedefaultdescription
id (angular)stringgeneratedthe id the popup's aria-describedby points at; React and Vue take it as a plain attribute

Close

No props of its own.

State and part attributes

partattributes
Triggeraria-haspopup="dialog", aria-expanded, aria-controls, data-popup-open (the active trigger), data-disabled, an id
Backdroparia-hidden="true", data-open / data-closed, data-starting-style, data-ending-style, data-nested, data-nested-drawer-open, data-swiping, the inline --drawer-swipe-progress
Viewportrole="presentation", data-open / data-closed, data-starting-style, data-ending-style, data-nested, data-nested-drawer-open, data-swiping
Popuprole="dialog", aria-modal="true", aria-labelledby, aria-describedby, tabindex="-1", data-open / data-closed, data-starting-style, data-ending-style, data-nested, data-nested-drawer-open, data-swipe-direction, data-swiping, data-swipe-dismiss (exiting after a swipe), data-expanded (all of it shown); the inline --nested-drawers, --drawer-swipe-movement-x, --drawer-swipe-movement-y, --drawer-snap-point-offset and --drawer-swipe-progress
Title, Descriptionan id (generated unless given)

Keyboard

keyaction
Enter, Space on the Triggeropens
Tab, Shift+Tabthe next or previous tabbable element of the popup, wrapping
Escapecloses (only the topmost layer, when one is open inside)

Snap points have no keys: the keyboard reaches all of the content and closes the drawer.

Exports

entryexports
@fragiola/primitives-react/drawerDrawer, DrawerRootProps, DrawerTriggerProps, DrawerTriggerState, DrawerPortalProps, DrawerBackdropProps, DrawerBackdropState, DrawerViewportState, DrawerPopupProps, DrawerPopupState, DrawerHandle, createDrawerHandle, SnapPoint, ResolvedSnapPoint, SwipeDirection, OverlayRootProps, OverlayTriggerRootProps, OverlayTriggerProps, OverlayPortalProps, OverlayHandle, OverlayActions, OpenChangeDetails, FocusTarget, ChangeDetails, ChangeReason, RenderProp
@fragiola/primitives-vue/drawerDrawer, DrawerRootProps, DrawerRootEmits, DrawerTriggerProps, DrawerTriggerState, DrawerPortalProps, DrawerBackdropProps, DrawerBackdropState, DrawerViewportState, DrawerPopupProps, DrawerPopupState, DrawerHandle, createDrawerHandle, SnapPoint, ResolvedSnapPoint, SwipeDirection, OverlayRootProps, OverlayRootEmits, OverlayRootSlots, OverlayTriggerRootProps, OverlayTriggerProps, OverlayPortalProps, OverlayHandle, OverlayActions, OpenChangeDetails, FocusTarget, ChangeDetails, ChangeReason, RenderSlotProps
@fragiola/primitives-angular/drawerDrawerRoot, DrawerTrigger, DrawerPortal, DrawerBackdrop, DrawerViewport, DrawerPopup, DrawerContent, DrawerTitle, DrawerDescription, DrawerClose, DrawerHandle, createDrawerHandle, SnapPoint, ResolvedSnapPoint, SwipeDirection, OverlayHandle, OverlayActions, OverlayPortalOptions, OpenChangeDetails, OpenChangeEvent, ValueChangeEvent, FocusTarget, ChangeDetails, ChangeReason, DrawerBackdropState, DrawerPopupState, DrawerState, DrawerSwipeState, DrawerTriggerState, DrawerViewportState