Figure schema for Sanity and Astro
A number with what it means: 1931 · practising in York since. Mostly inline on a page, as a row of three or four. A source and a change are extensions.
- Core fields
- 4
- Extensions
- 2 (4 fields)
- Licence
- MIT
Free, with no account. Same data in both editions: the Astro entry id is the Sanity slug.
Core fields
What every figure has. A slug is added in Sanity.
| Field | Type | Required | What it holds |
|---|---|---|---|
| value | string | Yes | FigureAs shown, with its unit: 96%, £212k, 1931. |
| label | string | Yes | What it is |
| note | string | No | Note |
| 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.
source: Source
Where the figure comes from, for a charity, a newsroom or a comparison site. Fits charities, news, comparison.
| Field | Type | Required | What it holds |
|---|---|---|---|
| source | string | No | Source |
| sourceUrl | url | No | Source link |
trend: Change
How it moved since the last period, for a dashboard or a results page. Fits finance, news, energy.
| Field | Type | Required | What it holds |
|---|---|---|---|
| change | string | No | Change+4%, −£12. |
| direction | one of up, down, flat | No | Direction |
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/stat.jsonThe CLI needs a components.json in the project. No CLI? Copy the files below: they are all there is.
// Sanity schema: stat 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: 'value', title: 'Figure', description: 'As shown, with its unit: 96%, £212k, 1931.', type: 'string', validation: (rule) => rule.required().max(12) }), defineField({ name: 'label', title: 'What it is', type: 'string', validation: (rule) => rule.required().max(120) }), defineField({ name: 'note', title: 'Note', type: 'string', validation: (rule) => rule.max(60) }), 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: 'value', maxLength: 64 }, validation: (rule) => rule.required() }); /** A number with what it means: 1931 · practising in York since. Mostly inline on a page, as a row of three or four. A source and a change are extensions. */export const stat = defineType({ name: 'stat', title: 'Figure', type: 'document', fields: [fields[0], slug, ...fields.slice(1)], orderings: [{ title: 'Order', name: 'order', by: [{ field: 'order', direction: 'asc' }] }], preview: { select: { title: 'value', subtitle: 'label' } },}); /** The same fields as an object, for a page that keeps its own figures inline. */export const statEntry = defineType({ name: 'statEntry', title: 'Figure', type: 'object', fields, preview: { select: { title: 'value', subtitle: 'label' } },}); /** GROQ: one stat as the StatRecord type (types/stat.ts) describes it. */export const statProjection = `{ "id": slug.current, value, label, note, order }`; /** GROQ: every stat, in order. */export const statListQuery = `*[_type == "stat" && defined(slug.current)] | order(order asc, _createdAt asc) ${statProjection}`;// TypeScript types: stat v1.// From themes.dev content models (https://www.themes.dev/sanity/models/), MIT. Your code now: edit it freely. /** A number with what it means: 1931 · practising in York since. Mostly inline on a page, as a row of three or four. A source and a change are extensions. */export interface Stat { /** As shown, with its unit: 96%, £212k, 1931. */ value: string; label: string; note?: 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 StatRecord = Stat & { 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 { stat, statEntry } from './schemaTypes/stat'; export default defineConfig({ // … schema: { types: [stat, statEntry] },}); // In your site: the projection returns the Figure type in types/stat.ts.import { statListQuery } from './schemaTypes/stat';const figures = await client.fetch(statListQuery);In Astro
// src/content.config.tsimport { defineCollection } from 'astro:content';import { file } from 'astro/loaders';import { statSchema } from './models/stat'; const figures = defineCollection({ loader: file('./src/content/figures.json'), schema: statSchema,}); export const collections = { figures };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.