A themed form

A form is themed in two places. The theme is code: it compiles the vocabulary of appearance, here the built-in size and surface axes plus the host axes tone, shape and font added with defineAxis, into one id and one stylesheet. Every engine receives that one object as layout, so a React form and a Radiant form carry the same data-se-theme. The stylesheet is CSS scoped by the class the config puts on the form: it sets the rhythm between fields, the two hooks the compact size reads and the measure it sets, and it reads the colour roles the tone axis sets and the font role the font axis sets.

The config only names what it wants: tone: 'brand', size: 'compact' and surface: 'outlined'. What each name means is decided by the theme and the stylesheet, so a different theme changes the look without touching the config.

Install

For React:

pnpm add @schema-engine/forms @schema-engine/forms-react @schema-engine/layout react react-dom zod

The React file imports the fields that the shadcn registry installs. Run pnpm dlx shadcn@latest add schema-engine/schema-engine/schema-form once, as the Contact form explains.

For Radiant:

pnpm add @schema-engine/forms @schema-engine/forms-radiant @schema-engine/layout @ecopages/radiant-ui @ecopages/radiant @ecopages/jsx @ecopages/signals zod

Set your app up as the getting-started page of your renderer says (Forms Radiant or Forms React), then copy the files below into one folder.

See it

Press Shuffle to draw a new tone, shape, font, size and surface, and watch the React and the Radiant form follow. The note above the forms names the current picks. Shuffle changes only the appearance picks of the config below. The theme and the stylesheet stay the same, and both renderers read the one compiled theme, so they look the same for every combination. The form starts as the config picks it and is rendered again, empty, with each change.

Tone brand, Shape rounded, Font sans, Size compact, Surface outlined
ReactRadiant

Account

Account

The demos use this site's styled widgets, not the example's own files, and send to no real server: a valid submit shows the notice the config names.

The theme

The built-in axes are size and surface. A host adds its own with defineAxis: here tone, whose values set the colour roles that Radiant UI (--primary, --on-primary) and shadcn (--primary, --primary-foreground) components read, shape, which sets the radius tokens, and font, which sets the font roles with system stacks only. createLayoutTheme compiles the axes into the theme.

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

/**
 * A host axis. Each value is a set of tokens: the colour roles Radiant UI (`--primary`, `--on-primary`) and a
 * shadcn theme (`--primary`, `--primary-foreground`) read.
 *
 * @remarks
 * An axis holds two to five values, so the tones are the neutral default and four accents. Every accent sets a white
 * `--on-primary` that reads at least 4.5:1 on its `--primary` (brand 5.2, forest 5.0, violet 7.1, ocean 5.9), and a
 * container pair that reads 7:1 or better.
 */
export const tone = defineAxis({
	$type: 'tone',
	metadata: { name: 'Tone', description: 'Accent colour of a form' },
	default: 'neutral',
	values: {
		neutral: {},
		brand: {
			'--primary': '#c2410c',
			'--on-primary': '#ffffff',
			'--primary-foreground': '#ffffff',
			'--primary-container': '#ffedd5',
			'--on-primary-container': '#7c2d12',
		},
		forest: {
			'--primary': '#15803d',
			'--on-primary': '#ffffff',
			'--primary-foreground': '#ffffff',
			'--primary-container': '#dcfce7',
			'--on-primary-container': '#14532d',
		},
		violet: {
			'--primary': '#6d28d9',
			'--on-primary': '#ffffff',
			'--primary-foreground': '#ffffff',
			'--primary-container': '#ede9fe',
			'--on-primary-container': '#4c1d95',
		},
		ocean: {
			'--primary': '#0369a1',
			'--on-primary': '#ffffff',
			'--primary-foreground': '#ffffff',
			'--primary-container': '#e0f2fe',
			'--on-primary-container': '#0c4a6e',
		},
	},
});

/**
 * A host axis for the corners of controls and containers: the radius tokens Radiant UI (`--radius-control`,
 * `--radius-button`, `--radius-container`) and a shadcn theme (`--radius`) read.
 *
 * @remarks
 * `rounded` sets nothing, so it is the radius the host theme already has. `pill` makes inputs, selects and buttons
 * fully round, and a textarea, which reads `--radius-control` too, round at its ends. Cards and fieldsets read
 * `--radius-container`, so they keep corners. A shadcn theme has one `--radius` for controls and cards alike, so that
 * one stays a length: `9999px` there would turn a card into an ellipse.
 */
export const shape = defineAxis({
	$type: 'shape',
	metadata: { name: 'Shape', description: 'Corner radius of controls and containers' },
	default: 'rounded',
	values: {
		sharp: {
			'--radius-control': '0',
			'--radius-button': '0',
			'--radius-container': '0',
			'--radius': '0',
		},
		rounded: {},
		pill: {
			'--radius-control': '9999px',
			'--radius-button': '9999px',
			'--radius-container': '1.5rem',
			'--radius': '1.25rem',
		},
	},
});

/**
 * A host axis for the typeface: the font roles Radiant UI (`--font-sans`, `--font-heading`) reads.
 *
 * @remarks
 * Only system stacks, so a pick downloads nothing. A stylesheet decides where the role is read: `themed.css` sets the
 * `font-family` of the form from `--font-sans`. `sans` sets nothing, so it is the typeface the host theme already has.
 */
export const font = defineAxis({
	$type: 'font',
	metadata: { name: 'Font', description: 'Typeface of a form' },
	default: 'sans',
	values: {
		sans: {},
		serif: {
			'--font-sans': "ui-serif, Georgia, Cambria, 'Times New Roman', Times, serif",
			'--font-heading': "ui-serif, Georgia, Cambria, 'Times New Roman', Times, serif",
		},
		mono: {
			'--font-sans': "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', monospace",
			'--font-heading': "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', monospace",
		},
	},
});

/**
 * The compiled theme: the built-in `size` and `surface` axes plus `tone`, `shape` and `font`. Pass it to every engine
 * as `layout`.
 */
export const theme = createLayoutTheme({ appearance: [...builtInAxes, tone, shape, font] });

The config

The config is data. appearance: { tone: 'brand' } on the form reaches every field, the card shell picks size and surface for itself, and className is the class the stylesheet is scoped by. Two half fields share a row.

import type { FormConfig } from '@schema-engine/forms';

/**
 * A form that picks values of three axes and carries the class its stylesheet is scoped by.
 *
 * @remarks
 * The config only names what it wants: `tone` is the host axis of `theme.ts`, `size` and `surface` are built in. What
 * each name means is decided by the theme and by `themed.css`, so the same config looks different under another theme.
 * The card recipe draws the submit button, and the `show_notification` entry runs once the form was submitted.
 */
export const themedForm: FormConfig = {
	id: 'themed',
	title: 'Create an account',
	className: 'themed',
	appearance: { tone: 'brand' },
	view: { $type: 'card', shell: { appearance: { size: 'compact', surface: 'outlined' } } },
	steps: [
		{
			id: 'account',
			title: 'Account',
			elements: [
				{ $type: 'input', id: 'first', name: 'first', label: 'First name', grid: { width: 'half' } },
				{ $type: 'input', id: 'last', name: 'last', label: 'Last name', grid: { width: 'half' } },
				{ $type: 'input', id: 'email', name: 'email', label: 'Email', inputType: 'email' },
			],
		},
	],
	actions: [
		{
			id: 'created',
			$type: 'show_notification',
			message: 'Account created.',
			variant: 'success',
			triggers: ['form_submit_success'],
		},
	],
};

The renderer

Each tab builds an engine on the compiled theme, passed as layout: an engine that is not built on it rejects the form with Unknown axis 'tone'. How each renderer emits the shared stylesheet is in Layout themes. The React renderer has no recipes of its own, so its tab starts with a card recipe, and it registers the installed fields. Radiant ships both. Both engines register NotificationActionRegistration, the package's own, which only logs the message because the package cannot know how an app shows one; the demos on this page register one that shows a toast, and Submit, notify and reset registers one of your own.

import { getAppearanceAttributes } from '@schema-engine/layout';
import { getSlotConfig } from '@schema-engine/forms/recipes';
import { CardShellConfigSchema, FormActionsConfigSchema, cardSpec } from '@schema-engine/forms/recipes/built-in';
import { useActiveStep, useViewConfig, useViewContext } from '@schema-engine/forms-react/hooks';
import { defineRecipe, type FormShellProps } from '@schema-engine/forms-react/primitives';

/**
 * The card around the form: it carries the `size` and `surface` picks as attributes and reads the tokens they set.
 *
 * @remarks
 * `--se-space` and `--se-measure` come from the theme's size axis, so the card is as tight as the pick says, and
 * `--se-border-width` from the surface axis, so `outlined` draws the border and `flat` does not.
 */
function Shell({ slots, viewConfig }: FormShellProps) {
	const { appearance } = getSlotConfig(viewConfig, 'shell', CardShellConfigSchema);
	return (
		<div
			{...getAppearanceAttributes(appearance)}
			style={{
				maxWidth: 'var(--se-measure, 28rem)',
				margin: '0 auto',
				padding: 'var(--se-space, 1.5rem)',
				border: 'var(--se-border-width, 1px) solid color-mix(in srgb, currentColor 25%, transparent)',
			}}
		>
			{slots.header}
			{slots.content}
			{slots.actions}
		</div>
	);
}

function Header() {
	const { form } = useViewContext();
	const step = useActiveStep();
	return <h2>{step.title || form.title}</h2>;
}

function Actions() {
	const { form } = useViewContext();
	const { labels } = useViewConfig('actions', FormActionsConfigSchema);
	return (
		<button type="submit" disabled={form.isSubmitting} aria-label={labels.submitAriaLabel}>
			{form.isSubmitting ? labels.submitting : labels.submit}
		</button>
	);
}

export const CardRecipe = defineRecipe(cardSpec, { shell: Shell, slots: { header: Header, actions: Actions } });
import { createFormEngine, NotificationActionRegistration } from '@schema-engine/forms';
import { FormRenderer } from '@schema-engine/forms-react';
import { CardRecipe } from './card.react';
import { themedForm } from './config';
import { theme } from './theme';
import { schemaFormRegistrations } from '@/components/schema-engine/forms/registrations';

/** The compiled theme goes in as `layout`, so the form carries its `data-se-theme` and the stylesheet applies. */
export const engine = createFormEngine({
	elements: schemaFormRegistrations,
	recipes: [CardRecipe],
	layout: theme,
	actions: [NotificationActionRegistration],
});

/** Renders the themed form. */
export function ThemedForm() {
	return <FormRenderer engine={engine} config={themedForm} />;
}
import { NotificationActionRegistration } from '@schema-engine/forms';
import { createRadiantFormEngine, FormRenderer, schemaFormRegistrations } from '@schema-engine/forms-radiant';
import { themedForm } from './config';
import { theme } from './theme';

/** The same compiled theme goes in as `layout`: both renderers carry one `data-se-theme`. */
export const engine = createRadiantFormEngine({
	elements: schemaFormRegistrations,
	layout: theme,
	actions: [NotificationActionRegistration],
});

/** Renders the themed form. */
export function ThemedForm() {
	return <FormRenderer engine={engine} config={themedForm} />;
}

The stylesheet

The stylesheet is scoped by the class of the config. It sets the font-family of the form from --font-sans, the role the font axis sets: an axis only sets tokens, and the stylesheet decides where a token is read. Three more kinds of setting meet in it. The gap token and the two hooks of the compact size are custom properties, so they work on the form's class, or on :root or a wrapper for every form below it. --se-measure has a literal value on the element that carries the size pick, so a rule that targets that element, with the specificity of a class, overrides it. The last rule keys on the data-se-tone attribute the renderer writes on the form and reads the colour role the axis sets.

/*
 * One stylesheet for every form that carries `className: 'themed'`. The tone axis of theme.ts decides what
 * `--primary` is and its font axis what `--font-sans` is; this file decides how the form is spaced, how tight its
 * compact size is and that its text is set in `--font-sans`.
 */
.themed {
	font-family: var(--font-sans);

	/* Rhythm: the gap token every grid of the form reads. */
	--se-grid-gap-default: 1.25rem;

	/* The hooks the built-in compact size reads. */
	--se-space-compact: 0.75rem;
	--se-control-height-compact: 1.75rem;
}

/* --se-measure is a literal value of the size pick: win on the element that carries it. */
.themed [data-se-size='compact'] {
	--se-measure: 30rem;
}

/*
 * The renderer writes data-se-tone on the form element, and the tone axis sets --primary below it: this file only
 * reads it.
 */
.themed[data-se-tone='brand'] {
	padding-inline-start: 1rem;
	border-inline-start: 0.25rem solid var(--primary);
}

What the tests check

The unit test checks that the form is valid in the React and the Radiant engine built on the theme and fails with Unknown axis 'tone' in one that is not, that both engines accept the show_notification entry of the config, so a config that names an action an engine did not register fails, and that every hook the stylesheet sets is one the compact size reads. The browser test renders both files: the first and last name share a row, and the card and the form carry the picks of the size, surface and tone axes. An end-to-end test of this page presses Shuffle and checks that the combination changes every time, that the note names it, and that the React and the Radiant form both carry the same colour, corners and font.

Take it further

  • Add values to a host axis, or replace a built-in one: Appearance axes.
  • Every token a host can override, and where to set it: Theming.
  • Compile a theme with your own breakpoints and gaps: Layout themes.