When to use it#
- Use it for the vertical rhythm between blocks: the bands of a page, the parts of a form or the items of a list.
- Pick the space with a gap class,
kv-stack--gap-2,-4or-8: a smaller step inside a group, a larger one between sections. With no class you get a step between-4and-8. - Use
asto make it the element the content needs, such as aulor aform. - Not for text flow: Prose has its own margins for headings and paragraphs. Not for putting blocks side by side: use Grid.
Example#
Contact us
Call the service number or write to us.
import { Heading, Stack } from '@kvirn-ui/react'
import { useStackTexts } from './texts.ts'
export function DefaultStack() {
const { texts } = useStackTexts()
return (
<Stack>
<Heading as="h3">{texts.sections.contact}</Heading>
<p>{texts.sections.contactText}</p>
</Stack>
)
}Use cases#
A contact page in two bands
A page is a series of bands. Nest a tight stack for each band’s own content inside a loose stack for the page, so the bands read as groups.
Contact us
Call the service number or write to us.
Opening hours
We answer on weekdays from 9 to 16.
import { Heading, Stack } from '@kvirn-ui/react'
import { useStackTexts } from './texts.ts'
export function StackOfSections() {
const { texts } = useStackTexts()
return (
<Stack className="kv-stack--gap-8">
<Stack className="kv-stack--gap-2">
<Heading as="h4">{texts.sections.contact}</Heading>
<p>{texts.sections.contactText}</p>
</Stack>
<Stack className="kv-stack--gap-2">
<Heading as="h4">{texts.sections.hours}</Heading>
<p>{texts.sections.hoursText}</p>
</Stack>
</Stack>
)
}Props in this example:
classNameon Stack
Address lines that belong together
Items that belong together sit closer than separate blocks. The same lines are shown with the three steps that differ from the default.
import { Stack } from '@kvirn-ui/react'
import { useStackTexts } from './texts.ts'
export function Gaps() {
const { texts } = useStackTexts()
return (
<Stack className="kv-stack--gap-8">
{(['2', '4', '8'] as const).map((gap) => (
<Stack key={gap} className={`kv-stack--gap-${gap}`}>
{texts.gaps.related.map((line) => (
<span key={line}>{line}</span>
))}
<strong>
{texts.gaps.label} {gap}
</strong>
</Stack>
))}
</Stack>
)
}Props in this example:
classNameon Stack
A list of services
Render the stack as a ul with li children and a screen reader (software that reads the screen aloud) announces it as a list.
import { Link, Stack } from '@kvirn-ui/react'
import { useStackTexts } from './texts.ts'
export function StackAsList() {
const { texts } = useStackTexts()
return (
<Stack className="kv-stack--gap-4" as="ul" aria-label={texts.list.label}>
{texts.list.items.map((item) => (
<li key={item}>
<Link href="#">{item}</Link>
</li>
))}
</Stack>
)
}Props in this example:
Information: Did you know?
Stack adds role="list" to a ul or ol itself, because Safari and VoiceOver drop the list role when the markers are gone. Don’t write the role yourself.
A feedback form
Render the stack as a form, and stack each label with its field in a tighter stack inside it. The reading order stays the DOM order.
import { Button, Heading, Stack } from '@kvirn-ui/react'
import { useId } from 'react'
import { useStackTexts } from './texts.ts'
export function StackedForm() {
const { texts } = useStackTexts()
const nameId = useId()
const emailId = useId()
return (
<Stack className="kv-stack--gap-4" as="form" onSubmit={(event) => event.preventDefault()}>
<Heading as="h4">{texts.form.title}</Heading>
<Stack className="kv-stack--gap-2">
<label htmlFor={nameId}>{texts.form.name}</label>
<input id={nameId} autoComplete="name" className="kv-input" />
</Stack>
<Stack className="kv-stack--gap-2">
<label htmlFor={emailId}>{texts.form.email}</label>
<input id={emailId} type="email" autoComplete="email" className="kv-input" />
</Stack>
<Button type="submit">{texts.form.send}</Button>
</Stack>
)
}Props in this example:
Accessibility#
What it does for you#
- APG pattern: none. Stack 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.
Stack is its children one below the other with an even space between them. It adds no ARIA, no text, no tabindex and no behaviour, and it has no data-* state. The one exception: when it renders a ul or ol, it adds role="list", because WebKit (Safari's engine) and VoiceOver drop the list role when the list has no markers (list-style: none). A role you pass yourself wins. Everything a user perceives comes from the consumer's children, which keep their own semantics and focus order.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| Stack | <div> → generic | role="list" on ul/ol only | The consumer adds a gap class (kv-stack--gap-2, -4 or -8) for another gap; the default adds none. as="ul" or "ol" with <li> children is a list |
| Stack | as | the chosen element's own | One element, chosen with a string. A value outside the list is a type error. className joins, other attributes and refs pass through |
| Stack | attributes | passed through | aria-*, id and every other attribute reach the element unchanged |
| Stack | never adds | no tabindex, inert or aria-hidden; no role except list on ul/ol | No handler, no heading, no live region, no text |
useStack() gives the same frozen props object (only className) for your own element.
It announces nothing.
What you need to do#
- Gap.
kv-stack--gap-2,-4and-8set the gap, from small to large. With none, the gap sits between-4and-8. Group related blocks with a smaller gap and separate sections with a larger one. - Landmarks. Stack has none to give: a landmark is a
Container(main,section) or aSection(nav,aside), always named. A page has onemain. - A list is
as="ul"or"ol". Stack setsrole="list"itself, because WebKit and VoiceOver drop the list role without markers. Don't write it. If you spreaduseStack()on your ownulorol, addrole="list"yourself: the hook returns only the class name. - 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).
Warning: Don't forget
Write the children in the order people should read and Tab through them. Stack can’t reorder them, and if your own CSS does, keyboard and screen reader users no longer follow what they see (1.3.2 Meaningful Sequence, 2.4.3 Focus Order).
Visual and modes#
Headless: Stack ships no CSS. With the default theme (@kvirn-ui/theme/theme.css):
- Focus indicator, target size, contrast: not applicable. Stack 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: a flex column with a gap: children keep their natural width and wrap their own text (1.4.10 Reflow, AA). No fixed heights (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.
Focus management#
- Strategy: native. Stack 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: Stack renders no overlay and sets no
overflow, so a child's focus ring is never clipped (2.4.11 Focus Not Obscured (Minimum), AA).
Allowed elements#
A tag outside the list changes the page's outline or semantics (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 |
|---|---|---|
| Stack | div (default), ul, ol, li, section, form | ul and ol with <li> children are lists, li a list item inside one, section with a name a region, form a form (name it for a landmark). No main, nav, aside: use Container or Section, which own landmarks |
WCAG success criteria#
- 1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A): a div has no role, and a
ulorolhasrole="list". - 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): no fixed sizes (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#
Stack has no keys of its own. What you put in it keeps its own keys.
API reference#
import { Stack, useStack } from '@kvirn-ui/react'Stack
Renders <div class="kv-stack"> with no role. As a ul or ol it gets role="list". It takes every attribute of a <div> and passes ref to it. It has no state, so no data-*, and it works in a server component. The default theme is described on Theming.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | – | Your own classes. They join the part’s class and never replace it, so the theme keeps styling the layout. Add a gap class: kv-stack--gap-2, kv-stack--gap-4 or kv-stack--gap-8. With none, the gap is 1.5rem in the default theme, between -4 and -8. |
as | 'div' | 'ul' | 'ol' | 'li' | 'section' | 'form' | 'div' | Changes the element: ul or ol with li children for a list, li inside one, section with aria-labelledby for a region, or form. Stack adds role="list" to a ul or ol and no other role. There is no main, nav or aside: use Container or Section. |
ref | Ref<HTMLElement> | – | Reaches the element, whichever it is. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-stack | always | The part class. Your className joins it. |
role | list, only on a ul or ol | Keeps the list a list in Safari and VoiceOver, which drop the role when the markers are gone. Your own role wins. |
kv-stack--gap-2 | you add it | The smallest space step between children: 0.5rem in the default theme. |
kv-stack--gap-4 | you add it | A small space step between children: 1rem in the default theme. |
kv-stack--gap-8 | you add it | A large space step between children: 2rem in the default theme. |
useStack
Use it when you can’t use as, for example on a component of your own. It adds no role, ARIA or tabindex, and it returns the same frozen object every time. On your own ul or ol, add role="list" yourself, which the component does for you.
Result
| Prop | Type | Default | Description |
|---|---|---|---|
stackProps | StackPartProps | – | 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 | stack.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | stack.test.tsx › adds role="list" to a ul and an ol, and no role to a div; expectNoA11yViolations |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | stack.test.tsx › keyboard › is not a Tab stop and keeps the children in DOM order |
| Focus order and management | pass | n/a | pass | stack.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 | pending | pending | pending | |
| VoiceOver + Safari + iOS | pending | pending | pending | |
| Release (before 1.0 and each minor) | ||||
| JAWS + Chrome + Windows | pending | pending | pending | |
| TalkBack + Chrome + Android | pending | pending | pending | |
| VoiceOver + Safari + macOS | pending | pending | pending |
What to check#
- Move through a page in reading order with NVDA or VoiceOver: the order matches what you see, at wide and narrow widths.
- As a
ulwithliItems, it is read as "list, N items". - A named
navorasideItem is found in the landmarks list by its name. - At 400% zoom nothing needs sideways scrolling.