Menu
Actions behind a button, at the point a right press lands, or down a submenu beside its row.
Previews are drawn in the theme you made.
Edit itActions #
Nothing chosen yet.
Nothing chosen yet.
export const MenuStyle = forSlots(MenuSlots)(Recipes.Menu({}))
/** An icon in a row, drawn with the inert builder: a row is data the Menu was given, drawn later. */
const rowIcon = (d: string): Html => outline(SlotView.inertBuilder<never>(), d)
// An `icon` before the label, a quiet `hint` and a `shortcut` at its end, and
// a `tone` for what cannot be undone. Two rows may share a label: a row is
// named by its `value`, so these two Share rows are told apart.
/** The rows of the menu a button opens. */
export const PAGE_MENU: ReadonlyArray<Menu.Row> = [
{
value: 'Rename',
label: 'Rename',
icon: rowIcon('M2.5 13.5l.5-3 7-7 2.5 2.5-7 7zM9 5l2 2'),
shortcut: ['F2'],
},
{ value: 'Duplicate', label: 'Duplicate', icon: rowIcon('M5.5 5.5h8v8h-8zM10.5 5.5v-3h-8v8h3') },
{ value: 'Move', label: 'Move', icon: rowIcon('M2.5 8h11M10 4.5L13.5 8 10 11.5') },
{ value: 'Share link', label: 'Share', hint: 'link' },
{ value: 'Share email', label: 'Share', hint: 'email' },
{
value: 'Delete',
label: 'Delete',
icon: rowIcon('M2.5 4.5h11M6 4.5v-2h4v2M4 4.5l.75 9h6.5l.75-9'),
tone: 'destructive',
},
]
/** The rows of the menu a right press opens, two opening submenus beside them. */
export const FILE_MENU: ReadonlyArray<Menu.Row> = [
{ value: 'New tab', label: 'New tab' },
{
value: 'Share',
label: 'Share',
items: [
{ value: 'Email', label: 'Email' },
{ value: 'Messages', label: 'Messages' },
{ value: 'Copy link', label: 'Copy link' },
],
},
{
value: 'Export',
label: 'Export',
items: [
{ value: 'PDF', label: 'PDF' },
{ value: 'PNG', label: 'PNG' },
{ value: 'SVG', label: 'SVG' },
],
isItemDisabled: item => item === 'SVG',
},
{ value: 'Print', label: 'Print' },
]
/** Each open menu is the component's; what each last chose is the Model's. */
export interface MenuState {
readonly pageMenu: Menu.Model
readonly pageChoice: Option.Option<string>
readonly fileMenu: Menu.Model
readonly fileChoice: Option.Option<string>
}
export const menu = <Message>(
style: NamedStyle<typeof MenuSlots>,
state: MenuState,
toPage: (message: Menu.Message) => Message,
toFile: (message: Menu.Message) => Message,
direction: 'ltr' | 'rtl',
h: HtmlBuilder<Message>,
): Html => {
const chosen = (choice: Option.Option<string>) =>
h.p(
[],
[
Option.match(choice, {
onNone: () => 'Nothing chosen yet.',
onSome: value => `Chose ${value}.`,
}),
],
)
return h.div(
[h.Style({ display: 'grid', gap: '0.75rem', justifyItems: 'start' })],
[
Menu.view(
{
model: state.pageMenu,
rows: PAGE_MENU,
buttonContent: h.span([], ['Page actions']),
isRowDisabled: value => value === 'Move',
onMessage: toPage,
direction,
mixins: [style.mixin, ...Menu.fluid],
},
h,
),
chosen(state.pageChoice),
h.div(
[
...Menu.target(toFile, h),
h.Tabindex(0),
h.AriaLabel('report.pdf, right-click or Shift+F10 for its actions'),
h.Style({
padding: '2rem 3rem',
border: '1px dashed currentColor',
borderRadius: '0.5rem',
cursor: 'context-menu',
}),
],
['report.pdf'],
),
Menu.view(
{
model: state.fileMenu,
rows: FILE_MENU,
buttonContent: h.span([], ['Actions for report.pdf']),
opensAt: 'point',
onMessage: toFile,
direction,
mixins: [style.mixin],
// The highlight must not chase the submenus' rows: they hold the menu's own rows.
submenuMixins: [style.mixin, ...Menu.fluid],
},
h,
),
chosen(state.fileChoice),
],
)
}View options #
Grid off, rulers on, Fit.
export const Zoom = Schema.Literals(['Fit', 'Actual size'])
/** What the View menu sets: the application keeps it, and builds the rows from it. */
export const ViewOptions = Schema.Struct({
grid: Schema.Boolean,
rulers: Schema.Boolean,
zoom: Zoom,
chosen: Schema.Option(Schema.String),
})
export type ViewOptions = typeof ViewOptions.Type
// Checkbox and radio rows send `Selected` and keep the menu open; rows of one
// group stand under its heading; a shortcut is drawn as keys at the row's end.
export const viewRows = (options: ViewOptions): ReadonlyArray<Menu.Row> => [
{
value: 'Grid',
label: 'Grid',
check: 'checkbox',
checked: options.grid,
group: 'Show',
shortcut: ['⌘', 'G'],
},
{ value: 'Rulers', label: 'Rulers', check: 'checkbox', checked: options.rulers, group: 'Show' },
{ value: 'Fit', label: 'Fit', check: 'radio', checked: options.zoom === 'Fit', group: 'Zoom' },
{
value: 'Actual size',
label: 'Actual size',
check: 'radio',
checked: options.zoom === 'Actual size',
group: 'Zoom',
},
{ value: 'Delete', label: 'Delete', tone: 'destructive', shortcut: ['⌫'] },
]
export const viewOptionsMenu = <Message>(
style: NamedStyle<typeof MenuSlots>,
state: { readonly menu: Menu.Model; readonly options: ViewOptions },
onMessage: (message: Menu.Message) => Message,
direction: 'ltr' | 'rtl',
h: HtmlBuilder<Message>,
): Html =>
h.div(
[h.Style({ display: 'grid', gap: '0.75rem', justifyItems: 'start' })],
[
Menu.view(
{
model: state.menu,
rows: viewRows(state.options),
buttonContent: h.span([], ['View']),
onMessage,
direction,
mixins: [style.mixin, ...Menu.fluid],
kbd: KbdStyle,
},
h,
),
h.p(
[],
[
`Grid ${state.options.grid ? 'on' : 'off'}, rulers ${state.options.rulers ? 'on' : 'off'}, ${state.options.zoom}.`,
...Option.match(state.options.chosen, {
onNone: () => [],
onSome: value => [` Chose ${value}.`],
}),
],
),
],
)A header and a footer #
Nothing chosen yet
import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Menu, MenuSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { note, stack } from '../../snippets/layout.js'
const rows: ReadonlyArray<Menu.Row> = [
{ value: 'profile', label: 'Profile', group: 'Account' },
{ value: 'billing', label: 'Billing', group: 'Account' },
{ value: 'team', label: 'Switch team', items: [{ value: 'acme', label: 'Acme Inc' }] },
{ value: 'out', label: 'Sign out', tone: 'destructive' },
]
// A Bundle holds the menu's state and answers a chosen row as `Selected`.
const App = Bundle.compose({ last: Schema.String }).pipe(
Bundle.withChild('menu', Bundles.Menu, {
args: { id: 'account-menu', rows, isAnimated: true },
onOut: out => model => ({
model: { ...model, last: Menu.OutMessage.match(out, { Selected: ({ value }) => value }) },
}),
}),
)
// `header` and `footer` are Html in the panel, above and below the rows, in
// slots of their own: they are no row, so the keys pass over them.
export default example({
title: 'A header and a footer',
slots: MenuSlots,
recipe: Recipes.Menu,
state: App.placements.runtime({ Model: App.Model, initial: { last: '' } }),
view: ({ style, model, onMessage, h }) =>
stack(h, [
Menu.view(
{
model: model.menu,
rows,
buttonContent: h.span([], ['Ada']),
onMessage: message => onMessage(App.Message.GotMenuMessage({ message })),
header: h.div([], [h.strong([], ['Ada Lovelace']), h.div([], ['ada@example.com'])]),
footer: h.span([], ['Foldkit Plus 0.41']),
mixins: [style.mixin, ...Menu.fluid],
},
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.
Rows of your own #
Nothing opened yet
import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Avatar, Bundles, Menu, MenuSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { note, stack } from '../../snippets/layout.js'
const people = [
{ value: 'ada', name: 'Ada Lovelace', text: 'Pushed the pricing page', time: '2m' },
{ value: 'grace', name: 'Grace Hopper', text: 'Asked for a review', time: '1h' },
]
const App = Bundle.compose({ last: Schema.String }).pipe(
Bundle.withChild('menu', Bundles.Menu, {
args: { id: 'inbox-menu', rows: people.map(({ value }) => ({ value })), isAnimated: true },
onOut: out => model => ({
model: { ...model, last: Menu.OutMessage.match(out, { Selected: ({ value }) => value }) },
}),
}),
)
// A row's `content` is drawn in place of its words: an avatar, a title and a
// time. `label` stays the row's name and its typeahead key.
export default example({
title: 'Rows of your own',
slots: MenuSlots,
recipe: Recipes.Menu,
state: App.placements.runtime({ Model: App.Model, initial: { last: '' } }),
view: ({ style, model, onMessage, h }) =>
stack(h, [
Menu.view(
{
model: model.menu,
rows: people.map(person => ({
value: person.value,
label: person.name,
content: h.span(
[h.Style({ display: 'flex', gap: '0.5rem', alignItems: 'center' })],
[
Avatar.view({ name: person.name }, h),
h.span([], [`${person.name}: ${person.text} (${person.time})`]),
],
),
})),
buttonContent: h.span([], ['Notifications']),
onMessage: message => onMessage(App.Message.GotMenuMessage({ message })),
mixins: [style.mixin],
},
h,
),
note(h, [model.last === '' ? 'Nothing opened yet' : `Opened: ${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.
A context menu #
Nothing chosen yet
import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Menu, MenuSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { note, stack } from '../../snippets/layout.js'
const rows: ReadonlyArray<Menu.Row> = [
{ value: 'open', label: 'Open', group: 'File', shortcut: ['⏎'] },
{ value: 'rename', label: 'Rename', group: 'File', shortcut: ['F2'] },
{
value: 'share',
label: 'Share',
group: 'File',
items: [
{ value: 'email', label: 'Email' },
{ value: 'link', label: 'Copy link' },
],
},
{ value: 'delete', label: 'Delete', group: 'Danger', tone: 'destructive', shortcut: ['⌫'] },
]
const App = Bundle.compose({ last: Schema.String }).pipe(
Bundle.withChild('menu', Bundles.Menu, {
args: { id: 'context-menu', rows, isAnimated: true },
onOut: out => model => ({
model: { ...model, last: Menu.OutMessage.match(out, { Selected: ({ value }) => value }) },
}),
}),
)
// A context menu is a Menu that opens where the area was pressed: a right
// press, Shift+F10 or the ContextMenu key, or a long press on a touch screen.
// `Menu.target` goes on the area; `opensAt: 'point'` places the panel at it.
export default example({
title: 'A context menu',
slots: MenuSlots,
recipe: Recipes.Menu,
state: App.placements.runtime({ Model: App.Model, initial: { last: '' } }),
view: ({ style, model, onMessage, h }) => {
const toMenu = (message: Menu.Message) => onMessage(App.Message.GotMenuMessage({ message }))
return stack(h, [
h.div(
[
...Menu.target(toMenu, h),
h.Tabindex(0),
h.AriaLabel('report.pdf, right-click or Shift+F10 for its actions'),
h.Style({
padding: '2rem 3rem',
border: '1px dashed currentColor',
borderRadius: '0.5rem',
cursor: 'context-menu',
}),
],
['report.pdf'],
),
Menu.view(
{
model: model.menu,
rows,
buttonContent: h.span([], ['Actions for report.pdf']),
opensAt: 'point',
onMessage: toMenu,
mixins: [style.mixin],
// The highlight must not chase the submenus' rows: they hold the menu's own rows.
submenuMixins: [style.mixin, ...Menu.fluid],
},
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.
Where it opens #
import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Menu, MenuSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const rows: ReadonlyArray<Menu.Row> = [
{ value: 'cut', label: 'Cut', shortcut: ['⌘', 'X'] },
{ value: 'copy', label: 'Copy', shortcut: ['⌘', 'C'] },
{ value: 'paste', label: 'Paste', shortcut: ['⌘', 'V'] },
]
const App = Bundle.compose({ last: Schema.String }).pipe(
Bundle.withChild('menu', Bundles.Menu, {
args: { id: 'edit-menu', rows, isAnimated: true },
onOut: out => model => ({
model: { ...model, last: Menu.OutMessage.match(out, { Selected: ({ value }) => value }) },
}),
}),
)
// `anchor` is merged over the default `{ placement: 'bottom-start', gap: 4 }`:
// here the panel opens above the button, at its end, with a wider gap.
export default example({
title: 'Where it opens',
slots: MenuSlots,
recipe: Recipes.Menu,
state: App.placements.runtime({ Model: App.Model, initial: { last: '' } }),
view: ({ style, model, onMessage, h }) =>
h.div(
[h.Style({ paddingBlockStart: '9rem' })],
[
Menu.view(
{
model: model.menu,
rows,
buttonContent: h.span([], ['Edit']),
onMessage: message => onMessage(App.Message.GotMenuMessage({ message })),
anchor: { placement: 'top-end', gap: 10 },
mixins: [style.mixin],
},
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, Menu, MenuSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const rows: ReadonlyArray<Menu.Row> = [
{ value: 'open', label: 'Open' },
{ value: 'rename', label: 'Rename' },
{ value: 'delete', label: 'Delete', tone: 'destructive' },
]
const Roomy = Recipes.Menu.style({ size: 'md' })
// The look and the anchor every menu of the application starts from. Its
// `mixins` and the call's concatenate; the Model, rows and Messages stay at the call.
const AppMenu = Menu.configure({
base: { mixins: [Roomy.mixin, ...Menu.fluid] },
presets: { above: { anchor: { placement: 'top-start' } } },
})
const App = Bundle.compose({ last: Schema.String }).pipe(
Bundle.withChild('menu', Bundles.Menu, {
args: { id: 'file-menu', rows, isAnimated: true },
onOut: out => model => ({
model: { ...model, last: Menu.OutMessage.match(out, { Selected: ({ value }) => value }) },
}),
}),
)
export default example({
title: 'Configure once',
slots: MenuSlots,
recipe: Recipes.Menu,
state: App.placements.runtime({ Model: App.Model, initial: { last: '' } }),
view: ({ model, onMessage, h }) =>
h.div(
[h.Style({ paddingBlockStart: '9rem' })],
[
AppMenu.view(
{
model: model.menu,
rows,
buttonContent: h.span([], ['File']),
preset: 'above',
onMessage: message => onMessage(App.Message.GotMenuMessage({ message })),
},
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
- Right to left The direction switch above turns the preview over; this is what each widget reads.