Skip to content
Components, switch product

Password

<gui-password> is a password field. It renders a native <input type="password"> with its label, hint and errors, and a button that shows the password as text and hides it again.

Code of the example above
PasswordBasic.tsx
import { GuiPassword } from '@golemui/gui-components/react';
import '@golemui/gui-components/index.css';
export function PasswordBasic() {
return <GuiPassword label="Password" />;
}

The element’s entry point is @golemui/gui-components/password, which registers <gui-password> and nothing else. To install the package and load the stylesheet, see Installation.

The @golemui/gui-components/react components have typed props and an on* prop for each event. React 18 or later.

PasswordField.tsx
import { useState } from 'react';
import { GuiPassword } from '@golemui/gui-components/react';
import '@golemui/gui-components/index.css';
export function PasswordField() {
const [password, setPassword] = useState('');
return (
<GuiPassword
label="Password"
name="password"
autocomplete="current-password"
value={password}
onGuiChange={(event) => setPassword(event.detail.value ?? '')}
/>
);
}

Properties

Property Type Default Description
localeId attribute locale-id string 'en' BCP 47 locale for formatting and parsing, such as en-US or es.
value string – The password.
icon string – Icon class name shown inside the control, for example from an icon font.
placeholder string – Text shown while the control is empty.
autocomplete string – The autocomplete hint passed to the inner native control.
showPasswordIcon attribute show-password-icon string – Icon class name of the button that shows the password, while the password is hidden.
hidePasswordIcon attribute hide-password-icon string – Icon class name of the button that hides the password, while the password is shown.
showPasswordLabel attribute show-password-label string – Accessible name of the button that shows the password. An empty value keeps the default.
hidePasswordLabel attribute hide-password-label string – Accessible name of the button that hides the password. An empty value keeps the default.
uid string – The id of the element's control. The ids of its label, hint and errors derive from it. Optional: when it is not set, the element generates one from its tag name and a page-wide counter, e.g. gui-textinput-3. Generated ids depend on creation order, so a server-rendered element must set uid or its ids will not match after hydration.

Form control properties

Every field has these, from the base class of the form controls.

Property Type Default Description
name string – The name the value is submitted under. Without it the element stays out of its form. Reflected, like a native control's: the form reads a single value's name from the attribute.
label string – The visible label, which also names the control for assistive technology. Without one the control has no accessible name; the date, time, range, tags and file fields warn about it in development builds.
hint string – Help text shown under the label and announced as the control's description.
errors string[] [] Error messages to show, typically from your own validation. They show as soon as they are set, unless touched is false.
touched boolean – Whether the user has interacted with the control. Leave it unset unless you validate on interaction: false holds errors back until it becomes true.
required boolean false The control needs a value: it is marked as required and, with a name, blocks submission.
readOnly attribute readonly boolean – The value can be read and focused but not changed.
disabled boolean – Disabled by its own attribute or by a disabled <fieldset> around it. Setting the property sets the attribute, like a native control's.
form read-only, no attribute HTMLFormElement | null – The <form> the element belongs to, if any.
validity read-only, no attribute ValidityState – The element's validity, like a native control's.
validationMessage read-only, no attribute string – The message the browser shows for an invalid value.

Methods

Method Description
checkValidity(): boolean –
reportValidity(): boolean –

Events

Every event bubbles and is composed, so you can listen on any ancestor.

Event Description
gui-input The user changed the value. detail.value is the new value.
gui-change The user committed the text, on blur or Enter. detail.value is the value.
gui-blur Focus left the control.

The button shows the password as plain text, and hides it again. Without icons it reads “Show” or “Hide”. show-password-icon and hide-password-icon replace the text with icons: each takes the class names of an icon, and the button shows the icon of what it does next. See Icons for the ways to draw them.

Code of the example above
PasswordToggle.tsx
import { GuiPassword } from '@golemui/gui-components/react';
import '@golemui/gui-components/index.css';
// Any icon font works. These rules draw Material Icons ligatures.
const iconStyles = `
.icon-visibility::before {
content: 'visibility';
font-family: 'Material Icons';
}
.icon-visibility-off::before {
content: 'visibility_off';
font-family: 'Material Icons';
}
`;
export function PasswordToggle() {
return (
<>
<GuiPassword
label="Wi-Fi password"
showPasswordIcon="icon-visibility"
hidePasswordIcon="icon-visibility-off"
/>
<style>{iconStyles}</style>
</>
);
}

The button’s accessible name is “Show password” or “Hide password”. Change it with show-password-label and hide-password-label, or translate it for the whole app, see Accessibility. Without icons, these labels also replace the visible text.

errors takes a list of messages from your own validation, and the field is marked invalid. To check the password as the user works, set the errors property from a gui-change listener, which fires when they leave the field or press Enter.

Code of the example above
PasswordValidation.tsx
import { useState } from 'react';
import { GuiPassword } from '@golemui/gui-components/react';
import '@golemui/gui-components/index.css';
export function PasswordValidation() {
const [errors, setErrors] = useState<string[]>([]);
return (
<GuiPassword
id="new-password"
label="New password"
hint="At least 12 characters."
autocomplete="new-password"
errors={errors}
onGuiChange={(event) => {
const length = (event.detail.value ?? '').length;
setErrors(
length >= 12
? []
: [`Use at least 12 characters. This one has ${length}.`],
);
}}
/>
);
}

disabled and readonly work as on the text input. A disabled field disables its button too; a read-only field keeps the button, so the user can still reveal the password.

With a name, <gui-password> submits the password as text under that name, and required blocks an empty submit. The field submits the same value whether the password shows or not.

Set autocomplete so password managers know what to fill in: current-password on a sign-in form, as below, and new-password where the user picks a password.

Code of the example above
PasswordNativeForm.tsx
import { useState, type SubmitEvent } from 'react';
import {
GuiButton,
GuiPassword,
GuiTextinput,
} from '@golemui/gui-components/react';
import '@golemui/gui-components/index.css';
export function PasswordNativeForm() {
const [result, setResult] = useState('');
const onSubmit = (event: SubmitEvent<HTMLFormElement>) => {
event.preventDefault();
const data = Object.fromEntries(new FormData(event.currentTarget));
setResult(`Submitted ${JSON.stringify(data)}`);
};
return (
<form id="sign-in" onSubmit={onSubmit}>
<div className="gui-grid gui-grid--row">
<div className="gui-grid__cell">
<GuiTextinput
label="Email"
name="email"
autocomplete="username"
required
/>
</div>
<div className="gui-grid__cell">
<GuiPassword
label="Password"
name="password"
autocomplete="current-password"
required
/>
</div>
</div>
<GuiButton type="submit" label="Sign in" />
<p id="sign-in-result" role="status">
{result}
</p>
</form>
);
}

How the elements submit, validate and reset is in Native forms.

<gui-password> renders into the light DOM, so you can style every part with plain CSS. These are the parts and their public classes:

gui-password
<gui-password class="gui-field">
<label class="gui-label" for="password" id="password_label">
<span class="gui-label__text"
>Password<span aria-hidden="true"> *</span></span
>
<div class="gui-widget-hint" id="password_hint" aria-hidden="true">
At least 12 characters.
</div>
</label>
<div class="gui-widget">
<input
type="password"
id="password"
class="gui-widget-input gui-password--icon"
aria-describedby="password_hint"
aria-required="true"
/>
<span
class="gui-widget-icon icon-lock"
data-icon="icon-lock"
aria-hidden="true"
></span>
<button
type="button"
class="gui-password__toggle gui-widget-icon"
aria-label="Show password"
>
<span aria-hidden="true">Show</span>
</button>
</div>
<div
class="gui-validator gui-validator--empty"
id="password_errors"
role="alert"
></div>
</gui-password>
ClassPart
gui-fieldThe element itself. Lines its parts up with the fields next to it in a gui-grid row.
gui-labelThe label, with the text and the hint.
gui-label__textThe label text and the required marker.
gui-widget-hintThe hint.
gui-widgetThe box around the control.
gui-widget-inputThe <input>, the part that looks like a field.
gui-password--iconOn the <input> when there is an icon: it makes room for it.
gui-widget-iconThe icon, with your icon classes. The button has it too.
gui-password__toggleThe show and hide button. With icons it also has the classes of the icon it shows, and their value in data-icon.
gui-validatorThe errors container. It is always there, so the alert role announces new errors.
gui-validator--emptyOn the errors container while there are no errors.
gui-validator__errorOne error message.

The input’s type turns to text while the password shows. The ids derive from uid, or from a generated id such as gui-password-3 when you don’t set one. Select by class or by tag, not by id.

The field reads these design tokens. Set them on :root to change every field, or on any element to change the fields inside it:

TokenWhat it styles
--gui-widget-sizeThe height of the field, and the size of the icon.
--gui-radius-mdThe corner radius.
--gui-bg-surfaceThe field’s background.
--gui-border-defaultThe border.
--gui-text-defaultThe text, the label, the icon and the button.
--gui-font-familyThe font.
--gui-font-smThe text size of the label and the field.
--gui-font-xsThe text size of the hint and the errors.
--gui-text-secondaryThe hint.
--gui-border-focusThe border while focused.
--gui-shadow-focusThe focus ring.
--gui-intent-errorThe border and the messages when there are errors.
--gui-shadow-focus-errorThe focus ring when there are errors.
--gui-bg-disabledThe background when disabled.
--gui-text-disabledThe text when disabled.
--gui-space-1The space between the label, hint and errors.

To restyle beyond the tokens, see Overriding styles; for dark mode and the Clay theme, see Themes and dark mode.

  • Name. The label names the field. Without one the field has no accessible name.
  • Description. The hint is the field’s description, through aria-describedby.
  • Errors. While errors show, the input has aria-invalid="true" and points at them with aria-errormessage, and screen readers announce new errors as they appear.
  • Required. required sets aria-required; the visual marker is hidden from screen readers.
  • The show and hide button. Its accessible name says what it does: “Show password”, then “Hide password”. show-password-label and hide-password-label change it on one field; an empty value keeps the default. To translate it for the whole app, use the keys showPassword and hidePassword, and show and hide for the visible text, see i18n.
  • Icons. The icons are hidden from assistive technology; the button’s name carries the meaning.
KeyAction
TabMoves focus to the field, then to the show and hide button.
Enter in the fieldCommits the value, as leaving the field does, and fires gui-change.
Enter or Space on the buttonShows or hides the password.
Every editing keyWorks as in any native password input.

What every element shares, such as translating the browser messages, is in Accessibility.