Mixins / foldkit-mixins

SlotView

namespace · packages/mixins/src/slotView.ts:8

export * as SlotView from './slotView.js'

SlotView is the structural equivalent of a Surface: a pure Foldkit view that publishes typed Slots and lets Mixins attach without forking it. It owns no state and runs no Effects. Attached Mixins are resolved into ordinary Foldkit attributes per slot, using the view's own h so Message capability masking is preserved.

In the guides

Members

SlotView.AssemblyRender

type · packages/mixins/src/slotView.ts:580

type AssemblyRender<Slots, Input, Message> = (
  input: Input,
  slots: SlotBuilders<Slots, Message>,
  h: HtmlBuilder<Message>,
  draw: (part: Part<Slots, Input, Message>) => Html,
) => Html

An assembly's own drawing: the parts it places, by draw, and anything else around them.

SlotView.attach

function · packages/mixins/src/slotView.ts:475

function attach<Provides extends string>(
  mixin: Supply<Provides>,
): <Slots, Input, Message, ViewProvides extends string = never>(
  view: SlotView<Slots, Input, Message, ViewProvides>,
) => SlotView<Slots, Input, Message, ViewProvides | Provides>
function attach(mixin: StaticMixin<never> | Mixin<never>): SlotViewTransform
function attach<MixinMessage>(
  mixin: Mixin<MixinMessage>,
): SlotViewTransformFor<MixinMessage>

Attach one Mixin. Returns a new view; the original is unchanged.

SlotView.Bound

interface · packages/mixins/src/slotView.ts:715

interface Bound<Slots, Input, Message> {
  readonly [BOUND]: true
  readonly slots: Slots
  readonly define: (
    render: SlotViewRender<Slots, Input, Message>,
    options?: {
      readonly name?: string
    },
  ) => SlotView<Slots, Input, Message>
  readonly behavior: <const Spec extends BehaviorSpec<Slots, Input, Message>>(
    spec: Spec,
    options?: {
      readonly name?: string
    },
  ) => NamedBehavior<Slots, Input, Message, ProvidesOf<Spec>>
  readonly parts: Parts<Slots, Input, Message>
}

Slots, an input and a Message universe named once. Its define, behavior and parts are SlotView.define, Behavior.forSlots and SlotView.parts with all three fixed, so nothing passed to them needs an annotation. A Behavior factory takes one where it takes the slots.

SlotView.boundSlots

function · packages/mixins/src/slotView.ts:778

function boundSlots<Slots>(given: Slots | Bound<Slots, any, any>): Slots

The slots a Behavior factory was given: the binder's, or the slots themselves.

SlotView.buildersFor

function · packages/mixins/src/slotView.ts:371

function buildersFor<Slots, Message, Input>(
  slots: Slots,
  mixins: ReadonlyArray<MixinFor<Message>>,
  context: {
    readonly input: Input
    readonly h: HtmlBuilder<Message>
  },
): SlotBuilders<Slots, Message>

Build one attrs resolver per published slot, evaluating Mixins lazily.

SlotView.define

function · packages/mixins/src/slotView.ts:411

function define<Slots, Input, Message>(
  slots: Slots,
  render: SlotViewRender<Slots, Input, Message>,
  options?: {
    readonly name?: string
  },
): SlotView<Slots, Input, Message>

Defines a view over published Slots.

Message has no anchor but the render callback's builder, so it is inferred from an explicit h: HtmlBuilder<Message> annotation on that parameter. Without one it resolves to unknown, and the first error appears later and elsewhere — an invariance mismatch at Style.attach/Behavior.attach that does not name the missing annotation. forMessages<Message>() fixes the Message universe up front instead, and leaves h contextually typed.

SlotView.describe

function · packages/mixins/src/describe.ts:196

function describe(view: AnyView): ViewDescription

The view as a serializable value: its slots, and each attachment in order with, per slot, what it requires, what it supplies and the classes and Mounts it contributes. A contribution that is a function of the input reads 'unknown': it is not called. A description is a function of one view value; attaching or detaching returns another view and leaves this one's alone.

SlotView.detach

function · packages/mixins/src/slotView.ts:522

function detach(name: string): SlotViewRemoval

The view without the attachment named name. Returns a new view; the original is unchanged. Refused (mixins:unknown-attachment, mixins:ambiguous-attachment) unless the view holds exactly one by that name, and by A11y.conform's check when the view was conformed to a pattern the attachment helped meet.

SlotView.forMessages

function · packages/mixins/src/slotView.ts:451

function forMessages<Message>(): MessageSlotView<Message>

Binds the view constructors to one Message universe, so the render callback no longer has to annotate h to pin it. Input is still inferred from the callback's own input annotation.

Example

ts
const Field = SlotView.forMessages<Message>().define(
  FieldSlots,
  (input: FieldInput, slots, h) =>
    h.input(slots.input.attrs([h.OnInput(value => Message.ChangedValue({ value }))])),
)

SlotView.inertBuilder

function · packages/mixins/src/slotView.ts:425

function inertBuilder<Message>(): HtmlBuilder<Message>

Foldkit's inertHtml typed for a Message universe, for rendering a view outside a runtime (tests, demos, static description). inertHtml is HtmlBuilder<never>, and the builder is invariant in Message, so this is the one cast: sound outside a runtime render, where no handler is ever dispatched. Inside one it is the same builder every view is given, so its handlers would reach the frame's update: use the view's own h there.

SlotView.isBound

function · packages/mixins/src/slotView.ts:766

function isBound<Slots, Input, Message>(
  value: Slots | Bound<Slots, Input, Message>,
): value is Bound<Slots, Input, Message>
function isBound(value: unknown): value is Bound<unknown, unknown, unknown>

Whether a factory was handed a Bound rather than bare slots. Asked of Slots | Bound<Slots, Input, Message>, each branch keeps its own type.

SlotView.MessageSlotView

interface · packages/mixins/src/slotView.ts:429

interface MessageSlotView<Message> {
  readonly define: <Slots, Input>(
    slots: Slots,
    render: SlotViewRender<Slots, Input, Message>,
    options?: {
      readonly name?: string
    },
  ) => SlotView<Slots, Input, Message>
}

The SlotView constructors with Message already fixed.

SlotView.over

function · packages/mixins/src/slotView.ts:745

function over<Slots>(
  slots: Slots,
): <Input, Message>() => Bound<Slots, Input, Message>

Binds slots, the view's input and its Message universe once.

Example

ts
const Search = SlotView.over(SearchSlots)<SearchInput, Message>()
const Clears = Search.behavior(
  { clear: { attributes: ({ h }) => [h.OnClick(Message.Cleared())] } },
  { name: 'Clears' },
)
const SearchField = Search.define((input, slots, h) =>
  h.div(slots.root.attrs(), [h.button(slots.clear.attrs(), ['Clear'])]),
).pipe(Behavior.attach(Clears))

SlotView.Part

interface · packages/mixins/src/slotView.ts:566

interface Part<Slots, Input, Message> {
  readonly name: string
  readonly reads: ReadonlyArray<keyof Input>
  readonly mixins: ReadonlyArray<MixinFor<Message>>
  readonly render: (
    input: any,
    slots: SlotBuilders<Slots, Message>,
    h: HtmlBuilder<Message>,
  ) => Html
}

One piece of an assembled view, drawn from its own selection of the view's input: the keys it names in reads, and nothing else. It is drawn again only when one of those values changed, by identity.

SlotView.Parts

interface · packages/mixins/src/slotView.ts:587

interface Parts<Slots, Input, Message> {
  readonly part: <const Reads extends ReadonlyArray<keyof Input>>(
    name: string,
    options: {
      readonly reads: Reads
      readonly behaviors?: ReadonlyArray<
        NamedBehavior<Slots, Pick<Input, Reads[number]>, Message, never>
      >
    },
    render: (
      input: Pick<Input, Reads[number]>,
      slots: SlotBuilders<Slots, Message>,
      h: HtmlBuilder<Message>,
    ) => Html,
  ) => Part<Slots, Input, Message>
  readonly assemble: (
    render: AssemblyRender<Slots, Input, Message>,
    options?: {
      readonly name?: string
    },
  ) => SlotView<Slots, Input, Message>
}

SlotView.parts

function · packages/mixins/src/slotView.ts:672

function parts<Slots>(
  slots: Slots,
): <Input, Message>() => Parts<Slots, Input, Message>

Parts over one set of Slots and one input, and the assembly that places them.

Example

ts
const Parts = SlotView.parts(PanelSlots)<PanelInput, Message>()
const Title = Parts.part('Title', { reads: ['title'] }, (input, slots, h) =>
  h.h2(slots.title.attrs(), [input.title]),
)
const Panel = Parts.assemble((input, slots, h, draw) => h.section(slots.root.attrs(), [draw(Title)]))

SlotView.Reads

interface · packages/mixins/src/slotView.ts:787

interface Reads<Root, Value> {
  readonly dependencies: ReadonlyArray<ReadonlyArray<string>>
  readonly read: (root: Root) => Value
}

What a region reads: the Model paths it depends on and how its value is read from the root. A foldkit-surface Projection is one, as built from the application's field references (Projection.struct({ cart: App.model.cart })), so the paths are the ones the references declare.

SlotView.Region

type · packages/mixins/src/slotView.ts:794

type Region<Root, Slots, Message> = (
  root: Root,
  slots: SlotBuilders<Slots, Message>,
  h: HtmlBuilder<Message>,
) => Html

A region of a SlotView's drawing, drawn again only when what it reads changed.

SlotView.region

function · packages/mixins/src/slotView.ts:826

function region<Root, Value, Slots, Message>(
  reads: Reads<Root, Value>,
  draw: (
    value: Value,
    slots: SlotBuilders<Slots, Message>,
    h: HtmlBuilder<Message>,
  ) => Html,
  options?: {
    readonly pathEpoch?: ((root: Root) => number) | undefined
  },
): Region<Root, Slots, Message>

A region: part of a SlotView's drawing that is given only what reads reads, and drawn again only when that changed. While every path reads depends on holds the value it held, the region does not read at all and Foldkit reuses its last drawing without diffing it; when one changed and the read gives the same fields as before, it is reused too. What reads no path of its own (a Remote read, alone or in a Projection.struct) is read on every render, and the region drawn again when that gives other fields. Like a Slot's lazy item, it is drawn again when a Mixin gives the Slots it used something different, and drawn afresh where there is no runtime frame.

draw reads only its value, so the region cannot read what it is not drawn again for. A region drawn inside another region or item is drawn in place, and the outer one every time.

Example

ts
const Header = SlotView.region(
  Projection.struct({ cart: App.model.cart }),
  ({ cart }, slots: SlotView.SlotBuilders<typeof Page.slots, Message>, h: HtmlBuilder<Message>) =>
    h.header(slots.header.attrs(), [`${cart.length} in the cart`]),
)
// In the page's render: Header(model, slots, h)

SlotView.replace

function · packages/mixins/src/slotView.ts:540

function replace(
  name: string,
  mixin: StaticMixin<never> | Mixin<never>,
): SlotViewRemoval
function replace<MixinMessage>(
  name: string,
  mixin: Mixin<MixinMessage>,
): <Slots, Input, Provides extends string = never>(
  view: SlotView<Slots, Input, MixinMessage, Provides>,
) => SlotView<Slots, Input, MixinMessage>

The view with mixin in the place of the attachment named name, so it resolves in the same order. Refused as detach is.

SlotView.resolution

function · packages/mixins/src/describe.ts:292

function resolution(view: AnyView, input: unknown): Resolution

What input makes each slot hold: class tokens, style property names, attribute names, event names and Mount names, each with the attachment that gave it. It reports no attribute value, style value or Mount args (a password in Value is the input's, and an attribute carries it), but class tokens are reported as given, so a class built from input text surfaces it. It runs no Mount: a handler is built with the inert builder and never called, and a contribution is called with input, as a render would. Where resolve would throw, the conflict is a finding. The view's own base attributes are not drawn, so a clash with one is not reported here.

SlotView.SlotBuilder

type · packages/mixins/src/slotView.ts:39

type SlotBuilder<Message, Slots = unknown> = {
  readonly attrs: <Base extends SlotAttributes<Message> = readonly []>(
    base?: Base,
    item?: SlotItem,
  ) => ReadonlyArray<Base[number] | ContributedAttribute<Message>>
  readonly lazy: <const Args extends Readonly<Record<string, unknown>>>(
    item: SlotItem,
    draw: (
      slots: SlotBuilders<Slots, Message>,
      h: HtmlBuilder<Message>,
      args: Args,
    ) => Html,
    args: Args,
  ) => Html
}

SlotView.SlotBuilders

type · packages/mixins/src/slotView.ts:66

type SlotBuilders<Slots, Message> = {
  readonly [K in keyof Slots]: SlotBuilder<Message, Slots>
}

SlotView.SlotView

interface · packages/mixins/src/slotView.ts:81

interface SlotView<Slots, Input, Message, Provides extends string = never>
  extends
    Pipeable<SlotView<Slots, Input, Message, Provides>>,
    Supplies<Provides> {
  (input: Input, h: HtmlBuilder<Message>): Html
  readonly name?: string
  readonly slots: Slots
  readonly mixins: ReadonlyArray<MixinFor<Message>>
  readonly render: SlotViewRender<Slots, Input, Message>
}

A view over published Slots. Provides is what its attachments supply, as `${slot}/${obligation}` names, which A11y.conform checks; a view defined bare supplies nothing.

SlotView.SlotViewRemoval

type · packages/mixins/src/slotView.ts:470

type SlotViewRemoval = <Slots, Input, Message, Provides extends string = never>(
  view: SlotView<Slots, Input, Message, Provides>,
) => SlotView<Slots, Input, Message>

A transform that takes an attachment away or puts another in its place. The view it makes supplies nothing at the type level, a Style taken or not: the type holds no attachment's name, and what one took may still be supplied by another, so subtracting it would refuse what is met. A11y.conform's run-time check is made again instead.

SlotView.SlotViewRender

type · packages/mixins/src/slotView.ts:70

type SlotViewRender<Slots, Input, Message> = (
  input: Input,
  slots: SlotBuilders<Slots, Message>,
  h: HtmlBuilder<Message>,
) => Html

SlotView.SlotViewTransform

type · packages/mixins/src/slotView.ts:454

type SlotViewTransform = <
  Slots,
  Input,
  Message,
  Provides extends string = never,
>(
  view: SlotView<Slots, Input, Message, Provides>,
) => SlotView<Slots, Input, Message, Provides>

A transform on any view, used by Message-free static mixins such as Style.

SlotView.SlotViewTransformFor

type · packages/mixins/src/slotView.ts:459

type SlotViewTransformFor<Message> = <
  Slots,
  Input,
  Provides extends string = never,
>(
  view: SlotView<Slots, Input, Message, Provides>,
) => SlotView<Slots, Input, Message, Provides>

A transform restricted to one Message universe, for Behavior mixins.

SlotView.toMarkdown

function · packages/mixins/src/describe.ts:334

function toMarkdown(description: ViewDescription): string

Deterministic Markdown for a description: stable key order, no timestamps.

SlotView.Unbound

type · packages/mixins/src/slotView.ts:775

type Unbound<Slots> = Slots extends Bound<any, any, any> ? never : Slots

Slots that are not a SlotView.over binder, so a binder that fails a factory's first overload is refused there, not read as slots.