Components

Slider

A number, or a low and a high one that cannot pass each other, across or upright, with marks, ticks and its value shown.

Previews are drawn in the theme you made.

Edit it
tone
size
track
value
label
scheme
direction

A number and a range #

Volume

Applies to this device only.

Price
export const SliderStyle = forSlots(SliderSlots)(Recipes.Slider({}))

/** Both values are the Model's: one number, and a range of two. */
export interface SliderValues {
  readonly volume: number
  readonly price: Slider.Range
}

/** The Message for each next value, of the kind its slider holds. */
export interface SliderMessages<Message> {
  readonly onVolume: (value: number) => Message
  readonly onPrice: (value: Slider.Range) => Message
}

export const slider = <Message>(
  style: NamedStyle<typeof SliderSlots>,
  values: SliderValues,
  messages: SliderMessages<Message>,
  h: HtmlBuilder<Message>,
): Html =>
  h.div(
    [h.Style({ display: 'grid', gap: '2rem', inlineSize: 'min(20rem, 100%)' })],
    [
      Slider.view(
        {
          id: 'preview-volume',
          label: 'Volume',
          description: 'Applies to this device only.',
          value: values.volume,
          onChange: messages.onVolume,
          step: 5,
          format: value => `${value}%`,
          showValue: true,
          style,
        },
        h,
      ),
      Slider.view(
        {
          id: 'preview-price',
          label: 'Price',
          value: values.price,
          onChange: messages.onPrice,
          max: 500,
          step: 10,
          marks: [0, 100, 200, 300, 400, 500],
          format: amount => `$${amount}`,
          words: { low: 'Minimum price', high: 'Maximum price' },
          style,
        },
        h,
      ),
    ],
  )

Saved on release #

Brightness

Saved: 60%

/** The value as it moves, and the one last let go of. */
export interface SliderCommit {
  readonly value: number
  readonly saved: number
}

// `onChange` sends every step on the way, so a drag sends dozens; `onCommit`
// sends once, when the reader lets go (and on each key press), which is the
// moment to save or ask a server.
export const sliderCommit = <Message>(
  style: NamedStyle<typeof SliderSlots>,
  state: SliderCommit,
  messages: {
    readonly onChange: (value: number) => Message
    readonly onCommit: (value: number) => Message
  },
  h: HtmlBuilder<Message>,
): Html =>
  h.div(
    [h.Style({ display: 'grid', gap: '0.75rem', inlineSize: 'min(20rem, 100%)' })],
    [
      Slider.view(
        {
          id: 'preview-brightness',
          label: 'Brightness',
          value: state.value,
          onChange: messages.onChange,
          onCommit: messages.onCommit,
          step: 5,
          format: value => `${value}%`,
          showValue: true,
          style,
        },
        h,
      ),
      h.p([h.AriaLive('polite')], [`Saved: ${state.saved}%`]),
    ],
  )

Vertical #

Level
/** A level on a vertical track, a dot at every step and words at the ends. */
export const sliderVertical = <Message>(
  style: NamedStyle<typeof SliderSlots>,
  level: number,
  onLevel: (value: number) => Message,
  h: HtmlBuilder<Message>,
): Html =>
  Slider.view(
    {
      id: 'preview-level',
      label: 'Level',
      value: level,
      onChange: onLevel,
      orientation: 'vertical',
      step: 10,
      ticks: true,
      marks: [
        { value: 0, label: 'Off' },
        { value: 100, label: 'Max' },
      ],
      showValue: true,
      format: value => `${value}%`,
      style,
    },
    h,
  )

Marks and ticks #

Budget
/** A range with labelled marks, its values at the end of the label's row. */
export const sliderMarks = <Message>(
  style: NamedStyle<typeof SliderSlots>,
  price: Slider.Range,
  onPrice: (value: Slider.Range) => Message,
  h: HtmlBuilder<Message>,
): Html =>
  Slider.view(
    {
      id: 'preview-budget',
      label: 'Budget',
      value: price,
      onChange: onPrice,
      max: 500,
      step: 50,
      ticks: true,
      marks: [
        { value: 0, label: '$0' },
        { value: 250, label: '$250' },
        { value: 500, label: '$500' },
      ],
      showValue: true,
      format: amount => `$${amount}`,
      words: { low: 'Minimum budget', high: 'Maximum budget' },
      style,
    },
    h,
  )

Three thumbs #

Gradient stops
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Slider, SliderSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Model = Schema.Struct({ stops: Schema.Array(Schema.Number) })
const Message = defineMessageUnion({ Moved: { stops: Schema.Array(Schema.Number) } })

// A list of values is a slider of as many thumbs. Each stops at its
// neighbours, named by its place (`words.thumb`), and the fill runs from the
// first to the last.
export default example({
  title: 'Three thumbs',
  slots: SliderSlots,
  recipe: Recipes.Slider,
  state: {
    Model,
    Message,
    init: () => ({ model: { stops: [20, 50, 80] } }),
    update: (_, { stops }) => ({ model: { stops } }),
  },
  view: ({ style, model, onMessage, h }) =>
    Slider.view(
      {
        id: 'stops',
        label: 'Gradient stops',
        value: model.stops,
        onChange: stops => onMessage(Message.Moved({ stops })),
        words: { thumb: 'Stop {index} of {count}' },
        showValue: true,
        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 ↗

  • What a form submits Give the widget a name and a form reads it as it reads a native field.
  • Right to left The direction switch above turns the preview over; this is what each widget reads.