Skip to content

[password field] Create new PasswordField component - #5114

Open
atomiks wants to merge 68 commits into
mui:masterfrom
atomiks:password-toggle-field
Open

atomiks wants to merge 68 commits into
mui:masterfrom
atomiks:password-toggle-field

Conversation

@atomiks

@atomiks atomiks commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Adds a new PasswordField component (in preview) — a headless password input with a button to reveal or hide the value.

Preview: https://deploy-preview-5114--base-ui.netlify.app/react/components/password-field

@atomiks atomiks added the type: new feature Expand the scope of the product to solve a new problem. label Jun 24, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jun 24, 2026 •

Copy link
Copy Markdown

commit: 3698bc7

@code-infra-dashboard

code-infra-dashboard Bot commented Jun 24, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+4.34KB(+0.82%) 🔺+1.28KB(+0.75%)

Details of bundle changes

Performance

No regressions in 36 benchmarks · precise to ±11.2% (±16.4% at worst) · ran 3m 54s · details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Jun 24, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 3698bc7
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6b0aa34a58758ae5eeb2bddd
😎 Deploy Preview https://deploy-preview-5114--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks
atomiks force-pushed the password-toggle-field branch 3 times, most recently from 45b4289 to 0d43992 Compare June 24, 2026 11:08
@atomiks
atomiks marked this pull request as draft June 24, 2026 11:09
@atomiks
atomiks force-pushed the password-toggle-field branch from 0d43992 to 5d2cb2e Compare June 24, 2026 11:17
@atomiks
atomiks marked this pull request as ready for review June 24, 2026 11:26
@atomiks
atomiks force-pushed the password-toggle-field branch 2 times, most recently from 90dd52a to 1be6262 Compare June 25, 2026 10:15
flaviendelangle

This comment was marked as outdated.

@aarongarciah

Copy link
Copy Markdown
Member

I pushed a couple of commits adjusting the icons and the toggle size.

…efault

- Test controlled `visible` + toggle click (notifies without flipping type locally)
- Test `nativeButton={false}` toggle (visibility flip + aria-disabled suppression)
- Test that wrapping a form around the input after mount still hides on reset
- Tighten the change event assertion to `MouseEvent`
- Rename the caret test to "preserves" and note it relies on focus retention
- Document the `autoComplete="current-password"` default and the `new-password` override
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Jul 3, 2026
@atomiks atomiks changed the title [password toggle field] Create new PasswordToggleField component [password field] Create new PasswordField component Jul 10, 2026
@atomiks
atomiks force-pushed the password-toggle-field branch from b399402 to 25ed658 Compare July 23, 2026 03:10
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Jul 23, 2026
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Aug 4, 2026
@jjenzz
jjenzz force-pushed the password-toggle-field branch from e6a9e65 to 9dd9d67 Compare November 27, 2026 17:43
@jjenzz

jjenzz commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Addressing #5114 (comment)

Bugs

  • 1. Canceled reset: fixed in 2b34ca0. A canceled reset now leaves both the value and the visibility as they were. The suggested microtask doesn't work for a real Reset click: microtasks run between listeners there, so it runs before onReset and reads defaultPrevented as false. It only works for a form.reset() call from script. Instead, the capture listener notes the reset, and a bubble listener on the same root, which runs after the form's handlers, restores the visibility only when the reset wasn't canceled. If onReset stops propagation, the reset is decided once the dispatch is over. Tests cover canceling, stopping propagation, both, and a real Reset click in Chromium. (Outdated: reversed in bbbb0db, see this comment.)

Tests

  • 1: added in a1bcb94. A shared fieldControlConformanceTests suite runs for Field.Control, Input and PasswordField.Input. It checks the label, the focused, filled, dirty, touched and validity attributes, autoFocus, onValueChange and Field's disabled, and it fails if Input drops its stateAttributesMapping or the control's ref.

Simplifications

  • 1: fixed in 29a6fe3.
  • 2: already done in 952b6ab, which removed data-pressed. data-revealed stays, matching the other parts, and so does aria-pressed.

Docs

  • 1: documented submit masking in the Input's description (38159a5). The reset half is already in onRevealedChange's description. I'm not adding a section or a Field example, because other component pages don't document their form mechanics. (Outdated: replaced in bbbb0db, see this comment.)
  • 2: handled in the component instead (2861a37). The Input carries a class and renders a deduplicated style element that hides ::-ms-reveal for it, as ScrollArea and Select do for their scrollbars, so consumers don't need to know about it. CSPProvider's nonce and disableStyleElements apply. The demos no longer need their own ::-ms-reveal CSS.

🤖 Comment posted by Amp

@jjenzz
jjenzz force-pushed the password-toggle-field branch from 9dd9d67 to 3b90f42 Compare November 27, 2026 17:48
@jjenzz

jjenzz commented Nov 27, 2026

Copy link
Copy Markdown
Member

@claude review

@github-actions

This comment has been minimized.

jjenzz and others added 3 commits November 27, 2026 18:32
Edge and Chromium on Windows add their own reveal button to password
inputs, so a field with a Toggle showed two, and every demo hid it with
`::-ms-reveal` CSS that consumers had to know to copy. The Input now
carries a class for it and renders the deduplicated style element that
targets that class, as ScrollArea and Select do for their scrollbars.
`CSPProvider`'s `nonce` and `disableStyleElements` apply.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a21cb9-530d-7520-b239-4780d6906d4e
Input stopped rendering Field.Control, so Field.Control's tests no longer
covered it, and Input's own tests were only conformance. A shared suite
now checks what Field.Control, Input and PasswordField.Input all get from
`useFieldControl`: the label, the focused, filled, dirty, touched and
validity attributes, `onValueChange` and Field's `disabled`.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a21cb9-530d-7520-b239-4780d6906d4e
The reset listener runs in the capture phase, before a form's `onReset`
can call `preventDefault()`, so a canceled reset kept the typed value but
still restored the initial visibility. The capture listener now only notes
the reset, and a bubble listener on the same root, which runs after the
form's handlers, restores the visibility when the reset wasn't canceled.
If a handler stops the reset's propagation, the bubble listener never
runs, so the reset is decided once the dispatch is over instead.

A microtask can't make this decision: for a real click, microtasks run
between listeners, before `onReset` does.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a21cb9-530d-7520-b239-4780d6906d4e
@jjenzz
jjenzz force-pushed the password-toggle-field branch from 3b90f42 to 2b34ca0 Compare November 27, 2026 18:32
jjenzz and others added 3 commits November 27, 2026 18:33
…mits

The submit masking was only described in the source, so a consumer
controlling `revealed` saw the input switch to `type="password"` during a
submit with no `onRevealedChange` call and nothing in the docs to explain
it. The Input's description now says so.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a21cb9-530d-7520-b239-4780d6906d4e
The Input's and Toggle's comments said their `type` couldn't be
overridden, but a render element's props win over the component's, as
they do for every Base UI part. That's intended, so the comments now say
only a spread `type` is overridden.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a21cb9-530d-7520-b239-4780d6906d4e
The description said masking keeps the password from being submitted as
plain text, but the form data is the same whatever the input's `type`
is. Masking keeps browsers from saving the password to autofill history
and lets password managers recognize it, so the description now says that.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a21cb9-530d-7520-b239-4780d6906d4e
@jjenzz

jjenzz commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Addressing #5114 (comment)

Bugs

  • 1: fixed in 2861a37. The Input now renders the style element alongside the class it uses, so the browser's reveal button is hidden without a Group. The Group no longer reads the CSP context, and a test renders the Input without a Group.
  • 2: kept, with the comments corrected in 15138ad. I want a render element to be able to override type and id, as it can on every other part. Someone who renders <input type="text" /> or <button type="submit" /> has opted out of what the component manages. The comments claimed that couldn't happen, so they now say only a spread type is overridden.
  • 3: not changing. Field.Label renders before the Input, so during server rendering nothing the Input resolves can reach it. Seeding the id during render doesn't change that, because the Root and the label have already rendered. Setting id on the Root, rather than on the render element, gives a correct label and aria-controls in server markup.
  • 4: not changing. To omit aria-controls until an Input mounts, I'd need mount state, and then server markup would never include aria-controls. That cost reaches every field, while the case it fixes is a Root without an Input.

Tests

  • 1: not adding. Since a render element's type is meant to win (Bug 2), there's no guard to test. That precedence belongs to useRenderElement, not this component.

Docs

  • 1: fixed in d634e7c. The description now gives the actual reason for masking: browsers don't save the password to autofill history, and password managers still recognize it. (Outdated: replaced in bbbb0db, see this comment.)
  • 2: not changing. The Toggle's own description, which appears further down the page in its API reference, already says to label it with text children or aria-label and not to change the label when it's pressed.

🤖 Comment posted by Amp

jjenzz
jjenzz previously approved these changes Nov 27, 2026
@michaldudak

This comment has been minimized.

@jjenzz
jjenzz dismissed their stale review November 27, 2026 20:53

the type change approach keeps coming up in reviews. i'm gonna ask agent to step back and find a simpler approach, and make sure it's testing real autofill etc.

The guide for `disableStyleElements` only covered ScrollArea and Select,
so following it left Edge's native password reveal button visible next to
PasswordField's Toggle. It now lists `<PasswordField.Input>` and gives the
rule that hides the button.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a21e9c-5c30-766a-b5d2-8df31fe830cb
@jjenzz

jjenzz commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Addressing #5114 (comment)

This also replaces how the Input handles submit, so the earlier comments about submit masking are struck through and point here.

Bugs

  • 1. Reset canceled from window: fixed in bbbb0db. The bubble listener is gone. Submits and resets are applied in the next task, in the order they fired, once every listener has had the chance to cancel a reset. So the visibility now changes one task after the value, and form.reset() followed by form.requestSubmit() reports 'form-reset' then 'form-submit' and ends hidden. (Outdated: reversed in bbbb0db, see this comment.) A native cancel no longer keeps the visibility; cancel 'form-reset' instead.
  • 2. Render element that mounts its input later: fixed in bbbb0db. The listeners are added from a callback ref that returns its cleanup. The merged ref from useMergedRefs runs that cleanup itself when the node changes, so it doesn't rely on React 19's ref cleanup.

Tests

  • 1: fixed in bbbb0db. The test now checks the display:none declaration.

Simplifications

  • 1: fixed in bbbb0db. All the refs go in useRenderElement's ref array.

Docs

  • 1: fixed in e8dfaae. The CSP guide lists <PasswordField.Input> and the ::-ms-reveal rule.
  • 2: fixed as a behaviour change instead of a note (bbbb0db). A revealed password must stay out of autofill history however the form is submitted, so I replaced submit masking. See below.

How a revealed password is kept out of autofill history

I tested in real Chrome 159, Edge 158, Firefox 161 and Safari 26.4 profiles, typing while revealed and checking the browsers' stored autofill data and save prompts. Switching the input to type="password" on submit didn't work:

  • Safari reads the form before submit fires, so it stored the password as plain text and didn't offer to save it, even on a submit button.
  • form.submit() fires no submit event, so nothing masked it in any browser.
  • Chrome stored a field that started revealed even when it was masked on submit.

The approach:

  • While revealed, the Input renders autocomplete="off". That kept the value out of autofill history in all of them, on a submit button, Enter, requestSubmit() and form.submit(). The oldest supported versions behaved the same with the final approach: no autofill history, and a save offer on every path, both when the field starts revealed and when it's revealed with the toggle. That's Safari 16.5, Chromium 111, Chrome 113, Edge 114 (the oldest Microsoft still publishes) and Firefox 113.
  • The Input mounts as type="password", on the server and on its first render, and switches to the visibility once mounted. Safari, Chrome and Firefox all remember that a field has been a password field and keep treating it as one after its type changes, so they still offer to save a password typed while it's revealed, even with autocomplete off. A field that has only ever been text gets no save offer. Sources: WebKit's m_hasEverBeenPasswordField, Blink's FormControlTypeForAutofill() (which reports a field that HasBeenPasswordField() as a password field), and Firefox's password manager, which finds password fields by hasBeenTypePassword. In Safari, this also gets the save offer straight away when an app submits with preventDefault() and then removes the form.

Since the form data doesn't depend on the input's type when the form is read, hiding on submit is only tidy-up (bbbb0db). Every submit attempt from the owning form hides the password through onRevealedChange with a cancelable 'form-submit' reason, including one a handler prevents, since most React forms submit with preventDefault(). An uncontrolled field updates its own state. form.submit() fires no submit event, so it doesn't hide the password, but the password still stays out of autofill history.

A field that starts revealed isn't filled with a saved password. I'm treating that as intended, so a saved password doesn't appear on screen without the user asking for it.

The forms experiment (a023f65) covers these cases by hand: each submit path, a prevented submit, a canceled hide, reset, and the autofill history check.


🤖 Comment posted by Amp

@jjenzz
jjenzz force-pushed the password-toggle-field branch 6 times, most recently from 8a850d1 to d5c0cd4 Compare November 28, 2026 09:32
@jjenzz

jjenzz commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Decision: how the form-submit and form-reset visibility changes are canceled (bbbb0db)

Submitting the owning form hides the password ('form-submit'), and resetting it restores the initial visibility ('form-reset'). Both are Base UI behaviour, so they're canceled the way every Base UI change is: eventDetails.cancel() in onRevealedChange, as the customization handbook documents.

preventDefault() on the form's submit or reset event cancels only what the browser does: submitting the form, or resetting its values. It doesn't change the visibility. So a prevented native reset keeps the values and still restores the visibility, unless 'form-reset' is canceled too.

<PasswordField.Root
  onRevealedChange={(revealed, eventDetails) => {
    if (eventDetails.reason === 'form-reset') {
      eventDetails.cancel();
    }
  }}
>

This goes back to what this reply said about michaldudak's Bug 5, and reverses the later fixes that made a native cancel also keep the visibility (Bug 1 here, and Bug 1 in round 4). Here's why:

  • It's consistent with the library. It uses the documented APIs for canceling Base UI behaviour, cancel() and preventBaseUIHandler(), and leaves preventDefault() for the browser's own behaviour. preventBaseUIHandler() can't apply here, because the reset event belongs to the consumer's <form>, not to a PasswordField part.
  • It's the simplest approach. Honouring a native cancel meant waiting until defaultPrevented was final: after the consumer's onReset, and after any window listener. That needed a deferred task, and then the submit had to be deferred too to keep the order. It added a queue, a timeout and special cases for a rare case.
  • Event order stays correct and synchronous. Both changes are decided when the event fires, in the order the events fire, before the consumer's handlers run. With the deferral, onRevealedChange ran a task later, after the consumer's handlers. form.reset() followed by form.requestSubmit() reports 'form-reset' then 'form-submit' and ends hidden.
  • There's one way to cancel our behaviour, for both events. atomiks' version honoured a native preventDefault() on reset but not on submit, since almost every React form prevents its submit. So the same call did different things on the two events. Now eventDetails.cancel() is the only way to stop either visibility change, and preventDefault() only ever cancels what the browser does, which is what native cancelling generally means.

The Root's onRevealedChange description says this, and a usage guideline on the page links the handbook's section on canceling a Base UI event.

The earlier comments that described the deferred approach are struck through and point here.


🤖 Comment posted by Amp

@jjenzz
jjenzz force-pushed the password-toggle-field branch from d5c0cd4 to 10ccd72 Compare November 28, 2026 09:33
jjenzz and others added 3 commits November 28, 2026 09:47
Masking the input on `submit` didn't keep a revealed password out of
autofill history. Safari reads the form before `submit` fires, and
`form.submit()` fires no `submit` event at all, so on those paths the
password was saved as plain text to autofill history, and no browser
offered to save it to its password manager.

While revealed, the Input now renders `autocomplete="off"`, which keeps
the value out of autofill history in Chrome, Edge, Firefox and Safari on
every submission path.

The Input also renders `type="password"` on the server and on its first
render, and switches to the visibility once it has mounted. WebKit,
Blink and Gecko all remember that a field has been a password field
and keep treating it as one after its type changes, so they still offer
to save a password typed while it's revealed, even with `autocomplete`
off. A field that has only ever been text gets no save offer. Mounting
as a password keeps those behaviours for a field that starts revealed:
- WebKit sets `m_hasEverBeenPasswordField` once an input has been a
  password field:
  https://github.com/WebKit/WebKit/blob/main/Source/WebCore/html/HTMLInputElement.cpp
- Blink's `FormControlTypeForAutofill()` reports a text field that
  `HasBeenPasswordField()` as a password field:
  https://chromium.googlesource.com/chromium/src/+/main/third_party/blink/renderer/core/html/forms/html_form_control_element.cc
- Firefox's password manager finds password fields by
  `hasBeenTypePassword`:
  https://searchfox.org/firefox-main/source/toolkit/components/passwordmgr/LoginManagerChild.sys.mjs

Since the form data doesn't depend on the input's type, hiding on submit
is only tidy-up. Every submit attempt hides the password through
`onRevealedChange` with a cancelable `'form-submit'` reason, including one
a handler prevents, since most React forms submit with `preventDefault()`.

Submits and resets are handled synchronously, in the order they fire. They
are Base UI behaviour, so they're canceled the documented way, with
`eventDetails.cancel()` for the `'form-submit'` or `'form-reset'` reason.
`preventDefault()` on the form's event only cancels what the browser does,
such as resetting the values, as it does elsewhere in the library. Waiting
for `defaultPrevented` to be final needed a deferred task, which fired the
changes after the consumer's handlers and out of order with them.
`form.reset()` followed by `form.requestSubmit()` reports both changes and
ends hidden: the last accepted change is kept until the script finishes, so
the submit isn't compared with the stale rendered value.

A usage guideline on the page says so, and links the handbook's section
on canceling a Base UI event.

Also from the review:
- The listeners are added from a callback ref, so a render element that
  mounts its `<input>` later is covered.
- The refs are merged once, in `useRenderElement`.
- The reveal-button test checks the `display:none` declaration.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a21e9c-5c30-766a-b5d2-8df31fe830cb
Exercises the field in a form: hiding on submit through each submission
path, restoring on reset, canceling either change, and reading the form
data while typing. A log shows each `onRevealedChange` call and what the
browser sees when it builds the form data, and a separate text field with
the same name checks whether a password reached autofill history.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a21e9c-5c30-766a-b5d2-8df31fe830cb
Safari treats a form posting to `about:blank` as insecure, so in frame
mode it asked for a username instead of reading it from the form. Every
target now posts back to the page, and the frame is hidden, since it
showed a white box in dark mode.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a21e9c-5c30-766a-b5d2-8df31fe830cb
@jjenzz
jjenzz force-pushed the password-toggle-field branch from 10ccd72 to 3698bc7 Compare November 28, 2026 09:47

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: password field Changes related to the password field component. type: new feature Expand the scope of the product to solve a new problem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants