Menubar
A row of menus with one tab stop, the arrows moving from one open menu to the next.
Previews are drawn in the theme you made.
Edit itMenubar #
Nothing chosen yet.
// A row's `shortcut` is drawn at its end and named in `aria-keyshortcuts`; the bar
// binds nothing, so the application listens for the keys.
export const MenubarStyle = forSlots(MenubarSlots)(Recipes.Menubar({}))
const MenubarMenuStyle = forSlots(MenuSlots)(Recipes.MenubarMenu({}))
/** The bar's menus, in the order of the Model's. */
export const MENUBAR: ReadonlyArray<Menubar.MenubarMenu> = [
{
label: 'File',
rows: [
{ value: 'New', label: 'New' },
{ value: 'Open', label: 'Open' },
{ value: 'Save', label: 'Save', shortcut: ['⌘', 'S'] },
],
},
{
label: 'Edit',
rows: [
{ value: 'Undo', label: 'Undo', shortcut: ['⌘', 'Z'] },
{ value: 'Redo', label: 'Redo', shortcut: ['⇧', '⌘', 'Z'] },
{ value: 'Copy', label: 'Copy', shortcut: ['⌘', 'C'] },
],
isRowDisabled: value => value === 'Redo',
},
{
label: 'View',
rows: [
{ value: 'Zoom in', label: 'Zoom in' },
{ value: 'Zoom out', label: 'Zoom out' },
{ value: 'Full screen', label: 'Full screen' },
],
},
]
/** The open menus are the bar's; the command chosen is the Model's. */
export interface MenubarState {
readonly bar: Menubar.Model
readonly chosen: Option.Option<string>
}
export const menubar = <Message>(
style: NamedStyle<typeof MenubarSlots>,
state: MenubarState,
onMessage: (message: Menubar.Message) => Message,
direction: 'ltr' | 'rtl',
h: HtmlBuilder<Message>,
): Html =>
h.div(
[h.Style({ display: 'grid', gap: '0.75rem', justifyItems: 'start' })],
[
Menubar.view(
{
label: 'Editor',
model: state.bar,
menus: MENUBAR,
onMessage,
direction,
style,
menuMixins: [MenubarMenuStyle.mixin, ...Menu.fluid],
},
h,
),
h.p(
[],
[
Option.match(state.chosen, {
onNone: () => 'Nothing chosen yet.',
onSome: chosen => `Chose ${chosen}.`,
}),
],
),
],
)A bar with a header and a footer #
Nothing chosen yet
import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Menubar, MenubarSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { note, stack } from '../../snippets/layout.js'
const MenuStyle = Recipes.MenubarMenu.style({})
// The bar's state is a Bundle: one shut menu for each id, the first trigger
// the tab stop. A chosen row comes out as `Selected`, with the menu's index.
const App = Bundle.compose({ last: Schema.String }).pipe(
Bundle.withChild('bar', Bundles.Menubar, {
args: { ids: ['file', 'edit'], isAnimated: true },
onOut: out => model => ({ model: { ...model, last: `menu ${out.menu}: ${out.value}` } }),
}),
)
// `header` and `footer` are Html in a menu's panel, above and below its rows.
export default example({
title: 'A bar with a header and a footer',
slots: MenubarSlots,
recipe: Recipes.Menubar,
state: App.placements.runtime({ Model: App.Model, initial: { last: '' } }),
view: ({ style, model, onMessage, h }) =>
stack(h, [
Menubar.view(
{
label: 'Application',
model: model.bar,
onMessage: message => onMessage(App.Message.GotBarMessage({ message })),
menus: [
{
label: 'File',
rows: [
{ value: 'new', label: 'New', shortcut: ['⌘', 'N'] },
{ value: 'open', label: 'Open', shortcut: ['⌘', 'O'] },
],
header: h.span([], ['Recent files']),
footer: h.span([], ['3 of 12 shown']),
},
{
label: 'Edit',
rows: [
{ value: 'undo', label: 'Undo', shortcut: ['⌘', 'Z'] },
{ value: 'redo', label: 'Redo', shortcut: ['⇧', '⌘', 'Z'] },
],
},
],
style,
menuMixins: [MenuStyle.mixin],
},
h,
),
note(h, [model.last === '' ? 'Nothing chosen yet' : `Chose ${model.last}`]),
]),
})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, Menubar, MenubarSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const BarStyle = Recipes.Menubar.style({})
const MenuStyle = Recipes.MenubarMenu.style({})
// The bar's look, its menus' look and where they open, stated once. A preset
// moves the menus: this one opens them above the bar.
const AppBar = Menubar.configure({
base: { style: BarStyle, menuMixins: [MenuStyle.mixin] },
presets: { footer: { anchor: { placement: 'top-start' } } },
})
const App = Bundle.compose({ last: Schema.String }).pipe(
Bundle.withChild('bar', Bundles.Menubar, {
args: { ids: ['view', 'help'] },
onOut: Bundle.ignore,
}),
)
export default example({
title: 'Configure once',
slots: MenubarSlots,
recipe: Recipes.Menubar,
state: App.placements.runtime({ Model: App.Model, initial: { last: '' } }),
view: ({ model, onMessage, h }) =>
h.div(
[h.Style({ paddingBlockStart: '8rem' })],
[
AppBar.view(
{
label: 'Application',
model: model.bar,
onMessage: message => onMessage(App.Message.GotBarMessage({ message })),
preset: 'footer',
menus: [
{ label: 'View', rows: [{ value: 'zoom', label: 'Zoom in' }] },
{ label: 'Help', rows: [{ value: 'docs', label: 'Documentation' }] },
],
},
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.
Related
- 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.