Toolbar
One tab stop over a row of controls, moved by the arrows, Home and End, with a group and a rule between sets.
Previews are drawn in the theme you made.
Edit itFormatting #
import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { modifyFields } from 'foldkit/struct'
import { Bundles, Button, Recipes, Toolbar, ToolbarSlots } from 'foldkit-mixins-ui'
import type { ToolbarControl } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Tool = Recipes.Button.style({ tone: 'neutral', variant: 'outline' })
// Which tools are on is the application's; where the one tab stop is belongs
// to the Toolbar's Bundle.
const App = Bundle.compose({ on: Schema.Array(Schema.String) }).pipe(
Bundle.withMessages({ Pressed: { id: Schema.String } }),
Bundle.withChild('bar', Bundles.Toolbar, { args: {} }),
)
export default example({
title: 'Formatting',
slots: ToolbarSlots,
recipe: Recipes.Toolbar,
state: App.placements.runtime({
Model: App.Model,
initial: { on: ['Bold'] },
update: App.placements.update((model, { id }) => ({
model: modifyFields(model, {
on: on => (on.includes(id) ? on.filter(each => each !== id) : [...on, id]),
}),
})),
}),
view: ({ style, model, onMessage, h }) => {
// A control is the application's own Button, handed the stop's attributes.
const one = (
label: string,
disabled = false,
): ToolbarControl<ReturnType<typeof onMessage>> => ({
id: label,
disabled,
draw: stop =>
Button.view(
{
label,
pressed: model.on.includes(label),
disabled: stop.disabled,
onClick: onMessage(App.Message.Pressed({ id: label })),
attributes: { button: stop.attributes },
style: Tool,
},
h,
),
})
return Toolbar.view(
{
id: 'format',
label: 'Text formatting',
roving: model.bar,
onFocus: Toolbar.focused(message => onMessage(App.Message.GotBarMessage({ message }))),
items: [one('Bold'), one('Italic'), 'separator', one('Link', true)],
style,
},
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 { Bundle } from 'foldkit-bundle'
import type { HtmlBuilder } from 'foldkit/html'
import {
Bundles,
Button,
Recipes,
Toolbar,
ToolbarSlots,
type ToolbarControl,
} from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Tool = Recipes.Button.style({ tone: 'neutral', variant: 'outline' })
// Each control is the application's own Button, handed the stop's attributes.
const control = <Outer>(h: HtmlBuilder<Outer>, label: string): ToolbarControl<Outer> => ({
id: label,
draw: stop => Button.view({ label, attributes: { button: stop.attributes }, style: Tool }, h),
})
const App = Bundle.compose({}).pipe(Bundle.withChild('bar', Bundles.Toolbar, { args: {} }))
// A toolbar of the application's own, stated once: every use starts from the
// base, and a named preset changes what differs. `orientation` and `direction`
// hold no Message, so they sit in a preset; the controls and the stop do not.
const AppToolbar = Toolbar.configure({
base: { loop: false },
presets: { stacked: { orientation: 'vertical' } },
})
export default example({
title: 'Configure once',
slots: ToolbarSlots,
recipe: Recipes.Toolbar,
state: App.placements.runtime({ Model: App.Model, initial: {} }),
view: ({ style, model, onMessage, h }) =>
AppToolbar.view(
{
id: 'stacked',
label: 'Actions',
preset: 'stacked',
roving: model.bar,
onFocus: Toolbar.focused(message => onMessage(App.Message.GotBarMessage({ message }))),
items: ['Open', 'Rename', 'Delete'].map(label => control(h, label)),
style,
},
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.