Components

Switch

A setting that takes effect at once.

Previews are drawn in the theme you made.

Edit it
tone
size
variant
scheme
direction

Basic #

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

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

// A checkbox's options on a switch's track: the field is the application's,
// the label names the switch and a press on it toggles.
export default example({
  title: 'Basic',
  slots: SwitchSlots,
  recipe: Recipes.Switch,
  state: {
    Model,
    Message,
    init: () => ({ model: { on: false } }),
    update: (model, { on }) => ({ model: model.on === on ? model : { on } }),
  },
  view: ({ style, model, onMessage, h }) =>
    Switch.view(
      {
        id: 'airplane',
        checked: model.on,
        onToggle: on => onMessage(Message.Toggled({ on })),
        label: 'Airplane mode',
        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.

Settings list #

A digest each morning.

As things happen.

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

const Model = Schema.Struct({ email: Schema.Boolean, push: Schema.Boolean })
const Message = defineMessageUnion({
  ToggledEmail: { on: Schema.Boolean },
  ToggledPush: { on: Schema.Boolean },
})

// A settings list: the words first, a line of description under them and the
// track at the far end, which is `position: 'end'`. Each `card` row is a look
// over the same markup, and pressing anywhere on it toggles its switch.
export default example({
  title: 'Settings list',
  slots: SwitchSlots,
  recipe: Recipes.Switch,
  pins: { variant: 'card' },
  state: {
    Model,
    Message,
    init: () => ({ model: { email: true, push: false } }),
    update: (model, message) =>
      Message.match(message, {
        ToggledEmail: ({ on }) => ({ model: { ...model, email: on } }),
        ToggledPush: ({ on }) => ({ model: { ...model, push: on } }),
      }),
  },
  view: ({ style, model, onMessage, h }) =>
    stack(h, [
      Switch.view(
        {
          id: 'email',
          checked: model.email,
          onToggle: on => onMessage(Message.ToggledEmail({ on })),
          label: 'Email notifications',
          description: 'A digest each morning.',
          position: 'end',
          style,
        },
        h,
      ),
      Switch.view(
        {
          id: 'push',
          checked: model.push,
          onToggle: on => onMessage(Message.ToggledPush({ on })),
          label: 'Push notifications',
          description: 'As things happen.',
          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.

Destructive #

This removes every page and cannot be undone.

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

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

// `tone: 'danger'` is a look; the confirmation text is the description.
export default example({
  title: 'Destructive',
  slots: SwitchSlots,
  recipe: Recipes.Switch,
  pins: { tone: 'danger' },
  state: {
    Model,
    Message,
    init: () => ({ model: { on: true } }),
    update: (model, { on }) => ({ model: model.on === on ? model : { on } }),
  },
  view: ({ style, model, onMessage, h }) =>
    Switch.view(
      {
        id: 'delete',
        checked: model.on,
        onToggle: on => onMessage(Message.Toggled({ on })),
        label: 'Delete the project',
        description: 'This removes every page and cannot be undone.',
        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.

Drag the thumb #

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

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

// Dragging the thumb is a feature passed beside the style: the page that
// never names `Switch.draggable` ships none of it.
export default example({
  title: 'Drag the thumb',
  slots: SwitchSlots,
  recipe: Recipes.Switch,
  state: {
    Model,
    Message,
    init: () => ({ model: { on: false } }),
    update: (model, { on }) => ({ model: model.on === on ? model : { on } }),
  },
  view: ({ style, model, onMessage, h }) => {
    const onToggle = (on: boolean) => onMessage(Message.Toggled({ on }))
    return Switch.view(
      {
        id: 'dragged',
        checked: model.on,
        onToggle,
        label: 'Drag, or press',
        mixins: [Switch.draggable(onToggle)],
        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 { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Switch, SwitchSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'

const Neutral = Recipes.Switch.style({ tone: 'neutral' })
const Danger = Recipes.Switch.style({ tone: 'danger' })

// Every switch of the application is neutral with its words first; one
// named preset is the destructive one.
const AppSwitch = Switch.configure({
  base: { style: Neutral, position: 'end' },
  presets: { danger: { style: Danger } },
})

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

export default example({
  title: 'Configure once',
  slots: SwitchSlots,
  recipe: Recipes.Switch,
  state: {
    Model,
    Message,
    init: () => ({ model: { on: true } }),
    update: (model, { on }) => ({ model: model.on === on ? model : { on } }),
  },
  view: ({ model, onMessage, h }) => {
    const onToggle = (on: boolean) => onMessage(Message.Toggled({ on }))
    return stack(h, [
      AppSwitch.view({ id: 'wifi', label: 'Wi-Fi', checked: model.on, onToggle }, h),
      AppSwitch.view(
        { id: 'wipe', label: 'Wipe on logout', preset: 'danger', checked: model.on, 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 ↗