Components

Tabs

Panels of one place, one shown at a time, moved between by the arrow keys; with icons, counts, a scrolling row and tabs that close.

Previews are drawn in the theme you made.

Edit it
variant
size
selection
overflow
scheme
direction

Underline #

Six people, three open milestones, one launch.

import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Recipes, Tabs, TabsSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

// The tabs' focus is the Bundle's; which tab is chosen is the application's,
// set from the `Selected` the Bundle reports and given back as `value`.
const App = Bundle.compose({ section: Schema.String }).pipe(
  Bundle.withChild('tabs', Bundles.Tabs, {
    args: { id: 'project' },
    onOut: out => model => ({ model: { ...model, section: out.value } }),
  }),
)

export default example({
  title: 'Underline',
  slots: TabsSlots,
  recipe: Recipes.Tabs,
  state: App.placements.runtime({ Model: App.Model, initial: { section: 'overview' } }),
  view: ({ style, model, onMessage, h }) =>
    Tabs.view(
      {
        model: model.tabs,
        onMessage: message => onMessage(App.Message.GotTabsMessage({ message })),
        label: 'Project',
        value: model.section,
        tabs: [
          {
            value: 'overview',
            label: 'Overview',
            panel: h.p([], ['Six people, three open milestones, one launch.']),
          },
          {
            value: 'activity',
            label: 'Activity',
            panel: h.p([], ['Ada published the pricing page an hour ago.']),
          },
          {
            value: 'settings',
            label: 'Settings',
            panel: h.p([], ['Who can see the project, and who can edit it.']),
          },
        ],
        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.

Pill #

Six people, one launch.

import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Recipes, Tabs, TabsSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const App = Bundle.compose({ section: Schema.String }).pipe(
  Bundle.withChild('tabs', Bundles.Tabs, {
    args: { id: 'pills' },
    onOut: out => model => ({ model: { ...model, section: out.value } }),
  }),
)

// A pill that glides to the chosen tab and a highlight that follows the
// pointer: `Tabs.fluid` is a value the application passes, so a page that
// never passes it never loads what moves them.
export default example({
  title: 'Pill',
  slots: TabsSlots,
  recipe: Recipes.Tabs,
  pins: { variant: 'pill' },
  state: App.placements.runtime({ Model: App.Model, initial: { section: 'overview' } }),
  view: ({ style, model, onMessage, h }) =>
    Tabs.view(
      {
        model: model.tabs,
        onMessage: message => onMessage(App.Message.GotTabsMessage({ message })),
        label: 'Project',
        value: model.section,
        tabs: [
          { value: 'overview', label: 'Overview', panel: h.p([], ['Six people, one launch.']) },
          { value: 'activity', label: 'Activity', panel: h.p([], ['Ada published a page.']) },
          { value: 'settings', label: 'Settings', panel: h.p([], ['Who can edit it.']) },
        ],
        style,
        mixins: [...Tabs.fluid],
      },
      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.

Icons and counts #

Twelve unread.

import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Badge, Bundles, Recipes, Tabs, TabsSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'

const App = Bundle.compose({ folder: Schema.String }).pipe(
  Bundle.withChild('tabs', Bundles.Tabs, {
    args: { id: 'mail' },
    onOut: out => model => ({ model: { ...model, folder: out.value } }),
  }),
)

// A row's `icon` stands before its words, and `trailing` after them: here a
// Badge, composed in, so the tabs hold no count of their own.
export default example({
  title: 'Icons and counts',
  slots: TabsSlots,
  recipe: Recipes.Tabs,
  state: App.placements.runtime({ Model: App.Model, initial: { folder: 'inbox' } }),
  view: ({ style, model, onMessage, h }) =>
    Tabs.view(
      {
        model: model.tabs,
        onMessage: message => onMessage(App.Message.GotTabsMessage({ message })),
        label: 'Mail',
        value: model.folder,
        tabs: [
          {
            value: 'inbox',
            label: 'Inbox',
            icon: outline(h, 'M2 9h3l1 2h4l1-2h3M2 9l2-6h8l2 6v4H2z'),
            trailing: Badge.view({ text: '12' }, h),
            panel: h.p([], ['Twelve unread.']),
          },
          {
            value: 'drafts',
            label: 'Drafts',
            icon: outline(h, 'M3 13l1-4 7-7 3 3-7 7z'),
            trailing: Badge.view({ text: '3' }, h),
            panel: h.p([], ['Three drafts.']),
          },
          {
            value: 'sent',
            label: 'Sent',
            icon: outline(h, 'M2 8l12-5-5 12-2-5z'),
            panel: h.p([], ['Nothing waiting.']),
          },
        ],
        style,
        mixins: [...Tabs.fluid],
      },
      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.

Vertical #

Inbox is empty.

import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Recipes, Tabs, TabsSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const App = Bundle.compose({ folder: Schema.String }).pipe(
  Bundle.withChild('tabs', Bundles.Tabs, {
    args: { id: 'folders' },
    onOut: out => model => ({ model: { ...model, folder: out.value } }),
  }),
)

// Upright: the arrows go up and down, the underline stands at the tabs' inline
// end, and the panel sits beside them.
export default example({
  title: 'Vertical',
  slots: TabsSlots,
  recipe: Recipes.Tabs,
  state: App.placements.runtime({ Model: App.Model, initial: { folder: 'inbox' } }),
  view: ({ style, model, onMessage, h }) =>
    Tabs.view(
      {
        model: model.tabs,
        onMessage: message => onMessage(App.Message.GotTabsMessage({ message })),
        label: 'Mail',
        orientation: 'vertical',
        value: model.folder,
        tabs: [
          { value: 'inbox', label: 'Inbox', panel: h.p([], ['Inbox is empty.']) },
          { value: 'drafts', label: 'Drafts', panel: h.p([], ['Drafts is empty.']) },
          { value: 'sent', label: 'Sent', panel: h.p([], ['Sent is empty.']) },
        ],
        style,
        mixins: [...Tabs.fluid, Tabs.readingKeys],
      },
      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.

Closable #

import { Option, Schema } from 'effect'
import { Tabs as UiTabs } from '@foldkit/ui'
import { Bundle } from 'foldkit-bundle'
import * as Command from 'foldkit/command'
import { modifyFields } from 'foldkit/struct'
import { Bundles, Recipes, Tabs, TabsSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const App = Bundle.compose({ files: Schema.Array(Schema.String), chosen: Schema.String }).pipe(
  Bundle.withMessages({ Closed: { file: Schema.String } }),
  Bundle.withChild('tabs', Bundles.Tabs, {
    args: { id: 'files' },
    onOut: out => model => ({ model: { ...model, chosen: out.value } }),
  }),
)

// Each tab closes with its cross or Delete. `Tabs.closing` says what is left
// and which tab takes focus, the one that followed the closed one.
export default example({
  title: 'Closable',
  slots: TabsSlots,
  recipe: Recipes.Tabs,
  state: App.placements.runtime({
    Model: App.Model,
    initial: { files: ['index.ts', 'view.ts', 'update.ts'], chosen: 'view.ts' },
    update: App.placements.update((model, { file }) => {
      const after = Tabs.closing(model.files, model.chosen, file)
      return {
        model: modifyFields(model, {
          files: () => after.tabs,
          chosen: chosen => Option.getOrElse(after.selected, () => chosen),
        }),
        commands: Command.mapMessages(
          Option.toArray(
            Option.map(after.focus, index => UiTabs.FocusTab({ id: model.tabs.id, index })),
          ),
          message => App.Message.GotTabsMessage({ message }),
        ),
      }
    }),
  }),
  view: ({ style, model, onMessage, h }) =>
    Tabs.view(
      {
        model: model.tabs,
        onMessage: message => onMessage(App.Message.GotTabsMessage({ message })),
        label: 'Open files',
        value: model.chosen,
        tabs: model.files.map(file => ({ value: file, label: file })),
        close: { onClose: file => onMessage(App.Message.Closed({ file })) },
        style,
        mixins: [...Tabs.fluid],
      },
      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.

Scrolling #

import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Recipes, Tabs, TabsSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const App = Bundle.compose({ chosen: Schema.String }).pipe(
  Bundle.withChild('tabs', Bundles.Tabs, {
    args: { id: 'scrolling' },
    onOut: out => model => ({ model: { ...model, chosen: out.value } }),
  }),
)

const files = [
  'index.ts',
  'view.ts',
  'update.ts',
  'model.ts',
  'message.ts',
  'routes.ts',
  'theme.ts',
]

// More tabs than fit: one line that scrolls, its edges fading where more lies
// past, and focus scrolling a tab into view.
export default example({
  title: 'Scrolling',
  slots: TabsSlots,
  recipe: Recipes.Tabs,
  pins: { overflow: 'scroll' },
  state: App.placements.runtime({ Model: App.Model, initial: { chosen: 'view.ts' } }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [h.Style({ inlineSize: 'min(20rem, 100%)' })],
      [
        Tabs.view(
          {
            model: model.tabs,
            onMessage: message => onMessage(App.Message.GotTabsMessage({ message })),
            label: 'Open files',
            value: model.chosen,
            tabs: files.map(file => ({ value: file, label: file })),
            style,
            mixins: [...Tabs.fluid],
          },
          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.

Disabled, chosen with Enter #

Three projects.

import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Recipes, Tabs, TabsSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const App = Bundle.compose({ plan: Schema.String }).pipe(
  Bundle.withChild('tabs', Bundles.Tabs, {
    // `Manual`: the arrows move focus, and Enter or Space chooses.
    args: { id: 'plans', activationMode: 'Manual' },
    onOut: out => model => ({ model: { ...model, plan: out.value } }),
  }),
)

// A row may be `disabled`: it stays in the list, and the arrows pass over it.
export default example({
  title: 'Disabled, chosen with Enter',
  slots: TabsSlots,
  recipe: Recipes.Tabs,
  state: App.placements.runtime({ Model: App.Model, initial: { plan: 'free' } }),
  view: ({ style, model, onMessage, h }) =>
    Tabs.view(
      {
        model: model.tabs,
        onMessage: message => onMessage(App.Message.GotTabsMessage({ message })),
        label: 'Plans',
        value: model.plan,
        tabs: [
          { value: 'free', label: 'Free', panel: h.p([], ['Three projects.']) },
          { value: 'team', label: 'Team', panel: h.p([], ['Unlimited projects.']) },
          { value: 'enterprise', label: 'Enterprise', disabled: 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.

Configure once #

Recipes and tokens.

import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Recipes, Tabs, TabsSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Pills = Recipes.Tabs.style({ variant: 'pill' })

// The look every tab list of the application starts from, and a named preset
// over it. `fluid` is imported here, in the application's own module, so a
// site that never names it never loads it.
const AppTabs = Tabs.configure({
  base: { style: Pills, mixins: [...Tabs.fluid] },
  presets: { sidebar: { orientation: 'vertical' } },
})

const App = Bundle.compose({ topic: Schema.String }).pipe(
  Bundle.withChild('tabs', Bundles.Tabs, {
    args: { id: 'topics' },
    onOut: out => model => ({ model: { ...model, topic: out.value } }),
  }),
)

export default example({
  title: 'Configure once',
  slots: TabsSlots,
  recipe: Recipes.Tabs,
  state: App.placements.runtime({ Model: App.Model, initial: { topic: 'styles' } }),
  view: ({ model, onMessage, h }) =>
    AppTabs.view(
      {
        model: model.tabs,
        onMessage: message => onMessage(App.Message.GotTabsMessage({ message })),
        label: 'Topics',
        preset: 'sidebar',
        value: model.topic,
        tabs: [
          { value: 'styles', label: 'Styles', panel: h.p([], ['Recipes and tokens.']) },
          { value: 'behaviour', label: 'Behaviour', panel: h.p([], ['Mounts and keys.']) },
        ],
      },
      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 ↗

  • Keys of your own Add the application’s keys with the routes option, beside the ones the widget handles; the widget’s conflict(routes) judges a candidate set without drawing it.
  • Right to left The direction switch above turns the preview over; this is what each widget reads.