Add a display variant
One section draws different fields in different cases — a description for one home, a residences table for several — and the user switches between them, starting from the case the section's own slots suggest.
Goal
Give one section a second set of fields, start each property on the case its content suggests, let the user switch, and show only the chosen case's fields in the editor.
A variant is an axis on the section, not a second section: one key, one bundle, one set of content. Before you reach for one, ask what moves the shape:
- The slots' content: a derived display. A table's size, how many photos a grid
draws, rows or cards, a line worded by a link, a mark printed when filled and left
out when empty. Declare no variant. The render reads the slots (
present, a bound list's length), so a user changes the shape by changing the content. A slot that only drops out when empty, nothing printed in its place, stays in the section's card, where the user fills it. - The user: a variant. One case draws a slot in another's place — a logo or the street printed in its place, a map or the floor plans — or the same slots in an arrangement no slot's content decides, photos as a grid or a carousel. Declare one variant the user can switch, and where the cases' slots differ, gate each slot with the cases that draw it: the switch is how the user reaches the other case's slots. This guide builds that.
What differs inside a case — a table's size, which rows print — is never a case: the render derives it from the slots. A case is a set of slots, not a layout.
Prerequisites
The workspace from Start, and a section to grow: here a Residences
section that describes one home in a paragraph and several in a table. Read
Slots if source is not yet familiar — a variant is
chosen the same ways a slot's value is.
Steps
1. Declare the axis
display.variant is one select({ from, source }) entry beside slots, with
no label — a label would make it a slot. The cases in from are yours to
name, the first is the default, and it is also the failure posture: a property
the decision could not be made for renders that case, so lead with the case every
property can draw.
Give the entry exactly one source:. compute({ reads }, fn) derives the case
each property starts on, and the editor offers the cases in the section's
settings, so the user can switch. Read the most directly related slot — the
section's own list, through its const, as a slot reads a sibling
(Sources) — never the fact upstream of it: a user
edits the slot, not the property. Until the user switches, the case re-derives
whenever an edit changes a slot it reads; once they switch, their choice stands.
import { bindItems, compute, fact, fill, list, object, row, Section, section, select, Slot, text } from "@homepages/concrete";
// Hoisted, so the variant can name it: the case counts what the section holds.
const units = list({
label: "Residences",
source: fact.property.units,
item: (el) => {
const unit = row({ label: "Unit", from: fact.property.units, source: el });
const name = text.short({ label: "Residence", source: unit.label });
return object({ label: name, fields: { unit, name } });
},
});
export const Residences = section(
{
key: "residences",
label: "Residences",
display: {
variant: select({
from: ["single", "units"],
source: compute({ reads: [units] }, (rows) => ((rows?.length ?? 0) > 1 ? "units" : "single")),
}),
},
slots: {
title: text.medium({
label: "Title",
cap: 64,
source: fill.write({
reads: [fact.property.address, fact.property.description],
brief: "One sentence on the property's character. About 40 characters.",
}),
}),
units,
},
},
({ slot }) => (
<Section>
<Slot.Text slot={slot.title} as="h2" />
<Slot.List slot={slot.units} as="ul">
{bindItems(slot.units).map((unit) => (
<Slot.Object key={unit.index} slot={unit} as="li">
<Slot.Text slot={unit.name} as="span" />
</Slot.Object>
))}
</Slot.List>
</Section>
),
);When no fact states the answer and it takes judgement over them,
fill.pick({ brief, reads }) makes the same decision with
the platform's model. A display pick chooses from the entry's from, exactly, so it
always answers with one of the declared cases and takes no none; and reads is
exactly what the model sees beside the brief, so a pick that declares none is
contract/display-evidence — the decision would
be made from nothing.
2. Gate each case's fields
when on a slot names the case or cases that draw it. The editor shows a slot's
field only in those cases, so a user never edits what the page cannot show, and a
fixture in another case leaves it empty. A slot every case draws — the title
and the units list above — takes no when.
import { fact, fill, text } from "@homepages/concrete";
const description = text.long({
label: "Description",
when: "single",
cap: 480,
source: fill.write({
reads: [fact.property.address, fact.property.description],
brief: "The home in three or four sentences: its layout, its finishes, then its setting.",
}),
});
const summary = text.medium({
label: "Summary",
when: "units",
cap: 160,
source: fill.write({
reads: [fact.property.address, fact.property.description],
brief: "What the residences share, in one sentence. About 120 characters.",
}),
});A gated slot is still an ordinary slot, so it still declares exactly one origin —
when decides where it is drawn and edited, never where its value comes from.
Two cases gating the same fields are one case: if their fields do not differ, the
difference is the render's to derive (step 3), not a variant's.
A when naming a case the entry does not declare is
contract/display. Fill still produces a gated slot
whatever case the content chose and stores it, so a section whose content later
takes the other case paints a populated layout with no refill — the gate is an editor and
fixture concern, not a fill one.
Gate the slot the default reads like any other. A cover that prints the building's
logo or, in its place, the street computes its default from the logo slot, gates the
logo to "logo" and the street to "address": a property with no logo starts on
the street, and a user who wants to upload one switches to "logo". That case then
draws the empty logo's null state — the box the user fills — rather than nothing.
Gate an item's fields the same way: a unit's specs that only the plans case prints
take that case, and the editor drops them from every item in the others. A slot that
only some of a case's count-derived layouts print (a banner under the longest table)
takes a case of its own: the compute starts that case from the same count (past
seven units, say), when gates the slot to it, and a user's switch still stands.
3. Branch the markup on variant
The render function's third binding is the stored case, typed as the union of the cases you declared.
import { bindItems, compute, fact, fill, list, object, row, Section, section, select, Slot, text } from "@homepages/concrete";
const units = list({
label: "Residences",
source: fact.property.units,
item: (el) => {
const unit = row({ label: "Unit", from: fact.property.units, source: el });
const name = text.short({ label: "Residence", source: unit.label });
return object({ label: name, fields: { unit, name, price: text.short({ label: "Price", source: unit.price }) } });
},
});
export const Residences = section(
{
key: "residences",
label: "Residences",
display: {
variant: select({
from: ["single", "units"],
source: compute({ reads: [units] }, (rows) => ((rows?.length ?? 0) > 1 ? "units" : "single")),
}),
},
slots: {
title: text.medium({
label: "Title",
cap: 64,
source: fill.write({
reads: [fact.property.address, fact.property.description],
brief: "One sentence on the property's character. About 40 characters.",
}),
}),
description: text.long({
label: "Description",
when: "single",
cap: 480,
source: fill.write({
reads: [fact.property.address, fact.property.description],
brief: "The home in three or four sentences: its layout, its finishes, then its setting.",
}),
}),
summary: text.medium({
label: "Summary",
when: "units",
cap: 160,
source: fill.write({
reads: [fact.property.address, fact.property.description],
brief: "What the residences share, in one sentence. About 120 characters.",
}),
}),
units,
},
},
({ slot, variant }) => (
<Section>
<Slot.Text slot={slot.title} as="h2" />
{variant === "single" ? <Slot.Text slot={slot.description} as="p" /> : <Slot.Text slot={slot.summary} as="p" />}
<Slot.List slot={slot.units} as="ul">
{bindItems(slot.units).map((unit) => (
<Slot.Object key={unit.index} slot={unit} as="li">
<Slot.Text slot={unit.name} as="span" />
<Slot.Text slot={unit.price} as="span" />
</Slot.Object>
))}
</Slot.List>
</Section>
),
);Render every gated slot in the branch that owns it, and in no other, and every
ungated slot — the list here — in every branch. A slot no branch ever renders is
render/missing-slot-marker: the editor has a
field it cannot select on the page. How many rows the list shows is its length,
read in the render — never a third case.
4. Author a fixture per case
A variant's default is computed from the section's slots, which a fixture fills
from its property as fill does from a customer's, so a fixture reaches a case by
standing on a property that starts on it — one home for "single", a building of
several for "units":
{
"property": "properties/cottage/property.json",
"content": {
"residences-1": {
"slots": {
"title": "A shingled cottage above the harbor.",
"description": "Two bedrooms under the eaves, a kitchen open to the porch, and a garden walk to the water."
}
}
}
}{
"property": "properties/triplex/property.json",
"content": {
"residences-1": { "slots": { "title": "Three full-floor flats." } }
}
}and both listed in fixtures/index.json, each property under properties and each fixture under fixtures.
Write one per case. This is not optional polish: with only the default case
authored, the gated slots of every other case never render in any fixture, and
the check fails at
render/missing-slot-marker naming each of
them. An instance may also state variant beside its slots, to show a case the
property would not start on — a user switching there: "residences-1": { "variant": "units" }.
5. Lock only what a manual case would print falsely
locked: true takes the switch out of the editor, so the content alone decides the
case. Reserve it for a case a user could pick that would print something false,
where the slot that decides it is in the editor in every case, so the user still
moves the shape by editing that slot. A locked variant never gates the slot it
reads. A sensible starting case is not a reason to lock: the computed default
already gives one.
Verify
Run concrete dev and open each case's fixture: each should paint its own layout,
and its section card should list only the fields that case draws. Open the
section's settings and switch cases: the page and the card should follow, and an
empty media slot should draw its null state. Then run concrete check; the render
stage renders every fixture, so an unrendered gated slot fails there rather than on
a customer's page.
