Mixins / foldkit-mixins-ui

Menu

namespace · packages/mixins-ui/src/menu.ts:52

export * as Menu from './menu.js'

A menu, opened by a button or at a point (a context menu), whose rows may open submenus, in three layers, each built on the one before.

The fork (MenuView, ./menuView.ts) is @foldkit/ui's Menu view with a toView seam upstream lacks; the Menu's Model, update and Mounts stay upstream's.

The panel adapter (MenuSlots, resolve, toView, fluid, overlayBars, drawAtPoint, pointTarget) styles a Menu drawn through the fork, whoever keeps its Model: toView is that Menu's toView. Menubar draws its menus through it, and so does an application that folds @foldkit/ui's Menu itself, for typed items or groups, which the component's rows do not have; drawAtPoint and pointTarget open such a Menu at a point. One recipe, Recipes.Menu, styles every panel and the rows' words and chevrons.

The component (init, update, view, target) is a menu of string rows drawn through the adapter. Its Model holds @foldkit/ui Menus, one for the menu and one per row with items, so each keeps its own keys, typeahead and rows, and update sorts out which Menu a key belongs to. A plain menu is a menu none of whose rows have items, and a context menu is the same menu with opensAt: 'point': a submenu, a button and a point are each one difference in the same Model.

Submenus are one level deep. A submenu is drawn inside its row, so its own anchor places it beside the row: its button is a mark the size of the row that nothing sees, focuses or presses. Being inside the row, every key, click and release in a submenu also bubbles to the menu's panel, which answers in the Menu's Messages. update reads those against its own Model: while a submenu is open, focus is in it, so the menu's key Messages and its blur are the submenu's and are dropped, and the menu's Closed is the submenu's Escape. The submenu's own Closed is dropped for that reason, and it draws no click-away: the menu's serves both, closing everything on pointerdown. The keys the Menu does not answer (ArrowRight, ArrowLeft, and Tab while a submenu is open) are heard on the menu's panel.

A submenu's row is a menuitem with aria-haspopup="menu" and aria-expanded. ArrowRight, Enter or Space on it opens the submenu with its first row active, and ArrowLeft or Escape closes it back to that row; under direction: 'rtl' the submenu opens to the left and the two arrows swap. Resting the pointer on it opens it after a short delay, and resting on another row closes it. Choosing a submenu's row closes the whole menu.

At a point, target goes on the area that opens the menu: a right press records its point, and the contextmenu that follows (a right press, Shift+F10 or the ContextMenu key, the browser's own menu kept shut) opens the menu there, its button a mark of no size the Menu's anchor places the rows against. Opened from the keyboard, with no press before it, the mark stands where the menu is drawn.

In the guides

Members

Menu.checkChosen

function · packages/mixins-ui/src/menu.ts:334

function checkChosen<Value extends string>(
  rows: ReadonlyArray<Row<Value>>,
  message: UiMenu.Message,
): Option.Option<CheckRow<Value>>

The check row a Menu Message chooses, if it chooses one. Send its Selected in place of the Message: the Menu's own update would close.

Menu.CheckRow

interface · packages/mixins-ui/src/menu.ts:250

interface CheckRow<Value extends string = string> extends RowParts<Value> {
  readonly check: 'checkbox' | 'radio'
  readonly checked: boolean
  readonly items?: undefined
}

A row that is on or off: menuitemcheckbox or menuitemradio, with aria-checked. Choosing it sends Selected and leaves the menu open, as Base UI's checkbox and radio items do, so several can be set in one visit; the application keeps checked, and a radio row's group is the rows of its group.

Menu.configure

function · packages/mixins-ui/src/menu.ts:1255

function configure<const Names extends string = never>(
  config: Presets<MenuPreset, Names>,
): {
  readonly view: <Outer, Value extends string = string>(
    options: Omit<MenuViewOptions<Outer, Value>, 'mixins'> & {
      readonly mixins?: MixinList<Outer> | undefined
    } & PresetOption<Names>,
    h: HtmlBuilder<Outer>,
  ) => Html
}

The application's own Menu: base for every use, presets chosen at a use with preset, and the call over both. mixins here and at the call concatenate, so a use may draw with the base's alone.

Menu.drawAtPoint

function · packages/mixins-ui/src/menu.ts:511

function drawAtPoint<Message>(
  point: Option.Option<Point>,
  h: HtmlBuilder<Message>,
): (resolved: ResolvedMenu<Message>) => Html

A draw for toView that opens a @foldkit/ui Menu at a point, for an application that folds the Menu itself: its button becomes a mark of no size at point (where it is drawn, with none), which the Menu's anchor places the rows against, so give the anchor no gap. The application keeps the point, from pointTarget's press, and opens the Menu with its Opened Message on pointTarget's onOpen.

Menu.fluid

const · packages/mixins-ui/src/menu/fluid.ts:11

const fluid: readonly [Mixin<never>]

What moves the open panel's highlight: on its rows, one highlight follows the pointer to the nearest row (--fk-popup-hover-*), and to the row the keys mark (data-active) while the pointer is away. Pass them beside the style; until they run, each row marks itself.

Menu.grouping

function · packages/mixins-ui/src/menu.ts:305

function grouping<Message>(
  rows: ReadonlyArray<{
    readonly group?: string | undefined
  }>,
  h: HtmlBuilder<Message>,
): Pick<UiMenu.ViewInputs<string>, 'itemGroupKey' | 'groupToHeading'>

The view inputs that set rows in their groups under their headings, for a Menu's viewInputs; none when no row names a group. A group is keyed by its first row, since a heading's words make no id.

Menu.HOLD_MS

const · packages/mixins-ui/src/menu.ts:542

const HOLD_MS: 500

How long a finger is held on the target area before the menu opens there.

Menu.init

function · packages/mixins-ui/src/menu.ts:628

function init<
  Value extends string,
  Structure extends {
    readonly value: Value
    readonly items?: ReadonlyArray<unknown> | undefined
  },
>(id: string, rows: ReadonlyArray<Structure>, options?: InitOptions): Model

A shut menu, and a shut submenu for each row with items.

Menu.InitOptions

interface · packages/mixins-ui/src/menu.ts:612

interface InitOptions {
  readonly isAnimated?: boolean | undefined
  readonly isModal?: boolean | undefined
}

How a menu opens and shuts; each is off by default, as upstream's.

Menu.ItemRow

interface · packages/mixins-ui/src/menu.ts:238

interface ItemRow<Value extends string = string> extends RowParts<Value> {
  readonly check?: undefined
  readonly items?: undefined
}

A row chosen to run it, which closes the menu.

Menu.keyShortcuts

function · packages/mixins-ui/src/menu.ts:297

function keyShortcuts(keys: ReadonlyArray<string>): string

The aria-keyshortcuts value of keys drawn as symbols: ['⌘', '⇧', 'P'] is Meta+Shift+P.

Menu.LeafRow

type · packages/mixins-ui/src/menu.ts:264

type LeafRow<Value extends string = string> = ItemRow<Value> | CheckRow<Value>

A row that opens nothing: what a Menubar's menus hold.

type · packages/mixins-ui/src/menu.ts:1245

type MenuPreset = Pick<
  MenuViewOptions<never>,
  'opensAt' | 'direction' | 'submenuMixins' | 'kbd' | 'anchor'
> & {
  readonly mixins?: MixinList<never>
}

What a Menu is given once: where it opens, its keys' look, and its mixins. The Model, rows and Messages are each use's.

const · packages/mixins-ui/src/menu/slots.ts:14

const MenuSlots: Slots.Contract<{ readonly wrapper: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>; readonly button: Slot.UnnamedSlot<Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>, readonly [Event.EventToken<"click">, Event.EventToken<"pointerdown">, Event.EventToken<"keydown">], readonly [Attr.AttrToken<"role">, Attr.AttrToken<"aria-expanded">, Attr.AttrToken<"disabled">, Attr.AttrToken<"aria-disabled">], readonly Slot.RequirementName[], boolean>; readonly backdrop: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly [Event.EventToken<"click">], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>; readonly items: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly [Event.EventToken<"keydown">, Event.EventToken<"pointerup">, Event.EventToken<"blur">], readonly [Attr.AttrToken<"role">], readonly Slot.RequirementName[], boolean>; readonly scroll: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]> …;

Menu is a nested Submodel: one button/backdrop/items group plus an item per entry (and group/heading/separator where the caller groups), so the resolver runs once per bundle and each base attribute passes through by identity.

The button owns click (and pointerdown/keydown while enabled); an item owns click while interactive. A Behavior adding its own handler to one of those is a conflict, not a second silent owner. Disabled buttons and items publish no click, so the contract still lists it as allowed.

interface · packages/mixins-ui/src/menu.ts:1013

interface MenuViewOptions<ParentMessage, Value extends string = string> {
  readonly model: Model
  readonly rows: ReadonlyArray<Row<Value>>
  readonly buttonContent: Html
  readonly isRowDisabled?: ((value: Value) => boolean) | undefined
  readonly onMessage: (message: Message) => NoInfer<ParentMessage>
  readonly opensAt?: 'button' | 'point' | undefined
  readonly direction?: 'ltr' | 'rtl' | undefined
  readonly mixins: MixinList<ParentMessage>
  readonly submenuMixins?: MixinList<ParentMessage> | undefined
  readonly kbd?: NamedStyle<typeof KbdSlots> | undefined
  readonly anchor?: AnchorConfig | undefined
  readonly header?: Html | undefined
  readonly footer?: Html | undefined
}

Menu.Message

const · packages/mixins-ui/src/menu.ts:576

const Message: MessageUnion<{ readonly GotMenu: { readonly message: MessageUnion<{ readonly Opened: { maybeActiveItemIndex: Schema.Option<Schema.Number>; }; readonly Closed: {}; readonly BlurredItems: {}; readonly ActivatedItem: { index: Schema.Number; activationTrigger: Schema.Literals<readonly ["Pointer", "Keyboard"]>; }; readonly DeactivatedItem: {}; readonly SelectedItem: { index: Schema.Number; item: Schema.String; }; readonly RequestedItemClick: { index: Schema.Number; }; readonly Searched: { key: Schema.String; maybeTargetIndex: Schema.Option<Schema.Number>; }; readonly CompletedDelayClearSearch: { version: Schema.Number; }; readonly MovedPointerOverItem: { index: Schema.Number; screenX: Schema.Number; screenY: Schema.Number; }; readonly CompletedFocusItems: {}; readonly CompletedFocusButton: {}; readonly CompletedLockScroll: {}; readonly CompletedUnlockScroll: {}; readonly CompletedInertOthers: {}; readonly CompletedRestoreInert: {}; readonly CompletedScrollIntoView: {}; readonly CompletedClickItem: {}; readonly ClickedButton: {}; readonly IgnoredMouseClick: {}; readonly SuppressedSpaceScroll: {}; readonly CompletedAnchorMenu: {}; readonly CompletedPortalMenuBackdrop: {}; readonly GotAnimationMessage: { message: import("foldkit/message").MessageUnion<{ readonly Showed: {}; readonly Hid: {}; readonly CompletedWaitForPaint: { generation: Schema.Number; }; readonly EndedAnimation: { generation: Schema.Number; }; }>; }; readonly PressedPointerOnButton: { pointerType: Schema.String; butt …;

Menu.Model

const · packages/mixins-ui/src/menu.ts:556

const Model: Schema.Struct<{ readonly menu: Schema.Struct<{ readonly id: Schema.String; readonly isOpen: Schema.Boolean; readonly isAnimated: Schema.Boolean; readonly isModal: Schema.Boolean; readonly animation: Schema.Struct<{ readonly id: Schema.String; readonly isShowing: Schema.Boolean; readonly transitionState: Schema.Literals<readonly ["Idle", "EnterStart", "EnterAnimating", "LeaveStart", "LeaveAnimating"]>; readonly transitionGeneration: Schema.Number; }>; readonly maybeActiveItemIndex: Schema.Option<Schema.Number>; readonly activationTrigger: Schema.Literals<readonly ["Pointer", "Keyboard"]>; readonly searchQuery: Schema.String; readonly searchVersion: Schema.Number; readonly maybeLastPointerPosition: Schema.Option<Schema.Struct<{ readonly screenX: Schema.Number; readonly screenY: Schema.Number; }>>; readonly maybeLastButtonPointerType: Schema.Option<Schema.String>; readonly maybePointerOrigin: Schema.Option<Schema.Struct<{ readonly screenX: Schema.Number; readonly screenY: Schema.Number; readonly timeStamp: Schema.Number; }>>; }>; readonly submenus: Schema.$Array<Schema.Struct<{ readonly row: Schema.Number; readonly value: Schema.String; readonly menu: Schema.Struct<{ readonly id: Schema.String; readonly isOpen: Schema.Boolean; readonly isAnimated: Schema.Boolean; readonly isModal: Schema.Boolean; readonly animation: Schema.Struct<{ readonly id: Schema.String; readonly isShowing: Schema.Boolean; readonly transitionState: Schema.Literals<readonly ["Idle", "EnterStart", "EnterAnima …;

Menu.OutMessage

const · packages/mixins-ui/src/menu.ts:605

const OutMessage: MessageUnion<{
  readonly Selected: {
    readonly value: Schema.String
    readonly submenu: Schema.Option<Schema.String>
  }
}>

Menu.overlayBars

const · packages/mixins-ui/src/menu/overlayBars.ts:11

const overlayBars: readonly [Mixin<never>]

What draws the open panel's scrollBar over its scroll (ScrollBars), in place of the browser's bar, on a pointer that can hover. Pass it beside the style when the menu scrolls (itemsScrollAttributes); without a scroll there is no bar.

Menu.Point

const · packages/mixins-ui/src/menu.ts:474

const Point: Schema.Struct<{
  readonly x: Schema.Number
  readonly y: Schema.Number
}>

A point in viewport pixels.

Menu.pointTarget

function · packages/mixins-ui/src/menu.ts:528

function pointTarget<Message>(
  options: PointTargetOptions<Message>,
  h: HtmlBuilder<Message>,
): ReadonlyArray<Attribute<Message>>

The attributes of the area a menu opens in at a point: a right press reports its point, and the contextmenu that follows asks to open, the browser's own menu kept shut. target is this for the component.

Menu.PointTargetOptions

interface · packages/mixins-ui/src/menu.ts:516

interface PointTargetOptions<Message> {
  readonly onPress: (point: Point) => Message
  readonly onOpen: Message
}

Menu.resolve

function · packages/mixins-ui/src/menu.ts:123

function resolve<Input, Message>(
  render: MenuRenderInfo,
  mixins: MixinList<Message>,
  context: ResolveContext<Input, Message>,
): ResolvedMenu<Message>

Resolves the menu's bundles (and every item's); pass-throughs stay intact.

Menu.ResolvedMenu

type · packages/mixins-ui/src/menu.ts:99

type ResolvedMenu<Message> = Omit<
  MenuRenderInfo,
  'wrapper' | 'button' | 'backdrop' | 'items' | 'scroll' | 'groups'
> & {
  readonly wrapper: SlotAttributes<Message>
  readonly button: SlotAttributes<Message>
  readonly buttonContent: Html
  readonly backdrop:
    | {
        key: string
        attributes: SlotAttributes<Message>
      }
    | undefined
  readonly items:
    | {
        key: string
        attributes: SlotAttributes<Message>
      }
    | undefined
  readonly scroll: SlotAttributes<Message> | undefined
  readonly rows: SlotAttributes<Message> | undefined
  readonly highlight: SlotAttributes<Message> | undefined
  readonly scrollBar: SlotAttributes<Message> | undefined
  readonly scrollThumb: SlotAttributes<Message> | undefined
  readonly groups: ReadonlyArray<ResolvedMenuGroup<Message>>
}

Menu.ResolvedMenuGroup

type · packages/mixins-ui/src/menu.ts:89

type ResolvedMenuGroup<Message> = Omit<
  MenuGroupRender,
  'heading' | 'group' | 'separator' | 'items'
> & {
  readonly heading: ResolvedMenuHeading<Message> | undefined
  readonly group:
    | {
        key: string
        attributes: SlotAttributes<Message>
      }
    | undefined
  readonly separator:
    | {
        key: string
        attributes: SlotAttributes<Message>
      }
    | undefined
  readonly items: ReadonlyArray<ResolvedMenuItem<Message>>
}

Menu.ResolvedMenuHeading

type · packages/mixins-ui/src/menu.ts:85

type ResolvedMenuHeading<Message> = Omit<MenuHeadingRender, 'attributes'> & {
  readonly attributes: SlotAttributes<Message>
}

Menu.ResolvedMenuItem

type · packages/mixins-ui/src/menu.ts:81

type ResolvedMenuItem<Message> = Omit<MenuItemRender, 'attributes'> & {
  readonly attributes: SlotAttributes<Message>
}

Menu.Row

type · packages/mixins-ui/src/menu.ts:267

type Row<Value extends string = string> = LeafRow<Value> | SubmenuRow<Value>

One row of the menu.

Menu.rowInputs

function · packages/mixins-ui/src/menu.ts:351

function rowInputs<Message, Value extends string>(
  rows: ReadonlyArray<Row<Value>>,
  isDisabled: ((value: Value) => boolean) | undefined,
  h: HtmlBuilder<Message>,
): Pick<
  UiMenu.ViewInputs<string>,
  | 'items'
  | 'itemToConfig'
  | 'itemToSearchText'
  | 'isItemDisabled'
  | 'itemGroupKey'
  | 'groupToHeading'
>

The Menu view inputs that put rows in the Menu: its items are the rows' values, so a row is told from another by its value, its disabled state is asked by value, and typeahead matches the labels. The rows' content is drawn by withRows (and the submenu's row by view), not by itemToConfig.

Menu.RowsOptions

interface · packages/mixins-ui/src/menu.ts:373

interface RowsOptions<Message> {
  readonly mixins: MixinList<Message>
  readonly kbd?: NamedStyle<typeof KbdSlots> | undefined
}

interface · packages/mixins-ui/src/menu.ts:257

interface SubmenuRow<Value extends string = string> extends RowParts<Value> {
  readonly items: ReadonlyArray<ItemRow<Value>>
  readonly isItemDisabled?: ((value: Value) => boolean) | undefined
  readonly check?: undefined
}

A row that opens a submenu of items, which are chosen to run them.

Menu.target

function · packages/mixins-ui/src/menu.ts:983

function target<ParentMessage>(
  onMessage: (message: Message) => ParentMessage,
  h: HtmlBuilder<ParentMessage>,
): ReadonlyArray<Attribute<ParentMessage>>

The attributes of the area a menu with opensAt: 'point' opens in: a right press records its point, and its contextmenu opens the menu; a finger or a pen held there HOLD_MS opens it too, as iOS fires no contextmenu for one. Give the area -webkit-touch-callout: none and user-select: none, or iOS shows its own callout over it.

Menu.toView

function · packages/mixins-ui/src/menu.ts:193

function toView<Message>(
  mixins: MixinList<Message>,
  context: ResolveContext<unknown, Message>,
  draw?: (resolved: ResolvedMenu<Message>) => Html,
): (render: MenuRenderInfo) => Html

The menu's toView: draw receives its bundles with mixins applied. Without one, it draws upstream's markup from them.

Menu.update

function · packages/mixins-ui/src/menu.ts:874

function update(model: Model, message: Message): Return

The menu after message; the same Model when nothing changes.

Menu.view

function · packages/mixins-ui/src/menu.ts:1066

function view<ParentMessage, Value extends string = string>(
  options: MenuViewOptions<ParentMessage, Value>,
  h: HtmlBuilder<ParentMessage>,
): Html

Menu.withRows

function · packages/mixins-ui/src/menu.ts:401

function withRows<Message, Value extends string>(
  resolved: ResolvedMenu<Message>,
  rows: ReadonlyArray<Row<Value>>,
  options: RowsOptions<Message>,
  h: HtmlBuilder<Message>,
): ResolvedMenu<Message>

A resolved menu with rows drawn in its rows, by index: each row's words (label), a checkbox or radio row's role, aria-checked and check mark, an icon, a hint, a shortcut and its aria-keyshortcuts, a tone, and a separator before every group after the first. Upstream draws a separator only from attributes an application cannot write, so it is drawn here.