When to use it#
- Use it to keep a page’s content inside a readable width: the whole page, a column of text or a form.
- Pick the width with
size:pagefor the page,readingfor text andformfor one form. - Use
aswhen the container should be a landmark (a region people can jump to) such asmain, or a namedsection. - Not for the text inside it: use Prose for headings, paragraphs and lists. Not for putting blocks side by side: use Grid. Not for stacking blocks with space between them: use Stack.
Example#
Kvirnby municipality
The content stays inside the page width.
import { Card, Container, Heading } from '@kvirn-ui/react'
import { useContainerTexts } from './texts.ts'
export function DefaultContainer() {
const { texts } = useContainerTexts()
return (
<Container>
<Card.Root>
<Card.Body>
<Heading as="h2">{texts.page.title}</Heading>
<p>{texts.page.text}</p>
</Card.Body>
</Card.Root>
</Container>
)
}Use cases#
A reading column
Long text is hard to read on a wide screen. A reading container keeps lines to a comfortable length, for guidance, news, case pages and lists of search results.
Waste collection
Bins are emptied every second week. Put the bin out by 7:00 on the day.
- Household waste
- Food waste
- Paper packaging
import { Container, Prose } from '@kvirn-ui/react'
import { useContainerTexts } from './texts.ts'
export function ReadingColumn() {
const { texts } = useContainerTexts()
return (
<Container size="reading">
<Prose>
<h2>{texts.reading.title}</h2>
<p>{texts.reading.intro}</p>
<ul>
{texts.reading.items.map((item) => (
<li key={item}>{item}</li>
))}
</ul>
</Prose>
</Container>
)
}Props in this example:
sizeon Container
Information: Did you know?
A reading container has no side margin of its own, so on a phone the text reaches the edge of the screen. Put it inside a page container, as “The page and a column inside it” shows below.
A form column
A form is easier to fill in when its fields aren’t stretched across the page. A form container gives it a narrow column at the start of the line.
Report a move
import { Button, Container, Field, Heading, TextInput } from '@kvirn-ui/react'
import { useContainerTexts } from './texts.ts'
export function FormColumn() {
const { texts } = useContainerTexts()
return (
<Container size="form">
<Heading as="h2">{texts.form.title}</Heading>
<form onSubmit={(event) => event.preventDefault()}>
<Field.Root required>
<Field.Label>{texts.form.label}</Field.Label>
<TextInput name="address" autoComplete="street-address" />
</Field.Root>
<Button type="submit" className="kv-button--primary">
{texts.form.send}
</Button>
</form>
</Container>
)
}Props in this example:
sizeon Container
The page and a column inside it
The reading and form widths add no side padding and sit at the start of the line. Put them inside a page container, which keeps the page’s side margin.
Welcome to Kvirnby
Find services, news and contact details.
import { Container, Heading } from '@kvirn-ui/react'
import { useContainerTexts } from './texts.ts'
export function NestedContainers() {
const { texts } = useContainerTexts()
return (
<Container>
<Container size="reading">
<Heading as="h2">{texts.main.title}</Heading>
<p>{texts.main.text}</p>
</Container>
</Container>
)
}Props in this example:
sizeon Container
A named region
Give the container the element a landmark needs with as, and name it. A page has one main, which a page-width container often is.
News
The library opens an hour earlier on weekdays.
import { Container, Heading } from '@kvirn-ui/react'
import { useId } from 'react'
import { useContainerTexts } from './texts.ts'
export function NamedRegion() {
const { texts } = useContainerTexts()
const headingId = useId()
return (
<Container size="reading" as="section" aria-labelledby={headingId}>
<Heading as="h2" id={headingId}>
{texts.region.title}
</Heading>
<p>{texts.region.text}</p>
</Container>
)
}Props in this example:
Warning: Don't forget
Make a container a landmark only for a region people want to jump to, and always name it with aria-labelledby or aria-label. The container itself adds no name (1.3.1 Info and Relationships).
Accessibility#
What it does for you#
- APG pattern: none. Container is not a widget, so there is no APG pattern.
- Deviations: none
- Native elements used:
<div>by default. The consumer picks another withas(allowed elements below), and the element's own semantics apply.
Container limits the width of the page's content. The page size is centred; reading and form start at the start edge. It adds no role, no ARIA, no text, no tabindex and no behaviour, and it has no data-* state. Everything a user perceives comes from the consumer's children, which keep their own semantics and focus order.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| Container | <div> → generic | none. The consumer adds it with the element | class="kv-container", with kv-container--reading or kv-container--form for size. as="main" or as="section" with aria-labelledby give it a role |
| Container | as | the chosen element's own | One element, chosen with a string. A value outside the list is a type error and, in JavaScript, warns once in development and renders the default element. className joins and the ref reaches the element |
| Container | attributes | passed through | aria-*, id and every other attribute reach the element unchanged |
| Container | never | no role, tabindex, inert, aria-hidden | No handler, no heading, no live region, no text |
useContainer() gives the same frozen props object (only className) for your own element.
It announces nothing.
What you need to do#
- Size.
'page'(default, a page width, padded) is the page;'reading'(a comfortable line length) is prose;'form'(a form width) is a form.readingandformare start-aligned and add no padding: nest apagecontainer outside if the page needs the gutter. - Landmarks.
as="main"oras="section"witharia-labelledbyonly for a region a user would want to jump to, always named. A page has onemain. - DOM order is the visual order. Write children in the order they are read and focused (1.3.2 Meaningful Sequence (A), 2.4.3 Focus Order (A)). There is no way to reorder them visually.
- Language. Text in another language goes in a nested
KvirnProvider, withuseLocale().localePropson your own element (3.1.2 Language of Parts (AA)).
Information: Did you know?
A page-width Container with as="main" gives the page its main landmark. Landmarks let people jump past repeated content (2.4.1 Bypass Blocks).
Visual and modes#
Headless: Container ships no CSS. With @kvirn-ui/theme/theme.css:
- Focus indicator, target size, contrast: not applicable. Container has no colour, no edge and no control of its own.
- forced-colors behaviour: not applicable, nothing is drawn.
- reduced-motion behaviour: no motion.
- Reflow and text spacing: the container sets a maximum width, never a fixed one, so it is never wider than the screen and nothing scrolls sideways at 320px or at 400% zoom (1.4.10 Reflow (AA)). It has no fixed height (1.4.12 Text Spacing (AA)). The reflow sweep checks every story at 320 CSS pixels for sideways scrolling, and the text-spacing sweep checks that no box clips text.
Warning: Don't forget
If you change the widths in your own CSS, keep them as a maximum width and not a fixed one. Then nothing scrolls sideways at 320px or at 400% zoom (1.4.10 Reflow).
Focus management#
- Strategy: native. Container never moves focus and renders no
tabindex. Its children keep their own order, which is the DOM order. - Initial focus: not moved.
- Trap: no.
- Restore to: not applicable.
- Never obscured by: Container renders no overlay and sets no
overflow, so a child's focus ring is never clipped (2.4.11 Focus Not Obscured (Minimum) (AA); 2.4.13 Focus Appearance (AAA), not counted toward AA).
Allowed elements#
A tag outside the list changes the page's outline or meaning (1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A)). as is a string, so it works from a Server Component.
| Part | `as` | Why |
|---|---|---|
| Container | div (default), main, section, article | main (one per page) and section with a name are landmarks, article a self-contained piece. No nav or aside: use Section |
WCAG success criteria#
- 1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A): no role of its own, so the element you choose decides.
- 1.3.2 Meaningful Sequence (A), 2.4.3 Focus Order (A): DOM order equals reading and focus order (the keyboard rows).
- 1.4.10 Reflow (AA): a maximum width, never a fixed one (the reflow sweep checks that nothing scrolls sideways at 320 CSS pixels). 1.4.12 Text Spacing (AA): no fixed heights, and the text-spacing sweep applies the wider spacing to every story and fails on a box that clips text.
Keyboard#
Container has no keys of its own. What you put in it keeps its own keys.
API reference#
import { Container, useContainer } from '@kvirn-ui/react'Container
Renders <div class="kv-container"> with no role. It takes every attribute of a <div> and passes ref to it. It has no state, so no data-*, and it renders on the server too. The default theme is described on Theming.
| Prop | Type | Default | Description |
|---|---|---|---|
size | 'page' | 'reading' | 'form' | 'page' | The measure. "page" is centred, at most 80rem wide, with inline padding. "reading" (45rem) and "form" (40rem) are start-aligned and add no padding. |
as | 'div' | 'main' | 'section' | 'article' | 'div' | Changes the element: main (one per page), section with a name, or article. Container adds no role of its own: the element’s meaning applies. In JavaScript, an element outside the list is replaced by a div and warns once in development. |
ref | Ref<HTMLElement> | – | Reaches the element, whichever it is. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-container | always | The part class. The default theme styles it. Your className joins it. |
kv-container--reading | size="reading" | A 45rem measure for prose. |
kv-container--form | size="form" | A 40rem measure for one form. |
useContainer
Use it to give an element or component of your own the container classes. It adds no role, ARIA or tabindex, and it returns the same frozen object for each size.
Options
| Prop | Type | Default | Description |
|---|---|---|---|
size | 'page' | 'reading' | 'form' | 'page' | The measure, as on Container. |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
containerProps | ContainerPartProps | – | Spread on your element: only a className. Join a class of your own with mergeProps. |
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 | container.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | container.test.tsx (axe and role, name and state): adds no role, ARIA, tabindex, inert or data attribute |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | 2 rows, each names a test in container.test.tsx |
| Focus order and management | pass | n/a | pass | container.test.tsx asserts focus |
| 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 <div>, or the main, section or article the consumer picks, with its own semantics; landmark semantics are the element's own native mapping; Container adds none; no state, focus or key handling |
| VoiceOver + Safari + iOS | n/a | n/a | n/a | native <div>, or the main, section or article the consumer picks, with its own semantics; landmark semantics are the element's own native mapping; Container adds none; no state, focus or key handling |
| Release (before 1.0 and each minor) | ||||
| JAWS + Chrome + Windows | n/a | n/a | n/a | native <div>, or the main, section or article the consumer picks, with its own semantics; landmark semantics are the element's own native mapping; Container adds none; no state, focus or key handling |
| TalkBack + Chrome + Android | n/a | n/a | n/a | native <div>, or the main, section or article the consumer picks, with its own semantics; landmark semantics are the element's own native mapping; Container adds none; no state, focus or key handling |
| VoiceOver + Safari + macOS | n/a | n/a | n/a | native <div>, or the main, section or article the consumer picks, with its own semantics; landmark semantics are the element's own native mapping; Container adds none; no state, focus or key handling |