Components

Table

Rows and columns, numbers aligned to the end; sorted, chosen and opened by the application.

Previews are drawn in the theme you made.

Edit it
rows
density
borders
layout
header
scheme
direction

Basic #

Recent invoices
InvoiceStatusAmount
INV-001Paid$250.00
INV-002Pending$150.00
INV-003Paid$350.00
INV-004Overdue$450.00
import { Recipes, Table, TableSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { invoices, money, type Invoice } from '../../snippets/invoices.js'

// A table is columns and rows; a column's `value` is text or markup. The row
// header (`th scope="row"`) is what a screen reader names a row by.
export default example({
  title: 'Basic',
  slots: TableSlots,
  recipe: Recipes.Table,
  view: ({ style, h }) =>
    Table.view(
      {
        caption: 'Recent invoices',
        columns: [
          { header: 'Invoice', rowHeader: true, value: (row: Invoice) => row.id },
          { header: 'Status', value: row => row.status },
          { header: 'Amount', numeric: true, value: row => money(row.amount) },
        ],
        rows: invoices,
        style,
      },
      h,
    ),
})

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

Sortable headings #

Recent invoices
INV-001Paid$250.00
INV-002Pending$150.00
INV-003Paid$350.00
INV-004Overdue$450.00
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Recipes, Table, TableSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { invoices, money, type Invoice } from '../../snippets/invoices.js'

const Key = Schema.Literals(['id', 'status', 'amount'])
const Model = Schema.Struct({
  sort: Schema.Option(
    Schema.Struct({ column: Key, direction: Schema.Literals(['ascending', 'descending']) }),
  ),
})
const Message = defineMessageUnion({ Sorted: { column: Key } })

const order = {
  id: (a: Invoice, b: Invoice) => a.id.localeCompare(b.id),
  status: (a: Invoice, b: Invoice) => a.status.localeCompare(b.status),
  amount: (a: Invoice, b: Invoice) => a.amount - b.amount,
}

// The table draws the order it is given; the application sorts. A press on a
// heading sends the column, and `Table.nextSort` cycles ascending,
// descending, none.
export default example({
  title: 'Sortable headings',
  slots: TableSlots,
  recipe: Recipes.Table,
  state: {
    Model,
    Message,
    init: () => ({ model: { sort: Option.none() } }),
    update: (model, { column }) => ({
      model: modifyFields(model, { sort: sort => Table.nextSort(sort, column) }),
    }),
  },
  view: ({ style, model, onMessage, h }) =>
    Table.view(
      {
        caption: 'Recent invoices',
        columns: [
          { header: 'Invoice', sortKey: 'id', value: (row: Invoice) => row.id },
          { header: 'Status', sortKey: 'status', value: row => row.status },
          { header: 'Amount', sortKey: 'amount', numeric: true, value: row => money(row.amount) },
        ],
        rows: Option.match(model.sort, {
          onNone: () => invoices,
          onSome: ({ column, direction }) =>
            [...invoices].sort(
              (a, b) => (direction === 'ascending' ? 1 : -1) * order[column](a, b),
            ),
        }),
        rowKey: row => row.id,
        sort: { by: model.sort, onSort: column => onMessage(Message.Sorted({ column })) },
        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.

Row selection #

Select all rowsInvoiceStatusAmount
Select INV-001INV-001Paid$250.00
Select INV-002INV-002Pending$150.00
Select INV-003INV-003Paid$350.00
Select INV-004INV-004Overdue$450.00
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Recipes, Table, TableSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { invoices, money, type Invoice } from '../../snippets/invoices.js'

const Model = Schema.Struct({ chosen: Schema.Array(Schema.String) })
const Message = defineMessageUnion({
  Chose: { id: Schema.String, selected: Schema.Boolean },
  ChoseAll: { ids: Schema.Array(Schema.String) },
})

const Boxes = Recipes.Checkbox.style({})

// The rows chosen are the application's. `control` is the one place Checkbox
// is imported: a table that selects nothing never loads it.
export default example({
  title: 'Row selection',
  slots: TableSlots,
  recipe: Recipes.Table,
  state: {
    Model,
    Message,
    init: () => ({ model: { chosen: ['INV-002'] } }),
    update: (model, message) => ({
      model: Message.match(message, {
        Chose: ({ id, selected }) =>
          modifyFields(model, {
            chosen: chosen => [...chosen.filter(each => each !== id), ...(selected ? [id] : [])],
          }),
        ChoseAll: ({ ids }) => modifyFields(model, { chosen: () => ids }),
      }),
    }),
  },
  view: ({ style, model, onMessage, h }) =>
    Table.view(
      {
        columns: [
          { header: 'Invoice', value: (row: Invoice) => row.id },
          { header: 'Status', value: row => row.status },
          { header: 'Amount', numeric: true, value: row => money(row.amount) },
        ],
        rows: invoices,
        rowKey: row => row.id,
        selection: {
          id: 'invoices-chosen',
          isSelected: row => model.chosen.includes(row.id),
          onToggle: (row, selected) => onMessage(Message.Chose({ id: row.id, selected })),
          onToggleAll: all =>
            onMessage(Message.ChoseAll({ ids: all ? invoices.map(row => row.id) : [] })),
          rowLabel: row => `Select ${row.id}`,
          control: Table.checkboxes(Boxes),
        },
        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.

Expandable rows #

DetailsInvoiceStatusAmount
INV-001Paid$250.00
INV-002Pending$150.00
INV-003Paid$350.00
INV-004Overdue$450.00

INV-004 was sent on the 1st and is overdue.

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Recipes, Table, TableSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { invoices, money, type Invoice } from '../../snippets/invoices.js'

const Model = Schema.Struct({ open: Schema.Array(Schema.String) })
const Message = defineMessageUnion({ Opened: { id: Schema.String, expanded: Schema.Boolean } })

// A row opens on a press to show its detail in a row below it. Which rows are
// open is the application's.
export default example({
  title: 'Expandable rows',
  slots: TableSlots,
  recipe: Recipes.Table,
  state: {
    Model,
    Message,
    init: () => ({ model: { open: ['INV-004'] } }),
    update: (model, { id, expanded }) => ({
      model: modifyFields(model, {
        open: open => [...open.filter(each => each !== id), ...(expanded ? [id] : [])],
      }),
    }),
  },
  view: ({ style, model, onMessage, h }) =>
    Table.view(
      {
        columns: [
          { header: 'Invoice', value: (row: Invoice) => row.id },
          { header: 'Status', value: row => row.status },
          { header: 'Amount', numeric: true, value: row => money(row.amount) },
        ],
        rows: invoices,
        rowKey: row => row.id,
        expansion: {
          id: 'invoices-open',
          isExpanded: row => model.open.includes(row.id),
          onToggle: (row, expanded) => onMessage(Message.Opened({ id: row.id, expanded })),
          detail: row =>
            h.p([], [`${row.id} was sent on the 1st and is ${row.status.toLowerCase()}.`]),
          label: row => `Details of ${row.id}`,
        },
        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.

Recent invoices
InvoiceStatusAmount (USD)
INV-001Paid$250.00
INV-002Pending$150.00
INV-003Paid$350.00
INV-004Overdue$450.00
4 invoices$1200.00
import { Recipes, Table, TableSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { invoices, money, type Invoice } from '../../snippets/invoices.js'

const total = invoices.reduce((sum, invoice) => sum + invoice.amount, 0)

// A column's `footer` draws a totals row after the body, and `header` takes
// markup as well as text.
export default example({
  title: 'Totals',
  slots: TableSlots,
  recipe: Recipes.Table,
  view: ({ style, h }) =>
    Table.view(
      {
        caption: 'Recent invoices',
        columns: [
          {
            header: 'Invoice',
            rowHeader: true,
            value: (row: Invoice) => row.id,
            footer: `${invoices.length} invoices`,
          },
          { header: 'Status', value: row => row.status },
          {
            header: h.span([], ['Amount (USD)']),
            numeric: true,
            value: row => money(row.amount),
            footer: money(total),
          },
        ],
        rows: invoices,
        style,
      },
      h,
    ),
})

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

Following highlight #

InvoiceStatusAmount
INV-001Paid$250.00
INV-002Pending$150.00
INV-003Paid$350.00
INV-004Overdue$450.00
import { Recipes, Table, TableSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { invoices, money, type Invoice } from '../../snippets/invoices.js'

// One highlight behind the rows follows the pointer to the nearest one. It is
// a value you pass: a table that does not draws none and ships none of it.
export default example({
  title: 'Following highlight',
  slots: TableSlots,
  recipe: Recipes.Table,
  view: ({ style, h }) =>
    Table.view(
      {
        columns: [
          { header: 'Invoice', rowHeader: true, value: (row: Invoice) => row.id },
          { header: 'Status', value: row => row.status },
          { header: 'Amount', numeric: true, value: row => money(row.amount) },
        ],
        rows: invoices,
        style,
        fluid: Table.fluid,
      },
      h,
    ),
})

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

Vertical, boxed cells #

Subscription
FactValue
PlanTeam
Seats12 of 15
Renews1 March
import { Recipes, Table, TableSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

interface Fact {
  readonly name: string
  readonly value: string
}

const facts: ReadonlyArray<Fact> = [
  { name: 'Plan', value: 'Team' },
  { name: 'Seats', value: '12 of 15' },
  { name: 'Renews', value: '1 March' },
]

// A table read down its first column: that column is the row header, and
// `header: 'hidden'` keeps the headings for a screen reader alone.
export default example({
  title: 'Vertical, boxed cells',
  slots: TableSlots,
  recipe: Recipes.Table,
  pins: { header: 'hidden', borders: 'cells' },
  view: ({ style, h }) =>
    Table.view(
      {
        caption: 'Subscription',
        columns: [
          { header: 'Fact', rowHeader: true, value: (row: Fact) => row.name },
          { header: 'Value', value: row => row.value },
        ],
        rows: facts,
        style,
      },
      h,
    ),
})

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

Empty #

InvoiceStatus

No invoices yet

Invoices you send show up here.

import { Empty, Recipes, Table, TableSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import type { Invoice } from '../../snippets/invoices.js'

const Quiet = Recipes.Empty.style({ size: 'compact' })

// While there are no rows, `empty` stands across the table: an `Empty.view`.
export default example({
  title: 'Empty',
  slots: TableSlots,
  recipe: Recipes.Table,
  view: ({ style, h }) =>
    Table.view(
      {
        columns: [
          { header: 'Invoice', value: (row: Invoice) => row.id },
          { header: 'Status', value: row => row.status },
        ],
        rows: [],
        empty: Empty.view(
          {
            title: 'No invoices yet',
            description: 'Invoices you send show up here.',
            style: Quiet,
          },
          h,
        ),
        style,
      },
      h,
    ),
})

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

Sticky header, striped #

InvoiceStatusAmount
INV-001Paid$250.00
INV-002Pending$150.00
INV-003Paid$350.00
INV-004Overdue$450.00
INV-001-BPaid$250.00
INV-002-BPending$150.00
INV-003-BPaid$350.00
INV-004-BOverdue$450.00
import { Recipes, Table, TableSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { invoices, money, type Invoice } from '../../snippets/invoices.js'

// The look is the recipe's: a sticky heading, striped rows, compact density.
// These three are held whatever the controls above pick.
export default example({
  title: 'Sticky header, striped',
  slots: TableSlots,
  recipe: Recipes.Table,
  pins: { header: 'sticky', rows: 'striped', density: 'compact' },
  view: ({ style, h }) =>
    h.div(
      [h.Style({ maxBlockSize: '9rem', overflowY: 'auto' })],
      [
        Table.view(
          {
            columns: [
              { header: 'Invoice', rowHeader: true, value: (row: Invoice) => row.id },
              { header: 'Status', value: row => row.status },
              { header: 'Amount', numeric: true, value: row => money(row.amount) },
            ],
            rows: [...invoices, ...invoices.map(row => ({ ...row, id: `${row.id}-B` }))],
            style,
          },
          h,
        ),
      ],
    ),
})

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

Configure once #

Roomy, from the base
Invoice
INV-001
INV-002
Dense, from a preset
InvoiceAmount
INV-001$250.00
INV-002$150.00
The style picked above
Invoice
INV-001
INV-002
import { Recipes, Table, TableSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { invoices, money, type Invoice } from '../../snippets/invoices.js'

// Stated once, at module scope: the look, the words and the highlight every
// table of the application starts from, and a preset over it.
const Dense = Recipes.Table.style({ density: 'compact', rows: 'plain' })
const Roomy = Recipes.Table.style({ density: 'comfortable' })

const AppTable = Table.configure({
  base: { style: Roomy, fluid: Table.fluid, words: { selectRow: 'Pick this row' } },
  presets: { dense: { style: Dense } },
})

// Each use gives the columns and rows. The picked style at the call wins over both.
export default example({
  title: 'Configure once',
  slots: TableSlots,
  recipe: Recipes.Table,
  view: ({ style, h }) =>
    h.div(
      [h.Style({ display: 'grid', gap: '1.5rem' })],
      [
        AppTable.view(
          {
            caption: 'Roomy, from the base',
            columns: [{ header: 'Invoice', value: (row: Invoice) => row.id }],
            rows: invoices.slice(0, 2),
          },
          h,
        ),
        AppTable.view(
          {
            caption: 'Dense, from a preset',
            preset: 'dense',
            columns: [
              { header: 'Invoice', value: (row: Invoice) => row.id },
              { header: 'Amount', numeric: true, value: row => money(row.amount) },
            ],
            rows: invoices.slice(0, 2),
          },
          h,
        ),
        AppTable.view(
          {
            caption: 'The style picked above',
            columns: [{ header: 'Invoice', value: (row: Invoice) => row.id }],
            rows: invoices.slice(0, 2),
            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 ↗