Mixins / foldkit-mixins

mixins

A shared view quickly collects extra props. One screen wants brand styling, another wants a tooltip, another needs aria-invalid, and another attaches analytics. Soon every caller's edge case lives inside the component, or teams copy-paste the template and maintain separate forks.

Instead, a view can declare named extension points once—such as root, label, or input—and let callers attach appearance or element-level behavior to those names from outside. Your view keeps its markup and Foldkit state, while callers style or decorate it without forking templates or colliding on CSS classes, ARIA attributes, or event handlers.

foldkit-mixins provides those typed extension points (Slots), the Style and Behavior pieces you attach to them, and the resolver that merges everything into ordinary Foldkit attributes. See themes and styled widgets running in the component gallery and the theme builder.

Install

bash
pnpm add foldkit-mixins

foldkit and effect are peer dependencies. foldkit-mixins-surface bridges a Surface projection, and foldkit-mixins-ui provides styled widgets and @foldkit/ui adapters.

Design-system modules live on subpaths so pages that only attach classes import nothing extra: foldkit-mixins/layers, /theme, /layout, /defaults, /prose, /utilities, /app, /testing, and /foundations.

Quick start

This example defines a text field view with root and input slots, attaches a class style and an aria-invalid behavior from outside, and keeps the input's own OnInput handler:

ts
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Attr, Behavior, Capability, Event, Slot, Slots, SlotView, Style } from 'foldkit-mixins'

const Message = defineMessageUnion({ ChangedValue: { value: Schema.String } })
type Message = typeof Message.Type
type FieldInput = { readonly value: string; readonly invalid: boolean }
const FieldSlots = Slots.define({
  root: Slot.make({ capability: Capability.Container }),
  input: Slot.make({ capability: Capability.TextInput, events: [Event.Input], attributes: [Attr.AriaInvalid] }),
})
const FieldStyle = Style.forSlots(FieldSlots)({
  root: Style.class('field'),
  input: Style.class('field-input'),
})
const Validation = Behavior.forSlots(FieldSlots)<FieldInput, Message>({
  input: Behavior.slot({
    requires: { capability: Capability.TextInput, attributes: [Attr.AriaInvalid] },
    attributes: ({ input, h }) => [h.AriaInvalid(input.invalid)],
  }),
})
export const Field = SlotView.forMessages<Message>()
  .define(FieldSlots, (input: FieldInput, slots, h) =>
    h.label(slots.root.attrs(), [
      h.input(slots.input.attrs([h.Value(input.value), h.OnInput(value => Message.ChangedValue({ value }))])),
    ]),
  )
  .pipe(Style.attach(FieldStyle), Behavior.attach(Validation))

Calling Field({ value: model.value, invalid: model.invalid }, h) from a parent view (with value: 'ada@' and invalid: true) renders:

html
<label class="field">
  <input class="field-input" value="ada@" aria-invalid="true" />
</label>

Typing emits ChangedValue to your Foldkit update, which owns the input's value. FieldStyle adds the classes, Validation adds aria-invalid="true" from input.invalid, and slots.input.attrs(...) merges both onto the <input> while keeping its OnInput handler intact. If a second attachment registers another OnInput, the resolver throws mixins:event-conflict instead of silently dropping one handler.

A Style's compiled rules (Style.pseudo, Style.media, a layer) arrive with the Slot that draws them. In a browser, the first time a Slot draws a class, its CSS is appended to <style data-foldkit-styles> in the microtask before paint, declaring the standard layer order unless a sheet on the page already declares one. Classes already present in a page stylesheet (from Style.stylesheet(...)) are skipped so nothing is duplicated. On the server, put Style.usedIn(html) in the document head (with foldkit-ssr, pass it through the head option).