Layout themes
@schema-engine/layout compiles the styling vocabulary of a document into one layout theme: the grid (breakpoint thresholds, gap tokens, the default minimum column width) and the appearance axes together. It knows nothing about forms, so any container of items can use it. @schema-engine/forms and both renderers build on it.
pnpm add @schema-engine/layout zodzod is a peer dependency because the grid, axis and recipe schemas use zod/mini.
What a theme holds
createLayoutTheme({ grid, appearance }) returns a frozen theme:
| Member | Holds |
|---|---|
id | One identity for the whole theme. It is the value of the data-se-theme attribute. |
css | One stylesheet, scoped by [data-se-theme="<id>"]. |
grid | The resolved grid vocabulary the emitters read. |
axes | Each appearance axis name with its closed list of values. |
appearance | The axes as registrations, in a read-only registry: values, tokens, default, metadata. |
Options that describe the same theme give the same id, whatever order the grid's responsive entries were written in. Axis, value and token order is part of the identity.
Compile a theme
import { builtInAxes, createLayoutTheme } from '@schema-engine/layout';
export const theme = createLayoutTheme({
grid: {
breakpoints: { md: '50rem' },
gap: { default: '1rem', loose: '1.5rem' },
minColumnWidth: '14rem',
responsive: { lg: { default: '1.5rem' } },
},
appearance: [...builtInAxes],
});Both halves are optional: createLayoutTheme() is the library default.
| Grid option | Default | Notes |
|---|---|---|
breakpoints | sm 40rem, md 48rem, lg 64rem, xl 80rem, 2xl 96rem | Overrides only the names you give. Thresholds must keep the order sm <= md <= lg <= xl <= 2xl; rem and em are compared at a 16px root. |
gap | none 0, compact 0.5rem, default var(--space-stack, 1rem), loose var(--space-block-lg, 1.5rem), wide var(--space-block-xl, 2rem) | Overrides only the tokens you give. A form picks a token with grid.gap, gapX or gapY. |
minColumnWidth | 15.625rem | Used by columns: 'auto' grids. |
responsive | none | Gap tokens that change from a breakpoint up. Breakpoint-major, like a form's own responsive: one entry per width, listing the tokens that change there. |
appearance is the list of axes. It defaults to builtInAxes (size and surface); a list without builtInAxes has neither. A later axis with the same $type replaces the earlier one, so [...builtInAxes, mine] adds an axis and, when mine is named size, overrides the built-in one. Write the list inline, or as const: a list kept in a plain variable widens, and typed defineForm stops narrowing axis values.
Inline or compiled
An engine takes the theme as layout, either compiled or as inline options:
import { createFormEngine } from '@schema-engine/forms';
import { builtInAxes, createLayoutTheme } from '@schema-engine/layout';
import { elements } from './registrations';
// Inline: the engine compiles a theme of its own.
const inline = createFormEngine({
elements,
layout: { grid: { breakpoints: { md: '50rem' } } },
});
// Compiled: one theme, passed to as many engines as you like.
const theme = createLayoutTheme({ appearance: [...builtInAxes] });
const compiled = createFormEngine({ elements, layout: theme });createRadiantFormEngine takes the same layout option. There are no separate grid or appearance engine options. engine.extend() does not take layout; the extended engine keeps its source's theme. engine.getLayout() returns the theme the engine runs on.
The engine rejects a value that is neither a theme nor { grid, appearance }, and it names the cause. A spread copy or a JSON copy of a compiled theme has lost its brand, so it throws Invalid layout option: unknown key 'id' and asks for the compiled theme itself, rather than compile its keys as options and silently drop the axes.
Share a theme between engines
Engines built on one compiled theme carry the same data-se-theme and the same stylesheet content, so a form embedded in a page matches the page, and a React form looks like a Radiant form of the same theme.
import { createFormEngine } from '@schema-engine/forms';
import { createLayoutTheme } from '@schema-engine/layout';
import { elements } from './registrations';
const theme = createLayoutTheme();
const checkout = createFormEngine({ elements, layout: theme });
const profile = createFormEngine({ elements, layout: theme });
export const sameTheme = checkout.getLayout() === profile.getLayout(); // trueHow the stylesheet reaches the page differs by renderer, by design:
- React hoists equal stylesheets to one
<style>element in the head. - Radiant has no head hoisting, so each form carries an identical, self-contained copy of the stylesheet. Every form is styled before hydration, whatever the render order, streaming, caching or removal of the other forms.
The stylesheet content is the same in both. A host that also wants the rules in its own head can serve engine.getLayout().css; the forms keep their copies, which hold the same rules.
See the two engines recipe for a working pair.
The neutral hooks
The stylesheet selects three things and nothing else: .se-grid, .se-grid-item and [data-se-theme]. There is no alias for another class name.
| Hook | A renderer puts it on |
|---|---|
.se-grid | Every grid container: step grids, and the inner grids of fieldsets and array rows. |
.se-grid-item | Every item placed in a grid: field hosts. |
[data-se-theme] | The form element, each grid and each item, with the theme's id as its value. |
The forms renderers add these beside their own classes (se-form-grid se-grid, se-form-field se-grid-item). The forms classes stay as styling hooks of your own; the layout rules never read them.
Custom properties of a container and an item
The grid and item rules read custom properties, which the renderer writes inline from the form's grid config. Only values an author wrote are written, so an element with no grid config has no style attribute. The exception is derived values: a container whose items use named widths gets --se-grid-template (12 tracks) and --se-grid-default-column even with no grid config, and a container that draws areas gets its template.
| Properties | On | Meaning |
|---|---|---|
--se-grid-template, --se-grid-areas, --se-grid-gap-x, --se-grid-gap-y | A container | Column template, drawn areas, column and row gap. |
--se-grid-default-column | A container | Where an item with no width goes when its siblings use named widths: 1 / -1, the full row. |
--se-grid-justify-items, --se-grid-align-items, --se-grid-justify-content, --se-grid-align-content | A container | Alignment. |
--se-item-grid-column, --se-item-grid-row | An item | Placement: a width or offset becomes a span, an area becomes both. |
--se-item-justify-self, --se-item-align-self | An item | Alignment of the item in its cell. |
--se-item-margin-block-start, --se-item-margin-block-end | An item | Rhythm above and below, from the gap tokens. |
--se-item-display | An item | none when hidden at that breakpoint. |
Each property has a variant per breakpoint (--se-item-grid-column-md, --se-grid-template-lg, and so on) that applies from that width up. The stylesheet falls back through the narrower variants, so you set only the widths that change.
An item that needs its own display (a flex field, for example) sets --se-item-display-mode instead of display. The item rule outranks a component's own class, and --se-item-display-mode is what a responsive hidden toggles against, so a hidden item comes back as the mode you set, not forced to block.
The gap tokens (--se-grid-gap-none, -compact, -default, -loose, -wide) are public: set them to restyle every grid. See Theming.
Nested themes
Every grid and item carries data-se-theme, so a form of theme B rendered inside a grid item of theme A has both themes above it. The token rules do not leak between them: an element takes the tokens of its nearest theme, whichever stylesheet comes later. A pick made on the element that carries the theme, or below it with no other theme in between, uses that theme's tokens. A form embedded in a page grid therefore keeps its own tone: brand while the page picks another.
The token rules (the tokens an axis value sets) stay at zero specificity, so a class of yours still wins. The grid and item rules do not: .se-grid[data-se-theme="<id>"] and .se-grid-item[data-se-theme="<id>"] outrank a lone class, so change placement through the properties above (and --se-item-display-mode for an item's own display), not with a class that sets grid-column or display. Two limits follow from the cascade:
- Tokens are custom properties, so they inherit. A pick made outside the embedded form still reaches it, on an axis the form does not pick, until the form picks a value that declares those tokens. A value that sets a token to
initial(the built-insize.default) hands it back to the component. - A theme re-entered below another theme (A inside B inside A) is exact in a browser with
@scope, which is Baseline since late 2025. A browser without it ignores that part and is right for every other nesting. In a browser with@scope, scope proximity outranks source order, so a zero-specificity rule such as:where(.x)of yours loses to a token rule on the same element. A class with any specificity still wins; prefer one.
A hand-built host
Any code that places items in a container can use the theme: a marketing page, a dashboard, a future document type. A renderer does three things: it emits the stylesheet, puts the hooks on the container and its items, and writes the custom properties.
import {
GRID_CLASS,
GRID_ITEM_CLASS,
createLayoutTheme,
getAppearanceAttributes,
getGridItemVariables,
getGridVariables,
} from '@schema-engine/layout';
import type { CssVariables, ElementGridConfig } from '@schema-engine/layout';
interface Item {
label: string;
grid?: ElementGridConfig;
}
const theme = createLayoutTheme();
const items: Item[] = [
{ label: 'First name', grid: { width: 'half' } },
{ label: 'Last name', grid: { width: 'half' } },
{ label: 'Email' },
];
function style(variables: CssVariables): string {
return Object.entries(variables)
.map(([name, value]) => `${name}:${value}`)
.join(';');
}
export function renderSection(): string {
const attributes = Object.entries(getAppearanceAttributes({ size: 'compact' }))
.map(([name, value]) => `${name}="${value}"`)
.join(' ');
const cells = items.map(
(item) =>
`<div class="${GRID_ITEM_CLASS}" data-se-theme="${theme.id}" style="${style(getGridItemVariables(item.grid))}">${item.label}</div>`,
);
return [
`<style>${theme.css}</style>`,
`<section class="${GRID_CLASS}" data-se-theme="${theme.id}" ${attributes} style="${style(getGridVariables(undefined, theme.grid, items))}">`,
...cells,
`</section>`,
].join('\n');
}getGridVariables(grid, theme.grid, items) takes the container's GridLayoutConfig (undefined for none), the theme's grid and the items it places; it gives the container 12 tracks and a 1 / -1 default placement when the items use named widths, and the template of its areas when it draws a layout. getGridItemVariables(item.grid) takes an item's ElementGridConfig. Both return a record of --* custom properties that is assignable to React CSSProperties and to a plain style object.
Appearance travels the same way: a value on the container becomes data-se-size="compact", and the theme's token rules read it.
A theme for a new domain
An appearance axis becomes a data-se-<axis> attribute, so it must not reuse an attribute a renderer already writes. The compiler hard-codes none: each domain passes its own as reservedNames, and assertAxesNotReserved(theme, reservedNames) re-checks a theme compiled elsewhere. The forms engine does this for its renderers' attributes (theme, step, and the others), so an axis named like one fails the engine build.
import { builtInAxes, createLayoutTheme, defineAxis } from '@schema-engine/layout';
const step = defineAxis({
$type: 'step',
metadata: { name: 'Step' },
default: 'first',
values: { first: {}, last: {} },
});
createLayoutTheme({ appearance: [...builtInAxes, step] }, { reservedNames: ['step'] }); // throwsThe package also holds the recipe protocol a domain uses to declare the slots and kinds of its view recipes (defineRecipeProtocol). The built-in form recipes stay in @schema-engine/forms.