Step schema for Sanity and Astro
One step of a process: how switching works, what happens after you call. Mostly inline, three to five in order. A time label and a span on a track are extensions.
- Core fields
- 3
- Extensions
- 2 (3 fields)
- Licence
- MIT
Free, with no account. Same data in both editions: the Astro entry id is the Sanity slug.
Core fields
What every step has. A slug is added in Sanity.
| Field | Type | Required | What it holds |
|---|---|---|---|
| title | string | Yes | Title |
| text | text | Yes | Text |
| order | number | No | Order |
Extensions
Fields for one kind of site, on top of the core. An extension never changes a core field, and there are no settings: a different need is a different extension.
span: Span
Where the step sits on a track: the unit it starts at and how many it lasts. Fits software, agencies, construction.
| Field | Type | Required | What it holds |
|---|---|---|---|
| start | number | No | Starts at |
| length | number | No | Lasts |
when: When
A time label for the step: Week 1, Day 3, March. Fits professional services, events, education.
| Field | Type | Required | What it holds |
|---|---|---|---|
| when | string | Yes | When |
Get the code
Plain TypeScript with no package of ours: the files become your code. Tested in a new Sanity Studio and a new Astro project.
For your Studio: the document type, the same fields as an inline object, a GROQ projection and a list query, and the TypeScript type the projection returns.
Install with the shadcn CLI
npx shadcn@latest add https://www.themes.dev/sanity/r/step.jsonThe CLI needs a components.json in the project. No CLI? Copy the files below: they are all there is.
// Sanity schema: step v1.// From themes.dev content models (https://www.themes.dev/sanity/models/), MIT. Your code now: edit it freely.import { defineArrayMember, defineField, defineType } from 'sanity'; const fields = [ defineField({ name: 'title', title: 'Title', type: 'string', validation: (rule) => rule.required().max(40) }), defineField({ name: 'text', title: 'Text', type: 'text', rows: 2, validation: (rule) => rule.required().max(240) }), defineField({ name: 'order', title: 'Order', type: 'number', validation: (rule) => rule.integer() }),]; /** The id other entries refer to, and the Astro edition's entry id: the same string in both. */const slug = defineField({ name: 'slug', title: 'Slug', type: 'slug', description: 'Lowercase letters, digits and hyphens.', options: { source: 'title', maxLength: 64 }, validation: (rule) => rule.required() }); /** One step of a process: how switching works, what happens after you call. Mostly inline, three to five in order. A time label and a span on a track are extensions. */export const step = defineType({ name: 'step', title: 'Step', type: 'document', fields: [fields[0], slug, ...fields.slice(1)], orderings: [{ title: 'Order', name: 'order', by: [{ field: 'order', direction: 'asc' }] }], preview: { select: { title: 'title', subtitle: 'text' } },}); /** The same fields as an object, for a page that keeps its own steps inline. */export const stepEntry = defineType({ name: 'stepEntry', title: 'Step', type: 'object', fields, preview: { select: { title: 'title', subtitle: 'text' } },}); /** GROQ: one step as the StepRecord type (types/step.ts) describes it. */export const stepProjection = `{ "id": slug.current, title, text, order }`; /** GROQ: every step, in order. */export const stepListQuery = `*[_type == "step" && defined(slug.current)] | order(order asc, _createdAt asc) ${stepProjection}`;// TypeScript types: step v1.// From themes.dev content models (https://www.themes.dev/sanity/models/), MIT. Your code now: edit it freely. /** One step of a process: how switching works, what happens after you call. Mostly inline, three to five in order. A time label and a span on a track are extensions. */export interface Step { title: string; text: string; order?: number;} /** What the GROQ projection returns: the fields and the id (the slug, the same as the Astro entry's id). */export type StepRecord = Step & { id: string };// Shared shapes of the generated content models./** An image: the path or URL without size suffix, and its alternative text (empty when the text beside it says it). */export interface ImageRef { src: string; alt: string; /** Intrinsic size, so the page reserves the box before the file arrives. */ width: number; height: number;} export interface Link { label: string; href: string;} /** Portable Text as Sanity stores it: blocks, and images with their alt text. */export type RichText = { _type: string; _key: string; [key: string]: unknown }[];Use it
In Sanity
// sanity.config.tsimport { step, stepEntry } from './schemaTypes/step'; export default defineConfig({ // … schema: { types: [step, stepEntry] },}); // In your site: the projection returns the Step type in types/step.ts.import { stepListQuery } from './schemaTypes/step';const steps = await client.fetch(stepListQuery);In Astro
// src/content.config.tsimport { defineCollection } from 'astro:content';import { file } from 'astro/loaders';import { stepSchema } from './models/step'; const steps = defineCollection({ loader: file('./src/content/steps.json'), schema: stepSchema,}); export const collections = { steps };An image is always { src, alt, width, height } in both editions: the GROQ projection reads the size from the asset, and in Astro it sits in your data, so pages never shift while they load.