Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Password Input for Svelte UI SDK

A password field with an eye button to show and hide what was typed, and hints for password managers. For sign-in and sign-up forms in Svelte on UI SDK.

On this page9 sections

A password input is a text field that hides what's typed behind dots, with an eye button at the end that shows the characters and hides them again. Use it wherever someone types a secret: a sign-in form, a sign-up form, a password reset, a field for an API key.

For text that isn't secret, a plain Input is enough. A one-time code sent by text message reads better in an Input OTP, with one box per digit.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Password Input anatomy: label, group, input, trigger, indicator

Usage

<script lang="ts">
import {
  PasswordInput, PasswordInputGroup, PasswordInputInput,
  PasswordInputTrigger,
} from "@/components/ui/password-input";
</script>
<PasswordInput class="w-full max-w-64">
  <PasswordInputGroup>
    <PasswordInputInput placeholder="Enter password" />
    <PasswordInputTrigger />
  </PasswordInputGroup>
</PasswordInput>

Accessibility

The field is a real input of type password while the characters are hidden and text while they show, so screen readers and password managers treat it as a password field. The eye button is a Tab stop that keeps the name Show password and says whether the password shows through aria-pressed. It toggles on Enter and Space.

Clicking the eye leaves focus in the input, and the input turns off spell check, autocorrect and auto-capitalization, so a shown password isn't spell-checked or changed by a phone keyboard. Submitting or resetting the form hides a shown password again. Give the field a visible label: inside a Field, UI SDK ties the FieldLabel to the input.

Tab
Moves focus from the input to the eye button, then on to the next field.
Shift+Tab
Moves focus from the eye button back to the input.
Enter
When focus is on the eye button, shows or hides the password.
Space
When focus is on the eye button, shows or hides the password.

Demos

Auto hide

Pressing the eye shows the password and starts a three-second timer, after which it's masked again whether or not anyone presses the button. The demo controls visible so the timer can set it back, and clears the timer when the password is hidden sooner, so each reveal gets its full three seconds. Use it where a revealed password shouldn't linger, such as a shared screen or a kiosk sign-in.

Preview width

Autocomplete

Two fields: Current password, with autoComplete set to current-password, and New password, with new-password. The first lets a password manager fill a saved password at sign-in. The second tells the browser to offer a generated one on sign-up or a reset, and a change-password form needs both.

Preview width

Controlled

The password lives in your own state, and the line under the field counts its characters as you type. Hold the value when something reacts to it while typing, such as a strength meter or a confirm field that has to match. When you only need it on submit, read it from the form data instead.

Preview width

Controlled visibility

The page keeps visible in its own state and updates it whenever the eye button asks to change it, so the button is no longer the only thing that can mask or reveal the password. Use it when something else must change it too, such as a form reset that hides it again. The Auto hide demo adds a timer to this setup.

Preview width

Custom indicator

A PasswordInputIndicator inside the trigger swaps the eye for a lock while the password is hidden, set as its fallback, and an open lock while it shows. Both icons carry aria-hidden, because the button's label already says what it does. Use your own pair when the product already has an icon for hidden content.

Preview width

Disabled

disabled on PasswordInput keeps the saved password, correct-horse, in the field but stops both typing and the eye button, and the line underneath says the field is disabled. Use it when the password can't be changed from this screen, such as an account that signs in through single sign-on. Say why in that line, since a faded field doesn't explain itself.

Preview width

Field

The input sits in a Field labeled Password, with the rule, at least 8 characters, written underneath where people read it before they submit. UI SDK ties the label to the input through the Field.

Preview width

Invalid

invalid on the Field and on PasswordInput turns the border red and sets aria-invalid on the input, and the line underneath asks for a valid password. Show it after a failed sign-in, or once the value is long enough to judge. Put the reason in that line, such as the rule that failed, since a red border says only that something is wrong.

Preview width

Size lg

size set to lg makes the field 36 pixels tall, with the eye button inside it. Use it when the password is the main control on a page of its own, such as a reset screen, next to other large inputs and buttons.

Preview width

Size md

md is the default size, written out here to compare with the other two. The field takes the height of a default Input Group in your style, so it lines up with the other inputs in a form. Leave size out and you get the same.

Preview width

Size sm

size set to sm brings the field down to 28 pixels, eye button included, for a dialog, a sidebar or a row of small controls. Use it only when the fields around it are small too, or the password field looks out of place beside them.

Preview width

FAQ

How do I show or hide the password from code in a Svelte Password Input?

Bind a boolean with bind:visible on PasswordInput, which the eye button then updates, or pass visible and update it in onVisibilityChange, which receives the state the eye button asks for first. To only start with the password shown, set defaultVisible instead and leave the rest to the button.

The Controlled visibility demo holds it in state, and Auto hide adds a timer.

How do I get the value of a Password Input in Svelte?

Bind the password with bind:value on PasswordInput, which updates on every keystroke, or pass onValueChange, which receives the new password first. PasswordInputInput also takes value with an oninput handler that reads the event's target, as the Controlled demo does.

In a form, give PasswordInputInput a name and read the password from the submitted form data instead, which keeps it out of your component state.

How do I make password managers fill or generate a password in the Password Input?

Set autoComplete on PasswordInput. UI SDK uses current-password when you leave it out, which lets a password manager fill a saved password at sign-in, and new-password on a sign-up or reset form lets the browser offer a generated one. For a secret that isn't a login, such as an API key, set it to off and add the managers' own opt-out attributes to PasswordInputInput, such as data-1p-ignore for 1Password.

The Autocomplete demo has one of each.

Can a Password Input hide the password again after a few seconds?

Yes, by controlling visible: when it turns on, start a timer that sets it back to hidden. The Auto hide demo does this with three seconds, so a revealed password doesn't sit on a shared screen.

Clear the timer when the person hides the password themselves or the component unmounts, as the demo does. Otherwise a timer from an earlier reveal can mask a later one before its three seconds are up.

How do I change the eye icons of the Password Input?

Put a PasswordInputIndicator inside PasswordInputTrigger with your two icons. The one in fallback shows while the password is hidden, and the one inside the indicator shows while it's visible, as in the Custom indicator demo with a closed and an open lock.

Mark both icons aria-hidden, since the button's label already says what it does. To change the default eye pair everywhere, edit PasswordInputIndicator in components/ui/password-input, which is your file once installed.

How do I use the Password Input in a form with validation?

Give PasswordInputInput a name, and the password submits with the form like any input. Native attributes such as required go on it too and behave as they do on any input.

Put the field in a Field with its rule as the description, as the Field demo does. When your check fails, set invalid on PasswordInput, which turns the border red and sets aria-invalid on the input, and say what failed under it, as in the Invalid demo.

Is the Password Input accessible to screen reader users?

Yes, once it has a visible label. The input stays a real password input while hidden, and the eye button is a Tab stop named Show password, whose pressed state says whether the password shows.

Inside a Field, UI SDK ties the FieldLabel to the input. Clicking the eye leaves focus in the input, so typing carries on.

How do I translate the Show password label of the Password Input?

Pass aria-label to PasswordInputTrigger, such as Mostrar contraseña, which replaces Show password. The name doesn't change while the password shows, because the button's pressed state says which way it is, so one string is enough. To translate it everywhere, edit that string in PasswordInputTrigger, in components/ui/password-input, which is your file once installed.

Keep the label describing the action, since it's what a screen reader says on the button.

API Reference

PasswordInput

<div> data-slot="password-input"

PropType
value?string

The value, for a controlled field. Two-way with bind:value.

Default
(valueLifted ? liftedValue : undefined)
defaultValue?string

The value it starts with. A form reset returns here.

onValueChange?(value: string, details: ChangeDetails<InputValueChangeReason>) → void

Called when the user changes the value: typing ("input-change"), a Clear part ("clear-press"), a form reset ("reset") or a controller ("imperative"). details.cancel() keeps the value as it was.

Type
(value: string, details: ChangeDetails<InputValueChangeReason>) → void
Parameters

value – string

details – ChangeDetails<InputValueChangeReason>

Returns
void
autoComplete?"current-password" | "new-password" | "off" | (string & {})

What the browser and password managers fill in: "current-password" for signing in, "new-password" for signing up or changing it.

Type
"current-password" | "new-password" | "off" | (string & {})
Default
"current-password"
controller?PasswordInputController

Reads, writes, shows and hides it from anywhere. Create one with createPasswordInputController().

debug?boolean

Logs every state change with its reason to the console, in development builds.

defaultVisible?boolean

The default visibility of the password input.

Default
false
disabled?boolean

Disables the field and the toggle. Inside a Field or a disabled <fieldset>, they follow it.

Default
false
form?string

The id of the form the field belongs to, when it isn't inside it.

invalid?boolean

Whether it's invalid.

Default
false
name?string

The name the password submits under. Inside a Field, the Field's name.

onVisibilityChange?(details: { visible: boolean }) → void

Runs when the password is shown or hidden: by the toggle, or hidden again as its form submits or resets.

Type
(details: { visible: boolean }) → void
Parameters

details – { visible: boolean }

Returns
void
readOnly?boolean

Makes the field read-only. The toggle still shows and hides it.

required?boolean

Whether a password is required to submit the form.

size?"sm" | "md" | "lg"

The size.

Type
"sm" | "md" | "lg"
Default
"md"
visible?boolean

Whether the password input is visible.

render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-capslock?string

Present while Caps Lock is on and the field has focus.

data-disabled?string

Present when disabled, by its own prop, a Field or a <fieldset>.

data-filled?string

Present while the field holds any text.

data-readonly?string

Present when the field is read-only.

data-required?string

Present when a password is required.

data-visible?string

Present while the password is shown, so the field is a text field.

PasswordInputGroup

PropType
debug?boolean

Logs every state change with its reason to the console, in development builds.

render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

PasswordInputInput

<input> data-slot="input-group-control"

PropType
class?string

Merged with the part's classes.

AttributeType
data-capslock?string

Present while Caps Lock is on and the field has focus.

data-disabled?string

Present when disabled, by its own prop, a Field or a <fieldset>.

data-filled?string

Present while the field holds any text.

data-readonly?string

Present when the field is read-only.

data-required?string

Present when a password is required.

data-visible?string

Present while the password is shown, so the field is a text field.

PasswordInputTrigger

<button> data-slot="password-input-toggle"

PropType
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-capslock?string

Present while Caps Lock is on and the field has focus.

data-disabled?string

Present when disabled, by its own prop, a Field or a <fieldset>.

data-filled?string

Present while the field holds any text.

data-readonly?string

Present when the field is read-only.

data-required?string

Present when a password is required.

data-size"icon-xs"
Default
"icon-xs"
data-variant"ghost"
Default
"ghost"
data-visible?string

Present while the password is shown, so the field is a text field.

PasswordInputIndicator

<span> data-slot="password-input-indicator"

PropType
fallback?Snippet

The fallback content to display when the password is not visible.

class?string

Merged with the part's classes.

usePasswordInput