Agent-readable docs index: /effect-swiftui/llms.txt. Full docs in one file: /effect-swiftui/llms-full.txt. Download /effect-swiftui/docs.zip to grep all markdown files locally.

AppKit 0.2.x: Add a custom native view

AppKit 0.2.x archive. These registrations mount AppKit views, not SwiftUI views. For the current renderer, see the SwiftUI quickstart.
The built-in controls include a slider but no segmented picker. To offer fixed steps of 1, 5, and 10, let Swift implement the picker and let the TypeScript component own the selected step. The picker is a leaf: TypeScript places it in the layout, but doesn’t describe its children.

Try the picker

From an AppKit 0.2.1 checkout, install dependencies with bun install --frozen-lockfile --ignore-scripts and run:
bun run bundle:presets swift run EffectCounter dist/presets.js
Select 5, then press Count: 0. The button now reads Count: 5. Press Hide presets: the picker disappears, but the count and step remain. Press Show presets: the new picker already has 5 selected.
Effect Presets window containing the step label, a segmented picker for 1, 5, or 10, a count button, and a Hide presets button
The segmented picker is supplied by SwiftUI. The step label and the two buttons around it are built-in AppKit elements.

Declare the contract in TypeScript

src/presets.ts gives the leaf a name and two types. The first type flows to Swift; the second flows back to TypeScript:
import { defineNativeView } from "effect-appkit" const PresetPicker = defineNativeView< { selected: number }, { step: number } >("PresetPicker")
Inside the component’s view, use the factory in place of a built-in element:
model.visible && h.row.children( PresetPicker({ selected: model.step }, event => Message.Choose(event)) )
For a selection of 5, Swift emits {"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.

Implement the Swift side

Add these types to the macOS target. The names of the Swift types are local; the JSON keys and value types must match. Here Int works because all three permitted steps are whole numbers.
struct PresetProps: Decodable { let selected: Int } struct PresetEvent: Encodable { let step: Int }
Choose one registration for PresetPicker:
This is the registration in 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) }

Register it on the surface

Pass the registration when you mount your bundle. For example, in a SwiftUI body with the loaded presets script:
EffectAppKitSurface(script: script, registrations: [presetPicker])
The AppKit equivalent is 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.

What crosses the boundary

Props must be plain JSON data: strings, booleans, finite numbers, 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.
Swift decodes each props value with 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.

When the native view lives and dies

  • Mount: The first props value constructs an NSView on the main actor. A SwiftUI registration creates an NSHostingView for its content.
  • Update: New props update the existing AppKit instance. The SwiftUI form replaces the hosting view’s rootView rather than creating another hosting view.
  • Remove: Removing the leaf, removing an ancestor, or closing the surface releases the mounted view and calls its AppKit dismantle closure if provided. Use it to cancel native observers or other work you started. Old emitters do not deliver events after removal.
  • Show again: A newly inserted leaf gets a new native instance. Keep anything that must survive removal in the TypeScript model, as the demo does with step.

Native containers and kept children

defineNativeContainer adds one renderer-managed slot, without exposing NSView to JavaScript. Give it h for the enclosing message type:
const Group = defineNativeContainer<{ title: string }>("Group") // In view(h, model): return Group(h, { title: model.title }).children(h.label.prop("value", model.summary))
Register the same name with 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.
If your layout needs separate insertion points rather than one stack, use 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.
For a retained tab, use 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.