Command
A search field over every command, filtered as you type and run by Enter.
Previews are drawn in the theme you made.
Edit itBasic #
Calendar
Search emoji
Calculator
Profile
Billing
Settings
Keyboard shortcuts
Documentation
Contact support
Nothing run yet
export const CommandStyle = forSlots(CommandSlots)(Recipes.Command({}))
/** The command menu's query and highlighted row, and the Messages that move them. */
export interface CommandState {
readonly query: string
readonly active: Option.Option<string>
readonly ran: Option.Option<string>
}
export interface CommandMessages<Message> {
readonly onQuery: (query: string) => Message
readonly onActive: (value: string) => Message
readonly onRun: (value: string) => Message
}
export const command = <Message>(
style: NamedStyle<typeof CommandSlots>,
state: CommandState,
messages: CommandMessages<Message>,
h: HtmlBuilder<Message>,
): Html =>
h.div(
[
h.Style({
display: 'grid',
gridTemplateColumns: 'minmax(0, 1fr)',
gap: '0.75rem',
justifyItems: 'start',
inlineSize: 'min(32rem, 100%)',
}),
],
[
Command.view(
{
id: 'palette',
label: 'Search commands',
placeholder: 'Type a command or search…',
groups: [
{
heading: 'Suggestions',
items: [
{
value: 'calendar',
label: 'Calendar',
icon: outline(h, 'M2 4h12v10H2zM2 7h12M5 2v3M11 2v3'),
},
{
value: 'emoji',
label: 'Search emoji',
keywords: ['smiley'],
icon: outline(
h,
'M8 14.5a6.5 6.5 0 1 0 0-13 6.5 6.5 0 0 0 0 13zM5.5 9.5s1 1.5 2.5 1.5 2.5-1.5 2.5-1.5M6 6h.01M10 6h.01',
),
},
{
value: 'calculator',
label: 'Calculator',
disabled: true,
icon: outline(
h,
'M3 1.5h10v13H3zM5 4h6v2.5H5zM5.5 9.5h.01M8 9.5h.01M10.5 9.5h.01M5.5 12h.01M8 12h.01M10.5 12h.01',
),
},
],
},
{
heading: 'Settings',
items: [
{
value: 'profile',
label: 'Profile',
shortcut: ['⌘', 'P'],
icon: outline(
h,
'M8 7a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5zM3 14c0-2.8 2.2-5 5-5s5 2.2 5 5',
),
},
{
value: 'billing',
label: 'Billing',
shortcut: ['⌘', 'B'],
icon: outline(h, 'M1.5 3.5h13v9h-13zM1.5 6.5h13M4 10h2'),
},
{
value: 'settings',
label: 'Settings',
keywords: ['preferences'],
shortcut: ['⌘', 'S'],
icon: outline(h, 'M2 4h6M11 4h3M2 12h2M7 12h7M9.5 2.5v3M5.5 10.5v3'),
},
],
},
{
heading: 'Help',
items: [
{
value: 'shortcuts',
label: 'Keyboard shortcuts',
shortcut: ['⌘', '/'],
icon: outline(
h,
'M1.5 4h13v8h-13zM4 6.5h.01M6.5 6.5h.01M9 6.5h.01M11.5 6.5h.01M5 9.5h6',
),
},
{
value: 'docs',
label: 'Documentation',
keywords: ['guide'],
icon: outline(
h,
'M2 3h4a2 2 0 0 1 2 2v9a1.5 1.5 0 0 0-1.5-1.5H2zM14 3h-4a2 2 0 0 0-2 2v9a1.5 1.5 0 0 1 1.5-1.5H14z',
),
},
{
value: 'support',
label: 'Contact support',
keywords: ['help'],
icon: outline(h, 'M2 3h12v8H6l-3 3v-3H2z'),
},
],
},
],
query: state.query,
onQuery: messages.onQuery,
active: state.active,
onActive: messages.onActive,
onRun: messages.onRun,
words: { empty: 'No commands found.' },
hints: [
{ keys: ['↑', '↓'], label: 'to navigate' },
{ keys: ['↵'], label: 'to select' },
],
style,
kbd: KbdStyle,
},
h,
),
h.p(
[],
[
Option.match(state.ran, {
onNone: () => 'Nothing run yet',
onSome: value => `Ran ${value}`,
}),
],
),
],
)In a dialog (⌘K) #
or press ⌘K
Nothing run yet
// The menu meets the panel's edges, and its title is for assistive technology alone.
export const PaletteDialogStyle = forSlots(DialogSlots)(
Recipes.Dialog.extend({
base: { panel: { padding: '0', gap: '0' }, title: Utilities.srOnly },
})({}),
)
const palette = <Message>(h: HtmlBuilder<Message>): ReadonlyArray<CommandGroup<string>> => [
{
heading: 'Pages',
items: [
{
value: 'new-page',
label: 'New page',
shortcut: ['⌘', 'N'],
icon: outline(h, 'M3 1h7l3 3v11H3zM8 7v5M5.5 9.5h5'),
},
{
value: 'search',
label: 'Search pages',
description: 'Titles and text',
keywords: ['find'],
icon: outline(h, 'M7 12.5a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zM14.5 14.5l-3.6-3.6'),
},
],
},
{
heading: 'Settings',
items: [
{
value: 'theme',
label: 'Change theme',
keywords: ['dark', 'light'],
opensPage: true,
icon: outline(h, 'M8 14.5a6.5 6.5 0 0 1 0-13zM8 1.5a6.5 6.5 0 0 1 0 13'),
},
],
},
]
const THEMES: ReadonlyArray<CommandGroup<string>> = [
{
items: [
{ value: 'light', label: 'Light' },
{ value: 'dark', label: 'Dark' },
{ value: 'system', label: 'As the system' },
],
},
]
/** The dialog is the component's; the query, the highlight and the pages stepped into are the Model's. */
export interface PaletteState {
readonly dialog: UiDialog.Model
readonly query: string
readonly active: Option.Option<string>
readonly pages: ReadonlyArray<string>
readonly ran: Option.Option<string>
}
export interface PaletteMessages<Message> {
readonly onOpen: Message
readonly toDialog: (message: UiDialog.Message) => Message
readonly onQuery: (query: string) => Message
readonly onActive: (value: string) => Message
readonly onRun: (value: string) => Message
readonly onBack: Message
}
// ⌘K or Ctrl+K opens it from anywhere on this page: the application lifts
// `Command.hotkey(onOpen)` into its subscriptions. "Change theme" steps
// into a page, and Backspace in the empty field steps back out.
export const commandDialog = <Message>(
style: NamedStyle<typeof CommandSlots>,
state: PaletteState,
messages: PaletteMessages<Message>,
h: HtmlBuilder<Message>,
): Html =>
h.div(
[h.Style({ display: 'grid', gap: '0.75rem', justifyItems: 'start' })],
[
h.p(
[],
[
Button.view({ label: 'Open the menu', onClick: messages.onOpen, style: ButtonStyle }, h),
' or press ',
Kbd.combo({ keys: ['⌘', 'K'], style: KbdStyle }, h),
],
),
h.submodel({
slotId: state.dialog.id,
model: state.dialog,
view: UiDialog.view,
viewInputs: {
toView: Dialog.toView([PaletteDialogStyle.mixin], { h }, dialog =>
h.dialog(
dialog.dialog,
dialog.isVisible
? [
h.div(dialog.backdrop),
h.div(dialog.panel, [
h.h2(dialog.title, ['Command menu']),
Command.view(
{
id: 'preview-palette',
label: 'Search commands',
placeholder: 'Type a command or search…',
groups: state.pages.length === 0 ? palette(h) : THEMES,
query: state.query,
onQuery: messages.onQuery,
active: state.active,
onActive: messages.onActive,
onRun: messages.onRun,
page: { trail: state.pages, onBack: messages.onBack },
hints: [
{ keys: ['↑', '↓'], label: 'to navigate' },
{ keys: ['↵'], label: 'to select' },
{ keys: ['esc'], label: 'to close' },
],
style,
kbd: KbdStyle,
},
h,
),
]),
]
: [],
),
),
},
toParentMessage: messages.toDialog,
}),
h.p(
[],
[
Option.match(state.ran, {
onNone: () => 'Nothing run yet',
onSome: value => `Ran ${value}`,
}),
],
),
],
)Configure once #
README.md
CHANGELOG.md
Nothing run yet
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Command, CommandSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { note, stack } from '../../snippets/layout.js'
const Model = Schema.Struct({
query: Schema.String,
active: Schema.Option(Schema.String),
ran: Schema.Option(Schema.String),
})
const Message = defineMessageUnion({
Typed: { query: Schema.String },
Moved: { value: Schema.String },
Ran: { value: Schema.String },
})
// The wording and hints every palette of the application starts from, and a
// named preset over them. The query, the highlight and what a row does stay
// the application's, at each use.
const AppPalette = Command.configure({
base: {
placeholder: 'Type a command or search',
words: { empty: 'Nothing matches' },
hints: [
{ keys: ['↑', '↓'], label: 'to move' },
{ keys: ['↵'], label: 'to run' },
],
},
presets: { files: { placeholder: 'Find a file', words: { empty: 'No such file' } } },
})
const groups = [
{
heading: 'Files',
items: [
{ value: 'readme', label: 'README.md' },
{ value: 'changelog', label: 'CHANGELOG.md', keywords: ['history'] },
],
},
]
export default example({
title: 'Configure once',
slots: CommandSlots,
recipe: Recipes.Command,
state: {
Model,
Message,
init: () => ({ model: { query: '', active: Option.none(), ran: Option.none() } }),
update: (model, message) => ({
model: Message.match(message, {
Typed: ({ query }) => ({ ...model, query }),
Moved: ({ value }) => ({ ...model, active: Option.some(value) }),
Ran: ({ value }) => ({ ...model, ran: Option.some(value) }),
}),
}),
},
view: ({ style, model, onMessage, h }) =>
stack(h, [
AppPalette.view(
{
id: 'files-palette',
label: 'Search files',
preset: 'files',
groups,
query: model.query,
onQuery: query => onMessage(Message.Typed({ query })),
active: model.active,
onActive: value => onMessage(Message.Moved({ value })),
onRun: value => onMessage(Message.Ran({ value })),
style,
},
h,
),
note(h, [
Option.match(model.ran, { onNone: () => 'Nothing run yet', onSome: v => `Ran ${v}` }),
]),
]),
})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.