SPB Git

Repository Public License

COMPONENT_SDK.md 2.7 KB

# Component SDK

A catalog component is three things that must agree: a manifest (the contract), a view (the implementation) and tests. A component without tests and a fixture counts as planned, not working.

# 1. Manifest (packages/prism-component-catalog/Sources/Catalog.swift)

swift
ComponentManifest(
    type: "prism.badge", name: "Badge", category: "foundation",
    summary: "A small colored status label.",
    props: [PropSpec("text", .string, "Badge text.", required: true), toneProp],
    events: [], acceptsChildren: false, permissions: [],
    keywords: ["tag", "status", "pill", "chip"],
    accessibilityRole: "staticText", accessibilityLabelProp: "text",
    examples: [["id": "status", "type": "prism.badge", "props": ["text": "Over budget", "tone": "danger"]]],
    fixture: fixture("prism.badge", ["text": "Verified", "tone": "success"])
)
  • PropSpec.kind: string, number, boolean, enum, list, object, query, any.
  • dynamic: true accepts {"$expr": …}; binding: .required demands {"$bind": "stateKey"} (inputs that write state).
  • fixture is a self-contained preview (root, objects, optional state and permissions). It is rendered in the Library and in tests.
  • examples are what the builder agent receives from catalog.examples; they must use only declared props and events.

After changing any manifest run scripts/update-catalog-lock.sh and review the diff of CatalogLock.swift: the registry rejects a manifest whose content hash differs from the lock.

# 2. View (packages/prism-renderer/Sources/*Components.swift)

Add a case to NodeView.content(_:) and a SwiftUI view that takes the PrismObject.

  • Read props through the runtime: runtime.string(o.props["text"]), runtime.bool(...), runtime.rows(o.props["query"]).
  • Write state through runtime.stateBinding(o); fire events with runtime.fire("change", on: o.id, value: …).
  • Never read files, the network or the clock directly; go through the runtime so permissions apply.
  • A control with nothing to do must be disabled and say why (hard rule 2).
  • Render children with ChildrenView(ids: o.children).

# 3. Tests

Automatic for every manifest: strict decode round trip, fixture validates with zero errors, examples use declared props, the fixture renders off-screen without runtime errors (RenderTests.everyComponentFixtureRenders). Add behaviour tests in RuntimeTests when the component runs logic.

# Validation rules a component gets for free

Unknown or missing props, wrong literal types, enum values, expression syntax and names, undeclared state keys, query tables and columns, events and actions, permissions the app has not declared, and a missing accessibility label are all reported by BlueprintValidator.

Files main
Menu

Theme