Skip to content
Thrum

Sign in form

A complete sign-in card for an app's login page. It checks the fields itself, hands the values to your onSubmit and shows the message you return; it makes no network call. For a passwordless flow that sends a code, use an input OTP in a form of your own.

Install

npx shadcn@latest add https://thrumui.dev/r/sign-in-form.json

Adds components/sign-in-form.tsx and installs motion. Nothing else in your project changes.

import { SignInForm } from "@/components/sign-in-form";

Data shape

Type
// What onSubmit receives.
type SignInValues = { email: string; password: string; remember: boolean };
// What it returns: a message to show, or nothing when the sign-in went through.
type SignInResult = string | null | undefined | void;
Sample
const values: SignInValues = {
  email: "ada@northwind.example",
  password: "correct horse",
  remember: true,
};

Props

PropTypeDefaultDescription
onSubmit(values: SignInValues) => SignInResult | Promise<SignInResult>–Called with the values once both fields pass their checks. Return an error message, or a promise of one, to show it. While the promise is pending the button shows a spinner and a second submit is ignored. A promise that rejects shows the unexpected message.
errorstring | nullDefault nullAn error the parent holds, shown above the fields: a session that ended, a locked account. A message returned by onSubmit takes its place.
titlestringDefault "Sign in"The heading. It also names the form.
descriptionstring–A line under the heading in the muted colour.
headingLevel1 | 2 | 3Default 2The level of the heading. Use 1 when the card is the page's main content.
defaultEmailstring–The email address the field starts with, for a returning visitor.
rememberMebooleanDefault trueShow the Remember me box. Its state reaches onSubmit as remember.
defaultRememberbooleanDefault falseWhether the Remember me box starts ticked.
forgotPasswordHrefstring–Where the Forgot password link goes. It sits beside the password label. Without it there is no link.
signUpHrefstring–Where the sign-up link in the footer goes. Without it there is no footer.
providersReactNode–Other ways in, under a divider: SignInFormProvider buttons, one per line.
labelsPartial<SignInLabels>–Replace any word the form prints: the field names, the button, the divider, the footer, the error title and the three field messages.
soundbooleanDefault truePlay the toggle sound on the Remember me box and the error sound when a sign-in fails, if the visitor has sound on.
classNamestring–Sets the size. The card is at most 384 pixels wide and fills a narrower container.
SignInFormProvider: iconReactNode–A neutral glyph before the text, 16 pixels, in the text colour. Use your own mark where a provider's rules allow it.
SignInFormProvider: ...propsComponentProps<"button">–Every button attribute: onClick to start the provider's flow, disabled, children for the text.

Motion and sound

Motion

  • Nothing moves on its own. The submit button swaps its text for a spinner while onSubmit works, keeping its width.
  • The field borders change colour over 150 ms when a field turns invalid or valid. Messages appear at once.
  • Reduced motion: the spinner stands still and the borders change at once.

Sound

  • Ticking or clearing Remember me plays the toggle sounds.
  • A failed sign-in plays the error sound once, when its message appears. Fields that fail their own checks are silent.

Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works

Accessibility

  • It is a native form named by its title. The email field has autocomplete username and the password field current-password, so password managers fill both.
  • Both fields are required. On submit an empty or malformed field stops it, takes the focus and shows its message under it; the message is linked to the field, so a screen reader reads it with the field's name.
  • A message from onSubmit appears in an alert region above the fields, with a title and a warning glyph, so it is read at once and never rests on colour.
  • While onSubmit works the button keeps the focus and says it is busy.
  • The show button names its state: Show password or Hide password. A password shown when the form is sent is hidden again first.
  • The card is a panel surface: under frosted glass it turns translucent and its text keeps 4.5:1 contrast.
KeysAction
Tab / Shift TabMove through email, the forgot link, password, the show button, Remember me, the submit button, the other ways in and the sign-up link.
EnterIn a field: submit. An empty or malformed field stops the submit and takes the focus.
SpaceOn Remember me: tick or clear it. On a button: press it.

Source

components/sign-in-form.tsx
"use client";

import { useState, type ComponentProps, type ReactNode } from "react";
import { motion } from "motion/react";

import { cn } from "@/lib/utils";
import { FAST, INSTANT, useReducedMotion } from "@/lib/thrum-motion";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
import { Checkbox } from "@/components/ui/checkbox";
import { Field, FieldControl, FieldError, FieldLabel } from "@/components/ui/field";
import { Form } from "@/components/ui/form";
import { Link } from "@/components/ui/link";
import { PasswordInput } from "@/components/ui/password-input";
import { Separator } from "@/components/ui/separator";

/** What the form hands to `onSubmit`. */
export type SignInValues = { email: string; password: string; remember: boolean };

/** What `onSubmit` returns: an error message to show, or nothing when the sign-in went through. */
export type SignInResult = string | null | undefined | void;

/** Every word the form prints, for translation. */
export type SignInLabels = {
  email: string;
  password: string;
  remember: string;
  forgotPassword: string;
  submit: string;
  divider: string;
  signUpPrompt: string;
  signUp: string;
  errorTitle: string;
  missingEmail: string;
  invalidEmail: string;
  missingPassword: string;
  unexpected: string;
};

const LABELS: SignInLabels = {
  email: "Email",
  password: "Password",
  remember: "Remember me",
  forgotPassword: "Forgot password?",
  submit: "Sign in",
  divider: "or",
  signUpPrompt: "No account yet?",
  signUp: "Create one",
  errorTitle: "Could not sign in",
  missingEmail: "Enter your email address.",
  invalidEmail: "Enter an email address like name@example.com.",
  missingPassword: "Enter your password.",
  unexpected: "Something went wrong. Try again.",
};

export type SignInFormProps = {
  /** Called with the values once the fields pass their checks. Return an error message to show it, or a promise of one; the button shows a spinner while the promise is pending. */
  onSubmit?: (values: SignInValues) => SignInResult | Promise<SignInResult>;
  /** An error the parent holds, shown above the fields. A message returned by `onSubmit` takes its place. */
  error?: string | null;
  /** The heading. */
  title?: string;
  /** A line under the heading. */
  description?: string;
  /** The level of the heading, to fit the page's outline. */
  headingLevel?: 1 | 2 | 3;
  /** The email address the field starts with. */
  defaultEmail?: string;
  /** Show the Remember me box. */
  rememberMe?: boolean;
  /** Whether the Remember me box starts ticked. */
  defaultRemember?: boolean;
  /** Where the Forgot password link goes. Without it there is no link. */
  forgotPasswordHref?: string;
  /** Where the sign-up link goes. Without it there is no footer. */
  signUpHref?: string;
  /** Buttons for other ways in, under a divider. Use `SignInFormProvider`. */
  providers?: ReactNode;
  /** Replace any of the words the form prints. */
  labels?: Partial<SignInLabels>;
  /** Play the toggle sound on the Remember me box and the error sound when a sign-in fails, if the visitor has sound on. */
  sound?: boolean;
  /** Size it with classes. It is at most 384 pixels wide and fills narrower containers. */
  className?: string;
};

/**
 * A sign-in card: email and password with their checks, Remember me, a forgot-password link, a submit button that shows
 * a spinner while `onSubmit` works, an error region, other ways in under a divider, and a sign-up link.
 * It makes no network call: `onSubmit` does the work and returns the message to show when it fails.
 */
export function SignInForm({
  onSubmit,
  error = null,
  title = "Sign in",
  description,
  headingLevel = 2,
  defaultEmail,
  rememberMe = true,
  defaultRemember = false,
  forgotPasswordHref,
  signUpHref,
  providers,
  labels,
  sound = true,
  className,
}: SignInFormProps) {
  const words = { ...LABELS, ...labels };
  const reduced = useReducedMotion();
  const Heading = `h${headingLevel}` as "h2";
  const [remember, setRemember] = useState(defaultRemember);
  const [pending, setPending] = useState(false);
  // Each failed attempt gets its own number, so the same message twice still appears, sounds and is read again.
  const [failure, setFailure] = useState<{ message: string; attempt: number } | null>(null);
  const shown = failure?.message ?? error;

  const submit = async (values: Record<string, unknown>) => {
    if (pending) return;
    setFailure(null);
    setPending(true);
    let message: SignInResult;
    try {
      message = await onSubmit?.({ email: String(values.email ?? "").trim(), password: String(values.password ?? ""), remember });
    } catch {
      message = words.unexpected;
    }
    setPending(false);
    if (message) setFailure((before) => ({ message, attempt: (before?.attempt ?? 0) + 1 }));
  };

  return (
    <Card data-slot="sign-in-form" className={cn("w-full max-w-sm gap-6 py-6", className)}>
      <CardHeader className="px-6">
        <CardTitle>
          <Heading className="text-lg leading-6 font-semibold [overflow-wrap:anywhere]">{title}</Heading>
        </CardTitle>
        {description && <CardDescription className="[overflow-wrap:anywhere]">{description}</CardDescription>}
      </CardHeader>
      <CardContent className="grid gap-5 px-6">
        <Form aria-label={title} aria-busy={pending || undefined} onFormSubmit={submit} className="gap-4">
          {shown && (
            <motion.div
              key={failure?.attempt ?? "error"}
              initial={reduced ? false : { opacity: 0, y: -4 }}
              animate={{ opacity: 1, y: 0 }}
              transition={reduced ? INSTANT : FAST}
            >
              <Alert variant="destructive" urgent sound={sound} data-slot="sign-in-form-error">
                <AlertTitle>{words.errorTitle}</AlertTitle>
                <AlertDescription>{shown}</AlertDescription>
              </Alert>
            </motion.div>
          )}
          <Field name="email">
            <FieldLabel>{words.email}</FieldLabel>
            <FieldControl type="email" required autoComplete="username" spellCheck={false} autoCapitalize="none" defaultValue={defaultEmail} />
            <FieldError match="valueMissing">{words.missingEmail}</FieldError>
            <FieldError match="typeMismatch">{words.invalidEmail}</FieldError>
          </Field>
          <Field name="password">
            <div className="flex flex-wrap items-baseline justify-between gap-x-3 gap-y-1">
              <FieldLabel>{words.password}</FieldLabel>
              {forgotPasswordHref && (
                <Link href={forgotPasswordHref} variant="subtle" className="text-sm">
                  {words.forgotPassword}
                </Link>
              )}
            </div>
            <FieldControl render={<PasswordInput />} required />
            <FieldError match="valueMissing">{words.missingPassword}</FieldError>
          </Field>
          {rememberMe && (
            <label className="flex w-fit items-start gap-2 text-sm leading-5">
              <span className="flex h-5 items-center">
                <Checkbox checked={remember} onCheckedChange={setRemember} sound={sound} />
              </span>
              {words.remember}
            </label>
          )}
          <Button type="submit" loading={pending} className="w-full">
            {words.submit}
          </Button>
        </Form>
        {providers && (
          <>
            <Separator>{words.divider}</Separator>
            <div data-slot="sign-in-form-providers" className="grid gap-2">
              {providers}
            </div>
          </>
        )}
      </CardContent>
      {signUpHref && (
        <CardFooter className="flex-wrap justify-center gap-x-1 gap-y-0 px-6 text-sm text-muted-foreground">
          {words.signUpPrompt}
          <Link href={signUpHref} className="font-medium">
            {words.signUp}
          </Link>
        </CardFooter>
      )}
    </Card>
  );
}

export type SignInFormProviderProps = ComponentProps<"button"> & {
  /** A neutral glyph before the text, drawn with `currentColor`. */
  icon?: ReactNode;
};

/** One other way in: a full-width outline button with a glyph and its text, such as "Continue with single sign-on". */
export function SignInFormProvider({ icon, className, children, ...props }: SignInFormProviderProps) {
  return (
    <Button data-slot="sign-in-form-provider" variant="outline" className={cn("w-full [&_svg]:size-4 [&_svg]:flex-none", className)} {...props}>
      {icon}
      <span className="truncate">{children}</span>
    </Button>
  );
}

Files and dependencies

Adds
components/sign-in-form.tsx
Packages
motion
Brings along
AlertButtonCardCheckboxFieldFormLinkPassword inputSeparatorMotion tokens

Changelog

  1. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor