Mixins / foldkit-mixins-ui

Toast

namespace · packages/mixins-ui/src/toast.ts:26

export * as Toast from './toast.js'

Toasts, in two layers.

The adapter (ToastSlots' container and entry, resolve, toView) styles the fork of @foldkit/ui's Toast (ToastView, ./toastView.ts), whose Model and update stay upstream's, for an application whose toasts hold a payload of its own.

The component (init, show, settle, dismiss, update, view) is a stack after Sonner: toasts of a Type with its icon, a title, a description and an action, newest in front. Collapsed, the ones behind peek out under the front one, smaller; pointing at the stack or focusing into it spreads them out, each at its own measured height. Each counts down its duration (4 seconds by default) only while it is among the max shown (3) and nothing holds the stack: the pointer on it, focus in it, or the page hidden. Holding keeps what is left, as Sonner does, and the count goes on from there. A loading toast waits until settle turns it into the outcome, which is how a promise toast goes from loading to success or error: the application runs the work as a Command and settles the toast with its result. A swipe toward the stack's edge, or the close button, or Escape inside a toast dismisses it; an action dismisses it and sends Acted. What a toast says is announced from two live regions beside the stack, politely, and an error assertively, so a loading toast settling to an error is heard at once.

In the guides

Members

Toast.Action

const · packages/mixins-ui/src/toast.ts:101

const Action: Schema.Struct<{
  readonly label: Schema.String
  readonly value: Schema.String
}>

A button on a toast: its words, and the value Acted sends back.

Toast.configure

function · packages/mixins-ui/src/toast.ts:825

function configure<const Names extends string = never>(
  config: Presets<ToastPreset, Names>,
): {
  readonly view: <Outer>(
    options: ToastViewOptions<Outer> & PresetOption<Names>,
    h: HtmlBuilder<Outer>,
  ) => Html
}

The application's own stack: base for every use, presets chosen at a use with preset, and the call over both. mixins concatenate in that order and words merge word by word; any other option given at the call replaces the preset's.

Toast.dismiss

function · packages/mixins-ui/src/toast.ts:440

function dismiss(model: Model, id: string): Return

The toast id leaving.

Toast.Entry

const · packages/mixins-ui/src/toast.ts:122

const Entry: Schema.Struct<{
  readonly id: Schema.String
  readonly type: Schema.Literals<
    readonly ['default', 'info', 'success', 'warning', 'error', 'loading']
  >
  readonly title: Schema.String
  readonly description: Schema.Option<Schema.String>
  readonly actions: Schema.$Array<
    Schema.Struct<{
      readonly label: Schema.String
      readonly value: Schema.String
    }>
  >
  readonly cancel: Schema.Option<Schema.String>
  readonly duration: Schema.Option<Schema.Number>
  readonly remaining: Schema.Number
  readonly countdown: TaggedUnion<{
    readonly Stopped: {}
    readonly Starting: {}
    readonly Running: { readonly since: Schema.Number }
  }>
  readonly version: Schema.Number
  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 swipe: Schema.Option<
    Schema.Struct<{
      readonly axis: Schema.Literals<readonly ['none', 'x', 'y']>
      readonly x: Schema.Number
      readonly y: Schema.Number
    }>
  >
  readonly swiped: Schema.Boolean
}>

Toast.Hold

const · packages/mixins-ui/src/toast.ts:652

const Hold: Mount.MountDefinitionNoArgs<
  'ToastHold',
  {
    readonly _tag: 'HeldFact'
    readonly by: 'focus' | 'pointer' | 'hidden' | 'touch'
    readonly on: boolean
  }
>

What holds the stack, as a Mount on its container: the mouse, or a finger or a pen, entering and leaving it, focus coming into it (not a tap's) and going out of it, and the page hidden and shown. Opened on a hidden page, it says so at once.

Toast.init

function · packages/mixins-ui/src/toast.ts:213

function init(id: string, options?: InitOptions): Model

An empty stack.

Toast.InitOptions

interface · packages/mixins-ui/src/toast.ts:199

interface InitOptions {
  readonly position?: Position | undefined
  readonly direction?: 'ltr' | 'rtl' | undefined
  readonly duration?: number | undefined
  readonly max?: number | undefined
  readonly threshold?: number | undefined
}

Toast.keepFocus

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

const keepFocus: Mixin<never>[]

Dismissing the toast that holds focus (its close button, its action) hands focus to the same button of the toast behind it, else of the one in front (an action with none to go to, to a close button). It is a mixin beside the style, not on by default: it sends no Message, which a Scene test of a page drawing the stack could not resolve. With no toast left, focus is not moved: recovery never guesses a place outside the stack.

Toast.Message

const · packages/mixins-ui/src/toast.ts:178

const Message: MessageUnion<{
  readonly Dismissed: { readonly id: Schema.String }
  readonly PressedAction: {
    readonly id: Schema.String
    readonly index: Schema.Number
  }
  readonly Held: {
    readonly by: Schema.Literals<
      readonly ['pointer', 'touch', 'focus', 'hidden']
    >
    readonly on: Schema.Boolean
  }
  readonly StartedClock: {
    readonly id: Schema.String
    readonly version: Schema.Number
    readonly at: Schema.Number
  }
  readonly StoppedClock: {
    readonly id: Schema.String
    readonly version: Schema.Number
    readonly elapsed: Schema.Number
  }
  readonly TimedOut: {
    readonly id: Schema.String
    readonly version: Schema.Number
  }
  readonly Swiped: {
    readonly id: Schema.String
    readonly fact: Schema.Union<
      readonly [
        Schema.TaggedStruct<
          'MoveStarted',
          {
            readonly pointerId: Schema.Number
            readonly pointerType: Schema.String
          }
        >,
        Schema.TaggedStruct<
          'Moved',
          {
            readonly pointerId: Schema.Number
            readonly deltaX: Schema.Number
            readonly deltaY: Schema.Number
            readonly pointerType: Schema.String
          }
        >,
        Schema.TaggedStruct<
          'MoveEnded',
          {
            readonly pointerId: Schema.Number
            readonly completed: Schema.Boolean
          }
        >,
      ]
    >
  }
  readonly GotAnimation: {
    readonly id: Schema.String
    readonly message: MessageUnion<{
      readonly Showed: {}
      readonly Hid: {}
      readonly CompletedWaitForPaint: { generation: Schema.Number }
      readonly EndedAnimation: { generation: Schema.Number }
    }>
  }
}>

Toast.Model

const · packages/mixins-ui/src/toast.ts:145

const Model: Schema.Struct<{ readonly id: Schema.String; readonly position: Schema.Literals<readonly ["top-start", "top-center", "top-end", "bottom-start", "bottom-center", "bottom-end"]>; readonly direction: Schema.Literals<readonly ["ltr", "rtl"]>; readonly duration: Schema.Number; readonly max: Schema.Number; readonly threshold: Schema.Number; readonly entries: Schema.$Array<Schema.Struct<{ readonly id: Schema.String; readonly type: Schema.Literals<readonly ["default", "info", "success", "warning", "error", "loading"]>; readonly title: Schema.String; readonly description: Schema.Option<Schema.String>; readonly actions: Schema.$Array<Schema.Struct<{ readonly label: Schema.String; readonly value: Schema.String; }>>; readonly cancel: Schema.Option<Schema.String>; readonly duration: Schema.Option<Schema.Number>; readonly remaining: Schema.Number; readonly countdown: TaggedUnion<{ readonly Stopped: {}; readonly Starting: {}; readonly Running: { readonly since: Schema.Number; }; }>; readonly version: Schema.Number; 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 swipe: Schema.Option<Schema.Struct<{ readonly axis: Schema.Literals<readonly ["none", "x", "y"]>; readonly x: Schema.Number; readonly y: Schema.Number; }>>; readonly swiped: Schema.Boolean; }>>; reado …;

Toast.nextId

function · packages/mixins-ui/src/toast.ts:375

function nextId(model: Model): string

The id the next toast shown will have: for a caller that raises toasts through a Bundle's helpers, which hand back no id, and settles or dismisses one later.

Toast.OutMessage

const · packages/mixins-ui/src/toast.ts:193

const OutMessage: MessageUnion<{
  readonly Acted: { readonly id: Schema.String; readonly value: Schema.String }
}>

Toast.Position

const · packages/mixins-ui/src/toast.ts:90

const Position: Schema.Literals<
  readonly [
    'top-start',
    'top-center',
    'top-end',
    'bottom-start',
    'bottom-center',
    'bottom-end',
  ]
>

Where the stack stands: a corner or the middle of the top or bottom edge.

Toast.resolve

function · packages/mixins-ui/src/toast.ts:58

function resolve<Input, Message>(
  render: ToastRenderInfo,
  mixins: MixinList<Message>,
  context: ResolveContext<Input, Message>,
): ResolvedToast<Message>

Resolves the toast's container and every entry.

Toast.ResolvedToast

type · packages/mixins-ui/src/toast.ts:52

type ResolvedToast<Message> = Omit<ToastRenderInfo, 'container' | 'entries'> & {
  readonly container: SlotAttributes<Message>
  readonly entries: ReadonlyArray<ResolvedToastEntry<Message>>
}

Toast.ResolvedToastEntry

type · packages/mixins-ui/src/toast.ts:48

type ResolvedToastEntry<Message> = Omit<ToastEntryRender, 'attributes'> & {
  readonly attributes: SlotAttributes<Message>
}

Toast.settle

function · packages/mixins-ui/src/toast.ts:411

function settle(model: Model, id: string, input: ToastInput): Return

The toast id says something else and counts down afresh: a loading toast's outcome. A toast already gone, or leaving, is left as it is.

Toast.show

function · packages/mixins-ui/src/toast.ts:378

function show(
  model: Model,
  input: ToastInput,
): Return & {
  readonly id: string
}

A new toast in front of the stack, and its id, for settle and dismiss.

Toast.Stack

const · packages/mixins-ui/src/toast.ts:718

const Stack: Mount.MountDefinitionNoArgs<'ToastStack', never>

The stack's heights, as a Mount on its list: each toast gets --fk-toast-offset, the heights of the toasts in front of it, and the list --fk-toast-front and --fk-toast-total, the front toast's height and all of theirs, for a recipe to stack and spread them. A toast is measured at the height of what it holds, which a collapsed one is clipped to the front's. It measures again as toasts come, go and change size or words.

Toast.ToastInput

interface · packages/mixins-ui/src/toast.ts:229

interface ToastInput {
  readonly type?: Type | undefined
  readonly title: string
  readonly description?: string | undefined
  readonly actions?: ReadonlyArray<Action> | undefined
  readonly cancel?: string | undefined
  readonly duration?: number | 'sticky' | undefined
}

What a toast says, for show and settle.

Toast.ToastPreset

type · packages/mixins-ui/src/toast.ts:814

type ToastPreset = Pick<
  ToastViewOptions<never>,
  'label' | 'words' | 'expand' | 'icons' | 'style'
> & {
  readonly mixins?: MixinList<never>
}

What a toast stack is given once: its look and each option that holds no Message.

Toast.ToastSlots

const · packages/mixins-ui/src/toast/slots.ts:19

const ToastSlots: Slots.Contract<{ readonly container: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly [Attr.AttrToken<"role">], readonly Slot.RequirementName[], boolean>; readonly entry: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly [Attr.AttrToken<"role">], readonly Slot.RequirementName[], boolean>; readonly list: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>; readonly icon: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>; readonly body: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>; readonly title: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Int …;

Toast is a nested Submodel: one live-region container plus an entry per toast. The resolver runs once per bundle and each base attribute passes through by identity.

Entries own hover and (when swipe is configured) pointer handlers, none of which has an Event token — but the resolver still refuses a second owner by event name. The dismiss control is drawn by the consumer's entryToView and is not a slot: its dismiss bundle passes through inside the entry's content.

The component draws the rest: the stack's list, each toast's icon, its body (title and description, where a swipe starts), its actions (each action button, then the cancel one) and close button, and the status and alert live regions.

Toast.ToastViewOptions

interface · packages/mixins-ui/src/toast.ts:785

interface ToastViewOptions<ParentMessage> {
  readonly model: Model
  readonly onMessage: (message: Message) => NoInfer<ParentMessage>
  readonly label?: string | undefined
  readonly words?: Partial<ToastWords> | undefined
  readonly expand?: boolean | undefined
  readonly icons?: Partial<Record<Type, Html>> | undefined
  readonly style?: NamedStyle<typeof ToastSlots> | undefined
  readonly mixins?: MixinList<ParentMessage> | undefined
}

Toast.ToastWords

interface · packages/mixins-ui/src/toast.ts:803

interface ToastWords {
  readonly label: string
  readonly close: string
}

The fixed text a stack draws.

Toast.toView

function · packages/mixins-ui/src/toast.ts:76

function toView<Message>(
  mixins: MixinList<Message>,
  context: ResolveContext<unknown, Message>,
  draw: (resolved: ResolvedToast<Message>) => Html,
): (render: ToastRenderInfo) => Html

The toast's toView: draw receives its bundles with mixins applied.

Toast.Type

const · packages/mixins-ui/src/toast.ts:86

const Type: Schema.Literals<
  readonly ['default', 'info', 'success', 'warning', 'error', 'loading']
>

What a toast says about itself: its icon, and whether it is announced politely.

Toast.update

function · packages/mixins-ui/src/toast.ts:522

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

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

Toast.view

function · packages/mixins-ui/src/toast.ts:843

function view<ParentMessage>(
  options: ToastViewOptions<ParentMessage>,
  h: HtmlBuilder<ParentMessage>,
): Html

Toast.words

const · packages/mixins-ui/src/toast.ts:811

const words = { label, close }

The defaults, in English.