InputsReact
A password field with a button that shows the password as text and hides it again.
Rendered by React 19.3.1
Loading React…Installation
npm i @hamur/reactNot on npm yet. The package goes out under the chosen name.
Anatomy
Import the component and assemble its parts:
<PasswordInput.Root>
<PasswordInput.Input />
<PasswordInput.Toggle>
<PasswordInput.Indicator />
</PasswordInput.Toggle>
</PasswordInput.Root>Examples
Controlled
A controlled visible on the Root, changed by the Toggle and by a checkbox beside the field; the state is shown below.
New Password
A sign-up Form: autoComplete="new-password", required and a minimum length, a Meter and Field.Description for the strength, Field.Error on submit; sending hides the password.
API reference
12 props across 4 parts, in React syntax. className, style, render are listed once, under Shared props.
IndicatorPasswordInput.Indicator
The icon inside the toggle, always mounted and hidden from assistive technology; swap an eye and an eye-off with CSS on data-visible. Renders a <span>.
| Data attribute | Description |
|---|---|
data-visible | Present while the password is shown as text. |
InputPasswordInput.Input
The password field, a native input that works with Field as Input does. Its type is password, or text while the password is shown. Renders an <input>.
| Prop | Type | Default |
|---|---|---|
onValueChangeCallback fired when the value changes. | ((value: string, eventDetails: { reason: "none"; event: Event; cancel: () => void; allowPropagation: () => void; isCanceled: boolean; isPropagationAllowed: boolean; trigger: Element | undefined; }) => void) | — |
defaultValueThe default value of the input. | string | number | readonly string[] | — |
valueThe value of the input. | string | number | readonly string[] | — |
autoCompleteThe autocomplete token list. | string | 'current-password' |
autoCapitalizeThe autocapitalize hint for virtual keyboards. | string | 'off' |
spellCheckWhether the browser checks the spelling of the value. | Booleanish | false |
| Data attribute | Description |
|---|---|
data-visible | Present while the password is shown as 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-touched | Present when the input has been touched (when wrapped in Field.Root). |
data-dirty | Present when the input's value has changed (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). |
RootPasswordInput.Root
Groups the input and the toggle that shows or hides the password, and holds whether it is shown. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
visibleWhether the password is shown as text. | boolean | — |
defaultVisibleWhether the password is initially shown as text. | boolean | false |
onVisibleChangeCallback fired before the visibility changes: on a toggle press, and when the form is | ((visible: boolean, eventDetails: PasswordInputRootChangeEventDetails) => void) | — |
disabledWhether the input and the toggle should ignore user interaction. | boolean | false |
labelsThe texts of the password input: toggle, the toggle's name when it has no aria-label or | Partial<PasswordInputLabels> | — |
| Data attribute | Description |
|---|---|
data-visible | Present while the password is shown as text. |
data-disabled | Present when the password input is disabled. |
TogglePasswordInput.Toggle
A button that shows or hides the password. Its name stays "Show password" and aria-pressed states whether the password is shown. Renders a <button>.
| Prop | Type | Default |
|---|---|---|
nativeButtonWhether the component renders a native <button> element when replacing it | boolean | true |
| Data attribute | Description |
|---|---|
data-visible | Present while the password is shown as text. |
data-disabled | Present when the password input is disabled. |
Shared props
Every part that renders an element takes these.
| Prop | Type |
|---|---|
classNameCSS class applied to the element, or a function that
returns a class based on the component's state. | string | ((state: State) => string | undefined) |
styleStyle applied to the element, or a function that
returns a style object based on the component's state. | React.CSSProperties | ((state: State) => React.CSSProperties | undefined) |
renderAllows you to replace the component's HTML element
with a different tag, or compose it with another component. | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State> |
Keyboard and accessibility
Keyboard
| Keys | Does | Part |
|---|---|---|
| Tab | Moves from the field to the button, which is always a Tab stop | Toggle |
| EnterSpace | Shows or hides the password and leaves focus on the button | Toggle |
| Enter | In the field, submits its form, which hides the password before sending | Input |
Accessibility
| Part | Sets | So that |
|---|---|---|
| Input | type="password" or "text", autocomplete, autocapitalize="off", spellcheck="false"; no name of its own | the field is named by its Field.Label, password managers fill it, and the text is not sent to a spell checker |
| Toggle | a button of type button with aria-pressed, aria-controls the Input's id, and the fixed name "Show password" (labels) | readers say "Show password, toggle button, pressed" or "not pressed"; the name does not flip with the state |
| Indicator | aria-hidden="true", always mounted | the icons are not read beside the button's name |
| Root | a div with data-visible and data-disabled; disabled disables the Input and the Toggle | the field and its button cannot disagree; a read-only Input still lets the password be shown |
Focus order
- The field, then the button: the Toggle is always in the Tab order and nothing takes it out.
- A pointer press on the button keeps focus in the field, with its caret and selection, and focuses the field when it was not focused; a key press leaves focus on the button.
- Nothing takes focus on mount or when the password is hidden by a submit or a reset.