Skip to main content
Primitives

Focus links

Makes links to a part of the same page move focus there.

Used by govuk-react's error summary and skip link.

Unstyled

The primitive on plain elements, with no GOV.UK markup or styles: what you start from in your own library.

Code
<>
  <FocusLinks.Root focusedClass="focused">
    <a href="#full-name">Enter your full name</a>
  </FocusLinks.Root>
  <p>
    <label htmlFor="full-name">Full name</label>
  </p>
  <input id="full-name" name="full-name" autoComplete="name" />
</>

In govuk-react

govuk-react's error summary, built with this primitive.

Code
<ErrorSummary.Composite {...args} disableAutoFocus={viewMode === 'docs'} />

Usage

import * as FocusLinks from '@govuk-react/primitives/focus-links';

<FocusLinks.Root focusedClass="focused">
  <a href="#main-content">Skip to main content</a>
</FocusLinks.Root>;

Makes links inside it, to a part of the same page, move focus there. A link to a form field focuses the field and scrolls its label into view, or its fieldset’s legend, as govuk-frontend’s error summary does. A link to anything else focuses it with tabindex="-1" and focusedClass until focus moves on, as govuk-frontend’s skip link does. Without JavaScript the browser follows the link as usual.

Inline script

To make the markup interactive before React hydrates, inline @govuk-react/primitives/inline/focus-links-element.js, or render it from React on the server:

import { InlineScript, focusLinks } from '@govuk-react/primitives/inline-script';

<InlineScript script={focusLinks} nonce={nonce} />;

How inline scripts work

API reference

FocusLinks.Root

PropType
focusedClass

Classes for a target that isn't a form field while it has focus.

string