When to use it#
- Use it first in the site header for a line a visitor may need on every page: who runs the service, the latest news, and a few tools such as language, easy-to-read or contact.
- Write the text first and the navigation last. Reading order and focus order follow the DOM, so they are the visual order.
- Give the navigation inside it a
labelthat differs from the main navigation, since the bar itself is not a landmark. - Not for the main menu: use Navigation. Not for a message that must be noticed: use Alert.
Example#
import { Link, Navigation, TopBar } from '@kvirn-ui/react'
import { useTopBarTexts } from './texts.ts'
export function DefaultTopBar() {
const { texts } = useTopBarTexts()
return (
<TopBar>
<p>
{texts.intro} <Link.Root href="#news">{texts.news}</Link.Root>
</p>
<Navigation.Root label={texts.tools} className="kv-navigation--horizontal">
<Navigation.List>
<Navigation.Item>
<Link.Root href="#language" current>
{texts.language}
</Link.Root>
</Navigation.Item>
<Navigation.Item>
<Link.Root href="#easy-read">{texts.easyRead}</Link.Root>
</Navigation.Item>
<Navigation.Item>
<Link.Root href="#contact">{texts.contact}</Link.Root>
</Navigation.Item>
</Navigation.List>
</Navigation.Root>
</TopBar>
)
}Use cases#
A tool that does something on the page
Some tools act on the page and don’t go to another one: print, say. Put a Button in the bar and give it the class kv-button--link. It looks like a link and a screen reader reads it as a button. Tab reaches it in the order you wrote.
import { Button, TopBar } from '@kvirn-ui/react'
import { useTopBarTexts } from './texts.ts'
export function ButtonTool() {
const { texts } = useTopBarTexts()
return (
<TopBar>
<p>{texts.hours}</p>
<Button className="kv-button--link" onClick={() => window.print()}>
{texts.print}
</Button>
</TopBar>
)
}Warning: Don't forget
Use a link to go to a page and a button to do something. A button answers Space as well as Enter, a link only Enter (4.1.2 Name, Role, Value, A).
Opening hours and a phone number
A bar doesn’t need a navigation. For a single link, such as the phone number of the contact centre, write the link directly in the bar after the text. The theme treats a link that stands on its own as a bar item: at least 24 pixels tall (2.5.8 Target Size (Minimum), AA), and underlined only when you point at it.
import { Link, TopBar } from '@kvirn-ui/react'
import { useTopBarTexts } from './texts.ts'
export function PhoneLink() {
const { texts } = useTopBarTexts()
return (
<TopBar>
<p>{texts.hours}</p>
<Link.Root href="tel:012345678">{texts.call}</Link.Root>
</TopBar>
)
}A bar with a fill
Set variant to primary, secondary or accent to fill the bar across the full width. Its text, links, focus ring and the current tool take the fill’s on-colour, the colour made to be read on that fill. The theme’s contrast check covers each fill: 4.5:1 for text, and 7:1 in the two high-contrast themes. A link in a sentence stays underlined.
import { Link, Navigation, TopBar } from '@kvirn-ui/react'
import { useTopBarTexts } from './texts.ts'
export function FilledTopBar() {
const { texts } = useTopBarTexts()
return (
<TopBar variant="accent">
<p>
{texts.intro} <Link.Root href="#news">{texts.news}</Link.Root>
</p>
<Navigation.Root label={texts.toolsFilled} className="kv-navigation--horizontal">
<Navigation.List>
<Navigation.Item>
<Link.Root href="#language" current>
{texts.language}
</Link.Root>
</Navigation.Item>
<Navigation.Item>
<Link.Root href="#easy-read">{texts.easyRead}</Link.Root>
</Navigation.Item>
<Navigation.Item>
<Link.Root href="#contact">{texts.contact}</Link.Root>
</Navigation.Item>
</Navigation.List>
</Navigation.Root>
</TopBar>
)
}Props in this example:
varianton TopBar
Warning: Don't forget
Write a sentence in a <p>, never a bare <div>: a link directly in a div of the bar counts as a bar item and loses its underline at rest, so it is no longer clear that it is a link (1.4.1 Use of Color, A).
Your own element
When you render the bar element yourself, for example inside a component of your own, call useTopBar and spread topBarProps on it. You get only the class: no role, ARIA or tabindex. To add a class of your own, join it with mergeProps.
import { Link, Navigation, useTopBar } from '@kvirn-ui/react'
import { useTopBarTexts } from './texts.ts'
export function OwnElement() {
const { texts } = useTopBarTexts()
const topBar = useTopBar({ variant: 'secondary' })
return (
<div {...topBar.topBarProps}>
<p>{texts.intro}</p>
<Navigation.Root label={texts.toolsOwn} className="kv-navigation--horizontal">
<Navigation.List>
<Navigation.Item>
<Link.Root href="#contact">{texts.contact}</Link.Root>
</Navigation.Item>
</Navigation.List>
</Navigation.Root>
</div>
)
}Props in this example:
varianton useTopBar (options)
Accessibility#
What it does for you#
- APG pattern: none: a plain
<div>around your text and a Navigation. - Deviations: none
- Native elements used:
<div>; inside it the elements you write (a<p>, a<nav>,<a href>).
| Part | Element / role | ARIA / state | Notes |
|---|---|---|---|
TopBar | <div>, no role | none | No landmark of its own: a Navigation inside is the landmark, named by its label. |
TopBar variant | class only | none | primary, secondary or accent adds kv-top-bar--<variant>; none adds no class. |
| children | yours | yours | Laid out at the start and the end in DOM order, so reading and focus order are the visual order (1.3.2 Meaningful Sequence, A; 2.4.3 Focus Order, A). |
The navigation's own rows (aria-current on one link) are the Navigation's. useTopBar({ variant }) gives the same frozen topBarProps (only className) for your own element.
It announces nothing.
What you need to do#
- Text first, the navigation last, each a child of the bar.
- Name the navigation with its
label, and mark the current link. - A link in the text says where it goes (2.4.4 Link Purpose (In Context), A).
- If you make the bar sticky, check that the focused control is not hidden behind it (2.4.11 Focus Not Obscured (Minimum), AA).
- Write a sentence in a
<p>, never a bare<div>: a link directly in a div of the bar is a bar item and loses its resting underline.
Warning: Don't forget
Name the navigation inside the bar, and give it a different name from the main menu. A screen reader user picks a landmark by its name. A development warning fires if a name is missing or used twice (2.4.6 Headings and Labels, AA).
Visual and modes#
- No fill by default: the page's colours.
primary,secondaryandaccentfill the bar, and its text, text links (a link in a sentence stays underlined at rest), focus ring and current navigation bar take the colour made for that fill. Text meets 4.5:1 on every fill, and 7:1 in the contrast themes, checked automatically in every theme. The focus ring has about the same colour as the fill itself, so it uses the on-fill colour instead. The primary site header is a darker band with the same on-fill colours. - A bar without a fill marks its current tool with bolder text and a straight bar at the item's block end, never the Navigation's solid fill. In forced colours the item takes the system link colour and the theme's own bar marks it.
- No type rules of its own: the text takes its size from what it sits on. Bar items (a link that is a direct child of the bar or of a side, a Navigation item, a link-look button) are at least 24 by 24 CSS px at every width (2.5.8 Target Size (Minimum), AA), in the bar's on-colour, underlined on hover only, with no hover background. A link in a sentence is not a bar item: it keeps its underline at rest (1.4.1 Use of Color, A). A popup opened from the bar keeps its own look.
- 320px: the two sides wrap onto their own rows, nothing is hidden (1.4.10 Reflow, AA). The text-spacing sweep applies the 1.4.12 spacing and fails on a box that clips its content.
- forced-colors behaviour: links take the system link colour. A variant's fill drops to the system background with a thin edge in the system text colour, the focus ring takes the highlight colour, and the theme's link-colour bar marks the current item; so does the primary site header's band.
- reduced-motion behaviour: nothing animates.
- RTL: logical properties; the text is at the right and the navigation at the left.
Focus management#
- Initial focus: not moved.
- Trap: no.
- Restore to: not needed.
- Never obscured by: nothing, as the bar is not sticky.
WCAG success criteria#
- 1.3.1 Info and Relationships (A)
- 1.3.2 Meaningful Sequence (A)
- 1.4.1 Use of Color (A)
- 1.4.3 Contrast (Minimum) (AA)
- 1.4.10 Reflow (AA)
- 1.4.11 Non-text Contrast (AA)
- 1.4.12 Text Spacing (AA)
- 2.4.3 Focus Order (A)
- 2.4.4 Link Purpose (In Context) (A)
- 2.4.7 Focus Visible (AA)
- 2.5.8 Target Size (Minimum) (AA)
- 4.1.2 Name, Role, Value (A)
Keyboard#
- Focus
- native
- Selection follows focus
- n/a
- Arrows wrap
- n/a
- Shortcuts
- none
| Key | Where | What happens |
|---|---|---|
| Tab | in the bar | Moves through its bar items in DOM order Test: top-bar.test.tsx › Tab moves through the bar items in DOM order |
| Shift+Tab | in the bar | Moves back through its bar items Test: top-bar.test.tsx › Shift+Tab moves back through the bar items |
| Enter | a link | Follows it Test: top-bar.test.tsx › Enter follows a link |
| Enter | a link-look button | Activates it Test: top-bar.test.tsx › Enter or Space activates a link-look button |
| Space | a link-look button | Activates it Test: top-bar.test.tsx › Enter or Space activates a link-look button |
The bar handles no key itself: arrows, Home and End do nothing (top-bar.test.tsx › the bar handles no key itself: arrows, Home and End move nothing). The links and buttons keep their native keys (Link, Button).
Read aloud#
| State or action | Expected phrase(s) as read aloud | Live region politeness | Test |
|---|---|---|---|
| The bar | its children in DOM order, no banner, region or group phrase of its own | none | top-bar.test.tsx › the bar reads in DOM order with no phrase of its own, the navigation by its label, the current tool as the current item, a link-look button as a button |
| The navigation | navigation, Verktyg (the label "Tools", in Swedish) | none | the same test |
| The current tool | link, Documentation, current item | none | the same test |
| A link-look button | button, Display settings | none | the same test |
API reference#
import { TopBar, useTopBar } from '@kvirn-ui/react'TopBar
Renders <div> with no role: it is not a landmark, a Navigation inside it is. It takes every attribute of a <div> and passes ref to it. Its children are yours, laid out at the start and the end in DOM order. The default theme is described on Theming.
| Prop | Type | Default | Description |
|---|---|---|---|
variant | 'primary' | 'secondary' | 'accent' | none | The fill. None adds no class and no fill: the page’s colours. primary, secondary or accent adds kv-top-bar--<variant>: a full-width fill with its on-colour text, links, focus ring and current navigation bar. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-top-bar | always | The part class. The default theme lays out the two sides and wraps them onto their own rows when there is no room, as at 320 pixels wide. |
kv-top-bar--primary | variant="primary" | The primary fill with its on-colour. The default adds no class. |
kv-top-bar--secondary | variant="secondary" | The secondary fill with its on-colour. |
kv-top-bar--accent | variant="accent" | The accent fill with its on-colour. |
useTopBar
Options
| Prop | Type | Default | Description |
|---|---|---|---|
variant | 'primary' | 'secondary' | 'accent' | none | The fill. Adds kv-top-bar--<variant> to topBarProps.className. |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
topBarProps | TopBarPartProps | – | Spread on your element: only the class. It adds no role, ARIA or tabindex, and the same frozen object comes back every time. |
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 | top-bar.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | top-bar.test.tsx › the root is a plain div with no role, and the Navigation inside it is the landmark; expectNoA11yViolations |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | 5 rows, each names a test (top-bar.test.tsx › Tab moves through the bar items in DOM order) |
| Focus order and management | pass | n/a | pass | top-bar.test.tsx asserts focus (toHaveFocus); first rule: Tab moves through the bar items in DOM order |
| 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#
With a screen reader and the keyboard, try:
- Tab goes through the bar in the order it is read: text links first, the tools last.
- The landmarks list does not show the bar. It shows its navigation, with its own name.
- The current tool is announced as current.
- A link-look button is announced as a button, and Enter and Space both press it.
- At 320px the two sides wrap, nothing is hidden, and there is no sideways scroll.
- On a filled bar in a high-contrast theme or forced colours, the text and the focus ring stay easy to see.