Components

Checkbox

A choice that is on or off, its state in your Model.

Previews are drawn in the theme you made.

Edit it
tone
size
variant
shape
scheme
direction

Basic #

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Checkbox, CheckboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Model = Schema.Struct({ checked: Schema.Boolean })
const Message = defineMessageUnion({ Toggled: { checked: Schema.Boolean } })

// The checked state is the application's field. The label is the control's
// name, and a press on it toggles the box; `name` submits it with a form.
export default example({
  title: 'Basic',
  slots: CheckboxSlots,
  recipe: Recipes.Checkbox,
  state: {
    Model,
    Message,
    init: () => ({ model: { checked: false } }),
    update: (model, { checked }) => ({ model: model.checked === checked ? model : { checked } }),
  },
  view: ({ style, model, onMessage, h }) =>
    Checkbox.view(
      {
        id: 'terms',
        checked: model.checked,
        onToggle: checked => onMessage(Message.Toggled({ checked })),
        label: 'Accept the terms',
        name: 'terms',
        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.

Label and description #

A short email when something ships, never more than once a month.

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Checkbox, CheckboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Model = Schema.Struct({ checked: Schema.Boolean })
const Message = defineMessageUnion({ Toggled: { checked: Schema.Boolean } })

// A line under the label that also describes the control to a screen reader.
export default example({
  title: 'Label and description',
  slots: CheckboxSlots,
  recipe: Recipes.Checkbox,
  state: {
    Model,
    Message,
    init: () => ({ model: { checked: true } }),
    update: (model, { checked }) => ({ model: model.checked === checked ? model : { checked } }),
  },
  view: ({ style, model, onMessage, h }) =>
    Checkbox.view(
      {
        id: 'marketing',
        checked: model.checked,
        onToggle: checked => onMessage(Message.Toggled({ checked })),
        label: 'Product updates',
        description: 'A short email when something ships, never more than once a month.',
        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.

Indeterminate #

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Checkbox, CheckboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Toppings = ['Cheese', 'Olives', 'Basil'] as const
const Model = Schema.Struct({ on: Schema.Array(Schema.Literals(Toppings)) })
const Message = defineMessageUnion({ ToggledAll: { checked: Schema.Boolean } })

// A dash while only some are chosen: `indeterminate` is the application's
// reading of its own state, and a press leaves the box checked or not.
export default example({
  title: 'Indeterminate',
  slots: CheckboxSlots,
  recipe: Recipes.Checkbox,
  state: {
    Model,
    Message,
    init: () => ({ model: { on: ['Cheese'] } }),
    update: (_, { checked }) => ({ model: { on: checked ? [...Toppings] : [] } }),
  },
  view: ({ style, model, onMessage, h }) =>
    Checkbox.view(
      {
        id: 'toppings',
        checked: model.on.length === Toppings.length,
        indeterminate: model.on.length > 0 && model.on.length < Toppings.length,
        onToggle: checked => onMessage(Message.ToggledAll({ checked })),
        label: `Toppings (${model.on.length} of ${Toppings.length})`,
        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.

Card #

Ten editors and every page.

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Checkbox, CheckboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Model = Schema.Struct({ checked: Schema.Boolean })
const Message = defineMessageUnion({ Toggled: { checked: Schema.Boolean } })

// The card is a look over the same markup: `variant: 'card'` boxes the row,
// fills it while checked, and stretches the label over it, so a press
// anywhere on the card toggles the box.
export default example({
  title: 'Card',
  slots: CheckboxSlots,
  recipe: Recipes.Checkbox,
  pins: { variant: 'card' },
  state: {
    Model,
    Message,
    init: () => ({ model: { checked: true } }),
    update: (model, { checked }) => ({ model: model.checked === checked ? model : { checked } }),
  },
  view: ({ style, model, onMessage, h }) =>
    Checkbox.view(
      {
        id: 'plan-team',
        checked: model.checked,
        onToggle: checked => onMessage(Message.Toggled({ checked })),
        label: 'Team',
        description: 'Ten editors and every page.',
        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.

Aside and position #

Beta
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Badge, Checkbox, CheckboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Tag = Recipes.Badge.style({})

const Model = Schema.Struct({ checked: Schema.Boolean })
const Message = defineMessageUnion({ Toggled: { checked: Schema.Boolean } })

// `labelAside` is a slot the checkbox places and does not understand: a
// Badge here, as easily a Tooltip's trigger. `position: 'end'` puts the words
// first and the box at the far end.
export default example({
  title: 'Aside and position',
  slots: CheckboxSlots,
  recipe: Recipes.Checkbox,
  state: {
    Model,
    Message,
    init: () => ({ model: { checked: true } }),
    update: (model, { checked }) => ({ model: model.checked === checked ? model : { checked } }),
  },
  view: ({ style, model, onMessage, h }) =>
    Checkbox.view(
      {
        id: 'beta',
        checked: model.checked,
        onToggle: checked => onMessage(Message.Toggled({ checked })),
        label: 'Early access',
        labelAside: Badge.view({ text: 'Beta', style: Tag }, h),
        position: 'end',
        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.

Disabled, read-only and invalid #

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Checkbox, CheckboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'

const Model = Schema.Struct({ checked: Schema.Boolean })
const Message = defineMessageUnion({ Toggled: { checked: Schema.Boolean } })

// `disabled` and `readOnly` refuse the press and say so (`aria-disabled`,
// `aria-readonly`); `invalid` shows the error edge and `aria-invalid`.
export default example({
  title: 'Disabled, read-only and invalid',
  slots: CheckboxSlots,
  recipe: Recipes.Checkbox,
  state: {
    Model,
    Message,
    init: () => ({ model: { checked: false } }),
    update: (model, { checked }) => ({ model: model.checked === checked ? model : { checked } }),
  },
  view: ({ style, model, onMessage, h }) => {
    const common = {
      checked: model.checked,
      onToggle: (checked: boolean) => onMessage(Message.Toggled({ checked })),
      style,
    }
    return stack(h, [
      Checkbox.view({ id: 'off', label: 'Disabled', disabled: true, ...common }, h),
      Checkbox.view({ id: 'locked', label: 'Read-only', readOnly: true, ...common }, h),
      Checkbox.view({ id: 'bad', label: 'Accept the terms', invalid: true, ...common }, 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 { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Checkbox, CheckboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'

const Round = Recipes.Checkbox.style({ shape: 'round', tone: 'neutral' })

// Every checkbox of the application is a round, neutral one with its words
// first, said once; a use gives only what is its own.
const AppCheckbox = Checkbox.configure({
  base: { style: Round, position: 'end' },
  presets: { start: { position: 'start' } },
})

const Model = Schema.Struct({ checked: Schema.Boolean })
const Message = defineMessageUnion({ Toggled: { checked: Schema.Boolean } })

export default example({
  title: 'Configure once',
  slots: CheckboxSlots,
  recipe: Recipes.Checkbox,
  state: {
    Model,
    Message,
    init: () => ({ model: { checked: true } }),
    update: (model, { checked }) => ({ model: model.checked === checked ? model : { checked } }),
  },
  view: ({ model, onMessage, h }) => {
    const onToggle = (checked: boolean) => onMessage(Message.Toggled({ checked }))
    return stack(h, [
      AppCheckbox.view(
        { id: 'sync', label: 'Sync over cellular', checked: model.checked, onToggle },
        h,
      ),
      AppCheckbox.view(
        {
          id: 'sync-start',
          label: 'Words after',
          preset: 'start',
          checked: model.checked,
          onToggle,
        },
        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 ↗