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 theFieldcomponent. See the forms guide. - Preventing form events:
preventDefault()on a form’ssubmitorresetevent doesn’t stop the visibility change. Cancel the change inonRevealedChangeinstead.
Anatomy
Import the component and assemble its parts:
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
- Name
- Description
Whether the password is initially revealed as plain text.
To render a controlled password field, use the
revealedprop instead.- Type
- Default
false
revealedboolean—
- Name
- Description
Whether the password is currently revealed as plain text.
To render an uncontrolled password field, use the
defaultRevealedprop instead.- Type
onRevealedChangefunction—
- Name
- 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, calleventDetails.cancel(). CallingpreventDefault()on the form’s event only cancels what the browser does, such as resetting the values.- Type
disabledbooleanfalse
- Name
- 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—
- Name
- Description
The parts of the password field.
- Type
PasswordFieldPreview.Root.StateHide
PasswordFieldPreview.Root.ChangeEventReasonHide
PasswordFieldPreview.Root.ChangeEventDetailsHide
Group
Groups the password input and its visibility toggle.
Renders a <div> element.
classNamestring | function—
- Name
- 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
ReactElementor 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.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—
- Name
- Description
The default value of the input. Use when uncontrolled.
- Type
valueUnion—
- Name
- Description
The value of the input. Use when controlled.
- Type
onValueChangefunction—
- Name
- Description
Callback fired when the
valuechanges. Use when controlled.- Type
autoCompleteReact.HTMLInputAutoCompleteAttribute'current-password'
- Name
- 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 rendersautocomplete="off"instead.- Type
- Default
'current-password'
disabledbooleanfalse
- Name
- Description
Whether the input should ignore user interaction. Use the
disabledprop onPasswordField.Rootto disable the toggle as well.- Type
- Default
false
classNamestring | function—
- Name
- 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
ReactElementor 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.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
- Name
- Description
Whether the component renders a native
<button>element when replacing it via therenderprop. Set tofalseif the rendered element is not a button (for example,<div>).- Type
- Default
true
disabledbooleanfalse
- Name
- Description
Whether the toggle should ignore user interaction. Use the
disabledprop onPasswordField.Rootto disable the input as well.- Type
- Default
false
classNamestring | function—
- Name
- 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
ReactElementor 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.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—
- Name
- 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
ReactElementor 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