Skip to contents

Password Field

A password input with a button to reveal or hide its value.

Note: Password Field is currently in preview. Its API may change before it becomes stable.

Usage guidelines

  • Form controls must have an accessible name: It can be created using a <label> element or the Field component. See the forms guide.
  • Preventing form events: preventDefault() on a form’s submit or reset event doesn’t stop the visibility change. Cancel the change in onRevealedChange instead.

Anatomy

Import the component and assemble its parts:

Anatomy

Examples

Animate icon path

Without match, the indicator stays mounted and reflects the visibility through data-revealed. Wrap an icon in it and use data-revealed to transition parts of the icon, such as the eye’s slash.

Animate between icons

With match, each indicator mounts and unmounts as the visibility changes. Use data-starting-style and data-ending-style to animate one icon out while the other animates in.

API reference

Root

Provides the password visibility and the input’s id to the other parts. Doesn’t render its own HTML element.

defaultRevealedbooleanfalse
Description

Whether the password is initially revealed as plain text.

To render a controlled password field, use the revealed prop instead.

Type
Default
false
revealedboolean—
Description

Whether the password is currently revealed as plain text.

To render an uncontrolled password field, use the defaultRevealed prop instead.

Type
onRevealedChangefunction—
Description

Callback fired when the password visibility changes. Besides the toggle, submitting the owning form hides the password ('form-submit'), and resetting it restores the initial visibility ('form-reset'). To prevent either, call eventDetails.cancel(). Calling preventDefault() on the form’s event only cancels what the browser does, such as resetting the values.

Type
disabledbooleanfalse
Description

Whether the component should ignore user interaction. Applies to both the input and the toggle.

Type
Default
false
idstring—
Name
Description

The id of the input element.

Type
childrenReact.ReactNode—
Description

The parts of the password field.

Type

PasswordFieldPreview.Root.PropsHide

Re-Export of Root props as PasswordFieldPreviewRootProps

PasswordFieldPreview.Root.StateHide

PasswordFieldPreview.Root.ChangeEventReasonHide

PasswordFieldPreview.Root.ChangeEventDetailsHide

Group

Groups the password input and its visibility toggle. Renders a <div> element.

classNamestring | function—
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function—
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
renderReactElement | function—
Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
data-revealed

Present when the password is revealed as plain text.

data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state (when wrapped in Field.Root).

data-invalid

Present when the field is in an invalid state (when wrapped in Field.Root).

data-dirty

Present when the field’s value has changed (when wrapped in Field.Root).

data-touched

Present when the field has been touched (when wrapped in Field.Root).

data-filled

Present when the field is filled (when wrapped in Field.Root).

data-focused

Present when the field is focused (when wrapped in Field.Root).

Attribute
Description
data-revealed

Present when the password is revealed as plain text.

data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state (when wrapped in Field.Root).

data-invalid

Present when the field is in an invalid state (when wrapped in Field.Root).

data-dirty

Present when the field’s value has changed (when wrapped in Field.Root).

data-touched

Present when the field has been touched (when wrapped in Field.Root).

data-filled

Present when the field is filled (when wrapped in Field.Root).

data-focused

Present when the field is focused (when wrapped in Field.Root).

PasswordFieldPreview.Group.PropsHide

Re-Export of Group props as PasswordFieldPreviewGroupProps

PasswordFieldPreview.Group.StateHide

Input

The password input. Renders an <input> element. It turns autocomplete off while revealed, so browsers don’t save the password to autofill history.

defaultValueUnion—
Description

The default value of the input. Use when uncontrolled.

Type
valueUnion—
Name
Description

The value of the input. Use when controlled.

Type
onValueChangefunction—
Description

Callback fired when the value changes. Use when controlled.

Type
autoCompleteReact.HTMLInputAutoCompleteAttribute'current-password'
Description

The browser autofill hint for the input. Use 'new-password' on sign-up or change-password forms so browsers and password managers offer to generate one. While the password is revealed, the input renders autocomplete="off" instead.

Type
Default
'current-password'
disabledbooleanfalse
Description

Whether the input should ignore user interaction. Use the disabled prop on PasswordField.Root to disable the toggle as well.

Type
Default
false
classNamestring | function—
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function—
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
renderReactElement | function—
Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
data-revealed

Present when the password is revealed as plain text.

data-disabled

Present when the input is disabled.

data-valid

Present when the input is in a valid state (when wrapped in Field.Root).

data-invalid

Present when the input is in an invalid state (when wrapped in Field.Root).

data-dirty

Present when the input’s value has changed (when wrapped in Field.Root).

data-touched

Present when the input has been touched (when wrapped in Field.Root).

data-filled

Present when the input is filled (when wrapped in Field.Root).

data-focused

Present when the input is focused (when wrapped in Field.Root).

Attribute
Description
data-revealed

Present when the password is revealed as plain text.

data-disabled

Present when the input is disabled.

data-valid

Present when the input is in a valid state (when wrapped in Field.Root).

data-invalid

Present when the input is in an invalid state (when wrapped in Field.Root).

data-dirty

Present when the input’s value has changed (when wrapped in Field.Root).

data-touched

Present when the input has been touched (when wrapped in Field.Root).

data-filled

Present when the input is filled (when wrapped in Field.Root).

data-focused

Present when the input is focused (when wrapped in Field.Root).

PasswordFieldPreview.Input.PropsHide

Re-Export of Input props as PasswordFieldPreviewInputProps

PasswordFieldPreview.Input.StateHide

PasswordFieldPreview.Input.ChangeEventReasonHide

PasswordFieldPreview.Input.ChangeEventDetailsHide

Toggle

A button that toggles the password’s visibility. Renders a <button> element. Label it with text children or aria-label. Its pressed state is announced to screen readers, so don’t change the label when it’s pressed.

nativeButtonbooleantrue
Description

Whether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).

Type
Default
true
disabledbooleanfalse
Description

Whether the toggle should ignore user interaction. Use the disabled prop on PasswordField.Root to disable the input as well.

Type
Default
false
classNamestring | function—
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function—
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
renderReactElement | function—
Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
data-revealed

Present when the password is revealed as plain text.

data-disabled

Present when the toggle is disabled.

Attribute
Description
data-revealed

Present when the password is revealed as plain text.

data-disabled

Present when the toggle is disabled.

PasswordFieldPreview.Toggle.PropsHide

Re-Export of Toggle props as PasswordFieldPreviewToggleProps

PasswordFieldPreview.Toggle.StateHide

ToggleIndicator

Indicates whether the password is revealed or hidden. Renders a <span> element that is hidden from assistive technology.

match'on' | 'off'—
Name
Description

The password state the indicator renders in. Use 'on' for the icon shown while the password is revealed (the toggle is pressed), and 'off' for the icon shown while it is masked. When omitted, the indicator always renders.

Type
classNamestring | function—
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function—
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
renderReactElement | function—
Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
data-revealed

Present when the password is revealed as plain text.

data-disabled

Present when the field is disabled.

data-starting-style

Present when the indicator is animating in.

data-ending-style

Present when the indicator is animating out.

Attribute
Description
data-revealed

Present when the password is revealed as plain text.

data-disabled

Present when the field is disabled.

data-starting-style

Present when the indicator is animating in.

data-ending-style

Present when the indicator is animating out.

PasswordFieldPreview.ToggleIndicator.PropsHide

Re-Export of ToggleIndicator props as PasswordFieldPreviewToggleIndicatorProps

PasswordFieldPreview.ToggleIndicator.StateHide