Components

Tree view

Nested rows that open and shut, one tab stop, the arrows moving through what shows.

Previews are drawn in the theme you made.

Edit it
guides
scheme
direction

Basic #

  • src
    • index.ts
  • pnpm-lock.yaml
  • README.md

Nothing chosen yet.

import { Option } from 'effect'
import { Bundles, Recipes, TreeView, TreeViewSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

// The tree's structure goes to the Bundle once: its keys, typeahead and checks
// read it. The view is given the same tree. `TreeView.fluid` glides one hover
// from row to row, after the pointer and the keys.
const files: ReadonlyArray<TreeView.TreeNode> = [
  {
    id: 'src',
    label: 'src',
    children: [
      {
        id: 'components',
        label: 'components',
        children: [
          { id: 'button', label: 'button.ts' },
          { id: 'tree', label: 'tree.ts' },
        ],
      },
      { id: 'index', label: 'index.ts' },
    ],
  },
  { id: 'tests', label: 'tests', children: [{ id: 'tree-test', label: 'tree.test.ts' }] },
  { id: 'lock', label: 'pnpm-lock.yaml', disabled: true },
  { id: 'readme', label: 'README.md' },
]

export default example({
  title: 'Basic',
  slots: TreeViewSlots,
  recipe: Recipes.TreeView,
  state: Bundles.TreeView.with({ id: 'tree-basic', nodes: files, open: ['src'] }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [h.Style({ display: 'grid', gap: '0.75rem', inlineSize: '16rem' })],
      [
        TreeView.view(
          { label: 'Files', model, nodes: files, onMessage, style, mixins: TreeView.fluid },
          h,
        ),
        h.p(
          [],
          [
            Option.match(model.selected, {
              onNone: () => 'Nothing chosen yet.',
              onSome: id => `Chose ${id}.`,
            }),
          ],
        ),
      ],
    ),
})

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 guides #

  • src
    • index.ts
  • README.md
import { Bundles, Recipes, TreeView, TreeViewSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'

const FOLDER = 'M1.5 4.5h4l1.5 1.5h7.5v6.5h-13z'
const FOLDER_OPEN = 'M1.5 4.5h4l1.5 1.5h6v2M1.5 12.5l2-4.5h11l-2 4.5z'
const FILE = 'M4 1.5h5l3 3v10H4zM9 1.5v3h3'

const files: ReadonlyArray<TreeView.TreeNode> = [
  {
    id: 'src',
    label: 'src',
    children: [
      { id: 'index', label: 'index.ts' },
      {
        id: 'components',
        label: 'components',
        children: [
          { id: 'button', label: 'button.ts' },
          { id: 'tree', label: 'tree.ts' },
        ],
      },
    ],
  },
  { id: 'readme', label: 'README.md' },
]

// A folder or file before each label, and a guide down each open folder. An
// icon is drawn by the view's builder, so the view decorates the tree the
// Bundle holds the structure of.
export default example({
  title: 'Icons and guides',
  slots: TreeViewSlots,
  recipe: Recipes.TreeView,
  pins: { guides: 'lines' },
  state: Bundles.TreeView.with({ id: 'tree-icons', nodes: files, open: ['src'] }),
  view: ({ style, model, onMessage, h }) => {
    const decorated = (level: ReadonlyArray<TreeView.TreeNode>): ReadonlyArray<TreeView.TreeNode> =>
      level.map(node =>
        node.children !== undefined
          ? {
              ...node,
              icon: outline(h, FOLDER),
              openIcon: outline(h, FOLDER_OPEN),
              children: decorated(node.children),
            }
          : { ...node, icon: outline(h, FILE) },
      )
    return h.div(
      [h.Style({ inlineSize: '16rem' })],
      [
        TreeView.view(
          {
            label: 'Files',
            model,
            nodes: decorated(files),
            onMessage,
            style,
            mixins: TreeView.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.

Checkboxes #

  • src
    • index.ts
  • README.md

Staged: index.

import { Bundles, Recipes, TreeView, TreeViewSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const files: ReadonlyArray<TreeView.TreeNode> = [
  {
    id: 'src',
    label: 'src',
    children: [
      { id: 'index', label: 'index.ts' },
      {
        id: 'components',
        label: 'components',
        children: [
          { id: 'button', label: 'button.ts' },
          { id: 'tree', label: 'tree.ts' },
        ],
      },
    ],
  },
  { id: 'readme', label: 'README.md' },
]

// `checkable` is stated once, in the Bundle's args: its `update` and the view
// both read it from the Model. `checked` holds the files alone; a folder reads
// checked or mixed from them. `checkedAs('trunk')` lists the outermost fully
// checked rows, so a checked folder stands for everything in it.
export default example({
  title: 'Checkboxes',
  slots: TreeViewSlots,
  recipe: Recipes.TreeView,
  state: Bundles.TreeView.with({
    id: 'tree-checks',
    nodes: files,
    checkable: true,
    open: ['src'],
    checked: ['index'],
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [h.Style({ display: 'grid', gap: '0.75rem', inlineSize: '16rem' })],
      [
        TreeView.view(
          {
            label: 'Files to commit',
            model,
            nodes: files,
            onMessage,
            style,
            mixins: TreeView.fluid,
          },
          h,
        ),
        h.p(
          [],
          [
            model.checked.length === 0
              ? 'Nothing staged.'
              : `Staged: ${TreeView.checkedAs(files, model.checked, 'trunk').join(', ')}.`,
          ],
        ),
      ],
    ),
})

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.

People and roles #

  • Engineering
    • Ada LovelaceLead
    • Alan TuringStaff
import { Avatar, Badge, Bundles, Recipes, TreeView, TreeViewSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { portrait } from '../../snippets/portrait.js'

const Face = Recipes.Avatar.style({ size: 'xs' })
const Mark = Recipes.Badge.style({})

const org: ReadonlyArray<TreeView.TreeNode> = [
  {
    id: 'eng',
    label: 'Engineering',
    children: [
      { id: 'ada', label: 'Ada Lovelace' },
      { id: 'alan', label: 'Alan Turing' },
    ],
  },
  { id: 'ops', label: 'Operations', children: [{ id: 'grace', label: 'Grace Hopper' }] },
]

// A node's `content` is drawn in place of its label (which still names the row
// and is what typing matches), `trailing` after it and `action` at the row's
// end. All three are Html, so the avatar, the role and the button are the
// application's own.
export default example({
  title: 'People and roles',
  slots: TreeViewSlots,
  recipe: Recipes.TreeView,
  state: Bundles.TreeView.with({ id: 'tree-people', nodes: org, open: ['eng'] }),
  view: ({ style, model, onMessage, h }) => {
    const person = (id: string, name: string, role: string, hue: number): TreeView.TreeNode => ({
      id,
      label: name,
      icon: Avatar.view({ name, src: portrait(hue), style: Face }, h),
      trailing: Badge.view({ text: role, style: Mark }, h),
    })
    const nodes: ReadonlyArray<TreeView.TreeNode> = [
      {
        ...org[0]!,
        children: [
          person('ada', 'Ada Lovelace', 'Lead', 210),
          person('alan', 'Alan Turing', 'Staff', 140),
        ],
      },
      { ...org[1]!, children: [person('grace', 'Grace Hopper', 'Lead', 20)] },
    ]
    return h.div(
      [h.Style({ inlineSize: '20rem' })],
      [
        TreeView.view(
          { label: 'People', model, nodes, onMessage, style, mixins: TreeView.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.

Configure once #

  • Guides
    • Getting started
    • Theming
  • API
import { Bundles, Recipes, TreeView, TreeViewSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Guided = Recipes.TreeView.style({ guides: 'lines' })

// What every tree of an application starts from: its look and its glide, said
// once. `fluid` is a value, so a tree that names it is the only one to ship it.
const AppTree = TreeView.configure({
  base: { style: Guided, mixins: TreeView.fluid },
  presets: { right: { direction: 'rtl' } },
})

const topics: ReadonlyArray<TreeView.TreeNode> = [
  {
    id: 'guides',
    label: 'Guides',
    children: [
      { id: 'start', label: 'Getting started' },
      { id: 'theme', label: 'Theming' },
    ],
  },
  { id: 'api', label: 'API' },
]

export default example({
  title: 'Configure once',
  slots: TreeViewSlots,
  recipe: Recipes.TreeView,
  state: Bundles.TreeView.with({ id: 'tree-configure', nodes: topics, open: ['guides'] }),
  view: ({ model, onMessage, h }) =>
    h.div(
      [h.Style({ inlineSize: '16rem' })],
      [AppTree.view({ label: 'Docs', model, nodes: topics, onMessage }, 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 ↗

  • Right to left The direction switch above turns the preview over; this is what each widget reads.