Mixins / foldkit-mixins-ui
Customizing @foldkit/ui components with adapters
Some widgets here wrap @foldkit/ui, Foldkit's unstyled components. You cannot reach inside an upstream component to add a class to its third element by hand, so this package gives each part a typed name—such as button or icon—called a Slot, and provides an adapter (toView and resolve) that attaches Style and Behavior mixins to those Slots.
This example attaches a custom CSS class to an @foldkit/ui Button using Button.toView:
import * as UiButton from '@foldkit/ui/button'
import type { HtmlBuilder } from 'foldkit/html'
import { defineMessageUnion } from 'foldkit/message'
import { Style } from 'foldkit-mixins'
import { Button, ButtonSlots } from 'foldkit-mixins-ui'
const Message = defineMessageUnion({ Saved: {} })
type Message = typeof Message.Type
const SaveStyle = Style.forSlots(ButtonSlots)({
button: Style.class('btn btn-primary'),
})
export const saveButton = (h: HtmlBuilder<Message>) =>
UiButton.view(
{
onClick: Message.Saved({}),
toView: Button.toView([SaveStyle.mixin], { h }, ({ button }) => h.button(button, ['Save'])),
},
h,
)UiButton.view builds the component's base attributes and calls toView; Button.toView merges SaveStyle onto the button Slot and hands the resolved attribute array to your draw callback, rendering <button class="btn btn-primary">Save</button> that sends Saved on click. You can append extra non-conflicting attributes right inside draw:
import * as UiInput from '@foldkit/ui/input'
import type { HtmlBuilder } from 'foldkit/html'
import { Style } from 'foldkit-mixins'
import { Input, InputSlots } from 'foldkit-mixins-ui'
const ZipStyle = Style.forSlots(InputSlots)({ input: Style.class('zip') })
export const zipInput = (h: HtmlBuilder<never>) =>
UiInput.view(
{
id: 'zip',
toView: Input.toView([ZipStyle.mixin], { h }, ({ input }) =>
h.input([...input, h.Autocomplete('off')]),
),
},
h,
)toView and resolve
Every adapter (all except Anchor, which is a Behavior) exports both toView and resolve. toView(mixins, { h, input? }, draw) hooks directly into an upstream component's toView callback; resolve(attributes, mixins, { h, input? }) resolves attribute bundles already in hand—for instance, when a Calendar mixin reads the current view mode from attributes._tag:
import type { CalendarAttributes } from '@foldkit/ui/calendar'
import type { HtmlBuilder } from 'foldkit/html'
import { Style } from 'foldkit-mixins'
import { Calendar, CalendarSlots } from 'foldkit-mixins-ui'
const CalendarStyle = Style.forSlots(CalendarSlots)({
root: Style.whenInput<CalendarAttributes['_tag']>(mode => mode === 'Years', Style.class('years')),
})
export const resolveCalendar = (attributes: CalendarAttributes, h: HtmlBuilder<never>) =>
Calendar.resolve(attributes, [CalendarStyle.mixin], { input: attributes._tag, h })Every view takes h last so handlers are type-checked against the current Message union; pass h down as a parameter rather than storing it across frames. Values that are not Slots (animatePanel, isVisible, activeIndex, selectedValue) pass through resolve untouched, and each adapter exports its named return type (ResolvedButton<Message>, ResolvedInput, ResolvedDialog, ResolvedRadioGroup<Value, Message>, ResolvedTabs, ResolvedCalendar, ResolvedMenu).
How the resolver protects component contracts
foldkit-mixins-ui delegates merge rules to foldkit-mixins instead of concatenating arrays:
- component base attributes and Submodel
ChildAttributes survive by identity; - classes combine additively and inline declarations merge by property;
- events and scalar attributes allow one owner, throwing
DiagnosticErrorif a Behavior tries to register a competingOnClickor structural role.
Submodel components remain Submodels
Dialog, Popover, Tooltip, Tabs, RadioGroup, Calendar, and Menu publish ChildAttributes carrying the child state machine's dispatcher. The resolver preserves them, keeping child boundaries intact, and applies per-item Slot contributions across repeated items while preserving each item's own event ownership:
Tabs -> { tablist, tabs, activeIndex }
RadioGroup -> { group, options, selectedValue, hiddenInput }
Calendar -> ResolvedDays | ResolvedMonths | ResolvedYears
Menu -> { wrapper, button, backdrop, items, scroll, groups, isVisible }
Toast -> { container, entries }
Listbox -> { wrapper, button, backdrop, items, scroll, groups, hiddenInputs, isVisible }
Combobox -> { wrapper, inputWrapper, input, toggleButton, backdrop, items, scroll, groups, hiddenInputs, isVisible }