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.

Link

A link that goes to another page or place. It is a native link, so it behaves the way people expect. Add the new-tab notice and the link says when it opens a new tab, also to a screen reader (software that reads the screen aloud).

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

When to use it#

  • Use a link to go somewhere: another page, a place on this page or a file.
  • Write text that says where the link goes: “Apply for a parking permit”, not “click here”.
  • Use kv-link--service for the link that starts an e-service (an online public service).
  • Not for an action that changes something, such as sending or saving: use a Button. A link has no disabled state: if the page isn’t available, show text instead.

Example#

Use cases#

A link that opens a new tab

Opening a new tab takes people somewhere they may not expect, so say so in the link’s name. Put Link.NewTabNotice inside the link: it adds that text in the language set on KvirnProvider. Link also adds noopener noreferrer to rel for you, so the new page can’t reach back to yours.

Props in this example:

Information: Did you know?

Link does not check that you added the notice: there is no type error and no warning, so review every target="_blank" link by hand. No level A or AA criterion requires the warning (3.2.5 Change on Request is level AAA), but without it a screen reader user isn’t told that a new tab opens. Better still, don’t open new tabs.

The current page in a list of links

Mark the link to the page the user is on, so a screen reader can tell them where they are and the default theme shows it by weight as well as colour. Link doesn’t read your router: you decide which link is current.

Props in this example:

Information: Did you know?

With a router (the part of your app that changes pages), compare the link’s address with the router’s own current address, and pass current="page" or false.

A link to a page in another language

Put the link in your own element with lang set to the language of its text, so a screen reader reads “Suomeksi” (in Finnish) in Finnish. hrefLang names the language of the page it goes to. It doesn’t change how the text is read.

This page is also available in Finnish: Suomeksi

Warning: Don't forget

Link never sets lang for you. Without it, a screen reader reads the Finnish text with the page’s language (3.1.2 Language of Parts).

A link to a file

People decide before they tap, and some pay for their data by the megabyte. Put the file type and size in the link text itself, in brackets. Don’t leave them to a title, an icon alone or text beside the link. Offer a web page version where you can. as="a" makes this a plain <a>, so a router never handles the file.

The budget for next year is published as a file.

Budget 2027 (PDF, 2.5 MB)

Prefer to read it on a page? Budget 2027 as a web page

Props in this example:

  • as on Link.Root

Information: Did you know?

Write the size with useFormat, so the number and unit follow the page’s language: “2.5 MB” in English, “2,5 MB” in Swedish, “2,5 Mt” in Finnish. The example’s own text exists in all five languages.

The way in to the service should be easy to find among plain links. Add the class kv-link--service and put the icon first, in Link.Icon. The icon is hidden from screen readers, so the link’s name is only its text.

Accessibility#

What it does for you#

  • APG pattern: none needed. A link is a native <a href> (APG Link only covers non-native links, which Link never renders).
  • Deviations: none
  • Native elements used: <a href>, rendered by the app's registered router link component, or by a native <a> when none is registered or as="a" is given. <span> for the new-tab notice. Allowed elements: Link.Root takes as as a component or a tag that renders an <a href> and forwards its ref; Link.NewTabNotice span (default), em or small; Link.Icon span (default) or i. Another tag on the last two warns once (as-not-allowed) and falls back to span.

Link navigates. An action is a Button. Link has no disabled prop, by type: a disabled link isn't a thing, so remove the link or render plain text instead.

PartElement / roleARIANotes
Link.Root<a href> → linkaria-current from currentcurrent: 'page', 'step', 'location', 'date', 'time' or true. false or absent sets nothing. data-current when set
target="_blank"nonerel="noopener noreferrer" is added to the consumer's own rel tokens, also with as="a" or an as component
keyboard focusnonedata-focus-visible while the link matches :focus-visible
another languagehrefLang passed through<span lang="fi"><Link.Root href="/fi" hrefLang="fi">Suomeksi</Link.Root></span> (3.1.2)
Link.Icon<span>, decorativearia-hidden="true"class="kv-link-icon". Always hidden: aria-hidden can't be turned off, so the icon is never part of the link's name (2.5.3). Put it first. Also exported as LinkIcon
Link.NewTabNotice<span>, part of the link's namenoneText from link.newTabNotice. The consumer decides whether to hide it visually. Also exported as LinkNewTabNotice
target="_blank" without a notice–Nothing: no type error and no dev warning. Adding the notice is the consumer's responsibility
link component doesn't render an <a>–Dev warning: the registered component must forward its ref and render an <a>

The service link is className="kv-link--service" on a Link: a look and not a role, so the element, the role and the keys are the same as any link. It has no disabled state, because a link has none (an unavailable e-service is text, not a dimmed link).

useLink gives the same linkProps and the resolved newTabNotice text for your own <a> or router link.

It announces nothing.

What you need to do#

  • Give every link text that says where it goes, in context (2.4.4 Link Purpose (In Context) (A)). No "click here".
  • A link that opens a new window or tab should say so (AAA): put <Link.NewTabNotice /> inside every target="_blank" link, or better, don't open new tabs (3.2.5 Change on Request (AAA)). Link does not check this: there is no type error and no dev warning. With useLink, render link.newTabNotice inside the link yourself.
  • If you hide the notice visually, hide it with a visually-hidden technique, not display: none or aria-hidden, so it stays in the name. Consider showing it: sighted users benefit too.
  • Set current="page" on the link to the current page, for example in navigation. Link doesn't detect it from the router.
  • For a link in another language, put it in your own element with lang (the text's language, 3.1.2 Language of Parts (AA)). hrefLang names the target's language, not the text's.
  • Register a router link component that forwards its ref and renders an <a>. Use as="a" for downloads and other links the router mustn't handle.
  • When overriding newTabNotice, keep it true to what happens (3.2.5 Change on Request (AAA)) and consistent with any visible text (2.5.3 Label in Name (A)).
  • Style [data-current] and [data-focus-visible] (or :focus-visible). Don't show the current page by colour alone (1.4.1 Use of Color (A)).

Warning: Don't forget

A link that says only “click here” or “read more” fails 2.4.4 Link Purpose (In Context) unless the paragraph, list item or table cell around it says where it goes. Keep the new-tab notice in the link’s name. To hide it from sight, use a visually-hidden style (it hides text from sight but not from screen readers), not display: none or aria-hidden. If you change its words, keep them the same as any visible text (2.5.3 Label in Name).

Visual and modes#

  • Focus indicator: headless. The browser's native ring by default. The default theme restyles it to at least 2px at 3:1 (2.4.7 Focus Visible (AA); 2.4.13 Focus Appearance (AAA)).
  • Target size: inline links in a sentence are exempt from 2.5.8 Target Size (Minimum) (AA). The default theme sets a minimum size for standalone links, and the service link and a Navigation item are as tall as the theme's control height (taller in the comfortable density, shorter in the compact one). A story asserts the 24px threshold for the service link, and another for a navigation item in the compact density.
  • forced-colors behaviour: a native <a href>, so the system's link colour applies. The service link keeps a thin edge in that colour, and its icon block is not filled: it has a divider in that colour, so the boundary survives (1.4.11 Non-text Contrast (AA)). A link with current is bolder in the default theme, so it is never shown by colour alone (1.4.1 Use of Color (A)). How the current page looks in a list of links, in forced colours too, is described in Navigation's contract (a fill and a bolder weight, and a straight bar in forced colours). A link in running text has 3:1 contrast against the body text (checked by the theme's contrast check) and is underlined on hover; the contrast themes, forced colours and a link in grey or red text (a caption, small, help text, an error message) keep the underline at rest (1.4.1). The forced-colours sweep checks that every link keeps a visible boundary and shows a focus indicator when it has focus (1.4.11).
  • reduced-motion behaviour: no motion, and the reduced-motion sweep checks that no animation keeps looping (2.3.3).
  • Reflow and text spacing: the reflow sweep runs every story at 320 CSS px and fails on horizontal overflow (1.4.10 Reflow, AA). The text-spacing sweep applies the 1.4.12 spacing and fails when a box clips its text.

Warning: Don't forget

If you restyle links, keep a link in running text clear by more than colour (1.4.1 Use of Color), keep a visible focus ring (2.4.7 Focus Visible), and keep a link that stands alone at least 24 × 24 CSS pixels (2.5.8 Target Size (Minimum)).

Focus management#

  • Initial focus: not moved. Link never moves focus.
  • Trap: no.
  • Restore to: not applicable. Where focus goes after a client-side navigation is the router's or the app's job (for example, to the new page's h1).
  • Never obscured by: Link renders no overlay.

WCAG success criteria#

  • 2.1.1 Keyboard (A): native <a href>, Enter (Keyboard rows above).
  • 2.4.4 Link Purpose (In Context) (A): consumer text, plus the new-tab notice in the name.
  • 2.4.7 Focus Visible (AA): data-focus-visible.
  • 3.1.2 Language of Parts (AA): yours, a lang ancestor on your own element. hrefLang is only passed through (link.test.tsx › passes hrefLang through).
  • 3.2.5 Change on Request (AAA, so it does not count toward the AA bar): the translated new-tab notice warns people before a link opens a new tab. Using it is the consumer's job: Link doesn't warn when it's missing.
  • 4.1.2 Name, Role, Value (A): role link, aria-current (link.test.tsx).
  • 1.4.1 Use of Color (A): the current page is shown by weight as well as colour, and a link in running text keeps its underline (Visual / modes above).
  • 1.4.11 Non-text Contrast (AA): the service link's boundary survives in forced colours (Visual / modes above).
  • 2.5.3 Label in Name (A): Link.Icon is hidden from assistive technology, so the name is the visible text.
  • 2.5.8 Target Size (Minimum) (AA): a story asserts the service link at 24 × 24 CSS px. Other standalone links are not tested.

Keyboard#

Focus
native
Selection follows focus
n/a
Arrows wrap
n/a
Shortcuts
none

A native <a href>: one Tab stop, in DOM order, with no tabindex. Enter is native.

KeyWhereWhat happens
TabLinkMoves focus to the link
Test: link.test.tsx › keyboard › Tab moves focus to the link
Shift+TabLinkMoves focus to the previous focusable element, and off the link
Test: link.test.tsx › keyboard › Shift+Tab moves focus off the link
EnterLinkFollows the link
Test: link.test.tsx › rendering › Enter activates the link; Space does not
EnterRouter linkFollows the link through the registered router, without a page load
Test: link.test.tsx › router link › Enter follows a router link without a page load
EnterLink with target=_blankOpens the link in a new tab. With the notice added, the name already said it would (3.2.5, AAA)
Test: link.test.tsx › keyboard › Enter on a new-tab link does not intercept the browser’s own handling
SpaceLinkNot handled (native: scrolls the page). Doesn't follow the link. Focus stays
Test: link.test.tsx › rendering › Enter activates the link; Space does not

Escape, arrow keys and Home / End are not handled.

API reference#

import { Link, useLink } from '@kvirn-ui/react'

Renders <a href> with the role link, rendered by the router link you registered on KvirnProvider (linkComponent), or a native <a> when none is. It takes every attribute of that element and passes ref to it. There is no disabled prop. Also exported as Link and LinkRoot. The default theme is described on Theming.

PropTypeDefaultDescription
current'page' | 'step' | 'location' | 'date' | 'time' | boolean–Marks the link as the current item in a set, as aria-current. Link does not detect it from the router. false or absent sets nothing.
targetstring–Passed to the element. "_blank" opens a new tab and adds rel="noopener noreferrer" to your own rel tokens.
relstring–Your own rel tokens, kept. A new-tab link gets noopener noreferrer added once.
messagesPartial<KvirnMessages['link']>–Overrides link.newTabNotice for this link and its Link.NewTabNotice.
asElementTypethe registered router link, or <a>Changes the element to a component of your own, with its props set on the Link. Link.Root as="a" bypasses the registered router link. It must render an <a href> and forward its ref.
Class or attributeValuesMeaning
kv-linkalwaysThe part class. The default theme styles it.
kv-link--serviceclass you addThe one link that starts an e-service: an outlined label with the icon in a block. A look, not a role. Theme variant.
aria-current"page", "step", "location", "date", "time" or "true"From current. Not passed directly.
data-currentpresent or absentThe link has current set.
data-focus-visiblepresent or absentThe link has keyboard focus (:focus-visible).

Renders <span> with the text of link.newTabNotice, such as (opens in a new tab). It is part of the link’s name. Also exported as LinkNewTabNotice.

PropTypeDefaultDescription
childrenReactNode–Your own text. It wins over every message. Empty text falls through to the message.
as'span' | 'em' | 'small''span'Changes the element. Another tag warns once in development.
Class or attributeValuesMeaning
kv-link-new-tab-noticealwaysThe part class. Your own class joins it.

Renders <span aria-hidden="true"> for a decorative icon, first in the link. Also exported as LinkIcon.

PropTypeDefaultDescription
childrenReactNode–The icon, for example <Icon name="arrow-forward" size="24" />, or any decorative SVG.
as'span' | 'i''span'Changes the element. It stays decorative: aria-hidden and the class are kept.
Class or attributeValuesMeaning
kv-link-iconalwaysThe part class. Your own class joins it.
aria-hidden"true"Always set, and it cannot be turned off, so the icon is never part of the name.

Options

PropTypeDefaultDescription
current'page' | 'step' | 'location' | 'date' | 'time' | boolean–Sets aria-current and data-current. false or absent sets nothing.
targetstring–Mirrors HTML target. "_blank" adds rel="noopener noreferrer".
relstring–Your own rel tokens, kept.
messagesPartial<KvirnMessages['link']>–Per-instance message overrides.

Result

PropTypeDefaultDescription
linkPropsLinkPartProps–Spread on an <a href> or your router link: class, target, rel, aria-current, focus handlers.
isCurrentboolean–Whether current is set.
isFocusVisibleboolean–Whether the link has keyboard (:focus-visible) focus.
opensInNewTabboolean–True for target="_blank": render newTabNotice inside the link.
newTabNoticestring–The resolved link.newTabNotice text, for example (opens in a new tab).

Strings

These are the default texts in @kvirn-ui/i18n.

For your whole app, pass messages to KvirnProvider. For one Link, pass its messages prop.

link.newTabNotice

Tells users a link opens in a new tab. It is part of the link’s name, used by Link.NewTabNotice and useLink().newTabNotice.

LanguageDefault text
Svenska(öppnas i en ny flik)
Suomi(avautuu uuteen välilehteen)
Norsk bokmål(åpnes i en ny fane)
Norsk nynorsk(blir opna i ei ny fane)
English(opens in a new tab)

Known issues#

  • Keyboard tests run in Chromium only. The keyboard rows run in Vitest browser mode on Chromium, not in WebKit. Test in Safari with your own assistive technology.
  • A target="_blank" link with no notice is not flagged. Neither a type error nor a dev warning catches it, so review it by hand.

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/apasslink.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apasslink.test.tsx › current="page" sets aria-current and data-current; expectNoA11yViolations
Keyboard contract rows (Vitest browser)passn/apass6 rows, each names a test (link.test.tsx › keyboard › Tab moves focus to the link)
Focus order and managementpassn/apasslink.test.tsx asserts focus (toHaveFocus); first rule: keyboard › Tab moves focus to the link
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#

With a screen reader and the keyboard, try:

  • Tab to the link: the focus ring shows, and "link" and the link text are read.
  • Press Enter: the link is followed. Space does not follow it.
  • The link to the current page is read as current.
  • A link that opens a new tab reads "(opens in a new tab)" as part of its name.
  • A link in another language is read in that language's voice.