Swipe unstable
Adds swipe-to-dismiss behavior to banners, notifications and list rows.
TrackSwipe writes movement to CSS variables and reports Started, then
either Cancelled or Dismissed with the direction.
Details
The mount decides whether to dismiss on release using distance and speed thresholds. Your styles move the element, and your component removes it after dismissal.
Swiping:
A press with a finger, a pen or the primary mouse button is followed,
unless it starts on a button, a link, a form control or anything with
data-swipe-ignore, the element included.
- At 6px the drag locks to the axis it moved furthest along. When no direction uses that axis, the press is dropped and nothing is reported.
- At the lock the mount reports
Started. - A release in one of the
directionsdismisses once the drag has moveddistancepx, or 10px while still moving atvelocitypx/ms. The mount reportsDismissedwith the direction. - Any other end reports
Cancelled: a release that is too short and too slow or against the directions, a cancelled pointer, a lost pointer capture or the window losing focus. - Changed args restart tracking and cancel any swipe in progress.
What the mount writes:
touch-action, inline, while it runs:nonewhen the directions use both axes,pan-ywhen they are all horizontal andpan-xwhen they are all vertical, so the browser keeps the pan along the axis no direction uses. With no directions, and when the mount ends, the inline value it found is put back.- While dragging, inline
transition: none, and--swipe-movement-xor--swipe-movement-yin px. Movement in a direction that isn't allowed is damped. - On
Cancelled, the movement is removed and the transition restored, so the element goes back with its own transition. - On
Dismissed, the transition is restored, the movement stays anddata-swipe-directionholds the direction, so the exit can continue from where the finger left the element.
Dismissal styles remain until the next swipe starts, even if the mount ends during the exit. Ending the mount resets any swipe still in progress.
The mount doesn't write data-swiping. Render it from Started and the
two ends when a style needs it.
Styling:
The element moves with translate: var(--swipe-movement-x, 0px) var(--swipe-movement-y, 0px) and a transition on translate. For the
exit, style [data-swipe-direction="right"] with
translate: calc(var(--swipe-movement-x) + 100%), and the other
directions alike.
Limits:
- It is for elements whose content doesn't scroll along the swipe axis. A
sheet with scrolling content is a
Drawer. - A swipe never starts from a button, a link or a form control, so a row that is itself a link can't be swiped.
- A dismissal is final: the mount expects the element to leave. To keep the element after all, render it under a new key.
- The movement starts at 0, and either dismisses or goes back. A row that snaps open to show actions isn't covered.
- An exiting element can be grabbed again. Pass
directions: []while it exits to prevent that.
Example Dismissing a banner to either side
import { Mount, UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Swipe } from "effect/ui/kit"import { Effect, Schema } from "effect"
const BannerMessage = UiSchema.TaggedEnum({ Swiped: { event: Swipe.Event }, FinishedTransition: {}})
const Banner = Html.component({ name: "Banner", model: Schema.Struct({ status: Schema.Literals(["shown", "leaving", "gone"]) }), message: BannerMessage}).make({ init: () => ({ status: "shown" }), update: { Swiped: ($, { event }) => event._tag === "Dismissed" ? { status: "leaving" } : $.model, // A cancelled swipe also fires transitionend when it returns. FinishedTransition: ($) => $.model.status === "leaving" ? { status: "gone" } : $.model }, view: (h, model) => model.status === "gone" ? null : h.div.class("banner") .on("transitionend", () => BannerMessage.FinishedTransition()) .onMount( Swipe.TrackSwipe({ directions: model.status === "shown" ? ["left", "right"] : [] }).pipe( Mount.map((event) => BannerMessage.Swiped({ event })) ) ) .children("Saved to your library")})
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Banner, {}))if (!html.includes("class=\"banner\"")) throw new Error("the banner isn't drawn")Models
A direction a swipe can dismiss in.
Signature
type Direction = typeof Direction.TypeWhat TrackSwipe reports about a swipe.
Signature
type Event = typeof Event.TypeThe args of TrackSwipe.
Signature
type TrackSwipeArgs = typeof TrackSwipeArgs.TypeMounts
trackSwipe
unstableReturns a stream that tracks swipes on node while running. Without
changes, it uses fixed args; otherwise, changed args restart tracking
and cancel any swipe in progress.
Details
An element takes one mount. To combine swipe tracking with other behavior,
merge this stream with your own inside a Mount.makeStream body.
Signature
declare function trackSwipe(node: HTMLElement, args: { readonly directions: readonly Array<"left" | "right" | "up" | "down">; readonly distance?: number; readonly velocity?: number;}, changes?: Stream<{ readonly directions: readonly Array<"left" | "right" | "up" | "down">; readonly distance?: number; readonly velocity?: number;}, never, never>): Stream<{ readonly _tag: "Started";} | { readonly _tag: "Cancelled";} | { readonly _tag: "Dismissed"; readonly direction: "left" | "right" | "up" | "down";}>TrackSwipe
unstableTracks swipes on an element and reports Events. Attach it with
onMount and map each event to a component message.
Details
Pointer movement updates inline styles without rendering the component. Changed args restart tracking and cancel any swipe in progress. See the module documentation for styling, dismissal thresholds and limits.
In a Scene, simulate a swipe event with mounted(Swipe.TrackSwipe, event).
Signature
declare const TrackSwipe: Factory<{ readonly directions: readonly Array<"left" | "right" | "up" | "down">; readonly distance?: number; readonly velocity?: number;}, { readonly _tag: "Started";} | { readonly _tag: "Cancelled";} | { readonly _tag: "Dismissed"; readonly direction: "left" | "right" | "up" | "down";}, HTMLElement, never>Schemas
The directions a swipe can dismiss in.
Signature
declare const Direction: Literals<readonly ["up", "down", "left", "right"]>Reports Started when a drag locks to an allowed axis, followed by
Cancelled when the swipe ends without dismissal or Dismissed with
the exit direction.
Signature
declare const Event: Union<readonly [TaggedStruct<"Started", {}>, TaggedStruct<"Cancelled", {}>, TaggedStruct<"Dismissed", { readonly direction: Literals<readonly ["up", "down", "left", "right"]>;}>]>TrackSwipeArgs
unstableOptions for TrackSwipe.
Details
directions: allowed dismissal directions. An empty array disables swiping.distance: dismissal distance in px. Defaults to 40.velocity: dismissal speed in px/ms for drags of at least 10px that fall short ofdistance. Defaults to 0.5.
Signature
declare const TrackSwipeArgs: Struct<{ readonly directions: $Array<Literals<readonly ["up", "down", "left", "right"]>>; readonly distance: optional<Number>; readonly velocity: optional<Number>;}>