Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Heading

A heading with its element as a required prop and its look as an optional one. The level gives the page its outline and the size only changes how it looks, so you never pick a level because of how big it should be.

Package
@kvirn-ui/react
Testing
Automated. Core n/a, Release n/a

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 h1 a 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

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

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.

Props in this example:

  • as on 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.

Props in this example:

  • as on 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

Props in this example:

  • level on useHeading (options)
  • size on useHeading (options)

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.

PartElement / roleARIANotes
Heading<h1> to <h6> → headingnone (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
HeadingAllowed elements: h1 to h6noneA 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)
Headingneverno role, tabindexNo 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 h1 per 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, so as is required. Never pick a level because of its size: set size instead.
  • 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, with useLocale().localeProps on 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. as can'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.

PropTypeDefaultDescription
as'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'RequiredThe 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 levelThe look, apart from the level. as="h4" looks like heading-4 unless you say otherwise. It never changes the element.
Class or attributeValuesMeaning
kv-headingalwaysThe part class. The default theme sets the heading colour and font.
kv-heading--display | kv-heading--heading-1 … kv-heading--heading-6always, one of themThe 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

PropTypeDefaultDescription
level1 | 2 | 3 | 4 | 5 | 6RequiredThe level, which the element follows.
size'display' | 'heading-1' | 'heading-2' | 'heading-3' | 'heading-4' | 'heading-5' | 'heading-6'the element’s own levelThe look, when it isn’t the level’s.

Result

PropTypeDefaultDescription
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 + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apassheading.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassheading.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)passn/apass1 row, each names a test in heading.test.tsx
Focus order and managementn/an/an/aa heading is not focusable by itself
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowsn/an/an/anative <h1> to <h6> with its own heading role; only classes added, no state, focus or key handling; nothing to operate
VoiceOver + Safari + iOSn/an/an/anative <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 + Windowsn/an/an/anative <h1> to <h6> with its own heading role; only classes added, no state, focus or key handling; nothing to operate
TalkBack + Chrome + Androidn/an/an/anative <h1> to <h6> with its own heading role; only classes added, no state, focus or key handling; nothing to operate
VoiceOver + Safari + macOSn/an/an/anative <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.