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.

Grid

Columns and rows from one to twelve, or as many as fit, each changing at a breakpoint. You choose the columns with the theme’s classes, so you write no CSS of your own. Grid adds no meaning of its own: what a screen reader reads and what the keyboard visits follows the order you write.

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

When to use it#

  • Use Grid to place things in columns: a page with a main part and a side part, or a set of service cards, news items or quick links.
  • Start with one column and add columns from a breakpoint (a width of the grid itself): kv-grid--cols-1 kv-grid--lg-cols-12. The grid measures its own width, so the same markup works in a sidebar, in a card and at 400% zoom.
  • For as many columns as fit, none narrower than you choose, use kv-grid--fit-sm, -md or -lg.
  • A grid of cards or links is a list. Render Grid.Root as a <ul> with one <li> per item, so a screen reader says how many there are.
  • Not for one column of things with space between them: use Stack. Not for data with rows and columns that people compare: use a Table.

Example#

Use cases#

A 12-column page layout

One column on a phone. When the grid is at least 64rem wide, the main part takes eight columns and the side part four. The cells are in reading order, so a keyboard and a screen reader get the order you see.

Apply for a preschool place

You can apply from the year your child turns one. Apply at least four months before you need the place.

Contact

Preschool administration, 08:00 to 16:00 on weekdays.

Props in this example:

Warning: Don't forget

Line placement (start-n, end-n) can put a later cell before an earlier one. Keep it in DOM order, because focus and reading order follow the DOM (1.3.2 Meaningful Sequence, 2.4.3 Focus Order).

A service page with navigation for its section

Pages in the same section link to each other from a side column of three of twelve columns, with the content over the other nine. Render the side Item as a nav and name it, and mark the current page with current.

Apply for a place at a preschool

You can apply from the year your child turns one. Apply at least four months before you need the place.

Props in this example:

Warning: Don't forget

A nav is a landmark (a region people can jump to) and needs a name, in the page’s language. An Item is never <main> by default: a page has one, so choose it with as where it fits (1.3.1 Info and Relationships).

Contact details beside the text

Information that belongs to the page but isn’t part of the text goes in a side column. Write the content first and the side after it, so the content is at the start of the line and is read and visited first. Render the side as an aside and name it.

Housing adaptation grant

You can get a grant if you need to adapt your home because of a disability. Apply before the work starts.

Props in this example:

  • as on Grid.Item

Information: Did you know?

The cells follow the DOM order. In a right-to-left language the grid mirrors, so the first cell sits at the right and the side column moves to the left.

Links with a few words fit in a narrow column, so more of them sit side by side. Choose the narrowest fit class and a smaller gap.

Props in this example:

Information: Did you know?

A grid that is narrower than the fit size gets one column, so nothing scrolls sideways at 320px.

Accessibility#

What it does for you#

  • APG pattern: none. Grid is a layout, not a widget, and it is not the ARIA grid role.
  • Deviations: none
  • Native elements used: <div> by default. Grid.Root takes ul or ol, and Grid.Item takes li, article, section, nav, aside or main, with as. The element's own semantics apply.

Grid lays children out on explicit column tracks chosen with classes (kv-grid--cols-12, kv-grid--md-cols-2, kv-grid--fit-md, kv-grid-item--lg-span-8). It adds no ARIA, no text, no tabindex and no behaviour, and it has no data-* state. As a ul or ol Root it adds role="list", because WebKit and VoiceOver drop the list role under list-style: none. Everything a user perceives comes from the consumer's children, which keep their own semantics and focus order.

PartElement / roleARIANotes
Grid.Rootinternal <div class="kv-grid-container">noneThe query container around the grid element. No role, no ARIA, no attribute beyond its class. Grid renders a wrapper and the grid element, so a container query can size it
Grid.Root<div> → generic, or <ul>/<ol> → listrole="list" on ul/ol onlyclass="kv-grid". Every prop, the ref and className go here. as="ul" with <li> Items is a list, and its count is announced
Grid.Item<div> → genericnone. The consumer adds it with the elementclass="kv-grid-item". Optional: any direct child is a cell. Outside a Root it warns once in development
Grid.Root/Itemasthe chosen element's ownA value outside the list is a type error. className joins, refs pass
bothneverno tabindex, inert, aria-hidden, data-*No handler, no heading, no live region, no text

useGrid() gives the same frozen props objects (only className): containerProps, rootProps and itemProps.

It announces nothing.

What you need to do#

  • A list of links or cards is a list. as="ul" or "ol" with <li> children (Grid.Item as="li"), so the count is announced (1.3.1 Info and Relationships (A)). Grid sets role="list" itself; don't write it.
  • No reordering. There is no order-* and no dense packing (1.3.2 Meaningful Sequence (A), 2.4.3 Focus Order (A)). start-*, end-* and row-start-* place an item on a line: placing a later item before an earlier one makes the visual order differ from the DOM, reading and focus order. Keep line placement in DOM order. (An accessibility trade-off.)
  • Start at one track. A bare cols-n (n > 1), flow-col without rows-n, or auto-cols-min|max keeps its tracks at 320px, so the cells squeeze and content overflows or overlaps (1.4.10 Reflow (AA)). Start with kv-grid--cols-1 or kv-grid--fit-* and add columns with a breakpoint form (kv-grid--md-cols-n). fit-* uses min(…, 100%) and never overflows.
  • Name the landmarks. A nav, aside or section Item needs aria-label or aria-labelledby, in the page's language. A page has one main: Grid never renders one by default.
  • 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

A grid has one column until you add more. Add columns from a breakpoint, such as kv-grid--lg-cols-3, or use a fit class. A bare kv-grid--cols-3 keeps three columns on a 320px screen, and content overflows (1.4.10 Reflow).

Visual and modes#

Headless: Grid ships no CSS. With @kvirn-ui/theme/theme.css:

  • Focus indicator, target size, contrast: not applicable. Grid 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 (1.4.10 Reflow (AA)): the reflow sweep checks every story at 320 CSS pixels for sideways scrolling. Text spacing (1.4.12 Text Spacing (AA)): Grid sets no fixed height, overflow or clip, and the text-spacing sweep applies the wider spacing to every story and fails on a box that clips text.

Warning: Don't forget

If you style a cell, don’t give it a fixed height or hide its overflow. When people widen the text spacing, the text would be cut off (1.4.12 Text Spacing).

Focus management#

  • Strategy: native. Grid 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: Grid renders no overlay and sets no overflow, position: sticky or fixed height, 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
Grid.Rootdiv (default), ul, olul and ol with li Items are lists whose count is announced. No landmark element: use Container or Section
Grid.Itemdiv (default), li, article, section, nav, aside, mainli inside a ul/ol Root. A named section, nav (navigation) or aside (related content) is a landmark, and one without a name is noise. main (one per page) is the consumer's choice, never the default. article only for self-contained content

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): the reflow sweep checks that nothing scrolls sideways at 320 CSS pixels. 1.4.12 Text Spacing (AA): no fixed height, overflow or clip, and the text-spacing sweep fails on a box that clips text.

Keyboard#

Grid has no keys of its own. What you put in it keeps its own keys.

Announcements#

What a screen reader reads#

State or actionRead aloudPolitenessTest
A div Root with Itemsonly the children's phrases in DOM order; none for the wrapper or the gridnonegrid.test.tsx › read aloud › a div Root reads only its children in DOM order
as="ul" Root with li Itemslist, listitem, <text>, end of list for each itemnonegrid.test.tsx › read aloud › a ul Root with li Items reads as a list with its items
An Item as="main"mainnonegrid.test.tsx › read aloud › a main Item reads as main
A named nav Itemnavigation, <name>nonegrid.test.tsx › read aloud › a named nav Item reads as a navigation
An Item as="section" with a nameregion, <name>nonegrid.test.tsx › read aloud › a named section Item reads as a region

API reference#

import { Grid, useGrid } from '@kvirn-ui/react'

Grid.Root

Renders <div class="kv-grid">, inside an internal <div class="kv-grid-container">, so the breakpoints answer to the grid’s own width. It takes every attribute of a <div> and passes ref to it. It has no state, so no data-*. Every prop and the ref go to the grid element, not to the wrapper. It has no role, except role="list" on a ul or ol. Put it in a block-level parent: in a shrink-to-fit parent (inline-flex, float) it collapses. Also exported as GridRoot. 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 kv-grid--cols-1 to -12 for a number of columns, kv-grid--fit-sm, -md or -lg for as many as fit, kv-grid--gap-n for the space, and a breakpoint for a wider grid: kv-grid--md-cols-2.
as'div' | 'ul' | 'ol''div'Changes the element: ul or ol with li children for a list whose count is announced. A list gets role="list", because Safari drops the list role when the bullets are removed. In JavaScript, an element outside the list is replaced by a div and warns once in development.
refRef<HTMLElement>–Reaches the grid element, not the wrapper around it.
Class or attributeValuesMeaning
kv-gridalwaysThe part class. The default theme styles it.
kv-grid-containeralwaysOn the internal wrapper around the grid element. The breakpoints measure its width.
kv-grid--cols-nyou add itn equal columns, 1 to 12. The default is one column. A bare cols-n (n above 1), flow-col without rows-n or auto-cols-min|max keeps its tracks at 320px, so content overflows or overlaps (1.4.10): start at cols-1 or fit-* and add columns with a breakpoint form, kv-grid--lg-cols-3.
kv-grid--fit-sm | -md | -lgyou add itAs many columns as fit, none narrower than 14rem, 18rem or 24rem.
kv-grid--gap-nyou add itThe space between cells. n is 0 to 6, 8, 10, 12 or 16. The default gap is 1.5rem; gap-0 removes it.
kv-grid--gap-x-n | kv-grid--gap-y-nyou add itThe space between columns only, or between rows only.
kv-grid--rows-n | flow-row | flow-col | auto-rows-… | auto-cols-…you add itExplicit rows (1 to 12), the direction new cells fill in, and the size of rows or columns the grid adds itself. They change how cells are placed, not the order of the DOM.
kv-grid--items-… | justify-items-… | place-items-… | content-… | justify-…you add itWhere cells sit inside their area (start, center, end, stretch), and where the whole set of tracks sits inside the grid.
kv-grid--sm-… | --md-… | --lg-… | --xl-… | --2xl-…you add itAny of the above from a breakpoint of the grid’s own width: sm 40rem, md 48rem, lg 64rem, xl 80rem, 2xl 96rem. For example kv-grid--lg-cols-12.

Grid.Item

Renders <div class="kv-grid-item"> with no role. It takes every attribute of a <div> and passes ref to it. It has no state, so no data-*. Optional: any direct child of Root is a cell. Also exported as GridItem. An Item outside a Root warns in development.

PropTypeDefaultDescription
classNamestring–Your own classes. They join the part’s class and never replace it, so the theme keeps styling the layout. Add kv-grid-item--span-n for the columns it covers, start-n and end-n for the column lines, row-span-n for rows, and a breakpoint: kv-grid-item--lg-span-8.
as'div' | 'li' | 'article' | 'section' | 'nav' | 'aside' | 'main''div'Changes the element: li inside a ul or ol Root, article, section, nav or aside with a name, or main (one per page, never the default). Grid adds no role of its own: the element’s meaning applies.
refRef<HTMLElement>–Reaches the element, whichever it is.
Class or attributeValuesMeaning
kv-grid-itemalwaysThe part class. The default theme styles it.
kv-grid-item--span-n | span-full | start-n | end-n | row-span-n | row-start-n | row-end-nyou add itThe columns and rows a cell covers, or the line it starts or ends at. Keep line placement in DOM order: reading and focus order follow the DOM.
kv-grid-item--self-… | justify-self-… | place-self-…you add itWhere one cell sits inside its own area, in place of what the Root chose.
kv-grid-item--subgrid | rows-subgridyou add itThe cell becomes a grid that shares the columns (or rows) of its parent, so its own cells line up with the cells around it.
kv-grid-item--lg-span-8you add itAny of the above from a breakpoint of the grid’s own width, as for the Root.

useGrid

Gives the classes for your own elements. It takes no options, and the classes add no role, ARIA or tabindex. On your own ul or ol, add role="list" yourself, which Grid.Root does for you.

Result

PropTypeDefaultDescription
containerPropsGridPartProps–Spread on an element that wraps only the grid element: it is the container the breakpoints answer to. Only className.
rootPropsGridPartProps–Spread on the grid’s element: only className.
itemPropsGridPartProps–Spread on a cell’s element: only className.

Known issues#

  • Grid in a shrink-to-fit parent. In an inline-flex parent, a float or a flex item with no grow, the grid gets no width, so its cells overflow. Put Grid in a block-level parent, or give the flex item a size. It follows from Grid's two-element Root, and there is no other workaround.

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/apassgrid.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassgrid.test.tsx (axe and role, name and state): adds role="list" to a ul and an ol Root, and no role to a div
Keyboard contract rows (Vitest browser)passn/apass2 rows, each names a test in grid.test.tsx
Focus order and managementpassn/apassgrid.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.