Components

Button

An action, in three tones, five treatments and three sizes.

Previews are drawn in the theme you made.

Edit it
tone
variant
size
scheme
direction

Basic #

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

// An `icon` stands before the label; the `icon` variant shows it alone,
// the label staying its name.
export default example({
  title: 'Basic',
  slots: ButtonSlots,
  recipe: Recipes.Button,
  view: ({ style, h }) =>
    Button.view({ label: 'Save changes', icon: outline(h, 'M3 8.5l3 3 7-7'), 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, ButtonSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { row } from '../../snippets/layout.js'

// `size` is one ladder of names, `xs` to `lg`, so each step is a style of
// its own and the same call draws all four.
const steps = (['xs', 'sm', 'md', 'lg'] as const).map(size => Recipes.Button.style({ size }))

export default example({
  title: 'Sizes',
  slots: ButtonSlots,
  recipe: Recipes.Button,
  pins: { size: 'md' },
  view: ({ h }) =>
    row(
      h,
      steps.map((style, index) =>
        Button.view({ label: ['Extra small', 'Small', 'Medium', 'Large'][index]!, style }, h),
      ),
    ),
})

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

Icon only #

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

const actions = [
  { label: 'Reply', path: 'M6.5 4L2.5 8l4 4M3 8h6.5a4 4 0 0 1 4 4' },
  { label: 'Copy', path: 'M5.5 5.5h8v8h-8zM10.5 5.5v-3h-8v8h3' },
  { label: 'Delete', path: 'M2.5 4.5h11M6 4.5v-2h4v2M4 4.5l.75 9h6.5l.75-9' },
]

// A row of actions with no words in sight: each label is its button's name.
export default example({
  title: 'Icon only',
  slots: ButtonSlots,
  recipe: Recipes.Button,
  pins: { tone: 'neutral', variant: 'icon' },
  view: ({ style, h }) =>
    row(
      h,
      actions.map(({ label, path }) => Button.view({ label, icon: outline(h, path), style }, h)),
      [h.Role('group'), h.AriaLabel('Message')],
    ),
})

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

Trailing content #

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

// `trailing` draws after the label: a chevron, a count, a shortcut. It is
// read as drawn, so the decorative chevron carries its own `aria-hidden`.
export default example({
  title: 'Trailing content',
  slots: ButtonSlots,
  recipe: Recipes.Button,
  view: ({ style, h }) =>
    row(h, [
      Button.view(
        {
          label: 'Continue',
          trailing: h.span([h.AriaHidden(true)], [outline(h, 'M6 4l4 4-4 4')]),
          style,
        },
        h,
      ),
      Button.view({ label: 'Stars', trailing: h.span([], ['1,240']), style }, h),
      Button.view({ label: 'Search', trailing: Kbd.combo({ keys: ['⌘', 'K'] }, h), style }, h),
    ]),
})

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

Two lines #

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

// A `description` is a second line under the label. Both name the button, so
// a screen reader reads them together.
export default example({
  title: 'Two lines',
  slots: ButtonSlots,
  recipe: Recipes.Button,
  pins: { variant: 'outline', tone: 'neutral' },
  view: ({ style, h }) =>
    row(h, [
      Button.view({ label: 'Deploy', description: 'To production, from main', style }, h),
      Button.view({ label: 'Roll back', description: 'To the last release', style }, h),
    ]),
})

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

Corner mark #

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

const Counts = Recipes.Badge.style({ appearance: 'solid', size: 'sm' })

// A `mark` sits on the button's corner: a count Badge here, so what it says
// is read with the label. The mark's slot places it; the Badge's own style
// draws it.
export default example({
  title: 'Corner mark',
  slots: ButtonSlots,
  recipe: Recipes.Button,
  pins: { tone: 'neutral', variant: 'outline', shape: 'square' },
  view: ({ style, h }) =>
    row(h, [
      Button.view(
        {
          label: 'Notifications',
          icon: outline(
            h,
            'M8 1.5a4.5 4.5 0 0 0-4.5 4.5v2.5L2 11h12l-1.5-2.5V6A4.5 4.5 0 0 0 8 1.5zM6.5 13a1.5 1.5 0 0 0 3 0',
          ),
          mark: Badge.view({ text: '3', tone: 'danger', style: Counts }, h),
          style,
        },
        h,
      ),
    ]),
})

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

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

const Quiet = Recipes.Button.style({ variant: 'link' })

// `href` draws an anchor in place of the button, with the same look: it has
// no `type` and no click, and `external` opens a new tab and says so aloud.
// `variant: 'link'` is the look of a link in running text.
export default example({
  title: 'As a link',
  slots: ButtonSlots,
  recipe: Recipes.Button,
  view: ({ style, h }) =>
    row(h, [
      Button.view({ label: 'Read the guide', href: '#link', style }, h),
      Button.view(
        { label: 'Open the repository', href: 'https://example.com', external: true, style },
        h,
      ),
      Button.view({ label: 'Not available yet', href: '#link', disabled: true, style }, h),
      Button.view({ label: 'Release notes', href: '#link', style: Quiet }, h),
    ]),
})

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

Busy and invalid #

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Button, ButtonSlots, Recipes, Spinner } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { row } from '../../snippets/layout.js'

const Refused = Recipes.Button.style({ tone: 'neutral', variant: 'outline' })
const Wheel = Recipes.Spinner.style({ size: 'sm', tone: 'current' })

const Model = Schema.Struct({ saving: Schema.Boolean })
const Message = defineMessageUnion({ Started: {}, Finished: {} })

// `busy` is `aria-busy` and a press that does nothing; the button stays
// focusable. It draws no spinner, so no page pays for one: the application
// composes its own into `icon`. `invalid` is its own mark (`data-invalid`).
export default example({
  title: 'Busy and invalid',
  slots: ButtonSlots,
  recipe: Recipes.Button,
  state: {
    Model,
    Message,
    init: () => ({ model: { saving: false } }),
    update: (model, message) => ({
      model: modifyFields(model, { saving: () => message._tag === 'Started' }),
    }),
  },
  view: ({ style, model, onMessage, h }) =>
    row(h, [
      Button.view(
        {
          label: model.saving ? 'Saving…' : 'Save changes',
          busy: model.saving,
          ...(model.saving ? { icon: Spinner.view({ style: Wheel }, h) } : {}),
          onClick: onMessage(Message.Started()),
          style,
        },
        h,
      ),
      Button.view(
        { label: 'Finish', onClick: onMessage(Message.Finished()), disabled: !model.saving, style },
        h,
      ),
      Button.view({ label: 'Choose a plan', invalid: true, style: Refused }, 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.

Toggle #

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Button, ButtonSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { row } from '../../snippets/layout.js'

const Format = Schema.Literals(['Bold', 'Italic', 'Underline'])
const Model = Schema.Struct({ on: Schema.Array(Format) })
const Message = defineMessageUnion({ Toggled: { format: Format } })

// `pressed` draws `aria-pressed` and the recipe's pressed look. Which are on
// is the application's own state, folded by hand: no Bundle is needed.
export default example({
  title: 'Toggle',
  slots: ButtonSlots,
  recipe: Recipes.Button,
  state: {
    Model,
    Message,
    init: () => ({ model: { on: ['Bold'] } }),
    update: (model, { format }) => ({
      model: modifyFields(model, {
        on: on => (on.includes(format) ? on.filter(each => each !== format) : [...on, format]),
      }),
    }),
  },
  view: ({ style, model, onMessage, h }) =>
    row(
      h,
      Format.literals.map(format =>
        Button.view(
          {
            label: format,
            pressed: model.on.includes(format),
            onClick: onMessage(Message.Toggled({ format })),
            style,
          },
          h,
        ),
      ),
      [h.Role('group'), h.AriaLabel('Text style')],
    ),
})

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 { Button, ButtonSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { row } from '../../snippets/layout.js'

// Stated once, at module scope: the look every button of the application
// starts from, and a named preset over it.
const Quiet = Recipes.Button.style({ tone: 'neutral', variant: 'outline' })
const Danger = Recipes.Button.style({ tone: 'danger' })

const AppButton = Button.configure({
  base: { style: Quiet },
  presets: { danger: { style: Danger } },
})

// Each use gives only what differs. A style at the call wins over both: the
// third button takes the one picked above.
export default example({
  title: 'Configure once',
  slots: ButtonSlots,
  recipe: Recipes.Button,
  view: ({ style, h }) =>
    row(h, [
      AppButton.view({ label: 'Cancel' }, h),
      AppButton.view({ label: 'Delete project', preset: 'danger' }, h),
      AppButton.view({ label: 'Save changes', style }, h),
    ]),
})

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

A look of your own #

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

// The shipped recipe has no `width` axis. `add` gives it one, and its tones,
// variants and sizes still apply; a value an axis already has is refused.
const Buttons = Recipes.Button.add({
  width: { full: { button: Style.self({ inlineSize: '100%' }) } },
})

export default example({
  title: 'A look of your own',
  slots: ButtonSlots,
  recipe: Buttons,
  pins: { width: 'full' },
  view: ({ style, h }) => Button.view({ label: 'Continue', style }, h),
})

In your application: recipe goes to forSlots, 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 ↗