Components

Picker

One control that is a dropdown on a desktop and a bottom sheet on a phone, with one Model and one Message for a choice made either way, in groups, with a clear button.

Previews are drawn in the theme you made.

Edit it
sheetBelow
trigger
size
scheme
direction

Sort order #

import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Picker, PickerSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { PickerMenu, PickerSheet } from '../../snippets/picker.js'

const sorts = ['relevance', 'price', 'name'] as const
const Sort = Picker.one(sorts)

// One control, one Model and one set of Messages at every width: a dropdown
// above the theme's `md` breakpoint, and below it an opener that shows the
// options in a bottom sheet. Both are drawn and the style shows one, so a
// server render is right at any width; narrow the window to see the sheet.
const App = Bundle.compose({ sort: Schema.Option(Schema.Literals(sorts)) }).pipe(
  Bundle.withChild('box', Bundles.Picker, {
    args: { id: 'sort' },
    onOut: out => model => ({ model: { ...model, sort: Sort.picked(model.sort, out) } }),
  }),
)

export default example({
  title: 'Sort order',
  slots: PickerSlots,
  recipe: Recipes.Picker,
  state: App.placements.runtime({ Model: App.Model, initial: { sort: Option.none() } }),
  view: ({ style, model, onMessage, h }) =>
    Picker.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Sort',
        choice: Sort,
        value: model.sort,
        option: sort =>
          ({
            relevance: { label: 'Most relevant' },
            price: { label: 'Lowest price' },
            name: { label: 'Name' },
          })[sort],
        placeholder: 'Sort by',
        style,
        listbox: [PickerMenu.mixin],
        sheet: [PickerSheet.mixin],
      },
      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.

Icons and descriptions #

import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Picker, PickerSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { PickerMenu, PickerSheet } from '../../snippets/picker.js'

const sorts = ['relevance', 'price', 'name'] as const
const Sort = Picker.one(sorts)
const rows = {
  relevance: { label: 'Most relevant', note: 'What matches best', glyph: '★' },
  price: { label: 'Lowest price', note: 'The cheapest first', glyph: '£' },
  name: { label: 'Name', note: 'A to Z', glyph: 'A' },
}

const App = Bundle.compose({ sort: Schema.Option(Schema.Literals(sorts)) }).pipe(
  Bundle.withChild('box', Bundles.Picker, {
    args: { id: 'sort-icons' },
    onOut: out => model => ({ model: { ...model, sort: Sort.picked(model.sort, out) } }),
  }),
)

// A row's `icon` and `description` are drawn in the dropdown and in the sheet
// alike, and the icon on the trigger.
export default example({
  title: 'Icons and descriptions',
  slots: PickerSlots,
  recipe: Recipes.Picker,
  state: App.placements.runtime({ Model: App.Model, initial: { sort: Option.some('price') } }),
  view: ({ style, model, onMessage, h }) =>
    Picker.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Sort',
        choice: Sort,
        value: model.sort,
        option: sort => ({
          label: rows[sort].label,
          description: rows[sort].note,
          icon: h.span([], [rows[sort].glyph]),
        }),
        placeholder: 'Sort by',
        style,
        listbox: [PickerMenu.mixin],
        sheet: [PickerSheet.mixin],
      },
      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.

Groups #

import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Picker, PickerSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { PickerMenu, PickerSheet } from '../../snippets/picker.js'

const sorts = ['relevance', 'newest', 'price', 'name'] as const
const Sort = Picker.one(sorts)

const App = Bundle.compose({ sort: Schema.Option(Schema.Literals(sorts)) }).pipe(
  Bundle.withChild('box', Bundles.Picker, {
    args: { id: 'sort-groups' },
    onOut: out => model => ({ model: { ...model, sort: Sort.picked(model.sort, out) } }),
  }),
)

// Rows next to each other with one group name stand under its heading, in
// the dropdown and in the sheet.
export default example({
  title: 'Groups',
  slots: PickerSlots,
  recipe: Recipes.Picker,
  state: App.placements.runtime({ Model: App.Model, initial: { sort: Option.none() } }),
  view: ({ style, model, onMessage, h }) =>
    Picker.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Sort',
        choice: Sort,
        value: model.sort,
        groupBy: sort => (sort === 'price' || sort === 'name' ? 'Field' : 'Smart'),
        heading: group => h.span([], [group]),
        placeholder: 'Sort by',
        style,
        listbox: [PickerMenu.mixin],
        sheet: [PickerSheet.mixin],
      },
      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.

A clear button #

import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { modifyFields } from 'foldkit/struct'
import { Bundles, ChoiceChange, Picker, PickerSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { PickerMenu, PickerSheet } from '../../snippets/picker.js'

const sorts = ['relevance', 'price', 'name'] as const
const Sort = Picker.one(sorts)

// The value is the application's, so the clear button sends the
// application's own Message, and the application empties its field through
// the same `Choice` that folds a pick. It is drawn beside the dropdown's
// trigger and beside the phone's opener.
const App = Bundle.compose({ sort: Schema.Option(Schema.Literals(sorts)) }).pipe(
  Bundle.withMessages({ Cleared: {} }),
  Bundle.withChild('box', Bundles.Picker, {
    args: { id: 'sort-clear' },
    onOut: out => model => ({ model: { ...model, sort: Sort.picked(model.sort, out) } }),
  }),
)

export default example({
  title: 'A clear button',
  slots: PickerSlots,
  recipe: Recipes.Picker,
  state: App.placements.runtime({
    Model: App.Model,
    initial: { sort: Option.some('name') },
    update: App.placements.update(model => ({
      model: modifyFields(model, {
        sort: sort => Sort.picked(sort, ChoiceChange.ClearedSelection()),
      }),
    })),
  }),
  view: ({ style, model, onMessage, h }) =>
    Picker.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Sort',
        choice: Sort,
        value: model.sort,
        placeholder: 'Sort by',
        clear: { onClear: onMessage(App.Message.Cleared()) },
        style,
        listbox: [PickerMenu.mixin],
        sheet: [PickerSheet.mixin],
      },
      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.

Invalid and disabled #

import { Option } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Picker, PickerSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'
import { PickerMenu, PickerSheet } from '../../snippets/picker.js'

const sorts = ['relevance', 'price', 'name'] as const
const Sort = Picker.one(sorts)

const price: Option.Option<(typeof sorts)[number]> = Option.some('price')

// Each Picker is placed once: its own open state and its own Sheet.
const App = Bundle.compose({}).pipe(
  Bundle.withChild('invalid', Bundles.Picker, {
    args: { id: 'sort-invalid' },
    onOut: Bundle.ignore,
  }),
  Bundle.withChild('off', Bundles.Picker, { args: { id: 'sort-off' }, onOut: Bundle.ignore }),
)

export default example({
  title: 'Invalid and disabled',
  slots: PickerSlots,
  recipe: Recipes.Picker,
  state: App.placements.runtime({ Model: App.Model, initial: {} }),
  view: ({ style, model, onMessage, h }) =>
    stack(h, [
      Picker.view(
        {
          model: model.invalid,
          onMessage: message => onMessage(App.Message.GotInvalidMessage({ message })),
          label: 'Sort',
          choice: Sort,
          value: Option.none(),
          placeholder: 'Sort by: required',
          invalid: true,
          style,
          listbox: [PickerMenu.mixin],
          sheet: [PickerSheet.mixin],
        },
        h,
      ),
      Picker.view(
        {
          model: model.off,
          onMessage: message => onMessage(App.Message.GotOffMessage({ message })),
          label: 'Sort',
          choice: Sort,
          value: price,
          disabled: true,
          style,
          listbox: [PickerMenu.mixin],
          sheet: [PickerSheet.mixin],
        },
        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.

Configure once #

import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Picker, PickerSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { PickerMenu, PickerSheet } from '../../snippets/picker.js'

// Stated once, at module scope: the dropdown's look and the sheet's, and the
// words every picker of the application says. Each use gives what differs.
const AppPicker = Picker.configure({
  base: {
    listbox: [PickerMenu.mixin],
    sheet: [PickerSheet.mixin],
    placeholder: 'Choose',
    words: { clear: 'Clear the choice' },
  },
})

const sorts = ['relevance', 'price', 'name'] as const
const Sort = Picker.one(sorts)

const App = Bundle.compose({ sort: Schema.Option(Schema.Literals(sorts)) }).pipe(
  Bundle.withChild('box', Bundles.Picker, {
    args: { id: 'sort-configured' },
    onOut: out => model => ({ model: { ...model, sort: Sort.picked(model.sort, out) } }),
  }),
)

export default example({
  title: 'Configure once',
  slots: PickerSlots,
  recipe: Recipes.Picker,
  state: App.placements.runtime({ Model: App.Model, initial: { sort: Option.none() } }),
  view: ({ style, model, onMessage, h }) =>
    AppPicker.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Sort',
        choice: Sort,
        value: model.sort,
        style,
      },
      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 ↗