Mixins / foldkit-mixins
Behavior: reusable element-level interaction
A Behavior is a reusable bundle of element-level interaction that does not own application
state. Its attributes are built while resolving from the view's input and h, so it can only
emit Messages from the view's own Message universe. Like an agent capability,
a Behavior points one way — toward a Message — and never gets a Model setter.
const Focus = Behavior.forSlots(FieldSlots)<FieldInput, Message>({
input: Behavior.slot({
requires: { capability: Capability.TextInput },
attributes: ({ input, h }) => (input.invalid ? [h.AriaInvalid(true)] : []),
mount: input => MeasureField(input.value), // your own Mount.define* action
}),
})requires is checked against the slot contract at definition time. A behavior that requires
Event.Click cannot attach to a slot that did not publish click. Multiple mount contributions
compose into one OnMount per element.
A slot rendered once per item (rows, tabs, options) tells the Behavior which repetition it is
resolving through the second argument of attrs:
h.ul(slots.list.attrs(), items.map((item, index) =>
h.li(slots.row.attrs([h.Key(item.id)], { index, id: item.id, count: items.length }), [item.label]),
))The Behavior receives it as item ({ index, id?, count? }) in attributes and as the second
argument of mount, so one Behavior can write tabindex="0" on the current row and -1 on the
rest, or aria-posinset and aria-setsize on each. Without an item argument, item is
undefined and the Behavior decorates the slot as a single element.
Ready-made Behaviors
Behaviors holds Behaviors that own no state. The first is Collection: describe the parent's
array once, and a view and the Behaviors on it agree about identity, order, and disabled items.
import { Behaviors } from 'foldkit-mixins'
type ToolbarInput = { readonly tools: ReadonlyArray<{ readonly id: string; readonly disabled: boolean }> }
declare const toolbar: ToolbarInput
const tools = Behaviors.Collection.of(toolbar.tools, {
id: tool => tool.id,
disabled: tool => tool.disabled,
}) // ids, size, at, indexOf, isDisabled, enabled, slotItem
const Ids = Behaviors.Collection.behavior(ToolbarSlots)<ToolbarInput, Message>({
item: 'tool',
items: input => Behaviors.Collection.of(input.tools, { id: tool => tool.id }),
posInSet: true,
})
// in the view: slots.tool.attrs([h.Key(tool.id)], tools.slotItem(index))of is pure and refuses two items with one id (mixins:duplicate-item-id), since an id is what
aria-activedescendant and aria-controls rely on. The Behavior writes id, aria-disabled,
and with posInSet aria-posinset and aria-setsize, reading the item context the view passes;
it contributes nothing to a slot resolved without one. Behaviors that need state, such as a
roving tab stop or a press, live in foldkit-primitives as a Bundle plus its Behavior.
Four more stateless Behaviors say a parent's state in ARIA, so a custom view gets it right without hand-writing it. Each takes slot names and functions of the view's input; none owns anything.
Behaviors.Disclosure.behavior(Slots)<Input, Message>({ trigger, content, open, id })writesaria-expandedandaria-controlson the trigger,idandhiddenon the content. The floor is<details>.Behaviors.ToggleState.behavior(Slots)({ control, state, as: 'checked' | 'pressed' })writesaria-checked(checkbox, switch, radio, option) oraria-pressed(a toggle button) from aboolean | 'mixed'.Behaviors.FieldAssociation.behavior(Slots)({ control, label, description?, error?, id, invalid?, required? })derives<id>,<id>-label,<id>-description,<id>-errorfrom one base id (FieldAssociation.ids(id)for a view that renders them itself) and writesid,for,aria-labelledby,aria-describedby(the error joins whileinvalid),aria-invalid,aria-required. Derived, not minted, so a resumed page and a test agree.Behaviors.SpinValue.behavior(Slots)({ control, value, onChange, min?, max?, step?, page?, text? })writes thespinbuttonrole andaria-value*, and steps the parent's number with ArrowUp and ArrowDown bystep, PageUp and PageDown bypage(default ten steps), Home and End to the bounds, clamped and default-prevented;SpinValue.spin(value, key, modifiers, bounds)is the pure step. A press-and-hold repeat and the wheel are not handled: Foldkit's wheel attribute carries no delta, and a repeat is a timer the Model would own.
Keys: one owner for a slot's keydown
The resolver allows one owner of an event on a slot, so a widget's own keys and the keys an
application wants to add cannot be two Behaviors on keydown. Behaviors.Keys is the one owner:
a widget's keys are named routes, and Keys.behavior folds the routes it is given into the
slot's single handler. A route says which presses it takes (Keys.chord('Escape'),
Keys.printable, or a Keys.where predicate) and what a press means: a Message, or Keys.focus(id,
message) to also move focus to the element with that id. A route that returns Option.none()
declines, and the press goes on to the page.
const SearchSlots = Slots.define({
root: Slot.make({ capability: Capability.Container }),
input: Slot.make({ capability: Capability.TextInput, events: [Event.Input, Event.KeyDown] }),
})
type SearchInput = { readonly query: string }
const { Keys } = Behaviors
const clearOnEscape = Keys.route<SearchInput, Message>('ClearOnEscape', {
on: [Keys.chord('Escape')],
// With nothing to clear the route declines, so Escape reaches the dialog around the field.
handle: ({ input }) =>
input.query === '' ? Option.none() : Option.some(Message.ChangedValue({ value: '' })),
})
// A widget takes the application's routes beside its own: this is how foldkit-mixins-ui's
// Command, SearchField, Tabs, Menubar and others take their `routes` option.
const searchKeys = (routes: ReadonlyArray<Behaviors.Keys.Route<SearchInput, Message>> = []) =>
Keys.behavior(SearchSlots)<SearchInput, Message>({
slot: 'input',
routes: [clearOnEscape, ...routes],
})
// The application adds a key without touching the widget:
const Submits = searchKeys([
Keys.route('Submit', {
on: [Keys.chord('Enter')],
handle: ({ input }) => Option.some(Message.ChangedValue({ value: input.query.trim() })),
}),
])Two routes that declare the same chord throw mixins:key-conflict when the Behavior is defined,
naming both, because nothing chains at run time and one press sends at most one Message. Where one
must win, Keys.first(a, b) asks a and falls to b only if a declines. To see a clash without drawing and catching, Keys.conflict(routes, Option.some('input')) returns the same diagnostic as an Option (Option.none() when no two routes declare one press), and takes the slot name only to say it in the message. A chord matches the
key a press reports, so Shift+a is Keys.chord('A'); a name that is neither one character nor a
named key ('Esc') throws mixins:unknown-key and says what was meant. A handled press is
default-prevented, an unhandled one untouched, a key of an IME composition reaches no route, and a
held key runs its route on every repeat. A Behavior that writes other attributes on the same slot
(a roving tab stop) installs its keys with Keys.attribute(routes, { slot }) inside its own
attributes, which is how the primitives expose theirs (ListNavigation.keys, SwipeActions.keys,
Move.nudge). A key the application only watches, without owning, is a Subscription
(keyboardEvents in foldkit-primitives/events), not a route.