Heading, from the Building blocks group of the nertia component library.
npx shadcn@latest add https://ner.sh/r/block-heading.jsonThe recipe carries this block's layout and variant; the install brings the component.
{
"registries": {
"@nertia": "https://ner.sh/r/{name}.json"
}
}npx shadcn@latest add @nertia/block-headingnpm create nersh@latest my-site -- --site https://nertia.ai/components/building-blocks/element-block-heading/site.json scaffolds a project from it.{
"slug": "element-block-heading",
"mode": "builder",
"status": "draft",
"skinId": "zinc",
"composition": {
"id": "element-block-heading",
"sections": [
{
"id": "block-heading",
"instanceId": "stage-block-heading"
}
]
},
"copy": {
"stage-block-heading.text": "Heading",
"stage-block-heading.level": "h1",
"stage-block-heading.size": ""
}
}
import type { Section } from '../types';
import { Component } from './Component';
import { schema } from './schema';
import { meta } from './meta';
import { writeCopy } from './writeCopy';
export const blockHeading: Section = { meta, schema, Component, writeCopy };
import type { SectionProps } from '../types';
import { compositionEditPath } from '@/lib/composition/compositionEditableCopy';
import { Heading } from '@/components/foundation';
import { DisplayTypeStyle } from '@/components/site/DisplayTypeStyle';
const HEADING_LEVELS = {
h1: { as: 'h1', level: 'hero' },
h2: { as: 'h2', level: 'display' },
h3: { as: 'h3', level: 'title' },
} as const;
type HeadingLevelKey = keyof typeof HEADING_LEVELS;
function resolveHeadingLevel(value: string | undefined): (typeof HEADING_LEVELS)[HeadingLevelKey] {
const key = (value ?? '').trim().toLowerCase() as HeadingLevelKey;
return HEADING_LEVELS[key] ?? HEADING_LEVELS.h2;
}
/** Display sizes resize the hero voice; blank or unknown keeps the level's own step. */
const SIZE_CLASS = {
large: 'site-heading-size-large',
display: 'site-heading-size-display',
mega: 'site-heading-size-mega',
} as const;
function resolveSize(value: string | undefined): keyof typeof SIZE_CLASS | null {
const key = (value ?? '').trim().toLowerCase();
return Object.hasOwn(SIZE_CLASS, key) ? (key as keyof typeof SIZE_CLASS) : null;
}
export function Component({ copy, instanceId, editorMarkers }: SectionProps) {
const edit = (slotKey: string) => compositionEditPath(instanceId, slotKey);
// A heading its owner emptied renders nothing on the page, so its wrapper collapses
// (`empty:hidden`); the editor keeps the placeholder to type into, as block-text does.
if (copy.text !== undefined && !copy.text.trim() && !editorMarkers) return null;
const text = copy.text || 'Heading';
const { as, level } = resolveHeadingLevel(copy.level);
const size = resolveSize(copy.size);
// The element panel's Color and Align are node style properties: the node stylesheet
// paints them on this root and its direct children, so the role defaults stand otherwise.
return (
<div data-section="block-heading" className="site-block-heading" data-size={size ?? undefined}>
{size ? <DisplayTypeStyle /> : null}
<Heading
as={as}
level={size ? 'hero' : level}
className={size ? SIZE_CLASS[size] : undefined}
dataEditPath={edit('text')}
>
{text}
</Heading>
</div>
);
}
import type { SectionCopySchemaField } from '../types';
export const schema: SectionCopySchemaField[] = [
{ key: 'text', label: 'Text', type: 'text', placeholder: 'Heading' },
{
key: 'level',
label: 'Level',
type: 'text',
required: false,
maxLength: 2,
helpText: 'H1 for the main page title, H2 for a section title, H3 for a heading inside a card or other smaller area.',
options: [
{ value: 'h1', label: 'H1' },
{ value: 'h2', label: 'H2' },
{ value: 'h3', label: 'H3' },
],
},
{
key: 'size',
label: 'Size',
type: 'text',
required: false,
maxLength: 7,
advanced: true,
helpText:
'Large sets the heading a step above a section title, for a price or a headline that leads a band. Display sets it far larger than the page title, for a type-only hero. Mega fills the width, for a wordmark or a one-line statement. Default keeps the size its level gives it.',
options: [
{ value: '', label: 'Default' },
{ value: 'large', label: 'Large', budgets: { text: { maxLength: 80 } } },
{ value: 'display', label: 'Display', budgets: { text: { maxLength: 60 } } },
{ value: 'mega', label: 'Mega', budgets: { text: { maxLength: 24 } } },
],
},
];
import type { SectionMeta } from '../types';
export const meta: SectionMeta = {
id: 'block-heading',
displayName: 'Heading',
family: 'component',
tags: ['component', 'heading', 'type'],
};
import type { BrandContext } from '@/lib/brandContext';
import type { CopyPlace } from '../types';
/**
* The page's lead heading (its opening hero's) is the brief's name, and so is a heading the
* pattern seeds at the mega size, which is the wordmark size; every other heading keeps what
* the pattern seeded.
*/
export function writeCopy(ctx: BrandContext, place?: CopyPlace) {
if (ctx.name && place?.lead?.role === 'heading') return { text: ctx.name };
if (ctx.name && place?.seeded.size?.trim().toLowerCase() === 'mega') return { text: ctx.name };
return { text: 'Heading' };
}
The registry source and the @nersh/render renderer are MIT licensed.