Guides

Bricks

The sections pages are built from, each a component plus a schema.

A brick is one kind of section: a component, and the schema of the data each use of it holds. A document's body is a list of entries, each { brick, id, data }: the brick's name, a stable id, and that entry's data.

{ "brick": "hero", "id": "a1b2", "data": { "heading": "Hello", "intro": "Welcome." } }

Defining a brick

import { defineBrick, fields } from '@mortar-kit/core';
import { For } from 'solid-js';

export const featureGrid = defineBrick({
  name: 'feature-grid',
  label: 'Feature grid',
  schema: {
    heading: fields.text({ label: 'Heading', default: 'Features' }),
    items: fields.list({
      label: 'Features',
      max: 6,
      of: fields.object({
        label: 'Feature',
        fields: { title: fields.text({ label: 'Title' }), text: fields.textarea({ label: 'Text' }) },
      }),
    }),
  },
  component: (props) => (
    <section>
      <h2>{props.data.heading}</h2>
      <For each={props.data.items}>{(item) => <article><h3>{item.title}</h3><p>{item.text}</p></article>}</For>
    </section>
  ),
});
  • name is what entries store, so treat it as permanent. Use lowercase letters, digits and dashes.
  • label is what the admin shows. It defaults to name.
  • schema drives validation, defaults and the admin's form. See Fields.

What the component receives

props.data always has the schema's shape. Stored data is normalized before rendering: missing fields get their defaults, and values of the wrong type are replaced by the default. So data that is old or edited by hand can't crash a brick. A value of the right type that breaks a rule, such as an empty required text, is kept, so the preview shows what the editor typed.

Read props.data.x where you use it, and never destructure props. PageView updates the data in place as the editor types, and lists inside a brick keep their DOM.

Unknown bricks

If a stored entry names a brick your code no longer defines, the site renders nothing for it and the admin shows a warning, so removing a brick never breaks a page. PageView's fallback prop chooses what to render instead.

Bricks that load data

A brick can declare load(data, content) to read other documents, such as the latest posts. See Loading data.

Bricks with forms

A brick can declare action to handle its own form, such as a contact form, and render a <ActionForm>. It works without JavaScript, and with it only the brick updates. See Handling forms.

Interactive bricks

Public pages can ship no JavaScript at all (see Pages without JavaScript). A brick that needs the browser, such as a form that answers in place or a carousel, says so with interactive: true. On such a page only that brick hydrates (an island), and only pages that show it load any JavaScript. See Islands.

BrickPropsinterface

import { BrickProps } from '@mortar-kit/core'

interface BrickProps<D, L = undefined, R = undefined>

Props every brick component receives.

Properties

data: D

The brick's data: the right shape, with defaults filled in (see Brick.normalize).

loaded: L

What the brick's load returned; undefined for a brick without one.

submission?: Submission<R>

The latest post of this entry's form (<ActionForm>) to its action: pending, then what it returned or threw. Undefined until the form is sent, and for a brick without an action.