Sign-in form (/mini)
A small login form comparing @lunarhue/forms-react/mini with TanStack Form.
Here is a two-field sign-in form built once with @lunarhue/forms-react/mini and once with TanStack Form. Both render identical markup and talk to the same mock API; switch between builds in the preview below to compare them.
Bundle size
| Build | @lunarhue/forms is | @lunarhue/forms 0.2.0 | TanStack Form 1.33.5 |
|---|---|---|---|
| Forms + app | 13.6 kB smaller (78%) | 3.7 kB | 17.4 kB |
| Total | 13.2 kB smaller (16%) | 71.4 kB | 84.6 kB |
| App only | 154 B smaller (11%) | 1.2 kB | 1.3 kB |
import { createForms, type FieldApi, fieldValidators, inputProps, useForm } from "@lunarhue/forms-react/mini";
import { signIn } from "../../shared/server.ts";
import { FieldShell, Page } from "../../shared/ui.tsx";
interface TextFieldProps {
readonly field: FieldApi<string>;
readonly label: string;
readonly type?: "text" | "email" | "password";
}
function TextField({ field, label, type = "text" }: TextFieldProps) {
return (
<FieldShell id={field.id} errorId={field.errorId} label={label} errors={field.shownErrors.map((error) => error.message)}>
<input {...inputProps(field, { type })} />
</FieldShell>
);
}
const forms = createForms({ fields: { TextField } });
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const loginForm = forms.define({
defaultValues: { email: "", password: "" },
validate: {
fields: fieldValidators({
email: (email) => (emailPattern.test(email) ? undefined : { message: "Enter a valid email." }),
password: (password) => (password.length >= 8 ? undefined : { message: "Use at least 8 characters." }),
}),
},
onSubmit: async (value, { store }) => {
const response = await signIn(value);
if (!response.ok) {
store.setErrors({ fields: Object.fromEntries(Object.entries(response.errors).map(([name, message]) => [name, { message }])) });
return undefined;
}
return `Signed in as ${value.email}.`;
},
});
export function App() {
const form = useForm(loginForm);
return (
<Page title="Sign in" lede="Using @lunarhue/forms-react/mini: sync field validation and server errors.">
<form {...form.formProps}>
<form.TextField name="email" label="Email" type="email" />
<form.TextField name="password" label="Password" type="password" />
<form.State select={(state) => state.submitting}>
{(submitting) => (
<div className="actions">
<button type="submit" disabled={submitting}>
{submitting ? "Signing in..." : "Sign in"}
</button>
</div>
)}
</form.State>
<form.State select={(state) => state.submission}>
{(submission) => <p role="status">{submission.status === "succeeded" ? submission.result : null}</p>}
</form.State>
</form>
</Page>
);
}import { useForm, useStore } from "@tanstack/react-form";
import { useId, useState } from "react";
import { signIn } from "../../shared/server.ts";
import { FieldShell, Page } from "../../shared/ui.tsx";
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
function checkEmail({ value }: { value: string }) {
return emailPattern.test(value) ? undefined : "Enter a valid email.";
}
function checkPassword({ value }: { value: string }) {
return value.length >= 8 ? undefined : "Use at least 8 characters.";
}
export function App() {
const emailId = useId();
const passwordId = useId();
const [status, setStatus] = useState("");
const form = useForm({
defaultValues: { email: "", password: "" },
validators: {
onSubmitAsync: async ({ value }) => {
const response = await signIn(value);
return response.ok ? undefined : { fields: response.errors };
},
},
onSubmit: ({ value }) => setStatus(`Signed in as ${value.email}.`),
});
const submitted = useStore(form.store, (state) => state.submissionAttempts > 0);
return (
<Page title="Sign in" lede="Using @lunarhue/forms-react/mini: sync field validation and server errors.">
<form
noValidate
onSubmit={(event) => {
event.preventDefault();
void form.handleSubmit();
}}
>
<form.Field name="email" validators={{ onChange: checkEmail }}>
{(field) => {
const errors =
field.state.meta.isBlurred || submitted
? field.state.meta.errors.filter((error): error is string => typeof error === "string")
: [];
const errorId = `${emailId}-errors`;
return (
<FieldShell id={emailId} errorId={errorId} label="Email" errors={errors}>
<input
id={emailId}
name={field.name}
type="email"
value={field.state.value}
onChange={(event) => field.handleChange(event.target.value)}
onBlur={() => {
field.handleBlur();
void field.validate("change");
}}
aria-invalid={errors.length > 0 ? true : undefined}
aria-describedby={errors.length > 0 ? errorId : undefined}
/>
</FieldShell>
);
}}
</form.Field>
<form.Field name="password" validators={{ onChange: checkPassword }}>
{(field) => {
const errors =
field.state.meta.isBlurred || submitted
? field.state.meta.errors.filter((error): error is string => typeof error === "string")
: [];
const errorId = `${passwordId}-errors`;
return (
<FieldShell id={passwordId} errorId={errorId} label="Password" errors={errors}>
<input
id={passwordId}
name={field.name}
type="password"
value={field.state.value}
onChange={(event) => field.handleChange(event.target.value)}
onBlur={() => {
field.handleBlur();
void field.validate("change");
}}
aria-invalid={errors.length > 0 ? true : undefined}
aria-describedby={errors.length > 0 ? errorId : undefined}
/>
</FieldShell>
);
}}
</form.Field>
<form.Subscribe selector={(state) => state.isSubmitting}>
{(submitting) => (
<div className="actions">
<button type="submit" disabled={submitting}>
{submitting ? "Signing in..." : "Sign in"}
</button>
</div>
)}
</form.Subscribe>
<p role="status">{status}</p>
</form>
</Page>
);
}What the form does
It collects an email and password with synchronous validation (valid email format, at least 8 characters for the password). Errors appear after a field is blurred or the form is submitted, then update on every keystroke. Submitting [email protected] returns a server-side refusal on the email field that clears as soon as you edit it.
Where the two implementations differ
On the @lunarhue/forms side, everything comes from @lunarhue/forms-react/mini: createForms, fieldValidators, inputProps, and useForm. Because /mini leaves out plugins, async validation, element tracking, and configurable show policies, the form ships only the tiny mini store (~2.3 kB brotli with React external). If this form ever needs async checks, dirty(), or focusOnInvalid, graduating it means changing the import to @lunarhue/forms-react (see Using /mini with the full entry).
On the TanStack Form side, we use useForm directly (skipping createFormHook to keep the TanStack bundle as lean as possible), with onChange validators plus a blur trigger and onSubmitAsync for the server check. Because TanStack Form ships one general-purpose store for every form, even a two-field login form carries the full library.
How bundle sizes were measured
scripts/size-compare.ts builds each app for production with Vite (minified, JavaScript only) in three slices: Forms + app (excluding React and react-dom), Total (everything), and App only (excluding the form libraries too).
Measured from built package outputs against TanStack Form 1.33.5 and enforced in CI. Source and shared Playwright specs live in examples/compare.
Last updated