Components

Button group

Related buttons joined into one control: icons, a split menu, a field, words, either way up.

Previews are drawn in the theme you made.

Edit it
size
width
scheme
direction

Basic #

import { Button, ButtonGroup, ButtonGroupSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Face = Recipes.Button.style({ tone: 'neutral', variant: 'outline' })

// Outlined buttons, joined: their borders overlap at each seam.
export default example({
  title: 'Basic',
  slots: ButtonGroupSlots,
  recipe: Recipes.ButtonGroup,
  view: ({ style, h }) =>
    ButtonGroup.view(
      {
        label: 'Message actions',
        items: ['Archive', 'Report', 'Snooze'].map(label => Button.view({ label, style: Face }, h)),
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Icons #

import { Button, ButtonGroup, ButtonGroupSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'

// `shape: 'square'` shows each icon alone; its label stays the button's name.
const Square = Recipes.Button.style({ tone: 'neutral', variant: 'outline', shape: 'square' })

const alignments = [
  { label: 'Align left', icon: 'M2 3.5h12M2 6.5h8M2 9.5h12M2 12.5h8' },
  { label: 'Align center', icon: 'M2 3.5h12M4 6.5h8M2 9.5h12M4 12.5h8' },
  { label: 'Align right', icon: 'M2 3.5h12M6 6.5h8M2 9.5h12M6 12.5h8' },
  { label: 'Justify', icon: 'M2 3.5h12M2 6.5h12M2 9.5h12M2 12.5h12' },
]

export default example({
  title: 'Icons',
  slots: ButtonGroupSlots,
  recipe: Recipes.ButtonGroup,
  view: ({ style, h }) =>
    ButtonGroup.view(
      {
        label: 'Text alignment',
        items: alignments.map(({ label, icon }) =>
          Button.view({ label, icon: outline(h, icon), style: Square }, h),
        ),
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Toolbar #

import { Button, ButtonGroup, ButtonGroupSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'
import { row } from '../../snippets/layout.js'

const Square = Recipes.Button.style({ tone: 'neutral', variant: 'outline', shape: 'square' })

const history = [
  { label: 'Undo', icon: 'M5.5 3.5L2.5 6.5l3 3M2.5 6.5h7a3.5 3.5 0 0 1 0 7h-2' },
  { label: 'Redo', icon: 'M10.5 3.5l3 3-3 3M13.5 6.5h-7a3.5 3.5 0 0 0 0 7h2' },
]
const marks = [
  {
    label: 'Bold',
    icon: 'M4.5 2.5h4.25a2.75 2.75 0 0 1 0 5.5H4.5zM4.5 8h5a2.75 2.75 0 0 1 0 5.5h-5z',
  },
  { label: 'Italic', icon: 'M7 2.5h5M4 13.5h5M10 2.5l-4 11' },
  { label: 'Underline', icon: 'M4.5 2.5v5a3.5 3.5 0 0 0 7 0v-5M3.5 14h9' },
]
const alignments = [
  { label: 'Align left', icon: 'M2 3.5h12M2 6.5h8M2 9.5h12M2 12.5h8' },
  { label: 'Align center', icon: 'M2 3.5h12M4 6.5h8M2 9.5h12M4 12.5h8' },
  { label: 'Align right', icon: 'M2 3.5h12M6 6.5h8M2 9.5h12M6 12.5h8' },
]

// Groups of groups: each joins its own buttons, and a gap keeps them apart.
export default example({
  title: 'Toolbar',
  slots: ButtonGroupSlots,
  recipe: Recipes.ButtonGroup,
  view: ({ style, h }) => {
    const group = (label: string, items: ReadonlyArray<{ label: string; icon: string }>) =>
      ButtonGroup.view(
        {
          label,
          items: items.map(item =>
            Button.view({ label: item.label, icon: outline(h, item.icon), style: Square }, h),
          ),
          style,
        },
        h,
      )
    return row(h, [
      group('History', history),
      group('Marks', marks),
      group('Alignment', alignments),
    ])
  },
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Split button #

Nothing chosen yet

import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Button, ButtonGroup, ButtonGroupSlots, Menu, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'
import { note, stack } from '../../snippets/layout.js'

const Face = Recipes.Button.style({ tone: 'neutral', variant: 'outline' })
const MenuStyle = Recipes.Menu.style({})

const rows: ReadonlyArray<Menu.Row> = [
  { value: 'Save as copy', label: 'Save as copy' },
  { value: 'Save as template', label: 'Save as template' },
  { value: 'Save and close', label: 'Save and close' },
]

// The Menu's state, packaged for a Bundle: its Model is the application's
// like every Menu's, and a choice is reported once.
const SaveMenu = Bundle.make('SaveMenu', {
  Model: Menu.Model,
  Message: Menu.Message,
  args: Schema.Struct({}),
  init: () => ({ model: Menu.init('split-save', rows, { isAnimated: true }) }),
  update: (model, message) => Menu.update(model, message),
})

const App = Bundle.compose({ last: Schema.String }).pipe(
  Bundle.withChild('menu', SaveMenu, {
    args: {},
    onOut: out => model => ({
      model: { ...model, last: Menu.OutMessage.match(out, { Selected: ({ value }) => value }) },
    }),
  }),
)

// A split button: the action, and a Menu of the others. The Menu's trigger is
// the group's last face, so it takes the group's end corners.
export default example({
  title: 'Split button',
  slots: ButtonGroupSlots,
  recipe: Recipes.ButtonGroup,
  state: App.placements.runtime({ Model: App.Model, initial: { last: '' } }),
  view: ({ style, model, onMessage, h }) =>
    stack(h, [
      ButtonGroup.view(
        {
          label: 'Save',
          items: [
            Button.view({ label: 'Save draft', style: Face }, h),
            Menu.view(
              {
                model: model.menu,
                rows,
                buttonContent: h.span(
                  [h.Role('img'), h.AriaLabel('More ways to save')],
                  [outline(h, 'M4 6l4 4 4-4')],
                ),
                onMessage: message => onMessage(App.Message.GotMenuMessage({ message })),
                mixins: [MenuStyle.mixin],
              },
              h,
            ),
          ],
          style,
        },
        h,
      ),
      note(h, [model.last === '' ? 'Nothing chosen yet' : `Chose: ${model.last}`]),
    ]),
})

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.

Separated #

import { Button, ButtonGroup, ButtonGroupSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'

const Filled = Recipes.Button.style({ tone: 'neutral' })

const clipboard = [
  {
    label: 'Cut',
    icon: 'M4.5 13.5a2 2 0 1 0 0-4 2 2 0 0 0 0 4zM11.5 13.5a2 2 0 1 0 0-4 2 2 0 0 0 0 4zM6 10l6-7.5M10 10L4 2.5',
  },
  { label: 'Copy', icon: 'M5.5 5.5h8v8h-8zM10.5 5.5v-3h-8v8h3' },
  { label: 'Paste', icon: 'M5.5 2h5v2.5h-5zM5.5 3H3v11h10V3h-2.5' },
]

// Solid buttons' fills meet edge to edge: `separated` draws a rule on every
// seam, where outlined buttons show their borders.
export default example({
  title: 'Separated',
  slots: ButtonGroupSlots,
  recipe: Recipes.ButtonGroup,
  view: ({ style, h }) =>
    ButtonGroup.view(
      {
        label: 'Clipboard',
        items: clipboard.map(({ label, icon }) =>
          Button.view({ label, icon: outline(h, icon), style: Filled }, h),
        ),
        separated: true,
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Input #

import { Button, ButtonGroup, ButtonGroupSlots, Input, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'

const Face = Recipes.Button.style({ tone: 'neutral', variant: 'outline' })
const Field = Recipes.Input.style({})

// A field joins the buttons like any other item, and takes the room they leave:
// the group spans its container.
export default example({
  title: 'Input',
  slots: ButtonGroupSlots,
  recipe: Recipes.ButtonGroup,
  pins: { width: 'fill' },
  view: ({ style, h }) =>
    h.div(
      [h.Style({ inlineSize: 'min(100%, 24rem)' })],
      [
        ButtonGroup.view(
          {
            label: 'Search',
            items: [
              Input.view(
                {
                  id: 'group-search',
                  type: 'search',
                  placeholder: 'Search issues',
                  style: Field,
                  draw: (resolved, h) => h.input([...resolved.input, h.AriaLabel('Search issues')]),
                },
                h,
              ),
              Button.view(
                {
                  label: 'Search',
                  icon: outline(
                    h,
                    'M7 12.5a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zM14.5 14.5l-3.6-3.6',
                  ),
                  style: Face,
                },
                h,
              ),
            ],
            style,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Text #

https://
import { Button, ButtonGroup, ButtonGroupSlots, Input, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'

const Square = Recipes.Button.style({ tone: 'neutral', variant: 'outline', shape: 'square' })
const Field = Recipes.Input.style({})

// Words are an item too, drawn in the group's `text` box: here a field's
// fixed prefix.
export default example({
  title: 'Text',
  slots: ButtonGroupSlots,
  recipe: Recipes.ButtonGroup,
  pins: { width: 'fill' },
  view: ({ style, h }) =>
    h.div(
      [h.Style({ inlineSize: 'min(100%, 24rem)' })],
      [
        ButtonGroup.view(
          {
            label: 'Site address',
            items: [
              'https://',
              Input.view(
                {
                  id: 'group-site',
                  placeholder: 'example.com',
                  style: Field,
                  draw: (resolved, h) => h.input([...resolved.input, h.AriaLabel('Site address')]),
                },
                h,
              ),
              Button.view(
                {
                  label: 'Copy address',
                  icon: outline(h, 'M5.5 5.5h8v8h-8zM10.5 5.5v-3h-8v8h3'),
                  style: Square,
                },
                h,
              ),
            ],
            style,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Vertical #

import { Button, ButtonGroup, ButtonGroupSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'
import { row } from '../../snippets/layout.js'

const Face = Recipes.Button.style({ tone: 'neutral', variant: 'outline' })
const Square = Recipes.Button.style({ tone: 'neutral', variant: 'outline', shape: 'square' })

const zoom = [
  { label: 'Zoom in', icon: 'M8 3v10M3 8h10' },
  { label: 'Zoom out', icon: 'M3 8h10' },
  { label: 'Fit to screen', icon: 'M2.5 6V2.5H6M10 2.5h3.5V6M13.5 10v3.5H10M6 13.5H2.5V10' },
]

// `orientation: 'vertical'` stacks the items, joined top to bottom, each as
// wide as the widest.
export default example({
  title: 'Vertical',
  slots: ButtonGroupSlots,
  recipe: Recipes.ButtonGroup,
  view: ({ style, h }) =>
    row(h, [
      ButtonGroup.view(
        {
          label: 'Zoom',
          orientation: 'vertical',
          items: zoom.map(({ label, icon }) =>
            Button.view({ label, icon: outline(h, icon), style: Square }, h),
          ),
          style,
        },
        h,
      ),
      ButtonGroup.view(
        {
          label: 'Layer order',
          orientation: 'vertical',
          items: ['Bring forward', 'Send backward', 'Send to back'].map(label =>
            Button.view({ label, style: Face }, h),
          ),
          style,
        },
        h,
      ),
    ]),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Sizes #

import { Button, ButtonGroup, ButtonGroupSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'
import { stack } from '../../snippets/layout.js'

const Face = Recipes.Button.style({ tone: 'neutral', variant: 'outline' })
const Square = Recipes.Button.style({ tone: 'neutral', variant: 'outline', shape: 'square' })
const Sizes = (['sm', 'md', 'lg'] as const).map(size => Recipes.ButtonGroup.style({ size }))

const alignments = [
  { label: 'Align left', icon: 'M2 3.5h12M2 6.5h8M2 9.5h12M2 12.5h8' },
  { label: 'Align center', icon: 'M2 3.5h12M4 6.5h8M2 9.5h12M4 12.5h8' },
  { label: 'Align right', icon: 'M2 3.5h12M6 6.5h8M2 9.5h12M6 12.5h8' },
]

// The group's `size` steps every item that has no size of its own.
export default example({
  title: 'Sizes',
  slots: ButtonGroupSlots,
  recipe: Recipes.ButtonGroup,
  pins: { size: 'md' },
  view: ({ h }) =>
    stack(
      h,
      Sizes.map(style =>
        ButtonGroup.view(
          {
            label: 'Text alignment',
            items: [
              ...alignments.map(({ label, icon }) =>
                Button.view({ label, icon: outline(h, icon), style: Square }, h),
              ),
              Button.view({ label: 'More', style: Face }, h),
            ],
            style,
          },
          h,
        ),
      ),
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Pagination #

Page 3 of 12
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Button, ButtonGroup, ButtonGroupSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'

const Square = Recipes.Button.style({ tone: 'neutral', variant: 'outline', shape: 'square' })

const pages = 12
const Model = Schema.Struct({ page: Schema.Number })
const Message = defineMessageUnion({ ChosePage: { page: Schema.Number } })

// A page count between two steps, drawn in the `text` box. The page is the
// Model's; each step is disabled at its end.
export default example({
  title: 'Pagination',
  slots: ButtonGroupSlots,
  recipe: Recipes.ButtonGroup,
  state: {
    Model,
    Message,
    init: () => ({ model: { page: 3 } }),
    update: (model, { page }) => ({
      model: page === model.page ? model : { page: Math.min(pages, Math.max(1, page)) },
    }),
  },
  view: ({ style, model, onMessage, h }) =>
    ButtonGroup.view(
      {
        label: 'Pages',
        items: [
          Button.view(
            {
              label: 'Previous page',
              icon: outline(h, 'M10 4L6 8l4 4'),
              disabled: model.page <= 1,
              onClick: onMessage(Message.ChosePage({ page: model.page - 1 })),
              style: Square,
            },
            h,
          ),
          `Page ${model.page} of ${pages}`,
          Button.view(
            {
              label: 'Next page',
              icon: outline(h, 'M6 4l4 4-4 4'),
              disabled: model.page >= pages,
              onClick: onMessage(Message.ChosePage({ page: model.page + 1 })),
              style: Square,
            },
            h,
          ),
        ],
        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 ↗