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 defineComponent call 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 .ref and assemble components with createLibrary and a root;
  • add components to a stock library with extend and slots, 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

Zod schema in WeatherCard.tsx props: z.object({ city: z.string(), temp: z.number(), sky: z.string(), units: z.enum([…]).optional(), }) 1 2 3 4 Key order is the argument order Required keys first, optional keys last, so the model may leave off the end. The most telling key goes in position 1. the prompt prints one signature line WeatherCard(city: string, temp: number, sky: string, units?: "celsius" | "fahrenheit") The call the model writes card = WeatherCard( "Pune" 31 "Sunny" "celsius" ) 1 city 2 temp 3 sky 4 units props = { city, temp, sky, units } what the component receives
  1. A component's props is a z.object. The keys are city, temp, sky and units, in that order.
  2. Each key gets a position: city is argument 1, units argument 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.
  3. The prompt turns the schema into one signature line. A ? marks an optional key. This is the text the model actually reads.
  4. The model writes plain values in that order. The parser maps them back to city, temp, sky and units, checks each one against its Zod type, and hands the result to component.

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

Day props: { date, min, max } Forecast props: { days: z.array(Day.ref) } Forecast(days: Day[]) What the prompt says the slot holds The program root = Forecast([d1, d2]) d1 = Day("Mon", 18, 27) d2 = Day("Tue", 17, 25) Each Day is its own line It streams and validates on its own, and a bad Day does not take Forecast down.
  1. Day is defined first. Forecast then declares days: z.array(Day.ref), a list of Day elements.
  2. In the prompt that prop prints as days: Day[]. The model reads the component name it must use for each item.
  3. The model writes Forecast([d1, d2]) and defines each day separately, so the children are references.
  4. 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]) to z.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.

VerbEffect
addAdds components. With slots, also lists each one in the named parents.
overrideReplaces the component of the same name.
removeDrops 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.

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?

  • Nothing, because OpenUI Lang matches arguments by name
  • The signature line changes, so the order of values it must write changes
  • Only the rendering order of the props
OpenUI Lang arguments are positional. The Zod key order is the argument order, so reordering keys changes the signature, the parser's mapping and what a valid call looks like.

Which field of defineComponent does the model never see?

  • component
  • description
  • props
The prompt carries the name, the signature built from props, and the description. component is the renderer and stays in your app.

You add WeatherCard with extend and no slots. What is missing?

  • The component is not in the library at all, so it can never render
  • No parent's signature mentions it, so the model is never told it may go inside a Card
  • The parser rejects it everywhere, including inside Stack and Card
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