Get started
Documentation menu

Guides

Blocks

Describe a block once, and the library, the inspector, validation and the saved file follow.

A block is a section of a page: a hero, a grid of features, a list of questions. Each block type has one description, made with defineBlock, and one Astro component that renders it.

Describing a block

import { z } from 'zod';
import { defineBlock, linkSchema } from '@vamped/editor/blocks';

export const callout = defineBlock({
  type: 'callout',
  label: 'Callout',
  description: 'A framed panel with a heading, a line of text and one button.',
  icon: 'callout',
  category: 'Call to action',
  schema: z.object({
    heading: z.string().trim().min(1, 'Add a heading').max(80),
    text: z.string().max(300).optional(),
    button: linkSchema.optional(),
  }),
  defaults: { heading: 'Free forever, like immortality.', button: { text: 'Find us on GitHub', href: 'https://github.com/vamped-hq/vamped' } },
  fields: [
    { name: 'heading', label: 'Heading', type: 'text', inline: true },
    { name: 'text', label: 'Text', type: 'textarea', inline: true, optional: true },
    { name: 'button', label: 'Button', type: 'link', group: 'Actions', optional: true },
  ],
});
Member What it is for
type The block’s name in saved files. Don’t rename it once pages use it.
label, description Shown in the block library. Write a description that says what the block looks like.
icon The name of an icon in your site config’s icons.
category The heading the block is listed under in the library, such as “Hero” or “Content”.
schema A Zod schema of the block’s props. It is the one source of truth: the editor validates with it while you type, Sveltia’s save is blocked while it fails, and your site build can validate with it too.
defaults The props of a new block. They are checked against schema by TypeScript.
fields How each prop is edited in the inspector. See Field types.

Put every block in one registry and hand it to the editor:

export const registry = createRegistry([hero, features, callout]);

The registry also gives you registry.pageSchema, the schema of a whole page file. Use it for your content collection so astro build fails on an invalid page:

// src/content.config.ts
const pages = defineCollection({
  loader: glob({ pattern: '*.yml', base: './src/content/pages' }),
  schema: registry.pageSchema,
});

Marking up the component

The editor finds blocks and fields in the preview by their markers:

  • the page renderer wraps each block in an element with data-ve-block="<id>", data-ve-type and data-ve-label;
  • each editable element gets data-ve-field="<path>".

Paths are the prop’s name, or a path into a list: items.2.title is the title of the third item, and items.2 the whole item. Fields marked inline: true can be edited right in the preview; clicking any marked element selects its field in the inspector.

Emit the markers only when Astro.locals.visualEditing is true. The preview route sets it; your published pages never see it. A small helper keeps components tidy:

// src/lib/visual-editing.ts
export const fieldAttrs = (locals: App.Locals | undefined) => (path: string) =>
  locals?.visualEditing ? { 'data-ve-field': path } : {};
---
const f = fieldAttrs(Astro.locals);
---
<ul>
  {items.map((item, i) => (
    <li {...f(`items.${i}`)}>
      <h3 {...f(`items.${i}.title`)}>{item.title}</h3>
    </li>
  ))}
</ul>

Block IDs

Every block on a page has a stable ID such as hero-k3j9xa, made when the block is added. The editor addresses blocks by ID, never by position, so reordering or deleting one block never edits another. Keep IDs when you write page files by hand, and make each one unique on its page.

Tips

  • Keep props flat and plain. Strings, numbers, booleans, links and lists of small objects edit well and diff well in Git.
  • Write clear errors. The messages in your Zod schema are what editors see under a field.
  • Optional means optional twice. Mark the Zod prop .optional() and the field optional: true, so the inspector offers to clear it.