When to use it#
- Use one on every page that has a header or a menu before the main content. People who use a keyboard can then jump past it, instead of pressing Tab through it on every page.
- Put it first in the page, before the header and any other link. Keep it in the page flow: don’t put it in a fixed bar that covers other content (2.4.11 Focus Not Obscured (Minimum), AA).
- Give the main content an id, such as
<main id="main">, and set the link’shrefto#main. A#address points to an element on the same page. - Not for a link to another page: use a Link. Not for text that only screen readers (software that reads the screen aloud) should get: use VisuallyHidden.
Example#
Press Tab until the link appears here, then Enter to jump.
Skip to main contentSite header with a menu and a search field
The main content starts here.
import { SkipLink } from '@kvirn-ui/react'
import { useSkipLinkTexts } from './texts.ts'
export function DefaultSkipLink() {
const { texts } = useSkipLinkTexts()
return (
<div>
<p>
<small>{texts.hint}</small>
</p>
<SkipLink href="#skip-link-main" />
<p>{texts.header}</p>
<div id="skip-link-main">
<p>{texts.main}</p>
</div>
</div>
)
}Use cases#
A link to something other than the main content
A long page may have a block that people want to reach directly, such as the application form. Write the label yourself so it says where the link goes.
Press Tab until the link appears here, then Enter to jump.
Skip to the application formSite header with a menu and a search field
The application form starts here.
import { SkipLink } from '@kvirn-ui/react'
import { useSkipLinkTexts } from './texts.ts'
export function CustomLabel() {
const { texts } = useSkipLinkTexts()
return (
<div>
<p>
<small>{texts.hint}</small>
</p>
<SkipLink href="#skip-link-form">{texts.customLabel}</SkipLink>
<p>{texts.header}</p>
<div id="skip-link-form">
<p>{texts.form}</p>
</div>
</div>
)
}Props in this example:
Warning: Don't forget
A label of your own is not translated for you. When it is in another language than the page, mark the language so screen readers pronounce it correctly (3.1.2 Language of Parts, level AA). See What you need to do.
Skip to the main content or to the search
Some pages have two things people look for first. Give each its own link, one after the other at the top: the main content first, then the search. Each link points to a target with its own id.
Press Tab to show the first link, and Tab again to show the second.
Skip to main contentSkip to searchSite header with a menu and a search field
The search field is here.
The main content starts here.
import { SkipLink } from '@kvirn-ui/react'
import { useSkipLinkTexts } from './texts.ts'
export function TwoLinks() {
const { texts } = useSkipLinkTexts()
return (
<div>
<p>
<small>{texts.hintTwo}</small>
</p>
<SkipLink href="#skip-link-two-main" />
<SkipLink href="#skip-link-two-search">{texts.searchLabel}</SkipLink>
<p>{texts.header}</p>
<div id="skip-link-two-search">
<p>{texts.search}</p>
</div>
<div id="skip-link-two-main">
<p>{texts.main}</p>
</div>
</div>
)
}Props in this example:
Your design system’s link component
Your project already has a link component with its own class or markup. Pass it with as. SkipLink gives it the other props, the ref (a way to reach the underlying element) and the kv-skip-link class, so the link still hides until it has focus.
Press Tab until the link appears here, then Enter to jump.
Skip to main contentSite header with a menu and a search field
The main content starts here.
import { SkipLink } from '@kvirn-ui/react'
import type { ComponentPropsWithRef } from 'react'
import { useSkipLinkTexts } from './texts.ts'
// Stands in for your design system's link: it adds its own class and passes on every prop and the ref.
function MunicipalityLink({ className, children, ...anchorProps }: ComponentPropsWithRef<'a'>) {
return (
<a {...anchorProps} className={['municipality-link', className].filter(Boolean).join(' ')}>
{children}
</a>
)
}
export function DesignSystemLink() {
const { texts } = useSkipLinkTexts()
return (
<div>
<p>
<small>{texts.hint}</small>
</p>
<SkipLink as={MunicipalityLink} href="#skip-link-design-system" />
<p>{texts.header}</p>
<div id="skip-link-design-system">
<p>{texts.designSystem}</p>
</div>
</div>
)
}Props in this example:
Information: Did you know?
Your component must render an <a href> and pass on its ref. Don’t use Link.Root here: a same-page address is not a route.
Your own link element
You need markup the component doesn’t give you. Spread the props from the hook on your own link and use the label from the hook. If you only want your design system’s link, use as, as in the case above.
Press Tab until the link appears here, then Enter to jump.
Skip to main contentSite header with a menu and a search field
The page content starts here.
import { useSkipLink } from '@kvirn-ui/react'
import { useSkipLinkTexts } from './texts.ts'
export function OwnElement() {
const { texts } = useSkipLinkTexts()
const { skipLinkProps, label } = useSkipLink({ href: '#skip-link-own' })
return (
<div>
<p>
<small>{texts.hint}</small>
</p>
<a {...skipLinkProps}>{label}</a>
<p>{texts.header}</p>
<div id="skip-link-own">
<p>{texts.ownElement}</p>
</div>
</div>
)
}Props in this example:
hrefon useSkipLink (options)skipLinkPropson useSkipLink (result)labelon useSkipLink (result)
Information: Did you know?
Put the whole of skipLinkProps on a native <a>. It carries the class, the address and the click handler that moves focus to the target (2.4.1 Bypass Blocks, A).
Accessibility#
What it does for you#
- APG pattern: none. WCAG technique G1 (a link at the top to skip to the main content) with C7 (hide it until it has focus).
- Deviations: none. Activating the link moves focus to the target. That is what the link is for.
- Native elements used:
<a href="#id">, neverLink.Root: a hash on the same page is not a route.
SkipLink is a native link with a class. It is the first Tab stop in the body, hidden until it has focus, and it moves focus to the main content.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| SkipLink | <a href> → link | none | class="kv-skip-link". The name is the rendered text, the message skipLink.label or your children: text and not aria-label, so it is translated and found. href is required and is a same-page #id |
| Target | the consumer's element | none | Not focusable by itself: the link adds tabindex="-1" on activation and removes it when focus leaves. A target that is already focusable (tabindex 0 or -1, a control) is left alone |
| SkipLink | as | the component's own | A component that renders an <a href> and forwards its ref. It gets the other props, the ref and the part's class (className joins, handlers chain, refs merge) |
It announces nothing.
What you need to do#
- Put it first in the body, before the header and any other link, and give the main content the id (
<main id="main">). - A missing target warns in development: the warning names the missing id.
- Language. A custom label is your own text. When it is in another language than the page, mark it: put it in a nested
KvirnProviderand spreaduseLocale().localePropson your own element (3.1.2 Language of Parts, AA). - Do not hide the target's focus ring. The default theme draws the focus ring on the target, only when focus came from the keyboard (
:focus-visible): don't setoutline: noneon[tabindex="-1"]. - Do not put it in a sticky layer that covers other content (2.4.11 Focus Not Obscured (Minimum), AA): it stays in the flow.
Warning: Don't forget
Put the link first in the page and give the main content the id it points to. If the id is missing, the link goes nowhere and people using a keyboard still have to tab through the whole header (2.4.1 Bypass Blocks, level A). A development warning tells you when the id is missing.
Visual and modes#
Headless: no CSS. With the default theme the link is hidden visually, but not removed from the page, until it has focus. Nothing is hidden with display: none, which would also remove it from the Tab order. When it has focus it appears in the normal flow of the page, with:
- link-coloured text that has enough contrast on the page background (1.4.3 Contrast (Minimum), AA), measured by an automated contrast check;
- an underline, so colour is not the only cue (1.4.1 Use of Color, A);
- a focus ring with at least 3:1 contrast (2.4.7 Focus Visible, AA);
- space around it and a target of at least 24 by 24 CSS pixels (2.5.8 Target Size (Minimum), AA).
It reflows with the page (1.4.10 Reflow, AA): the reflow sweep checks every story at 320 CSS pixels for horizontal overflow. In forced colours (a Windows contrast mode) it uses the system's link and background colours and a highlight-coloured ring, with no reliance on a background fill. If you restyle it, keep the contrast, the underline, the ring and the target size.
Focus management#
- Initial focus: not moved. The link is a Tab stop and nothing else.
- On activation: focus moves to the target element. The click handler does not call
preventDefault, so the browser still makes its own jump to the target. A test shows the browser's own jump is not cancelled. - A target made focusable has
tabindex="-1"until it loses focus. A target already focusable is left alone, and so is one with its owntabindex="-1". - Trap: no. Restore to: not applicable.
Warning: Don't forget
Keep the focus ring on the target. When the target can’t take focus yet, the link adds tabindex="-1" to it so focus can move there. If your styles remove the outline from such elements, someone using a keyboard can’t see where the link took them (2.4.7 Focus Visible, level AA).
WCAG success criteria#
- 2.4.1 Bypass Blocks (A): the link jumps to the main content.
- 2.4.3 Focus Order (A): first stop; the next Tab continues in the target.
- 2.4.7 Focus Visible (AA), 2.4.11 Focus Not Obscured (Minimum) (AA): visible and in the page flow while focused (a story with the default theme loaded).
- 1.4.1 Use of Color (A): the link is underlined, so colour is not the only cue (a theme property, not a test).
- 3.1.2 Language of Parts (AA): a custom label in another language is marked by the consumer (see Consumer responsibilities).
- 2.5.8 Target Size (Minimum) (AA), 1.4.10 Reflow (AA), 1.4.3 Contrast (Minimum) (AA) and 1.4.11 Non-text Contrast (AA): the default theme; the contrast is measured by an automated check.
- 4.1.2 Name, Role, Value (A):
linknamed by its text, and axe finds no violation.
Keyboard#
- Focus
- native
- Selection follows focus
- n/a
- Arrows wrap
- n/a
- Shortcuts
- none
Native tab order. No key is intercepted.
| Key | Where | What happens |
|---|---|---|
| Tab | page load or start | Focuses the skip link. It becomes visible at the top of the page Test: skip-link.test.tsx › SkipLink keyboard › the first Tab stop is the skip link |
| Tab | on the skip link | Moves to the next focusable element, and the skip link hides again Test: skip-link.test.tsx › SkipLink keyboard › Tab leaves the skip link for the next stop |
| Shift+Tab | on the skip link | Leaves the page content backwards (browser UI) Test: skip-link.test.tsx › SkipLink keyboard › Shift+Tab from the skip link leaves the document |
| Enter | on the skip link | Focuses the target (tabindex="-1" added if it was not focusable); the next Tab continues in itTest: skip-link.test.tsx › SkipLink keyboard › Enter moves focus to the target and the next Tab continues after it |
| Space | on the skip link | Not handled (a link; the page scrolls natively) Test: skip-link.test.tsx › SkipLink keyboard › Space is not handled |
A story with the default theme loaded proves the visible state: hidden before Tab, shown in the page flow while focused, hidden after Tab.
API reference#
import { SkipLink, useSkipLink } from '@kvirn-ui/react'SkipLink
Renders <a href> with the role link. It takes every attribute of an <a> and passes ref to it. The default theme is described on Theming.
| Prop | Type | Default | Description |
|---|---|---|---|
href | string | Required | A same-page address, "#main": the id of the main content. A development warning says so when no element has the id. |
children | ReactNode | the message skipLink.label | Your own label. It replaces the message, so you set its language: mark it if it differs from the page. |
messages | Partial<KvirnMessages['skipLink']> | – | Overrides the message for this link: { label: "Jump to the content" }. |
as | ElementType | 'a' | Changes the element to a component of your own, such as your design system’s link. It must render an <a href> and forward its ref, and it gets the other props, the ref and the part’s class. Don’t use Link.Root: a same-page address is not a route. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-skip-link | always | The part class. The default theme hides the link until it has focus. |
useSkipLink
Options
| Prop | Type | Default | Description |
|---|---|---|---|
href | string | Required | A same-page address, "#main": the id of the element the link jumps to. |
messages | Partial<KvirnMessages['skipLink']> | – | Overrides the message skipLink.label for this link. |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
skipLinkProps | SkipLinkPartProps | – | Spread on an <a>: class, href and a click handler that moves focus to the target. |
label | string | – | The message skipLink.label: the default text of the link. |
Strings
These are the default texts in @kvirn-ui/i18n.
For your whole app, pass messages to KvirnProvider. For one SkipLink, pass its messages prop.
skipLink.label
The link’s visible text, when you give no children. It says where the link goes.
| Language | Default text |
|---|---|
| Svenska | Hoppa till huvudinnehållet |
| Suomi | Siirry pääsisältöön |
| Norsk bokmål | Gå til hovedinnhold |
| Norsk nynorsk | Gå til hovudinnhald |
| English | Skip to main content |
Known issues#
- If you override the label, have a native speaker check it.
- Safari (WebKit): whether the focus ring shows on the target after someone presses Enter on the link is not covered by an automated test, so check it in your own service. In Safari, Tab only reaches links when "Press Tab to highlight each item" is on (or with Option+Tab), so test with that setting on. Don't hide the ring on
[tabindex="-1"].
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 | skip-link.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | skip-link.test.tsx › the name is the default label, rendered as text with no ARIA added; expectNoA11yViolations |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | 5 rows, each names a test (skip-link.test.tsx › SkipLink keyboard › the first Tab stop is the skip link) |
| Focus order and management | pass | n/a | pass | skip-link.test.tsx asserts focus (document.activeElement); first rule: SkipLink keyboard › the first Tab stop is the skip link |
| 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#
- Tab once on a fresh page: the link appears and its name is read.
- Press Enter: focus lands in the main content, and the next Tab continues after the link, not in the header.
- Tab without Enter: the link hides.
- At 400% zoom the focused link is visible and uncovered.
- In Safari, turn on "Press Tab to highlight each item" first, then repeat the first two steps.