Components

Navigation menu

The links of a site in a row or a column, the current page marked by a line that glides to it, and panels of links under its triggers.

Previews are drawn in the theme you made.

Edit it
panel
indicator
scheme
direction
export const NavStyle = forSlots(NavSlots)(Recipes.Nav({}))

const NavPage = Schema.Literals(['Overview', 'Projects', 'Team', 'Settings'])
type NavPage = typeof NavPage.Type

export const nav = <Message>(style: NamedStyle<typeof NavSlots>, h: HtmlBuilder<Message>): Html =>
  UiNav.view<NavPage>({
    items: NavPage.literals,
    ariaLabel: 'Workspace',
    // The demo's pages are not on this site, so each link stays on this page.
    toHref: () => '#',
    isItemCurrent: page => page === 'Projects',
    // The current link's line glides, and a faint pill follows the pointer.
    toView: Nav.toView<NavPage, Message>([style.mixin, ...Nav.fluid], { h }, page => page),
  })

Panels #

// A trigger shows a panel of links in the top layer below it; which one shows is Nav's Model.
export const siteNav: ReadonlyArray<Nav.NavEntry> = [
  { label: 'Overview', href: '#' },
  {
    id: 'packages',
    label: 'Packages',
    groups: [
      {
        title: 'Build',
        links: [
          { label: 'Mixins', href: '#', description: 'Slots, styles and behaviors.' },
          { label: 'Mixins UI', href: '#', description: 'Components on the slots.', current: true },
        ],
      },
      {
        title: 'Data',
        links: [
          { label: 'Remote', href: '#', description: 'Server facts, cached and live.' },
          { label: 'Sync', href: '#', description: 'Durable operations, offline first.' },
        ],
      },
    ],
  },
  { id: 'learn', label: 'Learn', groups: [{ links: [{ label: 'Guides', href: '#' }] }] },
  { label: 'Blog', href: '#' },
]

export const navPanels = <Message>(
  style: NamedStyle<typeof NavSlots>,
  model: Nav.Model,
  onMessage: (message: Nav.Message) => Message,
  h: HtmlBuilder<Message>,
): Html =>
  Nav.view(
    { model, entries: siteNav, label: 'Site', onMessage, mixins: [style.mixin, ...Nav.fluid] },
    h,
  )

A pill #

export const NavPillStyle = forSlots(NavSlots)(Recipes.Nav({ indicator: 'pill' }))

Vertical #

// A column: the arrows go up and down, and a panel opens beside it.
export const navVertical = <Message>(
  style: NamedStyle<typeof NavSlots>,
  model: Nav.Model,
  onMessage: (message: Nav.Message) => Message,
  h: HtmlBuilder<Message>,
): Html =>
  Nav.view(
    {
      model,
      entries: siteNav,
      label: 'Site',
      orientation: 'vertical',
      onMessage,
      mixins: [style.mixin, ...Nav.fluid],
    },
    h,
  )

A grid with icons and a footer #

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

// The open panel is the Bundle's; the current page is the application's, set
// on each entry's `current`.
const App = Bundle.compose({ page: Schema.String }).pipe(
  Bundle.withChild('nav', Bundles.Nav, { args: { id: 'grid-nav' } }),
)

// `icon` stands before a link's words, in a column of its own beside a panel
// link's two lines. `panel: 'grid'` lays the groups in equal columns, and
// `footer` is Html after them, in a slot of its own: no link, so no key stops on it.
export default example({
  title: 'A grid with icons and a footer',
  slots: NavSlots,
  recipe: Recipes.Nav,
  pins: { panel: 'grid' },
  state: App.placements.runtime({ Model: App.Model, initial: { page: '' } }),
  view: ({ style, model, onMessage, h }) =>
    Nav.view(
      {
        model: model.nav,
        onMessage: message => onMessage(App.Message.GotNavMessage({ message })),
        label: 'Site',
        mixins: [style.mixin],
        entries: [
          { label: 'Home', href: '#', icon: outline(h, 'M2 7l6-5 6 5v7H2zM6 14v-4h4v4') },
          {
            id: 'products',
            label: 'Products',
            groups: [
              {
                title: 'Build',
                links: [
                  {
                    label: 'Mixins',
                    href: '#',
                    description: 'Slots, styles and behaviours.',
                    icon: outline(h, 'M2 5l6-3 6 3v6l-6 3-6-3zM2 5l6 3 6-3M8 8v6'),
                  },
                ],
              },
              {
                title: 'Run',
                links: [
                  {
                    label: 'Sync',
                    href: '#',
                    description: 'Offline edits, in order.',
                    icon: outline(h, 'M3 8a5 5 0 0 1 9-3M13 8a5 5 0 0 1-9 3'),
                  },
                ],
              },
            ],
            footer: h.a([h.Href('#')], ['Learn more about the packages']),
          },
        ],
      },
      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 #

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

const Pill = Recipes.Nav.style({ indicator: 'pill' })

// The look and the direction every navigation of the application starts
// from, and a named preset over them. A use gives the Model and the entries.
const AppNav = Nav.configure({
  base: { mixins: [Pill.mixin, ...Nav.fluid] },
  presets: { sidebar: { orientation: 'vertical' } },
})

const App = Bundle.compose({ page: Schema.String }).pipe(
  Bundle.withChild('nav', Bundles.Nav, { args: { id: 'sidebar-nav' } }),
)

export default example({
  title: 'Configure once',
  slots: NavSlots,
  recipe: Recipes.Nav,
  state: App.placements.runtime({ Model: App.Model, initial: { page: '' } }),
  view: ({ model, onMessage, h }) =>
    AppNav.view(
      {
        model: model.nav,
        onMessage: message => onMessage(App.Message.GotNavMessage({ message })),
        label: 'Sections',
        preset: 'sidebar',
        entries: [
          { label: 'Overview', href: '#', current: true },
          { label: 'Settings', href: '#' },
          { label: 'Members', href: '#' },
        ],
      },
      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.