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 zod

zod is a peer dependency because the grid and axis schemas use zod/mini. A recipe's slot schemas take any Standard Schema.

What a theme holds

createLayoutTheme({ grid, appearance }) returns a frozen theme:

MemberHolds
idOne identity for the whole theme. It is the value of the data-se-theme attribute.
cssOne stylesheet, scoped by [data-se-theme="<id>"].
gridThe resolved grid vocabulary the emitters read.
axesEach appearance axis name with its closed list of values.
appearanceThe 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 optionDefaultNotes
breakpointssm 40rem, md 48rem, lg 64rem, xl 80rem, 2xl 96remOverrides only the names you give. Thresholds must keep the order sm <= md <= lg <= xl <= 2xl; rem and em are compared at a 16px root.
gapnone 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.
minColumnWidth15.625remUsed by columns: 'auto' grids.
responsivenoneGap 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.
query'media'What the breakpoints measure: the window ('media') or the grid's own wrapper ('container'). See Follow the container.

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

A form engine from @schema-engine/forms 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(); // true

How 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.

The themed form example builds its engine on one compiled theme, the way the docs do for both renderers.

The neutral hooks

The stylesheet selects three things and nothing else: .se-grid, .se-grid-item and [data-se-theme]. A theme that queries the container adds a fourth, .se-grid-container. There is no alias for another class name.

HookA renderer puts it on
.se-gridEvery grid container: step grids, and the inner grids of fieldsets and array rows.
.se-grid-itemEvery item placed in a grid: field hosts.
.se-grid-containerOnly with query: 'container': an element around each grid. GRID_CONTAINER_CLASS is its name. A media theme has no rule for it and renderers add none.
[data-se-theme]The form element, each grid, each item and each container, 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), --se-grid-tracks (12) and --se-grid-default-column even with no grid config, and a container that draws areas gets its template.

PropertiesOnMeaning
--se-grid-template, --se-grid-areas, --se-grid-gap-x, --se-grid-gap-yA containerColumn template, drawn areas, column and row gap.
--se-grid-tracksA containerNumber of column tracks the template draws, written beside it. Items read it to clamp a span.
--se-grid-auto-flowA containerrow or dense: how items without a position fill the grid.
--se-grid-default-columnA containerWhere 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-contentA containerAlignment.
--se-item-grid-column, --se-item-grid-rowAn itemPlacement: a width or offset becomes a span, a numeric span a span clamped to --se-grid-tracks, an area both.
--se-item-justify-self, --se-item-align-selfAn itemAlignment of the item in its cell.
--se-item-margin-block-start, --se-item-margin-block-endAn itemRhythm above and below, from the gap tokens.
--se-item-displayAn itemnone 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 mode applies to that item only: each grid resets it, so the fields inside a fieldset that sets grid stay block.

The gap tokens (--se-grid-gap-none, -compact, -default, -loose, -wide) are public: set them to restyle every grid. See Theming.

Follow the container, not the window

A breakpoint is a window width by default: the stylesheet emits @media (min-width: ...). A grid in a card, a sidebar or a panel then keeps the layout of the screen it sits on, not of the room it has, so a card that is narrow on a wide screen still gets its wide layout. grid: { query: 'container' } makes the same breakpoints measure the grid's own wrapper instead. Everything else stays: the breakpoint names and thresholds, the gap ramp, the cascade from narrower widths, and the document, which says what changes at each breakpoint and never how the theme measures it. A document reads the same wherever it is mounted.

import { createLayoutTheme } from '@schema-engine/layout';

/** The breakpoints measure the card, so `md` is a card 36rem wide, whatever the window. */
export const cardTheme = createLayoutTheme({
	grid: { query: 'container', breakpoints: { sm: '24rem', md: '36rem' } },
});

sm comes down with md because a breakpoint may not be larger than the one above it, and sm defaults to 40rem. The two modes are different themes: their id and their stylesheet differ, and a theme without the option is the media theme it always was. Container mode emits @container se-grid (min-width: ...) where media mode emits @media, with the same declarations inside, and adds one rule for the wrapper. The query is static CSS, with no observer and no script.

Give each grid a wrapper

A grid's template and the placement of its items must measure the same box, and an element cannot query its own size. So the box is an ancestor of the grid: an element with GRID_CONTAINER_CLASS and the theme's data-se-theme, around the grid. The theme names its container (se-grid), so a container of your own between the wrapper and the grid never captures the query.

import {
	GRID_CLASS,
	GRID_CONTAINER_CLASS,
	GRID_ITEM_CLASS,
	getGridItemVariables,
	getGridVariables,
	type ElementGridConfig,
	type GridLayoutConfig,
} from '@schema-engine/layout';
import { cardTheme } from './card-theme';

const grid: GridLayoutConfig = { columns: 1, responsive: { md: { columns: 2 } } };

const fields: { label: string; grid?: ElementGridConfig }[] = [
	{ label: 'Name', grid: { column: 'span 1', responsive: { md: { column: 'span 2' } } } },
	{ label: 'Email' },
	{ label: 'Phone' },
];

export function Card() {
	return (
		<aside className="card">
			<div className={GRID_CONTAINER_CLASS} data-se-theme={cardTheme.id}>
				<div
					className={GRID_CLASS}
					data-se-theme={cardTheme.id}
					style={getGridVariables(grid, cardTheme.grid, fields)}
				>
					{fields.map((field) => (
						<div
							key={field.label}
							className={GRID_ITEM_CLASS}
							data-se-theme={cardTheme.id}
							style={getGridItemVariables(field.grid)}
						>
							{field.label}
						</div>
					))}
				</div>
			</div>
		</aside>
	);
}

In a 20rem card the grid has one column and the name field spans one; in a 40rem card, on a phone or on a desktop alike, it has two and the name field spans both. Resize the card, not the window.

  • The forms renderers add the wrapper themselves, around every grid: the step's, and the inner grid of each fieldset and array row, so a nested grid follows its own box. A theme that queries the window gets no wrapper and the markup it always had. A container element you write (a fieldset or a row of your own) lays out its children with ChildGrid of @schema-engine/forms-react/primitives or @schema-engine/forms-radiant/primitives: it renders the grid and, under a container theme, the wrapper around it; under a media theme, the grid alone.
  • A subgrid has no wrapper. A container with grid: { subgrid: true } lays its children on the tracks of the grid it sits in, so its inner grid takes <ChildGrid subgrid>, which renders the grid with the se-subgrid class and no wrapper, and its breakpoints follow the container of the grid around it. Hand-written markup puts GRID_SUBGRID_CLASS (se-subgrid) and data-se-theme on the item, on every box between the item and the inner grid, and on the inner se-grid, and never container-type on any of them: containment makes a box an independent formatting context, and a subgrid in one silently falls back to a single column. Keep a <fieldset> out of the chain, since Chromium does not make one a subgrid: use a div with role="group" and aria-labelledby its heading, as the renderers do.
  • Your own markup wraps each grid, as above, including a grid inside a grid.
  • A grid with no wrapper is not an error. No container matches the query, so it keeps its narrowest layout at every width.

What containment costs

container-type: inline-size contains the wrapper's inline size, so three things change in a wrapped layout.

  • The wrapper does not size to its content. It is a block that fills its parent (display: block; width: 100%), which is what you want in a card or a panel. In a shrink-to-fit parent (an inline-block, a flex item without a flex basis, an auto grid track) it has no width of its own and collapses to zero. Give that parent a width, or make the track 1fr.
  • Fixed-position descendants stay inside it. Containment makes the wrapper the containing block of position: fixed descendants, so a floating element that is not in the top layer positions against the wrapper and not the window. Popovers and dialogs in the top layer are not affected.
  • em follows the container. In a container query an em threshold resolves against the font size of the container, not the root. Use rem or px for a breakpoint that must not move with a card's font size. The order check still compares them at a 16px root.

Container size queries are supported by every current browser (Baseline 2023), so media mode stays the default and has no fallback to maintain.

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-in size.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 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. The quick start builds one in a single file.

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.

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'] }); // throws

The 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.