Skip to main content
Primitives

Button

Prevents double clicks, and lets Space activate a link with role="button".

Used by govuk-react's button.

Unstyled

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

Default

Code
<Button.Root preventDoubleClick>Save and continue</Button.Root>

Link

Space activates a link with `role="button"`, as it does a button.

Code
<Button.Root asChild>
  <a href="#start" role="button">
    Start now
  </a>
</Button.Root>

In govuk-react

govuk-react's button, built with this primitive.

Code
<Button>
  Save and continue
</Button>

Usage

import * as Button from '@govuk-react/primitives/button';

<Button.Root preventDoubleClick>Save and continue</Button.Root>;

<Button.Root asChild>
  <a href="/start" role="button">
    Start now
  </a>
</Button.Root>;

With preventDoubleClick, a second click within a second of the first is ignored, so a form isn’t submitted twice. The click is stopped before React hears it, so onClick doesn’t run twice either. Space activates a link with role="button", as it does a button. Unlike the other primitives there’s no wrapper element, which would change a button group’s layout: importing the module adds one click and one keydown listener to the document, for any element marked by Root. Without React, mark buttons with data-button.

Inline script

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

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

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

How inline scripts work

API reference

Button.Root

PropType
asChild

Render the only child instead, such as a link with role="button", with this part's props merged onto it.

boolean
preventDoubleClick

Cancel a second click within a second of the first, so a form isn't submitted twice.

boolean