Skip to main content
Primitives

Password input

A button that shows and hides a password.

Used by govuk-react's password input.

Unstyled

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

Code
<>
  <label htmlFor="password">Password</label>
  <PasswordInput.Root>
    <PasswordInput.Input id="password" name="password" autoComplete="current-password" />
    <PasswordInput.Trigger />
    <PasswordInput.Status className="visually-hidden" />
  </PasswordInput.Root>
</>

In govuk-react

govuk-react's password input, built with this primitive.

Code
<PasswordInput.Field
  id="password-input"
  name="password"
  label="Password"
/>

Usage

import * as PasswordInput from '@govuk-react/primitives/password-input';

<label htmlFor="password">Password</label>
<PasswordInput.Root>
  <PasswordInput.Input id="password" name="password" autoComplete="current-password" />
  <PasswordInput.Trigger />
  <PasswordInput.Status className="visually-hidden" />
</PasswordInput.Root>;

Without JavaScript the field is a plain password field and the Trigger is hidden. On upgrade the element shows the button, which switches the input between type="password" and type="text", sets its own text (“Show” or “Hide”) and accessible name (“Show password” or “Hide password”), and announces the change in Status. Leave out Status and the element adds a visually hidden one after the input.

The input’s type is the state, kept when React renders the input again, so there is nothing to control. As in govuk-frontend, the password is hidden again when its form is submitted and when the page is restored from the back-forward cache. The text is English by default: pass showText, hideText, showLabel, hideLabel, shownAnnouncement and hiddenAnnouncement to Root.

Inline script

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

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

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

How inline scripts work

API reference

PasswordInput.Root

PropType
showText

The button's text when the password is hidden. Defaults to "Show".

string
hideText

The button's text when the password is visible. Defaults to "Hide".

string
showLabel

The button's accessible name when the password is hidden. Defaults to "Show password".

string
hideLabel

The button's accessible name when the password is visible. Defaults to "Hide password".

string
shownAnnouncement

Announced when the password becomes visible. Defaults to "Your password is visible".

string
hiddenAnnouncement

Announced when the password is hidden. Defaults to "Your password is hidden".

string

PasswordInput.Input

The password field. Its type starts as "password" and the element switches it.

PropType
asChild

Render the only child instead, with this part's props merged onto it.

boolean

PasswordInput.Status

A live region for the announcements. Make it visually hidden.

PropType
asChild

Render the only child instead, with this part's props merged onto it.

boolean

PasswordInput.Trigger

The show and hide button. Hidden until the element upgrades, which then sets its text and label.

PropType
asChild

Render the only child instead, with this part's props merged onto it.

boolean