Layout

@schema-engine/layout places items in a container and styles them by name, and it knows nothing about forms. It has a 12-track grid with named widths and drawn areas, named appearance axes such as size and surface, a compiler that turns both into one theme and one stylesheet, and a recipe protocol a domain uses to declare the frame around its content. Forms is the first domain built on it. Any host that puts items in a container can use the same rules: a marketing page, a dashboard, a document type.

Layout splits styling in two. The theme owns a vocabulary of names (half, compact, elevated, a gap token). A document owns the intent, written with those names in plain data. A renderer turns the intent into one DOM contract: data-se-* attributes and CSS custom properties.

Install

pnpm add @schema-engine/layout zod

The quick start also uses react and react-dom to write the markup. The grid and axis schemas use zod/mini, so zod is a peer dependency. A recipe's slot schemas take any Standard Schema that validates synchronously (see Recipe protocol). A form package installs this package for you: @schema-engine/forms depends on it, and the React and Radiant renderers declare it as a peer.

Quick start

A theme compiles the grid and the appearance into one stylesheet. A host emits that stylesheet, puts the hooks on its container and its items, and writes their custom properties. This needs no renderer and no forms; the host below is a React component, and the custom properties go straight into style because they are assignable to React's CSSProperties:

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

export const theme = createLayoutTheme();
import {
	GRID_CLASS,
	GRID_ITEM_CLASS,
	getAppearanceAttributes,
	getGridItemVariables,
	getGridVariables,
	type ElementGridConfig,
} from '@schema-engine/layout';
import { theme } from './theme';

interface Item {
	label: string;
	grid?: ElementGridConfig;
}

const items: Item[] = [
	{ label: 'First name', grid: { width: 'half' } },
	{ label: 'Last name', grid: { width: 'half' } },
	{ label: 'Email' },
];

export function renderSection() {
	return (
		<section
			className={GRID_CLASS}
			data-se-theme={theme.id}
			{...getAppearanceAttributes({ size: 'compact' })}
			style={getGridVariables(undefined, theme.grid, items)}
		>
			{items.map((item) => (
				<div
					key={item.label}
					className={GRID_ITEM_CLASS}
					data-se-theme={theme.id}
					style={getGridItemVariables(item.grid)}
				>
					{item.label}
				</div>
			))}
		</section>
	);
}
import { renderToStaticMarkup } from 'react-dom/server';
import { renderSection } from './section.react';

console.log(renderToStaticMarkup(renderSection()));
<section
	class="se-grid"
	data-se-theme="l1tvu4rv"
	data-se-size="compact"
	style="
		--se-grid-template: repeat(12, minmax(0, 1fr));
		--se-grid-tracks: 12;
		--se-grid-default-column: 1 / -1;
	"
>
	<div class="se-grid-item" data-se-theme="l1tvu4rv" style="--se-item-grid-column: span 6">
		First name
	</div>
	<div class="se-grid-item" data-se-theme="l1tvu4rv" style="--se-item-grid-column: span 6">
		Last name
	</div>
	<div class="se-grid-item" data-se-theme="l1tvu4rv">Email</div>
</section>

The theme id (l1tvu4rv here) is derived from the compiled vocabulary, so it changes when the vocabulary does.

createLayoutTheme() compiles the default vocabulary. theme.css is the stylesheet, scoped by [data-se-theme="<id>"]: add it to the page once. getGridVariables(grid, theme.grid, items) gives the container its tracks and, for items with named widths, a 1 / -1 default placement. getGridItemVariables(item.grid) gives each item its placement. Appearance travels the same way: size: 'compact' becomes data-se-size="compact" and the theme's token rules read it. Layout themes explains the theme, and Grid and placement the placement names.

Which tool do I use

The table names forms as the example consumer: its grid, appearance and view keys are the config of a form. Another domain reads the same keys from its own documents.

I want toUseRead
Put two fields on one rowgrid.width: half on each fieldPlacing fields in the grid
Centre or indent a fieldgrid.offsetsame
Size a field in tracks and pack small ones into gapsgrid.span: n on the field, autoFlow: 'dense' on the gridSizes in tracks
Draw a layout where one field spans rowsgrid.areas on the form or step, grid.area on each fieldDrawn layouts
Change the layout at a breakpointgrid.responsive, or an areas recordsame
Change the layout by the width of a card or panel, not the windowcreateLayoutTheme({ grid: { query: 'container' } })Layout themes
Place one field exactlyRaw column, row, placeSelfRaw grid lines
Make a form denser or roomierappearance: { size: 'compact' }Appearance axes
Change border, shadow or surfaceappearance: { surface: 'outlined' }same
Add a brand colour that every form can pick by nameA host axis with defineAxisAppearance axes
Give each of many entities its own colour (17 machines, every brand)Your own data-<domain> attribute and CSS that sets the tokensIdentity tokens
Change the rhythm between fields everywhereThe --se-grid-gap-* tokens, or createLayoutTheme({ grid: { gap } })Theming
Set a size, a width or a gap for one formA class in className that sets the tokenTheming
Style one form, step or field onceclassName, or style with custom propertiesEscape hatches
Show or hide a part of a recipe (descriptions, labels, orientation)Slot config on view, for example view.header.showDescriptionView recipes
Use a different frame around the fieldsA recipe, built in or your ownCustom recipes
Make two engines or two renderers look the sameOne compiled layout theme passed to bothLayout themes
Copy a working setupAn exampleA themed form

When two tools fit, prefer them in this order: a named width before a raw line, an axis value before a token override, a token override before a class, a class before inline style.

Four levels of layout

A form's grid and each element's own grid lay fields out. The levels run from the most to the least readable:

  1. width and offset for fields that flow in rows. Names are shares of 12 tracks: full, three-quarters, two-thirds, half, third, quarter.
  2. span for fields sized in tracks of a grid with columns, clamped to the tracks the grid has, with autoFlow: 'dense' on the grid to backfill gaps.
  3. areas for a layout you draw as rows of names, with area on each field.
  4. Raw grid lines (column, row, placeSelf) as the escape hatch.

Fieldsets and array rows lay their children out on a grid of their own, so the same names work inside a container. A fieldset with grid: { subgrid: true } lays them on its parent's tracks instead, so its fields line up with the fields around it. A step's grid replaces the form's grid for that step. The rules, the validation errors and the limits are on Placing fields in the grid.

Appearance

An appearance axis is one independent dimension of appearance with 2 to 5 named values, such as size or surface. Each value stands for a set of CSS tokens that components read. A form, a step, a field or a recipe slot picks a value by name, and the renderer writes it as data-se-<axis>="<value>". The engine ships size and surface; a host adds its own, such as tone. A value that every entity of a domain has for itself (a colour per machine) is not an axis: see Identity tokens. See Appearance axes.

Escape hatches

Names cover the common cases. Three hatches stay open for the rest, and they work the same in every renderer:

  • className on the form, a step or a field. The theme's token rules sit inside :where(), so a class of yours always wins over them on the same element. Its grid and item rules do not, so change placement with grid config or the --se-grid-* and --se-item-* properties.
  • style on the form, a step or a field. It accepts only CSS custom properties (keys starting with --, string or number values), because that is the one style shape that means the same in React, Radiant, JSON and YAML. A value cannot contain ;, { or }.
  • Raw column, row and placeSelf on a field.
{
	id: 'checkout',
	className: 'checkout-form',
	style: { '--se-grid-gap-default': '0.75rem' },
	steps: [/* ... */],
}

Where next