Skip to main content

Function: useAutofillSubmit()

useAutofillSubmit<TName>(formRef, options): UseAutofillSubmitResult

Defined in: use-autofill-submit.ts:156

Submits a form as soon as a password manager fills it, so choosing a saved login is all it takes to sign in.

A fill is told apart from typing by where the keyboard focus was: a field whose value changed while it was not the focused element was written by something other than the user's hands. Pasting into the focused field is deliberately not treated as a fill. Values are sampled on an interval, because some providers (notably iOS AutoFill) write to fields without any event a page can rely on.

Safeguards keep it from firing when the user has not asked for it:

  • A real gesture must reach the form first (see requireInteraction).
  • It arms once. Any submit, this one included, disarms it, so a fill racing a tap on the button cannot send the credentials twice. Call rearm() (or reset the form) to allow another attempt, e.g. after a failed sign-in.

Fields are looked up by name on every sample, so conditionally rendered fields work. A name that matches nothing, or a radio group, pauses detection until it resolves to a single input, select or textarea. The form ref must already be populated when the hook's effect runs.

The default submit uses requestSubmit() (iOS 16+); on older browsers it falls back to dispatching a cancelable submit event and then form.submit().

Type Parameters​

TName​

TName extends string

Parameters​

formRef​

RefObject<HTMLFormElement | null>

Ref to the <form> to watch and submit.

options​

UseAutofillSubmitOptions<TName>

See UseAutofillSubmitOptions.

Returns​

UseAutofillSubmitResult

Imperative rearm, disarm and isArmed controls.

Example​

const formRef = useRef<HTMLFormElement>(null);
const { rearm } = useAutofillSubmit(formRef, {
fields: ["email", "password"],
onAutofill: ({ values }) => values.email.includes("@"),
});

return (
<form ref={formRef} onSubmit={handleSubmit}>
<input name="email" autoComplete="username" />
<input name="password" type="password" autoComplete="current-password" />
<button>Sign in</button>
</form>
);

For a live, editable example, see the useAutofillSubmit docs page.