Defining components
The model can only use what you define. Each component is a name, a description and a Zod schema, plus the code that draws it. From that one definition OpenUI derives the signature the model reads, the rules the parser enforces and the view the browser renders.
By the end of this lesson you can
- write a
defineComponentcall and say which of its four fields the model sees; - predict the signature line, and the call a model writes, from the order of the keys in a Zod object;
- type a child slot with
.refand assemble components withcreateLibraryand aroot; - add components to a stock library with
extendandslots, and say why the slot matters; - recognise a reactive prop and the two things its component must do.
One definition, three consumers
defineComponent takes four fields. The name is the word the model writes in a program. The description is printed next to the signature, so it is how the model learns when to pick this component. props is a Zod object that declares every argument. The last field, component, is the renderer: it receives the props already evaluated and returns a view.
Key idea
The same definition feeds the three places that must agree. The system prompt prints name, props and description for the model. The parser checks every call against props. The Renderer hands the checked props to component. The model never sees component, and you cannot get the three out of step.
Reference code in this lesson uses @openuidev/react-lang, where component is a React component. The runnable labs use @openuidev/lang-core, where component is null because lang-core only parses and validates. The simulator on this page draws a fixed set of components itself.
Key order is argument order
OpenUI Lang has no argument names, so something must say which value goes where. That something is the order of the keys in the Zod object. The prompt prints the order as a signature, and the parser reads the model's values back into named props in the same order.
Zod keys, signature, call
- A component's
propsis az.object. The keys arecity,temp,skyandunits, in that order. - Each key gets a position:
cityis argument 1,unitsargument 4. The model has no other way to name them, so this order is part of your library's API. Put required keys first and optional keys last. - The prompt turns the schema into one signature line. A
?marks an optional key. This is the text the model actually reads. - The model writes plain values in that order. The parser maps them back to
city,temp,skyandunits, checks each one against its Zod type, and hands the result tocomponent.
The lab prints the signatures its library produces. Run it with cd openui && node 02-library/prompt.mjs:
signatures: Stack(children: any[]) — Vertical layout WeatherCard(city: string, temp: number, sky: string, units?: "celsius" | "fahrenheit") — Current weather for one city Forecast(days: Day[]) — A row of forecast days Day(date: string, min: number, max: number) — One forecast day UnitToggle(name: string, value?: $binding<"celsius" | "fahrenheit">) — Celsius / Fahrenheit switch
Try it: design the schema
Below is the WeatherCard schema as a list of keys. Move keys up and down, and mark each one optional or required. The signature the model would read, and a valid call for it, update as you go. A key that is optional and skipped in the middle of a call must still hold a place with null, which is why optional keys belong at the end.
Signature line (what the model reads)
A call that satisfies it (optional keys skipped when the call allows it)
This widget builds the schema with the simulator's OUI.defineComponent and OUI.z, a small look-alike of the real APIs. The signature format and the key-order rule are the real ones. The card is drawn by the simulator, not by WeatherCard.tsx.
Break it: the wrong order
The editor below uses a library that has WeatherCard with the signature above. Predict what happens first, then make each change.
Typed children with .ref
Many components hold other components. Every defineComponent result carries a .ref, a schema that means "an element of that component". Use it inside another component's props, and the signature says exactly which child is allowed there.
Day.ref makes the slot typed
Dayis defined first.Forecastthen declaresdays: z.array(Day.ref), a list ofDayelements.- In the prompt that prop prints as
days: Day[]. The model reads the component name it must use for each item. - The model writes
Forecast([d1, d2])and defines each day separately, so the children are references. - This is the idiomatic way to nest: one child per line, rather than one deeply nested value. Each line streams, validates and fails on its own. To allow several child types, pass
z.union([A.ref, B.ref])toz.array.
OpenUI rule
Compose with z.array(Child.ref) and keep schemas flat. Deeply nested object props cost tokens and raise error rates; separate component lines stream independently.
Assembling a library
createLibrary takes the components and one more choice: the root, the component every program must start with. The generated prompt tells the model to begin with root = Stack(...) for this library, which lets the shell of the UI appear while the rest is still streaming. The root also decides the fallback when a model forgets the root line, as the previous lesson showed. The root option is optional. If you give it, it must name one of the components, or createLibrary throws. Without it there is no fallback entry and the prompt has no root name to suggest, so always set it.
OpenUI rule
root must match the name of one of the library's components. Stock libraries differ: openuiLibrary uses Stack, openuiChatLibrary uses Card.
A library also takes optional componentGroups (named sections in the prompt, with notes), functions and actions. Keep the component list short: every component adds a line to the system prompt, and fewer choices mean fewer mistakes.
Extending a stock library
You rarely start from nothing. @openuidev/react-ui ships ready libraries with dozens of components, and library.extend(...) derives a new one from it. The base library is not changed.
The slots entry is the part people miss. A slot names a prop of an existing component, as "Parent.prop" (or just "Parent" when that parent has exactly one content slot), and splices the new component into that prop's type. Without it, the library contains WeatherCard, but no signature says a Card may hold one, so the model has no reason to put it there. With it, the prompt signature for Card lists WeatherCard among its children and the parser accepts it in that position. Stack children are typed any[], so Stack needs no slot.
| Verb | Effect |
|---|---|
add | Adds components. With slots, also lists each one in the named parents. |
override | Replaces the component of the same name. |
remove | Drops a component from the library and from every parent that listed it. |
See the effect on a signature. The button runs extend in this page on a copy of the simulator's stock library and prints the parent signatures it changes.
Before
After
The simulator's stock library has about 20 of the 95 real components, so the lists are shorter than openuiLibrary's. Its extend follows the same rule.
OpenUI rule
Create the derived library once, at module scope. The Renderer keeps its parser per library object, so a new library on every render makes it parse again from the start.
Reactive props, a first look
Most props are values. A reactive prop is also a place the component can write to. It lets the model bind an input to a $variable, and the variable then drives everything that reads it, with no round trip to the model. A component opts in with two steps.
- Mark the prop with
reactive(...). The prompt then printsvalue?: $binding<"celsius" | "fahrenheit">and the evaluator passes the binding itself, not only its current value. - Read and write through
useStateField, not React'suseState. ItssetValueupdates the variable and the runtime re-evaluates everything that reads it.
Click the toggle in the example. The program binds UnitToggle and WeatherCard to the same $units; the card's unit label changes with no model call. (This card only relabels; in a real app $units would also feed a Query, as in the next lessons.)
The next lessons cover variables in full. The simulator draws UnitToggle itself; the real one is the UnitToggle.tsx above.
Quick check
You reorder the keys of a component's Zod object. What changes for the model?
Which field of defineComponent does the model never see?
props, and the description. component is the renderer and stays in your app.You add WeatherCard with extend and no slots. What is missing?
add puts the component in the library. slots splices it into a parent's prop type, which is what the signature and the parser use. Stack children are any[], so it needs none.Recap
defineComponent=name,description, Zodpropsand acomponentrenderer. The model sees the first three; the parser enforcesprops; the Renderer runscomponent.- The order of Zod keys is the order of positional arguments, printed as the signature. Required first, optional last.
z.array(Child.ref)types a child slot and keeps each child on its own line.createLibrarybundles components with arootthat every program must start with. It is optional, but without it there is no fallback entry and no root name in the prompt, so always set it.extendderives a new library.slotslists a new component inside a parent's signature. Build it once at module scope.- A reactive prop (
reactive(...)plususeStateField) lets an input write to a$variable; the next lessons build on it.