When to use it#
- Use a heading at the top of every page and of every part of it that has a topic: a card, a sidebar, a group of form fields.
- Choose the level for the outline: one
h1a page, then no skipped levels. Choose the size, if it needs to differ, for the look. - Write a heading that names the topic or purpose, and that differs from the headings next to it.
- Not for making text bigger or bolder: do that in your CSS. Not for the name of a form field: use a Field label.
Example#
Contact us
import { Heading } from '@kvirn-ui/react'
import { useHeadingTexts } from './texts.ts'
export function DefaultHeading() {
const { texts } = useHeadingTexts()
return <Heading as="h3">{texts.contact.heading}</Heading>
}Use cases#
Another look without another level
A heading under an h3 is an h4, even when the design wants it larger. Keep the level for the outline and set size for the look.
Collection days
Report a missed collection
import { Heading } from '@kvirn-ui/react'
import { useHeadingTexts } from './texts.ts'
export function SizeApartFromLevel() {
const { texts } = useHeadingTexts()
return (
<div>
<Heading as="h4">{texts.look.levelOnly}</Heading>
<Heading as="h4" size="heading-2">
{texts.look.asLarger}
</Heading>
</div>
)
}Props in this example:
Warning: Don't forget
Choose each level for the outline, never for its size, and don’t skip a level. People who move through a page by heading rely on the levels (1.3.1 Info and Relationships). See What you need to do.
A heading that names a region
A region is a part of the page with its own name, such as a Section used as a landmark. Give the heading an id and point to it with aria-labelledby on the region. The region’s name is then the heading’s text, so the two never differ.
Opening hours
The recycling centre is open until 19:00 on weekdays.
import { Heading } from '@kvirn-ui/react'
import { useId } from 'react'
import { useHeadingTexts } from './texts.ts'
export function NamesRegion() {
const { texts } = useHeadingTexts()
const headingId = useId()
return (
<section aria-labelledby={headingId}>
<Heading as="h4" id={headingId}>
{texts.opening.heading}
</Heading>
<p>{texts.opening.text}</p>
</section>
)
}Props in this example:
ason Heading
Headings in running text
Inside a block of running text (a Prose) the headings get their margins from the Prose. In the default theme the levels h4 to h6 differ by size first, then by weight and letter spacing, and they do not get smaller on a small screen.
Waste collection
Food waste and residual waste are collected every other week.
Opening hours
The recycling centre is open until 19:00 on weekdays.
import { Heading, Prose } from '@kvirn-ui/react'
import { useHeadingTexts } from './texts.ts'
export function InProse() {
const { texts } = useHeadingTexts()
return (
<Prose>
<Heading as="h4">{texts.waste.heading}</Heading>
<p>{texts.waste.text}</p>
<Heading as="h5">{texts.opening.heading}</Heading>
<p>{texts.opening.text}</p>
</Prose>
)
}Props in this example:
ason Heading
Your own element
When the element is not a Heading, such as one drawn by your own component, take its classes from useHeading. Keep the level you pass in step with the element.
Waste collection
import { useHeading } from '@kvirn-ui/react'
import { useHeadingTexts } from './texts.ts'
export function OwnElement() {
const { texts } = useHeadingTexts()
const heading = useHeading({ level: 4, size: 'heading-3' })
return <h4 {...heading.rootProps}>{texts.waste.heading}</h4>
}Props in this example:
Accessibility#
What it does for you#
- APG pattern: none. A heading is not a widget.
- Deviations: none
- Native elements used:
<h1>to<h6>.
Heading renders the native heading element named by a required as, and an optional size sets its look apart from the level. It adds only classes, no role, ARIA, text or behaviour, so what a user perceives is the native heading.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| Heading | <h1> to <h6> → heading | none (aria-level implied) | as ('h1' to 'h6') is required and is the element and the level. class="kv-heading kv-heading--<size>": size (display, heading-1 to heading-6) is the look only, and defaults to the level's own: h1 to h6 look like heading-1 to heading-6. Attributes (id, aria-*) and the ref reach the element unchanged |
| Heading | Allowed elements: h1 to h6 | none | A string, so it works from a Server Component. Anything else is a type error and, in JS, warns once in development and renders <h2>. For a <legend> or another element use useHeading with your own element and keep its semantics (give a non-heading role="heading" and aria-level yourself) |
| Heading | never | no role, tabindex | No click handler, no live region. The size never changes the element or the level |
It announces nothing.
What you need to do#
- Choose the level for the outline, and the size for the look. Use one
h1per page and don't skip levels, so people who move through a page by heading can follow it (1.3.1 Info and Relationships, A; 2.4.6 Headings and Labels, AA). Heading can't know where it sits, soasis required. Never pick a level because of its size: setsizeinstead. - Describe the topic or purpose. Headings and labels are clear and differ from the ones next to them (2.4.6 Headings and Labels, AA).
- Language. Text in another language goes in a nested
KvirnProvider, withuseLocale().localePropson your own element (3.1.2 Language of Parts, AA).
Visual and modes#
Heading ships no CSS of its own. With the default theme, kv-heading sets the heading colour and font family (the theme's contrast check covers it, 1.4.3 Contrast (Minimum), AA), and the size class sets the type role (display, heading-1 to heading-6), in running text and outside it. The sizes fall in order from display to heading-6. Below 40rem display, heading-1, heading-2 and heading-3 step down; levels 4 to 6 don't. They are told apart by size first, then weight and letter spacing. heading-6 is smaller than the body text, which WCAG allows: it sets no minimum font size. Sizes are in rem, so they follow the browser's text size (1.4.4 Resize Text, AA). Margins come from kv-prose or the consumer. Target size, focus indicator and motion: not applicable.
Right to left and forced colours: a heading sets no directional or background styles, so it starts at the start of the line and keeps the system's text colour (1.4.3 Contrast (Minimum), AA; 1.4.11 Non-text Contrast, AA). Stories in right-to-left text and in forced colours check this.
Focus management#
- Initial focus: not moved. Heading never moves focus.
- Trap: no.
- Restore to: not applicable.
- A heading that a script focuses (a page title after navigation) needs
tabindex="-1", which the consumer adds.
WCAG success criteria#
- 1.3.1 Info and Relationships (A): a native heading with the consumer's level. One automated test per level, h1 to h6, and one for the server.
- 1.4.4 Resize Text (AA): the type roles are in rem.
- 2.4.6 Headings and Labels (AA): the text is the consumer's.
ascan't be forgotten.
Keyboard#
Heading has no keys of its own. What you put in it keeps its own keys.
API reference#
import { Heading, useHeading } from '@kvirn-ui/react'Heading
Renders <h1> to <h6> with the role heading, depending on as. Any other value warns in development and renders an <h2>. It takes every attribute of a heading and passes ref to it. The default theme is described on Theming.
| Prop | Type | Default | Description |
|---|---|---|---|
as | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | Required | The element, and so the level the page’s outline needs: as="h2" renders an <h2>. Heading can’t know where it sits, so it is required. A string, so it works from a Server Component. For a legend or another element, use useHeading with your own element. |
size | 'display' | 'heading-1' | 'heading-2' | 'heading-3' | 'heading-4' | 'heading-5' | 'heading-6' | the element’s own level | The look, apart from the level. as="h4" looks like heading-4 unless you say otherwise. It never changes the element. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-heading | always | The part class. The default theme sets the heading colour and font. |
kv-heading--display | kv-heading--heading-1 … kv-heading--heading-6 | always, one of them | The look: the size you give, or the one of the level’s own number. |
useHeading
Use it when the element is your own, such as a heading drawn by your own component. Pass the level you give the element, and spread rootProps on it.
Options
| Prop | Type | Default | Description |
|---|---|---|---|
level | 1 | 2 | 3 | 4 | 5 | 6 | Required | The level, which the element follows. |
size | 'display' | 'heading-1' | 'heading-2' | 'heading-3' | 'heading-4' | 'heading-5' | 'heading-6' | the element’s own level | The look, when it isn’t the level’s. |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
element | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | – | The element for the level: "h2" for 2. |
size | 'display' | 'heading-1' | 'heading-2' | 'heading-3' | 'heading-4' | 'heading-5' | 'heading-6' | – | The size that applies: the one given, or the level’s own. |
rootProps | { className: string } | – | Spread on your heading element: kv-heading and the size class. |
Testing#
The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.
| AT + browser + OS | Agent check | Human check | Result | Notes |
|---|---|---|---|---|
| Automated (re-runnable tests) | ||||
| axe, WCAG 2.2 A and AA, every story, 4 themes | pass | n/a | pass | heading.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | heading.test.tsx (axe and role, name and state): adds only its classes, no role or ARIA, and passes attributes and the ref through |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | 1 row, each names a test in heading.test.tsx |
| Focus order and management | n/a | n/a | n/a | a heading is not focusable by itself |
| Display modes (forced colours, 320px reflow, text spacing, reduced motion) | pass | n/a | pass | the four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI |
| Core (required for beta) | ||||
| NVDA + Chrome + Windows | n/a | n/a | n/a | native <h1> to <h6> with its own heading role; only classes added, no state, focus or key handling; nothing to operate |
| VoiceOver + Safari + iOS | n/a | n/a | n/a | native <h1> to <h6> with its own heading role; only classes added, no state, focus or key handling; nothing to operate |
| Release (before 1.0 and each minor) | ||||
| JAWS + Chrome + Windows | n/a | n/a | n/a | native <h1> to <h6> with its own heading role; only classes added, no state, focus or key handling; nothing to operate |
| TalkBack + Chrome + Android | n/a | n/a | n/a | native <h1> to <h6> with its own heading role; only classes added, no state, focus or key handling; nothing to operate |
| VoiceOver + Safari + macOS | n/a | n/a | n/a | native <h1> to <h6> with its own heading role; only classes added, no state, focus or key handling; nothing to operate |
What to check#
With a screen reader and the keyboard, try:
- The headings list shows the levels in order, and each name is clear.