bun install --frozen-lockfile --ignore-scripts and run:12bun run bundle:presets swift run EffectCounter dist/presets.js

src/presets.ts gives the leaf a name and two types. The first type flows to Swift; the second flows back to TypeScript:123456import { defineNativeView } from "effect-appkit" const PresetPicker = defineNativeView< { selected: number }, { step: number } >("PresetPicker")
view, use the factory in place of a built-in element:123model.visible && h.row.children( PresetPicker({ selected: model.step }, event => Message.Choose(event)) )
{"step":5}. The callback receives an object, not a JSON string. The component’s Choose update returns { ...model, step }; on the next render the picker receives {"selected":5}. Omit the callback for a display-only leaf. The factory does not register a Swift implementation or generate Swift types.Int works because all three permitted steps are whole numbers.12struct PresetProps: Decodable { let selected: Int } struct PresetEvent: Encodable { let step: Int }
PresetPicker:Sources/EffectCounter/PresetPicker.swift. The binding reads from the latest props and sends an event on user input. It does not keep separate @State for the selected step.import EffectAppKit import SwiftUI @MainActor let presetPicker = NativeViewRegistration(name: "PresetPicker") { (props: PresetProps, emit: @escaping @MainActor (PresetEvent) -> Void) in Picker("Step size", selection: Binding( get: { props.selected }, set: { emit(PresetEvent(step: $0)) } )) { ForEach([1, 5, 10], id: \.self) { step in Text("\(step)").tag(step) } } .pickerStyle(.segmented) .frame(width: 220) }
body with the loaded presets script:1EffectAppKitSurface(script: script, registrations: [presetPicker])
try EffectAppKitView(script: script, registrations: [presetPicker]). Registrations are per surface, not global. Names must be unique and nonempty, and must match the TypeScript name exactly. Keep the same registration instance across SwiftUI updates; creating a new one in body remounts the whole surface.null, dense arrays, or plain objects containing those values. Pass the whole props value, not a partial patch. Don’t pass functions, undefined, Date, NaN, class instances, or cyclic objects. TypeScript generics describe the intended shape; they are not runtime validators.JSONDecoder and encodes each event with JSONEncoder. A missing registration or an undecodable initial prop is a mount error. An event Swift cannot encode is logged and not sent. Keep identifiers, units, optional values, and numeric ranges consistent on both sides.NSView on the main actor. A SwiftUI registration creates an NSHostingView for its content.rootView rather than creating another hosting view.dismantle closure if provided. Use it to cancel native observers or other work you started. Old emitters do not deliver events after removal.step.defineNativeContainer adds one renderer-managed slot, without exposing NSView to JavaScript. Give it h for the enclosing message type:123const Group = defineNativeContainer<{ title: string }>("Group") // In view(h, model): return Group(h, { title: model.title }).children(h.label.prop("value", model.summary))
NativeViewRegistration(container: "Group", make:update:dismantle:). make receives (props, children: NSStackView, emit); return the supplied stack itself or a shell that contains it (for example an NSBox whose contentView is children). Keep that exact stack in the shell across updates. Configure its layout in Swift, but leave insertion, reordering and removal of its arranged subviews to the renderer. Props, optional events and teardown otherwise work like a leaf. Leaf/container mismatches fail explicitly.NativeViewRegistration(name:make:update:insertChild:removeChild:dismantle:). Insert the supplied child NSView at the requested index or in its named slot; remove that same view when asked. The renderer owns the child and its event sink. Don’t recreate it from the props. Travel Notebook uses this overload to place Sidebar, TaskQueue, and DocumentReader in its SwiftUI shell, so picking a checklist row resolves to that row rather than the entire window.h.keepAlive(s => Editor(s, { key: "editor" })) at the composition site. Leaving the tab detaches its native subtree into an off-window stack. Returning restores the same component model, controls, custom leaves and shell; parking does not call dismantle. A pending Component.lazy fallback also retains its identity across placement until the bundled loader resolves. This does not install an external JS chunk loader. A keep-alive predicate becoming false, or closing the owning component, ends the retention and dismantles registrations once. Do not rely on first responder surviving detachment.