Components

Label

The words that name a control, tied to it by the id it carries.

Previews are drawn in the theme you made.

Edit it
scheme
direction

Basic #

import { Input, Label, LabelSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'

const Field = Recipes.Input.style({})

// `for` is the id the control carries. Pass the one `FieldAssociation`
// derived, so there is no second naming scheme beside it.
export default example({
  title: 'Basic',
  slots: LabelSlots,
  recipe: Recipes.Label,
  view: ({ style, h }) =>
    stack(h, [
      Label.view({ for: 'label-email', text: 'Email address', style }, h),
      Input.view(
        {
          id: 'label-email',
          type: 'email',
          style: Field,
          draw: (resolved, h) => h.input(resolved.input),
        },
        h,
      ),
    ]),
})

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

Required and optional #

import { Input, Label, LabelSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'

const Field = Recipes.Input.style({})

// `required` draws a star the screen reader skips (the control itself says it
// is required); `optional` says so in words that are read. A field is one or
// the other.
export default example({
  title: 'Required and optional',
  slots: LabelSlots,
  recipe: Recipes.Label,
  view: ({ style, h }) =>
    stack(h, [
      Label.view({ for: 'label-name', text: 'Full name', required: true, style }, h),
      Input.view(
        { id: 'label-name', style: Field, draw: (resolved, h) => h.input(resolved.input) },
        h,
      ),
      Label.view({ for: 'label-nick', text: 'Nickname', optional: true, style }, h),
      Input.view(
        { id: 'label-nick', style: Field, draw: (resolved, h) => h.input(resolved.input) },
        h,
      ),
    ]),
})

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

Disabled and invalid #

import { Input, Label, LabelSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'

const Field = Recipes.Input.style({})

// `disabled` and `invalid` only mark the label (`data-disabled`, `data-invalid`):
// the control carries its own state, so pass both the same value.
export default example({
  title: 'Disabled and invalid',
  slots: LabelSlots,
  recipe: Recipes.Label,
  view: ({ style, h }) =>
    stack(h, [
      Label.view({ for: 'label-locked', text: 'Account id', disabled: true, style }, h),
      Input.view(
        {
          id: 'label-locked',
          disabled: true,
          style: Field,
          draw: (resolved, h) => h.input(resolved.input),
        },
        h,
      ),
      Label.view({ for: 'label-bad', text: 'Email address', invalid: true, style }, h),
      Input.view(
        {
          id: 'label-bad',
          invalid: true,
          style: Field,
          draw: (resolved, h) => h.input(resolved.input),
        },
        h,
      ),
    ]),
})

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

Trailing content #

Soon
import { Badge, Button, Input, Label, LabelSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'
import { stack } from '../../snippets/layout.js'

const Field = Recipes.Input.style({})
const Help = Recipes.Button.style({ tone: 'neutral', variant: 'icon', size: 'xs' })
const Soon = Recipes.Badge.style({ size: 'sm' })

// `trailing` stands beside the label, not inside it, so a button there is not
// part of what names the control.
export default example({
  title: 'Trailing content',
  slots: LabelSlots,
  recipe: Recipes.Label,
  view: ({ style, h }) =>
    stack(h, [
      Label.view(
        {
          for: 'label-key',
          text: 'API key',
          trailing: Button.view(
            {
              label: 'What is an API key?',
              icon: outline(h, 'M8 14.5a6.5 6.5 0 1 0 0-13 6.5 6.5 0 0 0 0 13zM8 7.5v3.5M8 5v.01'),
              style: Help,
            },
            h,
          ),
          style,
        },
        h,
      ),
      Input.view(
        { id: 'label-key', style: Field, draw: (resolved, h) => h.input(resolved.input) },
        h,
      ),
      Label.view(
        {
          for: 'label-domain',
          text: 'Custom domain',
          trailing: Badge.view({ text: 'Soon', tone: 'accent', style: Soon }, h),
          style,
        },
        h,
      ),
      Input.view(
        { id: 'label-domain', style: Field, draw: (resolved, h) => h.input(resolved.input) },
        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 ↗

Keyboard and accessibility

  • A native label with for set to the control’s id: pressing the label focuses the control, and the control takes the label as its name.

Text and number fields in the README ↗