A primitive of your own
Primitives and the registry says what a primitive is made of. This page writes one, all the way through, and then shows what you get for it.
The thing we are building is a status row: one service, how it is doing right now, and the detail underneath. The starter library has no such thing, because "how is our checkout doing" is your business rather than a general shape, and that is the ordinary reason to write one.
By the end of the page the row is on the screen, a change has been proposed to it, and three tests are holding it. Everything on this page below the file is produced by running that file, so if the primitive stops behaving this way the page stops building.
First, the one decision that is hard to undo
Before any code: what is a prop, and what is a child?
A status row has exactly one service name and exactly one state. There is never a second of either, and changing one is exactly one operation. So those are props.
The detail underneath repeats. One row might say nothing, another might say three sentences, and somebody will want to add a fourth later. So that is children.
Get it backwards and you pay either way. Fixed fields as children means a row whose service name was deleted is still a valid tree. Repeated content as props means "add a line to that row" is impossible to say without writing a new primitive.
That rule is Children and slots in one sentence, and it is worth settling before you write the schema, because the schema is the part that is expensive to change once pages are stored against it.
The file
Here it is, whole. It is one file in an ordinary project, and the filename is the primitive's type with the dots left in, which is the convention the scaffolder follows.
// A primitive of your own: one service, and how it is doing right now.
//
// This is the file *A primitive of your own* shows, read off disk as that page
// builds. It is an ordinary module in an ordinary project — nothing in it is
// available to the documentation site and not to you.
//
// Three decisions are worth watching for, because they are the three every
// primitive has to make:
//
// 1. What is a prop and what is a child. A service has one name and one
// state, so those are props. The detail underneath repeats, so it is
// children.
// 2. What a string may be. Every string here has a maximum, and `state` is
// a closed set rather than free text.
// 3. Where the words a reader sees come from. The state words are declared
// beside the component rather than written inside it, so a deployment can
// translate them.
import { createElement } from "react"
import { z } from "zod"
import { colour, hairline, radius, space, weight } from "@jam-overture/loom-primitives"
import type { LoomPrimitiveProps } from "@jam-overture/loom/react"
import { definePrimitive } from "@jam-overture/loom/sdk"
/**
* The words for the three states, and the reason they are not in the component.
*
* A primitive shows strings that are not in the tree — this one shows the word
* for a state the tree names as `offline`. Declaring them means a deployment can
* hand back its own, and `loom.text` reads whichever is in force.
*/
const STATE_WORDS = {
operational: "Operational",
degraded: "Degraded",
offline: "Offline",
} as const
type StateWord = keyof typeof STATE_WORDS
const props = z
.object({
service: z.string().min(1).max(60),
/**
* A closed set, which is the whole point of writing this primitive rather
* than a paragraph. A model may move a service from `operational` to
* `degraded`; it cannot invent `on fire`, and no schema anywhere has to be
* consulted to know that.
*/
state: z.enum(["operational", "degraded", "offline"]),
/** When it started, as the author wrote it — "09:14 UTC", "Tuesday". */
since: z.string().min(1).max(40).optional(),
})
.strict()
type Props = z.infer<typeof props>
/**
* The badge's two colors per state, taken from the theme and never typed.
*
* Nothing here is green, amber or red, and that is not squeamishness: a Loom
* palette names a canvas, an accent and a set of borders, and it names no
* color that means *bad*. A primitive that reached for `#b91c1c` would look
* right under the palette its author had open and would survive every re-theme
* unchanged, which is the one failure the token helpers exist to prevent.
*
* So the state is carried by the **word**, and the badge only says how much
* attention it is asking for.
*/
const BADGE: Readonly<Record<StateWord, { readonly background: string; readonly color: string }>> = {
operational: { background: colour("bg-surface-muted"), color: colour("fg-muted") },
degraded: { background: colour("accent-subtle"), color: colour("accent-strong") },
offline: { background: colour("fg-default"), color: colour("bg-surface") },
}
export const serviceStatus = definePrimitive({
type: "status.service",
description:
"One service and how it is doing right now. Its children are the detail, one sentence per node. Do not use it for anything nobody is measuring — an unmeasured service is worse than an absent one.",
props,
slots: [],
/** The props a reader reads as words. `state` is a state, not copy. */
copy: ["service", "since"],
text: STATE_WORDS,
component: ({ loom, props: given, children }: LoomPrimitiveProps<Props, StateWord>) =>
createElement(
"div",
{
/*
* Spread it, always. A primitive that drops this renders perfectly and
* cannot be selected in the portal, and nothing anywhere reports it.
*/
...loom.editable,
style: {
display: "grid",
gap: space(2),
padding: space(4),
border: `1px solid ${hairline()}`,
borderRadius: radius("md"),
background: colour("bg-surface"),
},
},
createElement(
"div",
{ style: { display: "flex", alignItems: "center", gap: space(3) } },
createElement(
"span",
{ style: { fontWeight: weight("heading"), color: colour("fg-default") } },
given.service
),
createElement(
"span",
{
style: {
...BADGE[given.state],
padding: `${space(1)} ${space(2)}`,
borderRadius: radius("full"),
fontSize: "0.75em",
},
},
loom.text[given.state]
),
given.since === undefined
? null
: createElement("span", { style: { color: colour("fg-subtle"), fontSize: "0.85em" } }, given.since)
),
children
),
})
A few things in there are worth stopping on.
Every string has a maximum
import { z } from "zod"
const props = z
.object({
service: z.string().min(1).max(60),
/**
* A closed set, which is the whole point of writing this primitive rather
* than a paragraph. A model may move a service from `operational` to
* `degraded`; it cannot invent `on fire`, and no schema anywhere has to be
* consulted to know that.
*/
state: z.enum(["operational", "degraded", "offline"]),
/** When it started, as the author wrote it — "09:14 UTC", "Tuesday". */
since: z.string().min(1).max(40).optional(),
})
.strict()
min(1) keeps an empty string out. max(60) is the one that matters most and
is the easiest to leave off: a name with no maximum is an invitation for
something to write a sentence where a label belongs, and your layout is the
thing that finds out.
state is an enum rather than a string, and that is the sharpest version of the
bargain this whole framework is making. A bounded vocabulary buys you
reviewability. Nobody has to read your component to know that this row can say
one of three things, and nobody has to write a rule forbidding the fourth.
.strict() is what makes an unrecognized prop a refusal rather than a value
that rides along unnoticed.
Behavior lives in the component, not in the tree
The tree says offline. It does not say what color that is, how big the badge
is, or whether the word is capitalised. All of that is in the component, which
means you can change every one of them without touching a single stored page.
This is the other half of the bargain. The tree is a small, reviewable thing precisely because it is not trying to be a rendering.
The state words are declared, not written inline
The row shows the word "Offline", and that word is nowhere in the tree. It is
declared beside the component and read back through loom.text, so a deployment
can hand back its own set and get a translated page without anybody editing a
component.
Nothing in it is a color
No hex, anywhere. The badge takes its two colors from the theme through the
token helpers in @jam-overture/loom-primitives, which return var()
references rather than values, so the component never learns which palette is
mounted. Making it look like yours is the
page about that, and the short version is that a literal color in a component
looks perfect under the palette its author had open and survives every re-theme
unchanged.
The states are told apart by the word rather than by green, amber and red, and that is not squeamishness. A Loom palette names a canvas, an accent and a set of borders, and it names nothing that means bad.
Registering it
A primitive nobody registered is a primitive nothing can render. You register yours beside the library rather than instead of it:
import { STARTER_PRIMITIVES } from "@jam-overture/loom-primitives"
import { createPrimitiveRegistry, describeRegistryError } from "@jam-overture/loom/sdk"
const built = createPrimitiveRegistry([...STARTER_PRIMITIVES, serviceStatus])
if (!built.ok) throw new Error(describeRegistryError(built.error))
export const registry = built.value
That is the whole of it. The registry refuses rather than repairs: a duplicate type, or a declaration that names a prop the schema does not have, and you get an error at composition rather than a surprise at render.
It renders
The frame below is a real tree, rendered through a registry holding the whole starter library plus the primitive above. The heading, the sentence and the arrangement are the library's. The three rows are yours.
The box under the frame is the runtime. Ask it to add a sentence and watch the new node land inside a row, which is the prop-or-child decision from the top of this page doing visible work.
Vaughan & Rill status
Everything we run, and how it is doing. Checked every minute.
Pages and search are answering normally.
Card payments are retrying. Nothing has been lost.
Paused while we rebuild the index.
Every one of these goes through the same pipeline a model’s answer would: interpreted, analysed, weighed, judged, applied, appended. The log is real and it is in this tab — reload the page and the example is back as it was.
show the treehide the tree
{
"kind": "element",
"id": "n_ownprimitive15",
"type": "loom.page",
"props": {
"fills": true,
"width": "readable",
"loom:theme": {
"palette": "minimal",
"fontPack": "minimal-sans",
"stylePreset": "precise"
}
},
"children": [
{
"kind": "element",
"id": "n_ownprimitive2",
"type": "loom.heading",
"props": {
"level": 2
},
"children": [
{
"kind": "text",
"id": "n_ownprimitive1",
"value": "Vaughan & Rill status"
}
]
},
{
"kind": "element",
"id": "n_ownprimitive4",
"type": "loom.prose",
"props": {
"tone": "muted"
},
"children": [
{
"kind": "text",
"id": "n_ownprimitive3",
"value": "Everything we run, and how it is doing. Checked every minute."
}
]
},
{
"kind": "element",
"id": "n_ownprimitive14",
"type": "loom.stack",
"props": {
"direction": "column",
"gap": "snug"
},
"children": [
{
"kind": "element",
"id": "n_ownprimitive7",
"type": "status.service",
"props": {
"service": "Storefront",
"state": "operational",
"since": "since Monday"
},
"children": [
{
"kind": "element",
"id": "n_ownprimitive6",
"type": "loom.prose",
"props": {
"tone": "muted",
"size": "small"
},
"children": [
{
"kind": "text",
"id": "n_ownprimitive5",
"value": "Pages and search are answering normally."
}
]
}
]
},
{
"kind": "element",
"id": "n_ownprimitive10",
"type": "status.service",
"props": {
"service": "Checkout",
"state": "degraded",
"since": "09:14 UTC"
},
"children": [
{
"kind": "element",
"id": "n_ownprimitive9",
"type": "loom.prose",
"props": {
"tone": "muted",
"size": "small"
},
"children": [
{
"kind": "text",
"id": "n_ownprimitive8",
"value": "Card payments are retrying. Nothing has been lost."
}
]
}
]
},
{
"kind": "element",
"id": "n_ownprimitive13",
"type": "status.service",
"props": {
"service": "Nightly export",
"state": "offline",
"since": "since 02:00 UTC"
},
"children": [
{
"kind": "element",
"id": "n_ownprimitive12",
"type": "loom.prose",
"props": {
"tone": "muted",
"size": "small"
},
"children": [
{
"kind": "text",
"id": "n_ownprimitive11",
"value": "Paused while we rebuild the index."
}
]
}
]
}
]
}
]
}What a model is told about it
An interpreter hands a model a catalog of what it may propose, and this is your primitive's row in it, read off the registry as this page built.
status.service
what a model is handed
One service and how it is doing right now. Its children are the detail, one sentence per node. Do not use it for anything nobody is measuring — an unmeasured service is worse than an absent one.
Props
- service · required
- since · optional
- state · required
Every value state may take
operational · degraded · offline
Words declared beside the component
- operational → Operational
- degraded → Degraded
- offline → Offline
Look at what is not in there. No types, no maximum lengths, no mention of
what the component does with any of it. A model is told the names, which of them
are required, and whatever you wrote in description.
That is why description is worth more than it looks. It is not a comment. It
is the most effective place on the whole primitive to say when not to use
this thing, and it is read by the only reader who cannot go and look at your
code.
What it refuses
Each block below is a real render that was refused, and the sentence in each one is the runtime's own rather than this page's paraphrase of it.
a tree holding the node, rendered through a registry without it
unknown-primitive
You wrote the primitive and never added it to the registry.
What the render reported
no primitive is registered for "status.service", so node n_ownunregistered1 and its subtree were omitted
state: "on fire"
invalid-props
A state outside the three the schema names.
What the render reported
node n_ownbadstate1 does not satisfy the props declared by "status.service", so it and its subtree were omitted — state: Invalid enum value. Expected 'operational' | 'degraded' | 'offline', received 'on fire'
no state at all
invalid-props
A node that says which service and never says how it is doing.
What the render reported
node n_ownnostate1 does not satisfy the props declared by "status.service", so it and its subtree were omitted — state: Required
service: "…" past its maximum
invalid-props
A sentence where a name belongs, which is what an unbounded string invites.
What the render reported
node n_ownlongname1 does not satisfy the props declared by "status.service", so it and its subtree were omitted — service: String must contain at most 60 character(s)
tone: "danger"
invalid-props
A prop the schema never declared. Calling .strict() on the schema is what refuses it.
What the render reported
node n_ownextraprop1 does not satisfy the props declared by "status.service", so it and its subtree were omitted — props: Unrecognized key(s) in object: 'tone'
The first is the mistake everybody makes once, and it is the only one here that is not about the schema at all: the tree is perfect and the deployment has never heard of the type.
The rest are the schema doing what you wrote it to do. Note that each render came back with one complaint rather than a list. A node whose props are refused is never called, so nothing underneath it is reached and nothing underneath it is reported.
Can a person edit it?
There is one failure in this system that reports nothing at all. A primitive
that forgets to spread loom.editable renders perfectly, passes every type
check, looks right on the page, and cannot be selected in the portal. Nobody
finds out until somebody tries to edit that part of a page and nothing happens.
auditRegistry is what finds it. It walks a registry, calls each component with
props built from that component's own schema, and reports what it saw. Here it
is run on the registry this page renders through.
What the audit saw
status.service · 4 configurations probed
Can a person select it in the portal?
yes — the decoration is on what it drew
Does it place the children it is handed?
yes
Any region it declared and never placed?
none
Did it throw on props its own schema accepts?
never, under every state probed
Does a sound tree report anything?
nothing at all
It reports and never refuses, so it is worth nothing unless something runs it. Scaffolding a project is where the scaffolder's own version of that test is explained, and this is that test's subject: a primitive somebody just wrote.
The three tests
This is what Testing what you built means by use your own tree and your own registry when the thing you are testing is your primitive. Build the tree yourself:
import { buildElement, buildText, createTree, sequentialIdFactory } from "@jam-overture/loom"
import type { LoomTree } from "@jam-overture/loom"
const statusPage = (state: string): LoomTree => {
const ids = sequentialIdFactory("status")
const detail = buildText(ids, "Card payments are retrying.")
const row = buildElement(ids, {
type: "status.service",
props: { service: "Checkout", state },
children: [detail],
})
return createTree(buildElement(ids, { type: "loom.page", props: {}, children: [row] }), ids)
}
Then three tests, and they are three different questions:
import { createThemeRegistry } from "@jam-overture/loom"
import { renderLoomTree } from "@jam-overture/loom/react"
import { auditRegistry } from "@jam-overture/loom/sdk"
import { expect, it } from "vitest"
const themes = createThemeRegistry()
it("renders a sound row with nothing to report", () => {
const rendered = renderLoomTree(statusPage("degraded"), {
resolver: registry,
validator: registry,
themes,
})
expect(rendered.diagnostics).toEqual([])
})
it("refuses a state nobody declared", () => {
const rendered = renderLoomTree(statusPage("on fire"), {
resolver: registry,
validator: registry,
themes,
})
expect(rendered.diagnostics.map((diagnostic) => diagnostic.code)).toEqual(["invalid-props"])
})
it("can be edited, and never throws on props its own schema accepts", () => {
const audit = auditRegistry(registry)
expect(audit.notDecorated).toEqual([])
expect(audit.throwsOnDeclaredProps).toEqual([])
})
The second one is the half that proves anything. A test that only renders the sound tree passes just as well against a schema with no bounds in it at all. Writing the pair is what establishes that the enum you added is the thing doing the work.
toEqual([]) rather than a length, in the first and the third, for the reason
above: a render reports its first complaint rather than all of them, so empty is
the only count that means what you want it to mean.
Then decide what a model may change about it
The primitive says what can be written. A policy says what may be written without you, and for a status row the answer is obvious once you see it:
import { gatePolicySchema } from "@jam-overture/loom"
export const policy = gatePolicySchema.parse({
policyId: "status",
protectedPropKeys: ["state"],
})
A model may rewrite the sentence under a row all day. It may not be the thing that declares a service back up. That is one line, and What AI may change is the rest of the settings.
Testing what you built has the test that proves the line is doing something, and it is worth writing: a policy nobody has asked the Gate about is a list of good intentions.