Mixins / foldkit-mixins-ui

Sheet

namespace · packages/mixins-ui/src/sheet.ts:37

export * as Sheet from './sheet.js'

A Sheet: a dialog pinned to an edge of the viewport, which its handle drags between resting points and, past the last, away, after vaul's Drawer. It is @foldkit/ui's Dialog (focus trap, Escape, the backdrop's dismissal, scroll lock, stacking) with a small Model of this module's beside it: the edge, the points it rests at, which one it rests at, and the drag running.

handle: Swipe -> Grabbed / Dragged / Released -> update -> point, or close
handle keys: Home / End / arrows -> SnappedTo -> update -> point
view: point -> --fk-sheet-snap; a drag -> --fk-sheet-offset -> the recipe's translate

A point is how much of the panel shows, from 0 to 1, so a point is a share of the panel and the CSS needs no measure; a drag is read against the panel's size, which the handle's Swipe measures as it starts. Let go, a flick (faster than FLICK_SPEED) goes on to the next point its way, or away past the last; a slower release rests at the nearest point, or goes away once dragged CLOSE_SHARE of the way out from the lowest. A drag past fully open is damped, as vaul's is. A press on the handle that barely moves steps to the next point, round to the first.

A finger drags it from anywhere on the panel (swipe.from: 'panel', the default): content that can still scroll the way the finger moves scrolls, and once it is at its end the finger takes the sheet, as vaul's does, so a long sheet scrolls back to its top and then closes. A field, a slider and data-no-swipe start no drag, and resting below its highest point the panel does not scroll, so a swipe up raises it. A mouse drags it by the handle, where text stays selectable; from: 'handle' keeps every pointer there. With more than one point the handle is also a focusable separator whose arrow keys, Home and End step between the points (SnappedTo); with one it is hidden from assistive technology, which has Escape and the sheet's own controls. Reduced motion keeps the drag (it follows the finger) and snaps the slide. How fast a flick is and how far a slow drag takes it away are swipe.flickSpeed and swipe.closeShare.

In the guides

Members

Sheet.close

function · packages/mixins-ui/src/sheet.ts:183

function close(model: Model): Return

Closes the Sheet; a no-op when it is closed.

Sheet.CLOSE_SHARE

const · packages/mixins-ui/src/swipe.ts:14

const CLOSE_SHARE: 0.25

The default closeShare: the share of the panel a slow drag takes it away by (vaul's 0.25).

Sheet.dragged

function · packages/mixins-ui/src/sheet.ts:395

function dragged(
  model: Model,
  drag: {
    readonly size: number
    readonly delta: number
  },
): number

How far in from fully open the panel is drawn during a drag: the drag itself, but damped past fully open as vaul's (8 * (ln(x + 1) - 2)), so the sheet gives a little and resists.

Sheet.FLICK_SPEED

const · packages/mixins-ui/src/swipe.ts:12

const FLICK_SPEED: 0.4

The default flickSpeed: a release faster than this, in pixels a millisecond, is a flick (vaul's 0.4).

Sheet.init

function · packages/mixins-ui/src/sheet.ts:129

function init(config: InitConfig): Model

A closed Sheet. Throws on points that are empty, out of order or out of (0, 1], or an opensAt among none.

Sheet.InitConfig

interface · packages/mixins-ui/src/sheet.ts:111

interface InitConfig {
  readonly id: string
  readonly side?: Side
  readonly points?: ReadonlyArray<number>
  readonly opensAt?: number
  readonly dismissible?: boolean
  readonly isAnimated?: boolean
  readonly swipe?: SwipeConfig
}

Sheet.isOpen

function · packages/mixins-ui/src/sheet.ts:380

function isOpen(model: Model): boolean

Whether the Sheet is open, for a parent's covered or its own reading.

Sheet.machine

const · packages/mixins-ui/src/sheet.ts:230

const machine: Machine.Machine<{ readonly dialog: { readonly id: string; readonly isOpen: boolean; readonly isAnimated: boolean; readonly animation: { readonly id: string; readonly isShowing: boolean; readonly transitionState: "Idle" | "EnterStart" | "EnterAnimating" | "LeaveStart" | "LeaveAnimating"; readonly transitionGeneration: number; }; readonly maybeFocusSelector: Option.Option<string>; }; readonly side: "bottom" | "left" | "right" | "top"; readonly points: readonly number[]; readonly at: number; readonly opensAt: number; readonly dismissible: boolean; readonly swipeFrom: "panel" | "handle"; readonly flickSpeed: number; readonly closeShare: number; readonly drag: Option.Option<{ readonly size: number; readonly delta: number; }>; }, Machine.Arms<{ GotDialog: Transition.Transition<{ readonly dialog: { readonly id: string; readonly isOpen: boolean; readonly isAnimated: boolean; readonly animation: { readonly id: string; readonly isShowing: boolean; readonly transitionState: "Idle" | "EnterStart" | "EnterAnimating" | "LeaveStart" | "LeaveAnimating"; readonly transitionGeneration: number; }; readonly maybeFocusSelector: Option.Option<string>; }; readonly side: "bottom" | "left" | "right" | "top"; readonly points: readonly number[]; readonly at: number; readonly opensAt: number; readonly dismissible: boolean; readonly swipeFrom: "panel" | "handle"; readonly flickSpeed: number; readonly closeShare: number; readonly drag: Option.Option<{ readonly size: number; readonly delta: number; }>; },  …;

The Sheet's declarative state machine: GotDialog, Grabbed, Dragged, Released, and SnappedTo with exact read/write footprints, ready for Machine.lift in an application's composed machine.

Sheet.Message

const · packages/mixins-ui/src/sheet.ts:98

const Message: MessageUnion<{
  readonly GotDialog: {
    readonly message: MessageUnion<{
      readonly RequestedOpen: {}
      readonly RequestedClose: {}
      readonly SucceededShowDialog: {}
      readonly FailedShowDialog: {}
      readonly SucceededAcquireResources: {}
      readonly FailedAcquireResources: {}
      readonly CompletedCloseDialog: {}
      readonly Unmounted: {}
      readonly CompletedReleaseDialogResources: {}
      readonly GotAnimationMessage: {
        message: import('foldkit/message').MessageUnion<{
          readonly Showed: {}
          readonly Hid: {}
          readonly CompletedWaitForPaint: { generation: Schema.Number }
          readonly EndedAnimation: { generation: Schema.Number }
        }>
      }
    }>
  }
  readonly Grabbed: { readonly size: Schema.Number }
  readonly Dragged: { readonly delta: Schema.Number }
  readonly Released: {
    readonly delta: Schema.Number
    readonly velocity: Schema.Number
    readonly completed: Schema.Boolean
  }
  readonly SnappedTo: { readonly at: Schema.Number }
}>

Sheet.Model

const · packages/mixins-ui/src/sheet.ts:73

const Model: Schema.Struct<{
  readonly dialog: Schema.Struct<{
    readonly id: Schema.String
    readonly isOpen: Schema.Boolean
    readonly isAnimated: Schema.Boolean
    readonly animation: Schema.Struct<{
      readonly id: Schema.String
      readonly isShowing: Schema.Boolean
      readonly transitionState: Schema.Literals<
        readonly [
          'Idle',
          'EnterStart',
          'EnterAnimating',
          'LeaveStart',
          'LeaveAnimating',
        ]
      >
      readonly transitionGeneration: Schema.Number
    }>
    readonly maybeFocusSelector: Schema.Option<Schema.String>
  }>
  readonly side: Schema.Literals<readonly ['top', 'right', 'bottom', 'left']>
  readonly points: Schema.$Array<Schema.Number>
  readonly at: Schema.Number
  readonly opensAt: Schema.Number
  readonly dismissible: Schema.Boolean
  readonly swipeFrom: Schema.Literals<readonly ['panel', 'handle']>
  readonly flickSpeed: Schema.Number
  readonly closeShare: Schema.Number
  readonly drag: Schema.Option<
    Schema.Struct<{
      readonly size: Schema.Number
      readonly delta: Schema.Number
    }>
  >
}>

Sheet.open

function · packages/mixins-ui/src/sheet.ts:179

function open(model: Model): Return

Opens the Sheet at its opensAt point.

Sheet.PRESS_SLOP

const · packages/mixins-ui/src/swipe.ts:16

const PRESS_SLOP: 4

A press on the handle that moves less than this, in pixels, is a press, not a drag.

Sheet.Return

type · packages/mixins-ui/src/sheet.ts:154

type Return = ReturnWithOutMessage<Model, Message, UiDialog.OutMessage>

The Sheet's step: its Model, its Commands, and the Dialog's Opened or Closed.

Sheet.settle

function · packages/mixins-ui/src/sheet.ts:194

function settle(release: {
  readonly points: ReadonlyArray<number>
  readonly at: number
  readonly size: number
  readonly delta: number
  readonly velocity: number
  readonly dismissible: boolean
  readonly flickSpeed: number
  readonly closeShare: number
}): Option.Option<number>

Where a release comes to rest: the index of a point, or none to go away. Pure, for the reasons in the module's comment; delta and velocity are toward the edge.

Sheet.SheetView

interface · packages/mixins-ui/src/sheet.ts:405

interface SheetView<Outer> {
  readonly model: Model
  readonly onMessage: (message: Message) => NoInfer<Outer>
  readonly style?: NamedStyle<typeof DialogSlots> | undefined
  readonly mixins?: MixinList<Outer>
  readonly words?: SheetWords | undefined
  readonly hasDescription?: boolean
  readonly covered?: boolean
  readonly scalesPage?: boolean
  readonly draw: (sheet: ResolvedDialog<Outer>) => Html
}

Sheet.SheetWords

interface · packages/mixins-ui/src/sheet.ts:400

interface SheetWords {
  readonly handle?: string | undefined
}

Sheet.Side

const · packages/mixins-ui/src/sheet.ts:70

const Side: Schema.Literals<readonly ['top', 'right', 'bottom', 'left']>

Sheet.SwipeConfig

interface · packages/mixins-ui/src/swipe.ts:19

interface SwipeConfig {
  readonly from?: SwipeFrom
  readonly flickSpeed?: number
  readonly closeShare?: number
}

How a Sheet or a Drawer is swiped; every field has the default its doc names.

Sheet.SwipeFrom

const · packages/mixins-ui/src/swipe.ts:8

const SwipeFrom: Schema.Literals<readonly ['panel', 'handle']>

Where a drag starts: anywhere on the panel for a finger, or the handle alone.

Sheet.swipeOf

function · packages/mixins-ui/src/swipe.ts:29

function swipeOf(
  id: string,
  config?: SwipeConfig,
): { swipeFrom: 'panel' | 'handle'; flickSpeed: number; closeShare: number }

config's fields with their defaults, refusing a speed that is not above 0 or a share outside (0, 1].

Sheet.update

function · packages/mixins-ui/src/sheet.ts:331

function update(model: Model, message: Message): Return

The Sheet after message; the same Model when nothing changes.

Sheet.view

function · packages/mixins-ui/src/sheet.ts:437

function view<Outer>(options: SheetView<Outer>, h: HtmlBuilder<Outer>): Html

The Sheet: @foldkit/ui's Dialog view, its bundles resolved as the Dialog's are, with the drag on the handle and its place on the panel. Draw handle in the panel; a sheet with points draws its content so that what matters shows at the lowest. With more than one point the handle is a focusable separator: its arrow keys (the one that raises it, and the one that lowers it, by the side), Home and End send SnappedTo as a release at that point does, and it does not close the sheet: Escape is the Dialog's.