Button, from the Building blocks group of the nertia component library.
npx shadcn@latest add https://ner.sh/r/block-button.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-buttonnpm create nersh@latest my-site -- --site https://www.nertia.ai/sections/building-blocks/element-block-button/site.json scaffolds a project from it.{
"slug": "element-block-button",
"mode": "builder",
"status": "draft",
"skinId": "zinc",
"composition": {
"id": "element-block-button",
"sections": [
{
"id": "block-button",
"instanceId": "stage-block-button"
}
]
},
"copy": {
"stage-block-button.label": "Button",
"stage-block-button.href": "#",
"stage-block-button.variant": "solid",
"stage-block-button.size": "1",
"stage-block-button.tone": "",
"stage-block-button.width": "",
"stage-block-button.icon": "",
"stage-block-button.iconOnly": ""
}
}
import type { Section } from '../types';
import { Component } from './Component';
import { schema } from './schema';
import { meta } from './meta';
import { writeCopy } from './writeCopy';
export const blockButton: Section = { meta, schema, Component, writeCopy };
import type { SectionProps } from '../types';
import { Editable } from '@/components/site/EditableText';
import { compositionEditPath } from '@/lib/composition/compositionEditableCopy';
import { Button, Icon } from '@/components/foundation';
import type { ButtonProps, IconProps } from '@/lib/foundation/contract';
import { cn } from '@/lib/utils';
import { isSiteButtonStatusTone, SITE_BUTTON_INFO_LINK_CLASSES, SITE_BUTTON_STATUS_CLASSES } from '@/lib/siteButtonTone';
type ButtonVariant = NonNullable<ButtonProps['variant']> | 'link';
const BUTTON_VARIANTS: readonly ButtonVariant[] = ['solid', 'soft', 'outline', 'ghost', 'glass', 'link'];
/**
* `link` is a ghost button (so it keeps the design system's button face, case and tracking)
* with no box: no inline padding, so its label sits flush with the copy above, an underline
* 6px below the text, and a 44px tap height. The skin's unlayered `.site-btn-*` rules set
* padding, border and `text-decoration: none`, so these utilities are important. The focus
* outline is the skin's `.site-btn-ghost:focus-visible`.
*/
const LINK_CLASSES =
'px-0! min-h-11 border-transparent! bg-transparent! shadow-none! underline! decoration-1! underline-offset-[6px]! hover:border-transparent! hover:bg-transparent! hover:decoration-2!';
/**
* The icon slot's closed set. Each draws from the site icon library under the same name, before the
* label; `arrow` draws the library's right arrow after it, at the label's own size.
*/
type ButtonIcon = IconProps['name'] | 'arrow';
const BUTTON_ICONS: readonly ButtonIcon[] = [
'instagram',
'tiktok',
'youtube',
'x',
'linkedin',
'github',
'mail',
'link',
'download',
'arrow',
];
/** Anything the owner types that is not a slot variant falls back to the primary CTA look. */
function resolveVariant(value: string | undefined): ButtonVariant {
const key = (value ?? '').trim().toLowerCase() as ButtonVariant;
return BUTTON_VARIANTS.includes(key) ? key : 'solid';
}
const BUTTON_TONES: readonly NonNullable<ButtonProps['tone']>[] = ['neutral', 'success', 'warning', 'signal', 'info'];
/** Blank or unknown is the accent. */
function resolveTone(value: string | undefined): NonNullable<ButtonProps['tone']> {
const key = (value ?? '').trim().toLowerCase() as NonNullable<ButtonProps['tone']>;
return BUTTON_TONES.includes(key) ? key : 'accent';
}
function resolveIcon(value: string | undefined): ButtonIcon | null {
const key = (value ?? '').trim().toLowerCase() as ButtonIcon;
return BUTTON_ICONS.includes(key) ? key : null;
}
export function Component({ copy, instanceId, overrides }: SectionProps) {
const edit = (slotKey: string) => compositionEditPath(instanceId, slotKey);
const label = copy.label || 'Button';
const href = copy.href || '#';
const variant = resolveVariant(copy.variant);
const link = variant === 'link';
const sizeRaw = (copy.size ?? '').trim();
const size = sizeRaw === '1' ? 1 : sizeRaw === '3' ? 3 : 2;
const resolvedTone = resolveTone(copy.tone);
// Info is a link's colour only; every other variant keeps the accent.
const tone = resolvedTone === 'info' && !link ? 'accent' : resolvedTone;
// `width: 'full'` fills the slot the button sits in (a pricing card's CTA). The fill is
// skin CSS keyed on `data-width` (siteTokens), not a utility: `.site-btn-*` sets its own
// unlayered `width: fit-content`, which a layered `w-full` cannot beat.
const full = (copy.width ?? '').trim().toLowerCase() === 'full';
const icon = resolveIcon(copy.icon);
// `space: 'loose'` sets the button further below the copy above it (a display hero's CTA).
const loose = (copy.space ?? '').trim().toLowerCase() === 'loose';
// An icon-only button still carries its label, for screen readers: a row of network marks.
const iconOnly = icon !== null && (copy.iconOnly ?? '').trim().toLowerCase() === 'true';
const labelNode = <Editable path={edit('label')!}>{label}</Editable>;
return (
<div
data-section="block-button"
data-width={full ? 'full' : undefined}
data-font-body={overrides?.['--font-body'] ? '' : undefined}
className={loose ? 'site-block-button mt-4 @cq-md/site:mt-8' : 'site-block-button'}
>
<Button
href={href}
variant={link ? 'ghost' : variant}
size={size}
tone={tone}
download={icon === 'download' || undefined}
className={
cn(
icon && 'gap-2',
link && LINK_CLASSES,
link && tone === 'info' && SITE_BUTTON_INFO_LINK_CLASSES,
variant === 'solid' && isSiteButtonStatusTone(tone) && SITE_BUTTON_STATUS_CLASSES[tone],
) || undefined
}
data-icon={icon ?? undefined}
data-variant={link ? 'link' : undefined}
>
{icon && icon !== 'arrow' ? <Icon name={icon} className="size-[1.125em] shrink-0" /> : null}
{iconOnly ? <span className="sr-only">{labelNode}</span> : labelNode}
{icon === 'arrow' ? <Icon name="arrow-right" className="size-[1em] shrink-0" /> : null}
</Button>
</div>
);
}
import type { SectionCopySchemaField } from '../types';
export const schema: SectionCopySchemaField[] = [
{ key: 'label', label: 'Label', type: 'text', placeholder: 'Get started', maxLength: 40 },
{ key: 'href', label: 'Link', type: 'link', placeholder: 'https://' },
{
key: 'variant',
label: 'Variant',
type: 'text',
placeholder: 'solid',
advanced: true,
options: [
{ value: 'solid', label: 'Solid' },
{ value: 'soft', label: 'Soft' },
{ value: 'outline', label: 'Outline' },
{ value: 'ghost', label: 'Ghost' },
{ value: 'glass', label: 'Glass' },
{ value: 'link', label: 'Link' },
],
},
{
key: 'size',
label: 'Size',
type: 'text',
placeholder: '2',
maxLength: 1,
advanced: true,
options: [
{ value: '1', label: 'Small' },
{ value: '', label: 'Medium' },
{ value: '3', label: 'Large' },
],
},
{
key: 'tone',
label: 'Tone',
type: 'text',
placeholder: 'accent',
maxLength: 7,
advanced: true,
helpText: 'Success and Warning fill a solid button with the status colour, green or orange, with a dark label. Signal follows the scheme shown: orange on a light page, green on a dark one. Info sets a link in the info colour, underlined on hover; other variants ignore it.',
options: [
{ value: '', label: 'Accent' },
{ value: 'neutral', label: 'Neutral' },
{ value: 'success', label: 'Success' },
{ value: 'warning', label: 'Warning' },
{ value: 'signal', label: 'Signal' },
{ value: 'info', label: 'Info' },
],
},
{
key: 'width',
label: 'Width',
type: 'text',
placeholder: '',
maxLength: 4,
advanced: true,
helpText: 'Full stretches the button to fill the width of its container. Fit sizes it to the label.',
options: [
{ value: '', label: 'Fit' },
{ value: 'full', label: 'Full' },
],
},
{
key: 'icon',
label: 'Icon',
type: 'text',
placeholder: '',
maxLength: 9,
advanced: true,
helpText: 'Shows a mark before the label, drawn in the site icon style. Arrow after draws an arrow after the label instead. Download also saves the linked file instead of opening it. None shows the label alone.',
options: [
{ value: '', label: 'None' },
{ value: 'instagram', label: 'Instagram' },
{ value: 'tiktok', label: 'TikTok' },
{ value: 'youtube', label: 'YouTube' },
{ value: 'x', label: 'X' },
{ value: 'linkedin', label: 'LinkedIn' },
{ value: 'github', label: 'GitHub' },
{ value: 'mail', label: 'Email' },
{ value: 'link', label: 'Link' },
{ value: 'download', label: 'Download' },
{ value: 'arrow', label: 'Arrow after' },
],
},
{
key: 'iconOnly',
label: 'Icon only',
type: 'text',
placeholder: '',
maxLength: 4,
advanced: true,
helpText: 'On shows the icon alone and keeps the label for screen readers. Off shows both. Needs an icon.',
options: [
{ value: '', label: 'Off' },
{ value: 'true', label: 'On' },
],
},
];
import type { SectionMeta } from '../types';
export const meta: SectionMeta = {
id: 'block-button',
displayName: 'Button',
family: 'component',
tags: ['component', 'button', 'cta'],
};
import type { BrandContext } from '@/lib/brandContext';
import type { CopyPlace } from '../types';
/** Only a solid button reads as the primary action; outline and ghost buttons are the secondary ones. */
const PRIMARY_VARIANTS = new Set(['', 'solid']);
/** The first button under the page's first heading, when it is a plain primary button, says the brief's action. */
export function writeCopy(ctx: BrandContext, place?: CopyPlace) {
const lead = place?.lead;
const seeded = place?.seeded ?? {};
const isPrimary =
lead?.role === 'follow' &&
lead.nth === 0 &&
Boolean(seeded.label) &&
!seeded.icon &&
PRIMARY_VARIANTS.has(seeded.variant ?? '');
if (ctx.action && isPrimary) return { label: ctx.action, href: '#' };
return { label: 'Button', href: '#' };
}
The registry source and the @nersh/render renderer are MIT licensed.