Drawer unstable
A panel that slides in from an edge of the screen, such as a bottom sheet or a navigation drawer, and is swiped away.
Drawer is a headless component over a Dialog: the Dialog keeps the
portal and the dialog role, focus, dismissal, transitions and nesting, and
the drawer adds the swipe. The consumer owns whether the drawer is open and
where it rests, passes them as open and snapPoint, and stores what
RequestedOpen, RequestedClose and Snapped ask for. A refused close
keeps the drawer open, and a swipe that asked for it slides back.
Anatomy
| Part | Element |
|---|---|
trigger |
<button> that opens the drawer |
swipeArea |
<div> outside the portal that a swipe opens the drawer from |
portal |
<div> in the top layer, around the parts below |
backdrop |
<div> that covers the page |
viewport |
<div> that positions the popup |
popup |
<div> with the drawer's content, which a swipe moves |
content |
<div> inside the popup where a mouse drag selects text |
snapTrigger |
<button> that moves the drawer to its next snap point |
snapStatus |
<div> live region for the current snap point |
title, description, close, initialFocus, finalFocus |
as for a Dialog |
Each part takes the consumer's builder and returns it, as a Dialog's do:
drawer.portal(h.div)?.children(drawer.popup(h.div).children(...)).
swipeArea takes { element, swipeDirection, disabled } and can be drawn
more than once. Its direction is the opposite of the drawer's by default.
Style the popup's position from the variables below, so a swipe moves it:
.popup { transform: translateY(var(--drawer-swipe-movement-y)) }, and
translateY(calc(var(--drawer-snap-point-offset) + var(--drawer-swipe-movement-y)))
with snap points. While a swipe runs the kit sets the popup's inline
transition to none, and puts it back after.
Swipes
A swipe starts from the popup with a mouse, a pen or a finger, in
swipeDirection. With a mouse or a pen, it doesn't start from content or
from a button, a, input, select, textarea, label,
[role="button"], [role="menuitem"], [role="menuitemcheckbox"],
[role="menuitemradio"] or [role="option"]. It also doesn't start from an
element with data-swipe-ignore. data-swipe-ignore="x"
or "y" ignores only touches along that axis. A touch on content that
scrolls scrolls it. One that starts with the content scrolled to its edge
nearest the drawer's, and moves toward the drawer's edge, swipes. A pinch, a selection of text and a swipe
while a nested drawer is open start nothing.
A release dismisses the drawer past half its size or faster than 0.5 px
per ms, and settles it back otherwise. With snapPoints, a release rests
on the nearest snap point, pushed further by a fast release, and emits
Snapped for a new one. Past the lowest, it dismisses.
A swipe on a swipe area emits RequestedOpen with the reason "swipe" once
it moves, so the drawer opens as the swipe starts. Once open is true the
popup follows the finger. A release past half the popup's size, or faster
than 0.1 px per ms, keeps it open, and a shorter one emits RequestedClose
with the reason "swipe". A consumer that refuses or delays the open sees
nothing follow the finger.
Data attributes
On top of the Dialog's. data-swipe-direction holds the direction,
up, down, left or right, and each of the others is present with
an empty value or absent:
popup:data-swiping,data-swipe-direction,data-swipe-dismisswhile a swipe's close runs,data-expandedat the snap point1,data-nested-drawer-openanddata-nested-drawer-swipingbackdrop:data-swipinganddata-swipe-dismissswipeArea:data-open/data-closed,data-disabled,data-swipe-directionanddata-swiping
The popup sets the CSS variables --nested-drawers, --drawer-height,
--drawer-frontmost-height (the height of the frontmost nested drawer),
--drawer-snap-point-offset, --drawer-swipe-movement-x and
--drawer-swipe-movement-y, and --drawer-swipe-strength, from 0.1 to 1:
a faster swipe's close ends sooner, as in
transition-duration: calc(var(--drawer-swipe-strength) * 400ms). The
backdrop sets --drawer-swipe-progress, from 0 when open to 1 when
swiped away, so opacity: calc(1 - var(--drawer-swipe-progress)) fades it.
With snap points it runs from 0 at the tallest snap point to 1 at the
next one, and rests there, so the backdrop shows only while the drawer is
expanded.
Where CSS.registerProperty exists, --drawer-swipe-movement-x,
--drawer-swipe-movement-y and --drawer-swipe-progress don't inherit.
To use one in a descendant, define an alias on the part. For example:
.popup { --movement: var(--drawer-swipe-movement-y) } lets descendants
read var(--movement).
With virtualKeyboard, the viewport sets --drawer-keyboard-inset while
the software keyboard covers part of it: read it as
var(--drawer-keyboard-inset, 0px).
Keyboard
As a Dialog's: as the close starts, focus goes back and the popup
becomes inert. A close that comes during a swipe ends the swipe. With
virtualKeyboard and the viewport part, the viewport follows the
software keyboard: the focused field scrolls into the part of the screen
the keyboard leaves, and a modal drawer keeps the page from scrolling
under it.
With snapPoints, snapTrigger emits Snapped on Enter, Space, click or
tap. It cycles through distinct snap points in height order, wrapping from
tallest to lowest without closing the drawer. Before measurement, it uses
the order of snapPoints instead. The button is disabled for horizontal
drawers or when fewer than two distinct snap points are available.
Give snapTrigger an accessible name and render the current position as
snapStatus's children. This polite live region announces text changes
after a press or swipe; the kit supplies no text. To also describe the
position when the button receives focus, set its aria-describedby to
the status's id.
Limits
- The popup carries the swipe's mount and the viewport the keyboard's, so neither takes a mount of the consumer's.
- Snap points apply to a drawer that swipes
upordown. - A mouse or a pen doesn't swipe from
snapTrigger, as from any button. A finger does, and that swipe doesn't press it. - On the server, and on a host that doesn't run mounts, nothing swipes.
Example A bottom sheet whose open state lives in the consumer's Model
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Drawer, DrawerSnapPoint } from "effect/ui/kit"import { Effect, Schema } from "effect"
const PageMessage = UiSchema.TaggedEnum({ RequestedOpenFilters: {}, RequestedCloseFilters: {}, SnappedFilters: { snapPoint: DrawerSnapPoint }})
const Page = Html.component({ name: "Page", model: Schema.Struct({ open: Schema.Boolean, snapPoint: DrawerSnapPoint }), message: PageMessage}).make({ init: () => ({ open: true, snapPoint: 0.5 }), update: { RequestedOpenFilters: ($) => $.evolve({ open: true }), RequestedCloseFilters: ($) => $.evolve({ open: false }), SnappedFilters: ($, { snapPoint }) => $.evolve({ snapPoint }) }, view: (h, model) => Drawer(h, { props: { open: model.open, snapPoints: [0.5, 1], snapPoint: model.snapPoint }, onEmit: { RequestedOpen: () => PageMessage.RequestedOpenFilters(), RequestedClose: () => PageMessage.RequestedCloseFilters(), Settled: () => undefined, Snapped: ({ snapPoint }) => PageMessage.SnappedFilters({ snapPoint }) }, view: (drawer) => h.div( drawer.trigger(h.button).children("Filters"), drawer.portal(h.div)?.children( drawer.backdrop(h.div.class("backdrop")), drawer.viewport(h.div.class("viewport")).children( drawer.popup(h.div.class("sheet")).children( drawer.snapTrigger(h.button.class("grabber")).attr("aria-label", "Resize filters"), drawer.snapStatus(h.div.class("sr-only")).children( model.snapPoint === 1 ? "Full height" : "Half height" ), drawer.title(h.h2).children("Filters"), drawer.content(h.div).children("Pick the filters to apply."), drawer.close(h.button).children("Done") ) ) ) ) })})
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Page, {}))if (!html.includes(`data-swipe-direction="down"`) || !html.includes(`class="sheet"`)) { throw new Error("the sheet isn't a drawer")}if (!html.includes(`role="status"`) || !html.includes("Half height")) { throw new Error("the sheet doesn't announce its height")}Components
A headless drawer: a Dialog that slides in from an edge of the screen
and is swiped away. Call it as a Dialog, with the consumer's h, a key
when it needs one, props, onEmit and a view closure that draws it
from its parts.
Details
The parts stack on the Dialog's: drawer.popup, drawer.backdrop and
drawer.viewport add the drawer's attributes and mounts to the
Dialog's, and drawer.content, drawer.swipeArea, drawer.snapTrigger
and drawer.snapStatus are its own. The popup carries the swipe's mount,
so it can't take one of the consumer's.
The drawer owns its Dialog and emits what it emits, so a Scene of the
consumer gives childEmit(Drawer, event) every event. A Scene of the
Drawer stands in for the Dialog with childEmit(Dialog, event).
History restore saves nothing of it: after Back, forward or a reload it
starts from init.
Signature
declare const Drawer: Headless<{ readonly disablePointerDismissal?: boolean; readonly finalFocus?: boolean; readonly forceRenderBackdrop?: boolean; readonly holdExit?: boolean; readonly id?: string; readonly initialFocus?: boolean; readonly keepMounted?: boolean; readonly modal?: boolean | "trap-focus"; readonly nested?: boolean; readonly open: boolean; readonly openMethod?: "mouse" | "touch" | "pen" | "keyboard" | null; readonly snapPoint?: string | number | null; readonly snapPoints?: readonly Array<string | number>; readonly snapToSequentialPoints?: boolean; readonly swipeDirection?: "left" | "right" | "up" | "down"; readonly topLayer?: boolean; readonly triggerId?: string | null; readonly virtualKeyboard?: boolean;}, { readonly _tag: "Settled"; readonly open: boolean;} | { readonly _tag: "RequestedOpen"; readonly reason: "trigger-press" | "swipe"; readonly triggerId: string | null;} | { readonly _tag: "RequestedClose"; readonly reason: "trigger-press" | "outside-press" | "escape-key" | "close-press" | "focus-out" | "close-watcher" | "swipe";} | { readonly _tag: "Snapped"; readonly snapPoint: string | number;}, { backdrop: <B extends PartTarget<"div">>(element: B) => ReturnType<ReturnType<B["attr"]> & {} | null["attr"]> | undefined; close: Part<"button", [], never>; closeMethod: "mouse" | "touch" | "pen" | "keyboard" | null; content: Part<"div", [], never>; description: <B extends PartTarget<"p">>(element: B) => ReturnType<B["attr"]> & Pick<Part<"p", [id: string], never>, "~R">; expanded: boolean; finalFocus: Part<string, [], never>; initialFocus: Part<string, [], never>; mounted: boolean; nested: boolean; nestedDialogs: number; nestedDrawers: number; nestedDrawerSwiping: boolean; open: boolean; openMethod: "mouse" | "touch" | "pen" | "keyboard" | null; popup: <B extends PartTarget<"div">>(element: B) => ReturnType<ReturnType<B["attr"]>["attr"]>; portal: <B extends PartTarget<"div">>(element: B) => ReturnType<B["attr"]> | undefined & Pick<Part<"div", [], never>, "~R">; snapStatus: Part<"div", [], never>; snapTrigger: Part<"button", [], never>; swipeArea: <B extends PartTarget<"div">>(options: { readonly disabled?: boolean; readonly element: B; readonly swipeDirection?: "left" | "right" | "up" | "down"; }) => ReturnType<B["attr"]> & Pick<Part<"div", [key: string, direction: "left" | "right" | "up" | "down", disabled: boolean], never>, "~R">; swipeDirection: "left" | "right" | "up" | "down"; swiping: boolean; title: <B extends PartTarget<"h1" | "h2" | "h3" | "h4" | "h5" | "h6">>(element: B) => ReturnType<B["attr"]> & Pick<Part<"h1" | "h2" | "h3" | "h4" | "h5" | "h6", [id: string], never>, "~R">; transitionStatus: DialogTransitionStatus; trigger: <B extends PartTarget<"button">>(element: B) => ReturnType<B["attr"]> & Pick<Part<"button", [triggerId: string | null, key: string, disabled: boolean], never>, "~R">; triggerId: string | null; viewport: <B extends PartTarget<"div">>(element: B) => ReturnType<ReturnType<B["attr"]>["attr"]>;}, never, never, Shared<"Dialog"> | Shared<"Drawer">, { open: boolean;}, { readonly count: number; readonly height: number; readonly swiping: boolean;}, never, { readonly area: string | null; readonly dismissed: boolean; readonly fontSize: number; readonly height: number; readonly nestedHeight: number; readonly nestedOpen: number; readonly nestedSwiping: boolean; readonly releases: number; readonly strength: number | null; readonly swiping: boolean; readonly viewport: number;}, { readonly _tag: "ShowedPopup";} | { readonly _tag: "ChangedNested"; readonly count: number; readonly height: number; readonly swiping: boolean;} | { readonly _tag: "StartedSwipe";} | { readonly _tag: "CancelledSwipe";} | { readonly _tag: "RefusedSwipe";} | { readonly _tag: "Measured"; readonly fontSize: number; readonly height: number; readonly viewport: number;} | { readonly _tag: "StartedAreaSwipe"; readonly key: string;} | { readonly _tag: "ReleasedAreaSwipe"; readonly displacement: number; readonly size: number | null; readonly velocity: number;} | { readonly _tag: "CancelledAreaSwipe";} | { readonly _tag: "PressedSnapTrigger";} | { readonly _tag: "RequestedOpenDialog"; readonly triggerId: string | null;} | { readonly _tag: "RequestedCloseDialog"; readonly reason: "trigger-press" | "outside-press" | "escape-key" | "close-press" | "focus-out" | "close-watcher";} | { readonly _tag: "SettledDialog"; readonly open: boolean;} | { readonly _tag: "ReleasedSwipe"; readonly deltaX: number; readonly deltaY: number; readonly direction: "left" | "right" | "up" | "down" | null; readonly height: number; readonly releaseVelocityX: number; readonly releaseVelocityY: number; readonly reversed: boolean; readonly velocityX: number; readonly velocityY: number; readonly width: number;}, QueryEntries<{}, {}>>DrawerProvider
unstableScales the page behind the drawers below it, as a native sheet does.
Headless: call it with the consumer's h and a view closure that draws
the page with drawerProvider.indent.
Details
indent: a<div>around the page. It getsdata-activewhile a drawer below the provider is open,data-inactiveotherwise, and a resting--drawer-swipe-progress: 0, or the backdrop's while an open drawer rests at a lower snap point. While a drawer is swiped, its--drawer-swipe-progressand--drawer-height, the height of the frontmost drawer, follow the finger. Give the element itsidon the builder passed in, if it needs one.indentBackground: a<div>behind the indent, with the samedata-activeanddata-inactive.active: whether a drawer below the provider is open.
A drawer reports to the nearest provider above it. One without a provider works as before.
History restore saves nothing of it: after Back, forward or a reload it
starts from init.
Signature
declare const DrawerProvider: Headless<{}, never, { active: boolean; indent: <B extends PartTarget<"div">>(element: B) => ReturnType<B["attr"]> & Pick<Part<"div", [id: string], never>, "~R">; indentBackground: Part<"div", [], never>;}, never, never, Shared<"DrawerProvider">, { indent: string | null;}, boolean, never, { readonly indent: string | null;}, { readonly _tag: "MountedIndent"; readonly id: string;}, QueryEntries<{}, {}>>Models
Why a drawer asks to close.
Signature
type DrawerDismissReason = typeof DrawerDismissReason.TypeWhat a Drawer emits.
Signature
type DrawerEvents = typeof DrawerEvents.TypeWhy a drawer asks to open.
Signature
type DrawerOpenReason = typeof DrawerOpenReason.TypeThe props of a Drawer.
Signature
type DrawerProps = typeof DrawerProps.TypeThe props of a DrawerProvider.
Signature
type DrawerProviderProps = typeof DrawerProviderProps.TypeHow much of a drawer shows at a snap point.
Signature
type DrawerSnapPoint = typeof DrawerSnapPoint.TypeThe direction a swipe dismisses a drawer in.
Signature
type DrawerSwipeDirection = typeof DrawerSwipeDirection.TypeSchemas
DrawerDismissReason
unstableWhy a drawer asks to close: a Dialog's reasons, and swipe.
Signature
declare const DrawerDismissReason: Literals<readonly ["trigger-press", "outside-press", "escape-key", "close-press", "focus-out", "close-watcher", "swipe"]>DrawerEvents
unstableRequestedOpen asks for the drawer to open, with the trigger's id and
the reason. A swipe on a swipe area has no trigger, so its triggerId is
null. RequestedClose asks for it to close, with the reason.
Settled reports that the transitions of an open or a close finished, as
a Dialog's does. Snapped asks for the drawer to rest at another snap
point after a swipe or a press on snapTrigger. The consumer decides
whether to change open and snapPoint.
Signature
declare const DrawerEvents: TaggedEnum<{ RequestedClose: TaggedStruct<"RequestedClose", { readonly reason: Literals<readonly ["trigger-press", "outside-press", "escape-key", "close-press", "focus-out", "close-watcher", "swipe"]>; }>; RequestedOpen: TaggedStruct<"RequestedOpen", { readonly reason: Literals<readonly ["trigger-press", "swipe"]>; readonly triggerId: NullOr<String>; }>; Settled: TaggedStruct<"Settled", { readonly open: Boolean; }>; Snapped: TaggedStruct<"Snapped", { readonly snapPoint: Union<readonly [Number, String]>; }>;}>DrawerOpenReason
unstableWhy a drawer asks to open: a press on a trigger, or a swipe on a swipe area.
Signature
declare const DrawerOpenReason: Literals<readonly ["trigger-press", "swipe"]>DrawerProps
unstableA Dialog's props, and how the drawer is swiped.
Details
open,modal,disablePointerDismissal,triggerId,keepMounted,forceRenderBackdrop,initialFocus,finalFocus,id,openMethod,holdExitandtopLayer: as for aDialog. WithtopLayer: falseandmodal: false, the drawer slides in from an edge of the element its viewport fills, such as a card withposition: relativeandoverflow: hidden, and a fractional snap point is a fraction of that viewport's height.swipeDirection: the direction a swipe dismisses the drawer in,"down"by default.snapPoints: the heights the drawer can rest at, from the lowest to the tallest. Only a drawer that swipes"up"or"down"uses them.snapPoint: the snap point the drawer rests at, controlled likeopen. Store whatSnappedreports. Left out ornull, it is the first one.snapToSequentialPoints: disables velocity-based snap skipping so drag distance determines the next snap point (a long drag can still cross multiple points).virtualKeyboard: the viewport follows the software keyboard, as the module describes. It needs theviewportpart.
Signature
declare const DrawerProps: Struct<{ readonly disablePointerDismissal: optional<Boolean>; readonly finalFocus: optional<Boolean>; readonly forceRenderBackdrop: optional<Boolean>; readonly holdExit: optional<Boolean>; readonly id: optional<String>; readonly initialFocus: optional<Boolean>; readonly keepMounted: optional<Boolean>; readonly modal: optional<Union<readonly [Boolean, Literal<"trap-focus">]>>; readonly nested: optional<Boolean>; readonly open: Boolean; readonly openMethod: optional<NullOr<Literals<readonly ["mouse", "touch", "pen", "keyboard"]>>>; readonly snapPoint: optional<NullOr<Union<readonly [Number, String]>>>; readonly snapPoints: optional<$Array<Union<readonly [Number, String]>>>; readonly snapToSequentialPoints: optional<Boolean>; readonly swipeDirection: optional<Literals<readonly ["up", "down", "left", "right"]>>; readonly topLayer: optional<Boolean>; readonly triggerId: optional<NullOr<String>>; readonly virtualKeyboard: optional<Boolean>;}>DrawerProviderProps
unstableThe props of a DrawerProvider: none.
Signature
declare const DrawerProviderProps: Struct<{}>DrawerSnapPoint
unstableHow much of a drawer shows at a snap point: a number up to 1 is a
fraction of the viewport's height, a larger number is pixels, and a string
is a length in px or rem, such as "320px" or "20rem".
Signature
declare const DrawerSnapPoint: Union<readonly [Number, String]>DrawerSwipeDirection
unstableThe direction a swipe dismisses a drawer in, which is also the edge it
sits on: "down" for a bottom sheet, "left" for a drawer on the left.
Signature
declare const DrawerSwipeDirection: Literals<readonly ["up", "down", "left", "right"]>