Components

Sheet

A dialog from an edge of the screen whose handle drags it between resting points and away, a drawer as on a phone.

Previews are drawn in the theme you made.

Edit it
size
handle
title
close
motion
scheme
direction

A drawer #

Twelve results near you.

import * as UiDialog from '@foldkit/ui/dialog'
import { SlotView } from 'foldkit-mixins'
import { Bundles, Button, DialogSlots, Recipes, Sheet } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { slots } from '../../snippets/theme.js'

const Primary = Recipes.Button.style({})
const Quiet = Recipes.Button.style({ tone: 'neutral', variant: 'ghost' })
// The application's page, stepped back behind a sheet drawn with `scalesPage`.
const Page = slots({ page: Recipes.sheetPage })

// Opens at half its height (`points: [0.5, 1]`): its handle drags it to the
// next point, and down past the first one away. The page beside it, not
// around it, steps back while it is open. The buttons send the Dialog's own
// Messages, which the Sheet's Message wraps.
const open = Sheet.Message.GotDialog({ message: UiDialog.Message.RequestedOpen() })
const close = Sheet.Message.GotDialog({ message: UiDialog.Message.RequestedClose() })

export default example({
  title: 'A drawer',
  slots: DialogSlots,
  recipe: Recipes.Sheet,
  state: Bundles.Sheet.with({ id: 'filters', side: 'bottom', points: [0.5, 1] }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [h.Style({ display: 'grid', gap: '0.75rem', justifyItems: 'start' })],
      [
        h.div(
          SlotView.buildersFor(Page.slots, [Page.style.mixin], { input: undefined, h }).page.attrs([
            h.Style({ display: 'grid', gap: '0.75rem', justifyItems: 'start' }),
          ]),
          [
            h.p([], ['Twelve results near you.']),
            Button.view({ label: 'Filters', onClick: onMessage(open), style: Quiet }, h),
          ],
        ),
        Sheet.view(
          {
            model,
            onMessage,
            mixins: [style.mixin],
            scalesPage: true,
            draw: sheet =>
              h.dialog(
                sheet.dialog,
                sheet.isVisible
                  ? [
                      h.div(sheet.backdrop),
                      h.div(sheet.panel, [
                        h.div(sheet.handle),
                        h.h2(sheet.title, ['Filters']),
                        h.p([], ['Only what is in stock, nearest first.']),
                        h.p([], ['Drag the handle up for every filter, or down to close.']),
                        Button.view(
                          { label: 'Show results', onClick: onMessage(close), style: Primary },
                          h,
                        ),
                      ]),
                    ]
                  : [],
              ),
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

From the side #

import * as UiDialog from '@foldkit/ui/dialog'
import { Bundles, Button, DialogSlots, Recipes, Sheet } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})
const Quiet = Recipes.Button.style({ tone: 'neutral', variant: 'ghost' })

const open = Sheet.Message.GotDialog({ message: UiDialog.Message.RequestedOpen() })
const close = Sheet.Message.GotDialog({ message: UiDialog.Message.RequestedClose() })

// From the end edge (`side: 'right'`) with no handle: a side panel.
export default example({
  title: 'From the side',
  slots: DialogSlots,
  recipe: Recipes.Sheet,
  state: Bundles.Sheet.with({ id: 'profile', side: 'right' }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [],
      [
        Button.view({ label: 'Edit profile', onClick: onMessage(open), style: Quiet }, h),
        Sheet.view(
          {
            model,
            onMessage,
            mixins: [style.mixin],
            hasDescription: true,
            draw: sheet =>
              h.dialog(
                sheet.dialog,
                sheet.isVisible
                  ? [
                      h.div(sheet.backdrop),
                      h.div(sheet.panel, [
                        h.button(sheet.closeButton, ['✕']),
                        h.h2(sheet.title, ['Edit profile']),
                        h.p(sheet.description, ['Changes show once you save. Escape keeps them.']),
                        h.div(sheet.footer, [
                          Button.view(
                            { label: 'Cancel', onClick: onMessage(close), style: Quiet },
                            h,
                          ),
                          Button.view(
                            { label: 'Save', onClick: onMessage(close), style: Primary },
                            h,
                          ),
                        ]),
                      ]),
                    ]
                  : [],
              ),
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Not dismissible #

import * as UiDialog from '@foldkit/ui/dialog'
import { Bundles, Button, DialogSlots, Recipes, Sheet } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})
const Quiet = Recipes.Button.style({ tone: 'neutral', variant: 'ghost' })

const open = Sheet.Message.GotDialog({ message: UiDialog.Message.RequestedOpen() })
const close = Sheet.Message.GotDialog({ message: UiDialog.Message.RequestedClose() })

// `dismissible: false`: a drag moves it between its points and springs back,
// but never takes it away; only its button closes it.
export default example({
  title: 'Not dismissible',
  slots: DialogSlots,
  recipe: Recipes.Sheet,
  state: Bundles.Sheet.with({ id: 'checkout', points: [0.4, 0.8], dismissible: false }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [],
      [
        Button.view({ label: 'Start checkout', onClick: onMessage(open), style: Quiet }, h),
        Sheet.view(
          {
            model,
            onMessage,
            mixins: [style.mixin],
            draw: sheet =>
              h.dialog(
                sheet.dialog,
                sheet.isVisible
                  ? [
                      h.div(sheet.backdrop),
                      h.div(sheet.panel, [
                        h.div(sheet.handle),
                        h.h2(sheet.title, ['Confirm your order']),
                        h.p([], ['Two items, delivered on Thursday.']),
                        Button.view(
                          { label: 'Place order', onClick: onMessage(close), style: Primary },
                          h,
                        ),
                      ]),
                    ]
                  : [],
              ),
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Nested #

import * as UiDialog from '@foldkit/ui/dialog'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, DialogSlots, Recipes, Sheet } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})
const Quiet = Recipes.Button.style({ tone: 'neutral', variant: 'ghost' })

const open = Sheet.Message.GotDialog({ message: UiDialog.Message.RequestedOpen() })
const close = Sheet.Message.GotDialog({ message: UiDialog.Message.RequestedClose() })

// Two Sheets in two fields. The second is drawn inside the first one's
// `dialog`, beside its panel, as nested dialogs are, and the first steps back
// while it is open (`covered`).
const App = Bundle.compose({}).pipe(
  Bundle.withChild('plans', Bundles.Sheet, {
    args: { id: 'plans' },
    onOut: Bundle.ignore,
  }),
  Bundle.withChild('compare', Bundles.Sheet, {
    args: { id: 'compare', points: [0.6] },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Nested',
  slots: DialogSlots,
  recipe: Recipes.Sheet,
  state: App.placements.runtime({ Model: App.Model, initial: {} }),
  view: ({ style, model, onMessage, h }) => {
    const toPlans = (message: Sheet.Message) => onMessage(App.Message.GotPlansMessage({ message }))
    const toCompare = (message: Sheet.Message) =>
      onMessage(App.Message.GotCompareMessage({ message }))
    const inner = Sheet.view(
      {
        model: model.compare,
        onMessage: toCompare,
        mixins: [style.mixin],
        draw: sheet =>
          h.dialog(
            sheet.dialog,
            sheet.isVisible
              ? [
                  h.div(sheet.backdrop),
                  h.div(sheet.panel, [
                    h.div(sheet.handle),
                    h.h2(sheet.title, ['Compare plans']),
                    h.p([], ['Starter is free; Team is $12 an editor; Business adds sign-on.']),
                    Button.view({ label: 'Back', onClick: toCompare(close), style: Primary }, h),
                  ]),
                ]
              : [],
          ),
      },
      h,
    )
    return h.div(
      [],
      [
        Button.view({ label: 'Choose a plan', onClick: toPlans(open), style: Quiet }, h),
        Sheet.view(
          {
            model: model.plans,
            onMessage: toPlans,
            mixins: [style.mixin],
            covered: Sheet.isOpen(model.compare),
            draw: sheet =>
              h.dialog(
                sheet.dialog,
                sheet.isVisible
                  ? [
                      h.div(sheet.backdrop),
                      h.div(sheet.panel, [
                        h.div(sheet.handle),
                        h.h2(sheet.title, ['Plans']),
                        h.p([], ['Team: ten editors and every page.']),
                        Button.view(
                          { label: 'Compare plans', onClick: toCompare(open), style: Primary },
                          h,
                        ),
                      ]),
                      inner,
                    ]
                  : [],
              ),
          },
          h,
        ),
      ],
    )
  },
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Usage

Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.

The recipe ↗This code ↗

  • Keeping focus when something is removed A control that leaves the page takes its focus with it: say where focus goes next, to a neighbour (FocusRecovery), to the opener (focusReturn) or to a control (keepFocus).