Nothing picked yet

Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. Copy the component into your project

    The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.

    src/ui/menubar.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { Menu, Menubar, MenuTriggerOptions } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    type MenubarParts = Component.Headless.Parts<typeof Menubar>
    type MenuParts = Component.Headless.Parts<typeof Menu>
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    /**
     * The bar. Draw a `Menu` inside it for each menu, with a `MenubarTrigger` and
     * the popup and items from `#ui/menu`. The kit sets `data-orientation`, which
     * turns the bar into a column.
     */
    export const MenubarRoot = <Msg>(
      h: Html.H<Msg>,
      menubar: Pick<MenubarParts, "root">,
      options: ClassOption & { readonly label?: string | undefined } = {}
    ) =>
      menubar.root(
        h.div
          .class(cn(
            "group/menubar inline-flex items-center gap-0.5 rounded-md border bg-background p-[3px] shadow-sm",
            "data-[orientation=vertical]:min-w-40 data-[orientation=vertical]:flex-col data-[orientation=vertical]:items-stretch",
            options.class
          ))
          .attr("aria-label", options.label)
      )
    
    /** The trigger of one menu of the bar. In a vertical bar it points at the popup beside it. */
    export const MenubarTrigger = <Msg>(
      h: Html.H<Msg>,
      menu: Pick<MenuParts, "trigger">,
      options: MenuTriggerOptions & ClassOption = {}
    ) => {
      const { class: className, ...trigger } = options
      return menu.trigger({
        ...trigger,
        element: h.button.class(cn(
          "flex h-7 cursor-default items-center gap-2 rounded-sm px-2.5 text-[13px] font-medium text-foreground select-none",
          "transition-colors duration-150 hover:bg-accent hover:text-accent-foreground focus-visible:outline-offset-0",
          "data-[popup-open]:bg-accent data-[popup-open]:text-accent-foreground",
          "data-[disabled]:pointer-events-none data-[disabled]:opacity-45",
          "group-data-[orientation=vertical]/menubar:w-full",
          "group-data-[orientation=vertical]/menubar:after:ml-auto group-data-[orientation=vertical]/menubar:after:size-1.5",
          "group-data-[orientation=vertical]/menubar:after:rotate-45 group-data-[orientation=vertical]/menubar:after:border-t-[1.5px]",
          "group-data-[orientation=vertical]/menubar:after:border-r-[1.5px] group-data-[orientation=vertical]/menubar:after:border-muted-foreground",
          "group-data-[orientation=vertical]/menubar:after:content-['']",
          className
        ))
      })
    }
    
  3. Import it

    import { MenubarRoot, MenubarTrigger } from "#ui/menubar"
    

Usage

Basic.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Menu, Menubar } from "@effect/ui-kit"
import { Schema } from "effect"
import { MenuItem, MenuPopup, MenuPositioner, MenuSeparator, MenuShortcut } from "#ui/menu"
import { MenubarRoot, MenubarTrigger } from "#ui/menubar"

const MenubarBasicMessage = UiSchema.TaggedEnum({
  RequestedOpenMenu: { menu: Schema.String },
  RequestedCloseMenu: { menu: Schema.String },
  PickedMenuItem: { value: Schema.String }
})

const menus = [
  {
    name: "File",
    sections: [
      [
        { value: "new-tab", label: "New tab", shortcut: "⌘T" },
        { value: "new-window", label: "New window", shortcut: "⌘N" }
      ],
      [
        { value: "share", label: "Share…" },
        { value: "print", label: "Print…", shortcut: "⌘P" }
      ]
    ]
  },
  {
    name: "Edit",
    sections: [
      [
        { value: "undo", label: "Undo", shortcut: "⌘Z" },
        { value: "redo", label: "Redo", shortcut: "⇧⌘Z" }
      ],
      [
        { value: "cut", label: "Cut", shortcut: "⌘X" },
        { value: "copy", label: "Copy", shortcut: "⌘C" },
        { value: "paste", label: "Paste", shortcut: "⌘V" }
      ]
    ]
  },
  {
    name: "View",
    sections: [
      [
        { value: "reload", label: "Reload", shortcut: "⌘R" },
        { value: "fullscreen", label: "Toggle fullscreen", shortcut: "⌃⌘F" }
      ]
    ]
  }
]

export const MenubarBasic = Html.component({
  name: "MenubarBasic",
  model: Schema.Struct({ open: Schema.NullOr(Schema.String), picked: Schema.NullOr(Schema.String) }),
  message: MenubarBasicMessage
}).make({
  init: () => ({ open: null, picked: null }),
  update: {
    RequestedOpenMenu: ($, { menu }) => $.evolve({ open: menu }),
    // The menu that closes for another one can report after it has opened.
    RequestedCloseMenu: ($, { menu }) => $.model.open === menu ? $.evolve({ open: null }) : $.model,
    PickedMenuItem: ($, { value }) => $.evolve({ picked: value })
  },
  view: (h, model) =>
    h.div.class("flex flex-col items-center gap-4").children(
      Menubar(h, {
        props: {},
        view: (menubar) =>
          MenubarRoot(h, menubar, { label: "Application" }).children(
            menus.map(({ name, sections }) =>
              Menu(h, {
                key: name,
                props: { open: model.open === name, items: sections.flat(), sideOffset: 6 },
                onEmit: {
                  RequestedOpen: () => MenubarBasicMessage.RequestedOpenMenu({ menu: name }),
                  RequestedClose: () => MenubarBasicMessage.RequestedCloseMenu({ menu: name }),
                  Picked: ({ value }) => MenubarBasicMessage.PickedMenuItem({ value }),
                  Highlighted: () => undefined,
                  RequestedQuery: () => undefined,
                  Settled: () => undefined
                },
                view: (menu) =>
                  h.div.children(
                    MenubarTrigger(h, menu).children(name),
                    MenuPositioner(h, menu).children(
                      MenuPopup(h, menu).children(
                        sections.flatMap((section, index) => [
                          index === 0 ? undefined : MenuSeparator(h, menu),
                          ...section.map(({ label, shortcut, value }) =>
                            MenuItem(h, menu, { value }).children(label, shortcut === undefined ? undefined : MenuShortcut(h).children(shortcut))
                          )
                        ])
                      )
                    )
                  )
              })
            )
          )
      }),
      h.p.class("text-xs text-muted-foreground tabular-nums").children(model.picked === null ? "Nothing picked yet" : `Picked ${model.picked}`)
    )
})

Examples

ArrowRight opens the highlighted item's submenu, and otherwise moves to the next menu.

Nothing picked yet

Checkbox and radio items

A View menu of checkbox items and a Theme menu with one radio group.

Theme: system. Panels: sidebar

Vertical

With orientation "vertical" the popups open beside the bar, and ArrowDown and ArrowUp move between the triggers.

ArrowDown and ArrowUp move between the menus

Disabled menus and items, not modal

A disabled menu is skipped by the arrow keys. With modal false, the page keeps scrolling while a menu is open.

Selection is disabled, and so is Save

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
rootThe <div> around the menus, with role="menubar". It adds no markup.
MenuEach menu is an ordinary Menu call inside the root, with a key. Its trigger becomes a menuitem of the bar.

API reference

Props

PropTypeDescription
orientation"horizontal" | "vertical"Which arrow keys move between the triggers, and which side the popups open on.
modalbooleanWhile a menu is open, the page doesn't scroll and a press outside only closes the menu. True by default.
disabledbooleanEvery menu in the menubar ignores the user.
loopFocusbooleanThe arrow keys wrap from the last trigger to the first. True by default.

Events

EventDescription
(none)The menubar emits nothing. Each Menu reports its own RequestedOpen, RequestedClose and Picked: keep which one is open as one value in the Model.

Data attributes

AttributeDescription
data-orientationOn the root: "horizontal" or "vertical".
data-modalOn the root while the menubar is modal.
data-has-submenu-openOn the root while one of its menus is open.
data-popup-openOn the trigger of the open menu.
data-instant="group"On the positioner and the popup when the open menu moves to another trigger: turn the transitions off.
data-highlighted / data-checked / data-disabledOn the items, as in a Menu.

The components this page's file and examples import.

  • Menu: A button that opens a list of actions, links, checkbox and radio items, and submenus.

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.