API: components
PasswordInput
A password field whose text a toggle button shows and hides, keeping the focus and the caret, as a form control, in each framework.
A password text field with a button that shows and hides what was typed: a sign-in form, a new
password, a recovery key. The Input is a native <input> whose type switches between password
and text; the VisibilityTrigger is the APG's toggle button
(button pattern). It is a form control: the
Input carries the name, submits, resets and validates, alone or in a Field.
The APG has no password pattern and Base UI no PasswordInput: the rest follows Zag/Ark's
PasswordInput and Radix's PasswordToggleField. It does not measure the password's strength (a
Meter or styled UI).
Behaviour
- The trigger is a toggle button:
aria-pressedtells whether the text is shown andaria-controlsnames the Input; its name is the app's and never changes ("Show password"). - Focus and caret: a pointer press on the trigger does not take the focus from the Input (its
mousedown's default is prevented), and the Input's selection is put back after itstypechanges, so the user goes on typing where they were; a keyboard press keeps the focus on the trigger. - Forms: the Input is the form control (no hidden input). A form submission hides the text
again before the browser reads the form (a browser must not remember it as plain text); a reset
returns the text and the visibility to their defaults (reason
none). - Read-only: the trigger still shows and hides the text; disabled: the Input and the trigger are disabled.
- In a Field: the Input is the labelable and the validated element
(
aria-labelledby,aria-describedby,aria-invalid);validategets the text. - Reasons:
input-change(typing),trigger-press(the trigger),none(a submission or a reset).
Anatomy
| part | element | data-primitives-part | role |
|---|---|---|---|
| Root | div | password-input-root | none; holds the text and the visibility |
| Input | input | password-input-input | type="password" (no role), or a textbox while shown |
| VisibilityTrigger | button | password-input-visibility-trigger | a toggle button |
Usage
import { PasswordInput } from "@fragiola/primitives-react/password-input";
<label htmlFor="password">Password</label>
<PasswordInput.Root id="password" name="password" required>
<PasswordInput.Input autoComplete="current-password" />
<PasswordInput.VisibilityTrigger aria-label="Show password">
<EyeIcon />
</PasswordInput.VisibilityTrigger>
</PasswordInput.Root>;Props
Every part also takes what the primitive contract gives:
render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which
it forwards (the Input: autoComplete, minLength, placeholder). The names below are React's;
Vue binds them the same way (v-model:value, v-model:visible, @value-change,
@visible-change), and Angular as inputs and outputs ([(value)], [(visible)],
(valueChangeDetails) and (visibleChangeDetails), each with { value, details } or
{ visible, details }).
Root
| prop | type | default | description |
|---|---|---|---|
value | string | the text, controlled | |
defaultValue | string | "" | the initial text, uncontrolled |
onValueChange | (value: string, details: { reason }) => void | every change of the text | |
visible | boolean | whether the text is shown, controlled | |
defaultVisible | boolean | false | the initial visibility, uncontrolled |
onVisibleChange | (visible: boolean, details: { reason }) => void | every change of the visibility | |
disabled | boolean | false | disables the Input and the trigger |
readOnly | boolean | false | the Input is read-only; the trigger still works |
required | boolean | false | the Input is required |
name | string | the Input's name (a Field's wins) | |
id | string | the Input's id (generated when not given) |
Input
No props of its own.
VisibilityTrigger
No props of its own.
State and part attributes
| part | attributes |
|---|---|
| every part | data-visible while shown, data-disabled, data-readonly, data-required |
| Root | in a Field, the field's states |
| Input | type ("password" or "text"), autocapitalize="off", spellcheck="false", disabled, readonly, required; in a Field, aria-labelledby, aria-describedby, aria-invalid |
| VisibilityTrigger | type="button", aria-pressed, aria-controls (the Input's id), disabled |
Keyboard
| key | action |
|---|---|
| Tab | the Input, then the trigger |
| Enter, Space (on the trigger) | shows or hides the text; the focus stays on the trigger |
| Enter (in the Input) | the browser's: submits the form |
Exports
| entry | exports |
|---|---|
@fragiola/primitives-react/password-input | ChangeDetails, ChangeReason, PasswordInput, PasswordInputInputProps, PasswordInputRootProps, PasswordInputState, PasswordInputVisibilityTriggerProps, RenderProp |
@fragiola/primitives-vue/password-input | ChangeDetails, ChangeReason, PasswordInput, PasswordInputRootEmits, PasswordInputRootProps, PasswordInputState, RenderSlotProps |
@fragiola/primitives-angular/password-input | ChangeDetails, ChangeReason, PasswordInputInput, PasswordInputRoot, PasswordInputState, PasswordInputVisibilityTrigger, PasswordInputVisibleChangeEvent, ValueChangeEvent |