SectionHeading
Introduce a section with a consistent hierarchy; a supporting lead is optional.
Default
Clear intent.
Show code
---
import SectionHeading from '@dhekode/ui/patterns/SectionHeading.tsx';
---
<SectionHeading level={3} title="Clear intent." />With lead
Clear intent.
Supporting detail adds context.
Show code
---
import SectionHeading from '@dhekode/ui/patterns/SectionHeading.tsx';
---
<SectionHeading level={3} title="Clear intent." lead="Supporting detail adds context." />Editorial layout
Clear intent.
Supporting detail adds context.
Show code
---
import SectionHeading from '@dhekode/ui/patterns/SectionHeading.tsx';
---
<SectionHeading level={3} layout="editorial" title="Clear intent." lead="Supporting detail adds context." />Display scale
Clear intent.
Show code
---
import SectionHeading from '@dhekode/ui/patterns/SectionHeading.tsx';
---
<SectionHeading level={3} scale="display" title="Clear intent." />Heading levels
Heading level two
Heading level three
Show code
---
import SectionHeading from '@dhekode/ui/patterns/SectionHeading.tsx';
---
<SectionHeading level={2} title="Heading level two" />
<SectionHeading level={3} title="Heading level three" />API reference
titleType:
stringDefault:
requiredHeading text.
idType:
stringDefault:
unsetApplied to the heading for section labelling.
leadType:
stringDefault:
unsetSupporting paragraph.
levelType:
2 | 3Default:
2Choose the level from document hierarchy, not desired size.
layoutType:
'stacked' | 'editorial'Default:
'stacked'Editorial sets title and lead beside each other on the 12-column grid (5 and 7 columns, or 7 and 5 at display scale) above a shared guide on large screens; stacks on smaller ones.
scaleType:
'heading' | 'display'Default:
'heading'Display uses the existing display type scale for a short, prominent statement. It does not change the heading level.
classNameType:
string | undefinedDefault:
unsetAdds classes to the React component root, after its own utilities. React components take className, not class.
Slots: None.
Use a unique ID when linking Section.labelledby. It never creates an h1.
Original component source
import { classNames } from '@dhekode/utils/class-names';
type Layout = 'stacked' | 'editorial';
type Scale = 'heading' | 'display';
export interface SectionHeadingProps {
id?: string | undefined;
title: string;
lead?: string | undefined;
/** Heading level, chosen from the page's outline; the look stays the same. */
level?: 2 | 3 | undefined;
layout?: Layout | undefined;
scale?: Scale | undefined;
className?: string | undefined;
}
const layouts: Record<Layout, string> = {
stacked: 'gap-md',
editorial:
'gap-x-gutter gap-y-xl border-b-(length:--dk-border-width-hairline) border-solid border-surface-guide pb-xl',
};
/**
* Heading and optional lead. Editorial headings with a lead sit beside it on the site's
* 12-column grid from large screens: 5 and 7 columns, or 7 and 5 at display scale.
*/
const editorialSpans: Record<Scale, { heading: string; lead: string }> = {
heading: { heading: 'lg:col-span-5', lead: 'lg:col-span-7' },
display: { heading: 'lg:col-span-7', lead: 'lg:col-span-5' },
};
export default function SectionHeading({
id,
title,
lead,
level = 2,
layout = 'stacked',
scale = 'heading',
className,
}: SectionHeadingProps) {
const Heading = level === 3 ? 'h3' : 'h2';
const besideLead = layout === 'editorial' && lead !== undefined;
return (
<div
className={classNames(
'dk-section-heading mb-2xl grid grid-cols-1',
layouts[layout],
besideLead && 'lg:grid-cols-12 lg:items-end',
className,
)}
data-layout={layout}
data-scale={scale}
>
<Heading
id={id}
className={classNames(
'max-w-measure-heading wrap-anywhere',
scale === 'display' && 'text-display leading-tight font-bold tracking-display',
besideLead && editorialSpans[scale].heading,
)}
>
{title}
</Heading>
{lead && (
<p
className={classNames(
'dk-lead min-w-0',
besideLead && `${editorialSpans[scale].lead} lg:justify-self-end`,
)}
>
{lead}
</p>
)}
</div>
);
}Best practices
- Keep heading levels in document order.
- Use the lead to add context and omit redundant eyebrow labels.