Components

Listbox

A custom select: one value or several from a list that opens under its button, with icons, descriptions, groups, a trailing part, a clear button, and your own rule for what may be chosen.

Previews are drawn in the theme you made.

Edit it
size
trigger
check
width
scheme
direction

Basic #

import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Listbox, ListboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const fruits = ['Apple', 'Banana', 'Cherry', 'Durian'] as const
const Fruit = Listbox.one(fruits)

// The fruit is the application's own field. The Listbox holds whether it is
// open and the row the keys are on, and reports a choice, which `Fruit.picked`
// folds into the field.
const App = Bundle.compose({ fruit: Schema.Option(Schema.Literals(fruits)) }).pipe(
  Bundle.withChild('box', Bundles.Listbox, {
    args: { id: 'fruit' },
    onOut: out => model => ({ model: { ...model, fruit: Fruit.picked(model.fruit, out) } }),
  }),
)

export default example({
  title: 'Basic',
  slots: ListboxSlots,
  recipe: Recipes.Listbox,
  state: App.placements.runtime({ Model: App.Model, initial: { fruit: Option.none() } }),
  view: ({ style, model, onMessage, h }) =>
    Listbox.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Fruit',
        choice: Fruit,
        value: model.fruit,
        placeholder: 'Pick a fruit',
        style,
        mixins: Listbox.fluid,
      },
      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 #

import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Listbox, ListboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const fruits = ['Apple', 'Banana', 'Cherry', 'Durian'] as const
const Fruit = Listbox.one(fruits)
const glyphs: Readonly<Record<(typeof fruits)[number], string>> = {
  Apple: '🍎',
  Banana: '🍌',
  Cherry: '🍒',
  Durian: '🥭',
}

const App = Bundle.compose({ fruit: Schema.Option(Schema.Literals(fruits)) }).pipe(
  Bundle.withChild('box', Bundles.Listbox, {
    args: { id: 'fruit-icons' },
    onOut: out => model => ({ model: { ...model, fruit: Fruit.picked(model.fruit, out) } }),
  }),
)

// A row's `icon` is drawn before its label, in the list and on the trigger:
// a glyph, an avatar, a flag or a coloured dot are all the same option.
export default example({
  title: 'Icons',
  slots: ListboxSlots,
  recipe: Recipes.Listbox,
  state: App.placements.runtime({ Model: App.Model, initial: { fruit: Option.some('Cherry') } }),
  view: ({ style, model, onMessage, h }) =>
    Listbox.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Fruit',
        choice: Fruit,
        value: model.fruit,
        option: fruit => ({ label: fruit, icon: h.span([], [glyphs[fruit]]) }),
        placeholder: 'Pick a fruit',
        style,
        mixins: Listbox.fluid,
      },
      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.

Descriptions #

import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Listbox, ListboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const plans = ['Hobby', 'Team', 'Business'] as const
const Plan = Listbox.one(plans)
const notes: Readonly<Record<(typeof plans)[number], string>> = {
  Hobby: 'For one person, free',
  Team: 'Shared projects and roles',
  Business: 'Single sign-on and audit logs',
}

const App = Bundle.compose({ plan: Schema.Option(Schema.Literals(plans)) }).pipe(
  Bundle.withChild('box', Bundles.Listbox, {
    args: { id: 'plan' },
    onOut: out => model => ({ model: { ...model, plan: Plan.picked(model.plan, out) } }),
  }),
)

// A line under each label, in the list only: the trigger shows the label.
export default example({
  title: 'Descriptions',
  slots: ListboxSlots,
  recipe: Recipes.Listbox,
  state: App.placements.runtime({ Model: App.Model, initial: { plan: Option.some('Team') } }),
  view: ({ style, model, onMessage, h }) =>
    Listbox.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Plan',
        choice: Plan,
        value: model.plan,
        option: plan => ({ label: plan, description: notes[plan] }),
        style,
        mixins: Listbox.fluid,
      },
      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, Listbox, ListboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const fruits = ['Apple', 'Banana', 'Cherry', 'Durian'] as const
const Fruit = Listbox.one(fruits)

const App = Bundle.compose({ fruit: Schema.Option(Schema.Literals(fruits)) }).pipe(
  Bundle.withChild('box', Bundles.Listbox, {
    args: { id: 'fruit-groups' },
    onOut: out => model => ({ model: { ...model, fruit: Fruit.picked(model.fruit, out) } }),
  }),
)

// Rows next to each other with one group name stand under its heading.
export default example({
  title: 'Groups',
  slots: ListboxSlots,
  recipe: Recipes.Listbox,
  state: App.placements.runtime({ Model: App.Model, initial: { fruit: Option.none() } }),
  view: ({ style, model, onMessage, h }) =>
    Listbox.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Fruit',
        choice: Fruit,
        value: model.fruit,
        groupBy: fruit => (fruit === 'Apple' || fruit === 'Banana' ? 'Every day' : 'In season'),
        heading: group => h.span([], [group]),
        placeholder: 'Pick a fruit',
        style,
        mixins: Listbox.fluid,
      },
      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 trailing part #

import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Listbox, ListboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const zones = ['Lisbon', 'New York', 'Tokyo'] as const
const Zone = Listbox.one(zones)
const offsets: Readonly<Record<(typeof zones)[number], string>> = {
  Lisbon: 'UTC+0',
  'New York': 'UTC−5',
  Tokyo: 'UTC+9',
}

const App = Bundle.compose({ zone: Schema.Option(Schema.Literals(zones)) }).pipe(
  Bundle.withChild('box', Bundles.Listbox, {
    args: { id: 'zone' },
    onOut: out => model => ({ model: { ...model, zone: Zone.picked(model.zone, out) } }),
  }),
)

// `trailing` is drawn after the label at the row's end: an offset, a count,
// a badge or a shortcut. It shows in the list, not on the trigger.
export default example({
  title: 'A trailing part',
  slots: ListboxSlots,
  recipe: Recipes.Listbox,
  state: App.placements.runtime({ Model: App.Model, initial: { zone: Option.none() } }),
  view: ({ style, model, onMessage, h }) =>
    Listbox.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Time zone',
        choice: Zone,
        value: model.zone,
        option: zone => ({ label: zone, trailing: h.span([], [offsets[zone]]) }),
        placeholder: 'Pick a time zone',
        style,
        mixins: Listbox.fluid,
      },
      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.

Several #

import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Listbox, ListboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const toppings = ['Basil', 'Capers', 'Mushrooms', 'Olives', 'Peppers'] as const
const Toppings = Listbox.many(toppings)

// A choice of several: `many` flips the row reported, the list stays open as
// rows are chosen, and `multiple: true` tells the Bundle to keep it so.
const App = Bundle.compose({ toppings: Schema.Array(Schema.Literals(toppings)) }).pipe(
  Bundle.withChild('box', Bundles.Listbox, {
    args: { id: 'toppings', multiple: true },
    onOut: out => model => ({
      model: { ...model, toppings: Toppings.picked(model.toppings, out) },
    }),
  }),
)

export default example({
  title: 'Several',
  slots: ListboxSlots,
  recipe: Recipes.Listbox,
  state: App.placements.runtime({ Model: App.Model, initial: { toppings: ['Basil', 'Olives'] } }),
  view: ({ style, model, onMessage, h }) =>
    Listbox.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Toppings',
        choice: Toppings,
        value: model.toppings,
        placeholder: 'Pick toppings',
        style,
        mixins: Listbox.fluid,
      },
      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.

Dots, overlapping on the trigger #

import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Listbox, ListboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { t } from '../../snippets/theme.js'

const statuses = ['Backlog', 'In progress', 'Done'] as const
const Statuses = Listbox.many(statuses)
const colours: ReadonlyMap<string, string> = new Map([
  ['Backlog', t.text.muted],
  ['In progress', t.warning.default],
  ['Done', t.success.default],
])

const App = Bundle.compose({ statuses: Schema.Array(Schema.Literals(statuses)) }).pipe(
  Bundle.withChild('box', Bundles.Listbox, {
    args: { id: 'statuses', multiple: true },
    onOut: out => model => ({
      model: { ...model, statuses: Statuses.picked(model.statuses, out) },
    }),
  }),
)

const dot = (colour: string, overlap: boolean) =>
  ({
    display: 'inline-block',
    inlineSize: '0.75em',
    blockSize: '0.75em',
    borderRadius: '50%',
    background: colour,
    outline: `2px solid ${t.surface.base}`,
    marginInlineStart: overlap ? '-0.3em' : '0',
  }) satisfies Record<string, string>

// `icon` puts a dot before each row, and `trigger` draws what the trigger
// shows for the rows chosen: here, their dots overlapping.
export default example({
  title: 'Dots, overlapping on the trigger',
  slots: ListboxSlots,
  recipe: Recipes.Listbox,
  state: App.placements.runtime({
    Model: App.Model,
    initial: { statuses: ['Backlog', 'Done'] },
  }),
  view: ({ style, model, onMessage, h }) =>
    Listbox.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Status',
        choice: Statuses,
        value: model.statuses,
        option: status => ({
          label: status,
          icon: h.span([h.Style(dot(colours.get(status) ?? '', false))], []),
        }),
        trigger: chosen =>
          h.span(
            [h.Style({ display: 'inline-flex', alignItems: 'center' })],
            chosen.map((row, index) =>
              h.span([h.Style(dot(colours.get(row.label) ?? '', index > 0))], []),
            ),
          ),
        placeholder: 'Any status',
        style,
        mixins: Listbox.fluid,
      },
      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, Listbox, ListboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const fruits = ['Apple', 'Banana', 'Cherry', 'Durian'] as const
const Fruit = Listbox.one(fruits)

// 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.
const App = Bundle.compose({ fruit: Schema.Option(Schema.Literals(fruits)) }).pipe(
  Bundle.withMessages({ Cleared: {} }),
  Bundle.withChild('box', Bundles.Listbox, {
    args: { id: 'fruit-clear' },
    onOut: out => model => ({ model: { ...model, fruit: Fruit.picked(model.fruit, out) } }),
  }),
)

export default example({
  title: 'A clear button',
  slots: ListboxSlots,
  recipe: Recipes.Listbox,
  state: App.placements.runtime({
    Model: App.Model,
    initial: { fruit: Option.some('Cherry') },
    update: App.placements.update(model => ({
      model: modifyFields(model, {
        fruit: fruit => Fruit.picked(fruit, ChoiceChange.ClearedSelection()),
      }),
    })),
  }),
  view: ({ style, model, onMessage, h }) =>
    Listbox.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Fruit',
        choice: Fruit,
        value: model.fruit,
        placeholder: 'Pick a fruit',
        clear: { onClear: onMessage(App.Message.Cleared()) },
        words: { clear: 'Clear the fruit' },
        style,
        mixins: Listbox.fluid,
      },
      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, disabled, and a row that cannot be chosen #

import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Listbox, ListboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'

const fruits = ['Apple', 'Banana', 'Cherry', 'Durian'] as const
const Fruit = Listbox.one(fruits)

const apple: Option.Option<(typeof fruits)[number]> = Option.some('Apple')

// Each Listbox is placed once: its own open state and its own row.
const App = Bundle.compose({ fruit: Schema.Option(Schema.Literals(fruits)) }).pipe(
  Bundle.withChild('invalid', Bundles.Listbox, {
    args: { id: 'fruit-invalid' },
    onOut: out => model => ({ model: { ...model, fruit: Fruit.picked(model.fruit, out) } }),
  }),
  Bundle.withChild('off', Bundles.Listbox, { args: { id: 'fruit-off' }, onOut: Bundle.ignore }),
  Bundle.withChild('sold', Bundles.Listbox, { args: { id: 'fruit-sold' }, onOut: Bundle.ignore }),
)

export default example({
  title: 'Invalid, disabled, and a row that cannot be chosen',
  slots: ListboxSlots,
  recipe: Recipes.Listbox,
  state: App.placements.runtime({ Model: App.Model, initial: { fruit: Option.none() } }),
  view: ({ style, model, onMessage, h }) =>
    stack(h, [
      Listbox.view(
        {
          model: model.invalid,
          onMessage: message => onMessage(App.Message.GotInvalidMessage({ message })),
          label: 'Fruit',
          choice: Fruit,
          value: model.fruit,
          placeholder: 'Pick a fruit: required',
          invalid: Option.isNone(model.fruit),
          style,
        },
        h,
      ),
      Listbox.view(
        {
          model: model.off,
          onMessage: message => onMessage(App.Message.GotOffMessage({ message })),
          label: 'Fruit',
          choice: Fruit,
          value: apple,
          disabled: true,
          style,
        },
        h,
      ),
      Listbox.view(
        {
          model: model.sold,
          onMessage: message => onMessage(App.Message.GotSoldMessage({ message })),
          label: 'Fruit',
          choice: Fruit,
          value: Option.none(),
          option: fruit => ({
            label: fruit,
            description: fruit === 'Durian' ? 'Out of season' : undefined,
            disabled: fruit === 'Durian',
          }),
          placeholder: 'Durian is out of season',
          style,
          mixins: Listbox.fluid,
        },
        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 choice of your own: at most two #

import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Listbox, ListboxSlots, Recipes, type Choice } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

// A rule of the application's own, written against the public `Choice`
// interface: the shipped `many`, with a third pick refused.
const upToTwo = <Item extends string>(
  items: ReadonlyArray<Item>,
): Choice<Item, ReadonlyArray<Item>> => {
  const many = Listbox.many(items)
  return {
    ...many,
    picked: (value, change) => {
      const next = many.picked(value, change)
      return next.length > 2 ? value : next
    },
  }
}

const colours = ['Amber', 'Blue', 'Coral', 'Green'] as const
const Colours = upToTwo(colours)

const App = Bundle.compose({ colours: Schema.Array(Schema.Literals(colours)) }).pipe(
  Bundle.withChild('box', Bundles.Listbox, {
    args: { id: 'two-colours', multiple: true },
    onOut: out => model => ({ model: { ...model, colours: Colours.picked(model.colours, out) } }),
  }),
)

export default example({
  title: 'A choice of your own: at most two',
  slots: ListboxSlots,
  recipe: Recipes.Listbox,
  state: App.placements.runtime({ Model: App.Model, initial: { colours: ['Blue'] } }),
  view: ({ style, model, onMessage, h }) =>
    Listbox.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Colours, up to two',
        choice: Colours,
        value: model.colours,
        placeholder: 'Pick up to two',
        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.

Configure once #

import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Listbox, ListboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { row } from '../../snippets/layout.js'

const Quiet = Recipes.Listbox.style({ trigger: 'ghost', size: 'sm' })

// Stated once, at module scope: the look every filter of the application
// starts from, and a named preset over it.
const Filter = Listbox.configure({
  base: { style: Quiet, placeholder: 'Any', mixins: Listbox.fluid },
  presets: { owner: { placeholder: 'Anyone', words: { clear: 'Clear the owner' } } },
})

const statuses = ['Open', 'Closed'] as const
const owners = ['Ada', 'Grace', 'Linus'] as const
const Status = Listbox.one(statuses)
const Owner = Listbox.one(owners)

const App = Bundle.compose({
  status: Schema.Option(Schema.Literals(statuses)),
  owner: Schema.Option(Schema.Literals(owners)),
}).pipe(
  Bundle.withChild('statusBox', Bundles.Listbox, {
    args: { id: 'filter-status' },
    onOut: out => model => ({ model: { ...model, status: Status.picked(model.status, out) } }),
  }),
  Bundle.withChild('ownerBox', Bundles.Listbox, {
    args: { id: 'filter-owner' },
    onOut: out => model => ({ model: { ...model, owner: Owner.picked(model.owner, out) } }),
  }),
)

// Each use gives only what differs: its label, its value and its rows.
export default example({
  title: 'Configure once',
  slots: ListboxSlots,
  recipe: Recipes.Listbox,
  state: App.placements.runtime({
    Model: App.Model,
    initial: { status: Option.none(), owner: Option.none() },
  }),
  view: ({ model, onMessage, h }) =>
    row(h, [
      Filter.view(
        {
          model: model.statusBox,
          onMessage: message => onMessage(App.Message.GotStatusBoxMessage({ message })),
          label: 'Status',
          choice: Status,
          value: model.status,
        },
        h,
      ),
      Filter.view(
        {
          model: model.ownerBox,
          onMessage: message => onMessage(App.Message.GotOwnerBoxMessage({ message })),
          label: 'Owner',
          choice: Owner,
          value: model.owner,
          preset: 'owner',
        },
        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 look of your own #

import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Style } from 'foldkit-mixins'
import { Bundles, Listbox, ListboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { t } from '../../snippets/theme.js'

const fruits = ['Apple', 'Banana', 'Cherry', 'Durian'] as const
const Fruit = Listbox.one(fruits)

// The shipped recipe has no `rows` axis. `add` gives it one, and its sizes,
// triggers and checks still apply; a value an axis already has is refused.
const Divided = Recipes.Listbox.add({
  rows: {
    divided: {
      item: Style.self({ borderBlockEnd: `${t.border.thin} solid ${t.outline.default}` }),
    },
  },
})

const App = Bundle.compose({ fruit: Schema.Option(Schema.Literals(fruits)) }).pipe(
  Bundle.withChild('box', Bundles.Listbox, {
    args: { id: 'fruit-divided' },
    onOut: out => model => ({ model: { ...model, fruit: Fruit.picked(model.fruit, out) } }),
  }),
)

export default example({
  title: 'A look of your own',
  slots: ListboxSlots,
  recipe: Divided,
  pins: { rows: 'divided' },
  state: App.placements.runtime({ Model: App.Model, initial: { fruit: Option.none() } }),
  view: ({ style, model, onMessage, h }) =>
    Listbox.view(
      {
        model: model.box,
        onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
        label: 'Fruit',
        choice: Fruit,
        value: model.fruit,
        placeholder: 'Pick a fruit',
        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 ↗