Components

Checkbox group

A question answered with any number of options, under its legend.

Previews are drawn in the theme you made.

Edit it
variant
orientation
scheme
direction

Alerts #

Email me about

When someone names you.

export const CheckboxGroupStyle = forSlots(CheckboxGroupSlots)(Recipes.CheckboxGroup({}))

export const checkboxGroup = <Message>(
  style: NamedStyle<typeof CheckboxGroupSlots>,
  checked: ReadonlyArray<string>,
  onToggle: (value: string) => Message,
  h: HtmlBuilder<Message>,
): Html =>
  CheckboxGroup.view(
    {
      id: 'alerts',
      legend: 'Email me about',
      options: [
        { value: 'mentions', label: 'Mentions', description: 'When someone names you.' },
        { value: 'replies', label: 'Replies to my comments' },
        { value: 'digest', label: 'A weekly digest' },
        { value: 'offers', label: 'Offers', disabled: true },
      ],
      checked,
      onToggle,
      style,
    },
    h,
  )

Descriptions #

Email me about

When someone names you.

When someone answers you.

Monday, a short one.

export const checkboxGroupDescribed = <Message>(
  style: NamedStyle<typeof CheckboxGroupSlots>,
  checked: ReadonlyArray<string>,
  onToggle: (value: string) => Message,
  h: HtmlBuilder<Message>,
): Html =>
  CheckboxGroup.view(
    {
      id: 'alerts-described',
      legend: 'Email me about',
      options: [
        { value: 'mentions', label: 'Mentions', description: 'When someone names you.' },
        { value: 'replies', label: 'Replies', description: 'When someone answers you.' },
        { value: 'digest', label: 'A weekly digest', description: 'Monday, a short one.' },
      ],
      checked,
      onToggle,
      style,
    },
    h,
  )

Select all #

Email me about
// A first row that checks every option or none; Offers is locked on, and
// stays on through both.
export const checkboxGroupSelectAll = <Message>(
  style: NamedStyle<typeof CheckboxGroupSlots>,
  checked: ReadonlyArray<string>,
  onToggle: (value: string) => Message,
  onSet: (values: ReadonlyArray<string>) => Message,
  h: HtmlBuilder<Message>,
): Html =>
  CheckboxGroup.view(
    {
      id: 'alerts-choices',
      legend: 'Email me about',
      options: [
        { value: 'mentions', label: 'Mentions' },
        { value: 'replies', label: 'Replies' },
        { value: 'digest', label: 'A weekly digest' },
        { value: 'offers', label: 'Offers', disabled: true },
      ],
      checked,
      onToggle,
      selectAll: { label: 'Select all', onSet },
      style,
    },
    h,
  )

Invalid #

Email me about

Pick at least one.

export const checkboxGroupInvalid = <Message>(
  style: NamedStyle<typeof CheckboxGroupSlots>,
  checked: ReadonlyArray<string>,
  onToggle: (value: string) => Message,
  h: HtmlBuilder<Message>,
): Html =>
  CheckboxGroup.view(
    {
      id: 'alerts-invalid',
      legend: 'Email me about',
      options: [
        { value: 'mentions', label: 'Mentions' },
        { value: 'replies', label: 'Replies' },
        { value: 'digest', label: 'A weekly digest' },
      ],
      checked,
      onToggle,
      // Invalid until one is checked: the error says what would make it valid.
      ...(checked.length === 0 ? { error: 'Pick at least one.' } : {}),
      style,
    },
    h,
  )

Cards with marks #

Notify me by

A digest each morning.

New

Only for urgent changes.

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Badge, CheckboxGroup, CheckboxGroupSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

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

const Model = Schema.Struct({ checked: Schema.Array(Schema.String) })
const Message = defineMessageUnion({ Toggled: { value: Schema.String } })

// Each option is a row of content: a mark before the words (`icon`), a line
// under them (`description`, text or Html) and a badge after (`trailing`).
// `variant: 'card'` boxes each row and stretches its label over it, so a
// press anywhere on the card chooses it.
export default example({
  title: 'Cards with marks',
  slots: CheckboxGroupSlots,
  recipe: Recipes.CheckboxGroup,
  pins: { variant: 'card' },
  state: {
    Model,
    Message,
    init: () => ({ model: { checked: ['email'] } }),
    update: (model, { value }) => ({
      model: modifyFields(model, { checked: checked => CheckboxGroup.toggled(checked, value) }),
    }),
  },
  view: ({ style, model, onMessage, h }) =>
    CheckboxGroup.view(
      {
        id: 'channels',
        legend: 'Notify me by',
        options: [
          {
            value: 'email',
            label: 'Email',
            description: 'A digest each morning.',
            icon: h.span([], ['@']),
          },
          {
            value: 'sms',
            label: 'Text message',
            description: 'Only for urgent changes.',
            icon: h.span([], ['#']),
            trailing: Badge.view({ text: 'New', tone: 'accent', style: Tag }, h),
          },
        ],
        checked: model.checked,
        onToggle: value => onMessage(Message.Toggled({ value })),
        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 ↗