Skip to main content
Kobalte

Password Input

Since v2.1.0@kobalte/core/password-inputSource

A text field for entering a password, with a button that shows or hides what was typed.

Import

import { PasswordInput } from "@kobalte/core/password-input";
// or
import { Root, Label, Input, VisibilityTrigger, Indicator, ... } from "@kobalte/core/password-input";

Features

  • Built on TextField, so labelling, description, error message, validation and form submission behave the same.
  • PasswordInput.Input switches its type between password and text, and defaults to autocomplete="current-password", spellcheck="false" and autocapitalize="off".
  • PasswordInput.VisibilityTrigger is a toggle button: it exposes its state with aria-pressed, points at the input with aria-controls, and keeps a fixed aria-label so screen readers announce "Show password, toggle button, pressed/not pressed".
  • Pressing the trigger with a pointer keeps focus and the caret in the input.
  • Can be controlled or uncontrolled, for both the value and the visibility.
  • Renders type="password" on the server, so the password is never shown before hydration.

Anatomy

The password input consists of:

  • PasswordInput: The root container for the password input.
  • PasswordInput.Label: The label that gives the user information on the password input.
  • PasswordInput.Input: The native HTML input of the password input.
  • PasswordInput.VisibilityTrigger: The button that toggles the visibility of the password.
  • PasswordInput.Indicator: Renders different content depending on the visibility, typically an eye icon.
  • PasswordInput.Description: The description that gives the user more information on the password input.
  • PasswordInput.ErrorMessage: The error message that gives the user information about how to fix a validation error on the password input.
<PasswordInput>
<PasswordInput.Label />
<PasswordInput.Input />
<PasswordInput.VisibilityTrigger>
<PasswordInput.Indicator />
</PasswordInput.VisibilityTrigger>
<PasswordInput.Description />
<PasswordInput.ErrorMessage />
</PasswordInput>

Example

export function BasicExample() {
return (
<PasswordInput class={style["password-input"]}>
<PasswordInput.Label class={style["password-input__label"]}>
Password
</PasswordInput.Label>
<div class={style["password-input__control"]}>
<PasswordInput.Input class={style["password-input__input"]} />
<PasswordInput.VisibilityTrigger
class={style["password-input__trigger"]}
>
<PasswordInput.Indicator fallback={<EyeOffIcon />}>
<EyeIcon />
</PasswordInput.Indicator>
</PasswordInput.VisibilityTrigger>
</div>
</PasswordInput>
);
.password-input {
display: flex;
flex-direction: column;
gap: 4px;
}
.password-input__label {
color: hsl(240 6% 10%);
font-size: 14px;
font-weight: 500;
user-select: none;
}
.password-input__control {
position: relative;
display: inline-flex;
width: 240px;
}
.password-input__input {
width: 100%;
border-radius: 6px;
padding: 6px 40px 6px 12px;
font-size: 16px;
outline: none;
background-color: white;
border: 1px solid hsl(240 6% 90%);
color: hsl(240 4% 16%);
transition:
border-color 250ms,
color 250ms;
}
.password-input__input:hover {
border-color: hsl(240 5% 65%);
}
.password-input__input:focus-visible {
outline: 2px solid hsl(200 98% 39%);
outline-offset: 2px;
}
.password-input__input[data-invalid] {
border-color: hsl(0 72% 51%);
color: hsl(0 72% 51%);
}
.password-input__input[data-disabled] {
opacity: 0.5;
}
.password-input__trigger {
position: absolute;
top: 50%;
right: 6px;
transform: translateY(-50%);
display: inline-flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
border: none;
border-radius: 4px;
background: transparent;
color: hsl(240 4% 46%);
cursor: pointer;
}
.password-input__trigger:hover {
color: hsl(240 6% 10%);
background-color: hsl(240 5% 96%);
}
.password-input__trigger:focus-visible {
outline: 2px solid hsl(200 98% 39%);
outline-offset: 1px;
}
.password-input__trigger[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
.password-input__description {
color: hsl(240 5% 26%);
font-size: 12px;
user-select: none;
}
.password-input__error-message {
color: hsl(0 72% 51%);
font-size: 12px;
user-select: none;
}
[data-theme*="dark"] .password-input__label {
color: hsl(240 5% 84%);
}
[data-theme*="dark"] .password-input__input {
background-color: hsl(240 4% 16%);
border: 1px solid hsl(240 5% 34%);
color: hsl(0 100% 100% / 0.9);
}
[data-theme*="dark"] .password-input__input:hover {
border-color: hsl(240 4% 46%);
}
[data-theme*="dark"] .password-input__input[data-invalid] {
border-color: hsl(0 72% 51%);
color: hsl(0 72% 51%);
}
[data-theme*="dark"] .password-input__trigger {
color: hsl(240 5% 65%);
}
[data-theme*="dark"] .password-input__trigger:hover {
color: hsl(0 100% 100% / 0.9);
background-color: hsl(240 5% 26%);
}
[data-theme*="dark"] .password-input__description {
color: hsl(240 5% 65%);
}

EyeIcon and EyeOffIcon stand for any icon components.

Usage

Controlled visibility

The visible prop controls whether the password is shown. Use it with onVisibleChange to keep your own state in sync. defaultVisible sets the initial visibility when the component is uncontrolled.

The password is hidden.

import { PasswordInput } from "@kobalte/core/password-input";
import { createSignal } from "solid-js";
function ControlledExample() {
const [visible, setVisible] = createSignal(false);
return (
<>
<PasswordInput visible={visible()} onVisibleChange={setVisible}>
<PasswordInput.Label>Password</PasswordInput.Label>
<PasswordInput.Input />
<PasswordInput.VisibilityTrigger>
<PasswordInput.Indicator fallback={<EyeOffIcon />}>
<EyeIcon />
</PasswordInput.Indicator>
</PasswordInput.VisibilityTrigger>
</PasswordInput>
<p>The password is {visible() ? "visible" : "hidden"}.</p>
</>
);
}

New password

PasswordInput.Input defaults to autocomplete="current-password". On sign-up or password change forms, set autocomplete="new-password" so password managers offer to generate one.

Use at least 12 characters.
<PasswordInput>
<PasswordInput.Label>New password</PasswordInput.Label>
<PasswordInput.Input autocomplete="new-password" />
<PasswordInput.VisibilityTrigger>
<PasswordInput.Indicator fallback={<EyeOffIcon />}>
<EyeIcon />
</PasswordInput.Indicator>
</PasswordInput.VisibilityTrigger>
<PasswordInput.Description>Use at least 12 characters.</PasswordInput.Description>
</PasswordInput>

Error message

The PasswordInput.ErrorMessage component shows an error message when the password input is invalid.

import { PasswordInput } from "@kobalte/core/password-input";
import { createSignal } from "solid-js";
function ErrorMessageExample() {
const [value, setValue] = createSignal("1234");
return (
<PasswordInput
value={value()}
onChange={setValue}
validationState={value().length < 8 ? "invalid" : "valid"}
>
<PasswordInput.Label>Password</PasswordInput.Label>
<PasswordInput.Input />
<PasswordInput.VisibilityTrigger>
<PasswordInput.Indicator fallback={<EyeOffIcon />}>
<EyeIcon />
</PasswordInput.Indicator>
</PasswordInput.VisibilityTrigger>
<PasswordInput.ErrorMessage>
Your password must be at least 8 characters long.
</PasswordInput.ErrorMessage>
</PasswordInput>
);
}

Disabled

A disabled password input also disables its visibility trigger.

API Reference

PasswordInput

PasswordInput is equivalent to the Root import from @kobalte/core/password-input.

PropDescription
visibleboolean
Whether the password is shown as plain text (controlled).
defaultVisibleboolean
Whether the password is shown as plain text when initially rendered. Useful when you do not need to control the visibility.
onVisibleChange(visible: boolean) => void
Event handler called when the visibility of the password changes.
translationsPasswordInputIntlTranslations
Localization strings.
valuestring
The controlled value of the password input.
defaultValuestring
The default value when initially rendered. Useful when you do not need to control the value.
onChange(value: string) => void
Event handler called when the value of the password input changes.
namestring
The name of the password input, used when submitting an HTML form. See MDN.
validationState'valid' | 'invalid'
Whether the password input should display its "valid" or "invalid" visual styling.
requiredboolean
Whether the user must fill the password input before the owning form can be submitted.
disabledboolean
Whether the password input is disabled. Also disables the visibility trigger.
readOnlyboolean
Whether the password input can be selected but not changed by the user.
Data attributeDescription
data-visiblePresent when the password is shown as plain text.
data-validPresent when the password input is valid according to the validation rules.
data-invalidPresent when the password input is invalid according to the validation rules.
data-requiredPresent when the user must fill the password input before the owning form can be submitted.
data-disabledPresent when the password input is disabled.
data-readonlyPresent when the password input is read only.

PasswordInput.Label, PasswordInput.Input, PasswordInput.Description and PasswordInput.ErrorMessage share the same validation data-attributes. PasswordInput.Input, PasswordInput.VisibilityTrigger and PasswordInput.Indicator also get data-visible.

PasswordInput.Input

Renders type="password" while hidden and type="text" while visible. Any prop can be overridden.

PropDescription
autocompletestring
Defaults to "current-password". Use "new-password" on sign-up.
spellcheck"true" | "false"
Defaults to "false".
autocapitalizestring
Defaults to "off".

PasswordInput.VisibilityTrigger

Toggles the visibility on click. Its aria-label comes from translations.visibilityTrigger ("Show password") unless you pass your own, and it stays the same in both states: aria-pressed carries the state.

Data attributeDescription
data-visiblePresent when the password is shown as plain text.
data-disabledPresent when the password input is disabled.

PasswordInput.Indicator

PropDescription
childrenJSX.Element
The content rendered while the password is visible.
fallbackJSX.Element
The content rendered while the password is hidden.

The indicator is rendered with aria-hidden="true"; the trigger provides the accessible name.

PasswordInput.ErrorMessage

PropDescription
forceMountboolean
Used to force mounting when more control is needed. Useful when controlling animation with SolidJS animation libraries.

Rendered elements

ComponentDefault rendered element
PasswordInputdiv
PasswordInput.Labellabel
PasswordInput.Inputinput
PasswordInput.VisibilityTriggerbutton
PasswordInput.Indicatorspan
PasswordInput.Descriptiondiv
PasswordInput.ErrorMessagediv

Accessibility

Keyboard Interactions

KeyDescription
TabMoves focus between the input and the visibility trigger.
SpaceWhen focus is on the visibility trigger, toggles the visibility.
EnterWhen focus is on the visibility trigger, toggles the visibility.

Last updated: 12/4/26, 3:58 PM