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.

Stack

Its children one below the other, with an even space between them. It adds no behaviour and no new meaning: the element you choose decides what a screen reader says.

Package
@kvirn-ui/react
Testing
Automated. Core 0 of 2, Release 0 of 3

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, -4 or -8: a smaller step inside a group, a larger one between sections. With no class you get a step between -4 and -8.
  • Use as to make it the element the content needs, such as a ul or a form.
  • 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.

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.

Props in this example:

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.

Visiting addressKvirn Street 1KvirnbyGap 2
Visiting addressKvirn Street 1KvirnbyGap 4
Visiting addressKvirn Street 1KvirnbyGap 8

Props in this example:

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.

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.

Send feedback

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 with as (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.

PartElement / roleARIANotes
Stack<div> → genericrole="list" on ul/ol onlyThe 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
Stackasthe chosen element's ownOne element, chosen with a string. A value outside the list is a type error. className joins, other attributes and refs pass through
Stackattributespassed througharia-*, id and every other attribute reach the element unchanged
Stacknever addsno tabindex, inert or aria-hidden; no role except list on ul/olNo 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, -4 and -8 set the gap, from small to large. With none, the gap sits between -4 and -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 a Section (nav, aside), always named. A page has one main.
  • A list is as="ul" or "ol". Stack sets role="list" itself, because WebKit and VoiceOver drop the list role without markers. Don't write it. If you spread useStack() on your own ul or ol, add role="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, with useLocale().localeProps on 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
Stackdiv (default), ul, ol, li, section, formul 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 ul or ol has role="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.

PropTypeDefaultDescription
classNamestring–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.
refRef<HTMLElement>–Reaches the element, whichever it is.
Class or attributeValuesMeaning
kv-stackalwaysThe part class. Your className joins it.
rolelist, only on a ul or olKeeps the list a list in Safari and VoiceOver, which drop the role when the markers are gone. Your own role wins.
kv-stack--gap-2you add itThe smallest space step between children: 0.5rem in the default theme.
kv-stack--gap-4you add itA small space step between children: 1rem in the default theme.
kv-stack--gap-8you add itA 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

PropTypeDefaultDescription
stackPropsStackPartProps–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 + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apassstack.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassstack.test.tsx › adds role="list" to a ul and an ol, and no role to a div; expectNoA11yViolations
Keyboard contract rows (Vitest browser)passn/apassstack.test.tsx › keyboard › is not a Tab stop and keeps the children in DOM order
Focus order and managementpassn/apassstack.test.tsx asserts focus
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 + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

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 ul with li Items, it is read as "list, N items".
  • A named nav or aside Item is found in the landmarks list by its name.
  • At 400% zoom nothing needs sideways scrolling.