Skip to content
← Blocks

Password Reset Form

A working password reset form with illustrative data and frontend interactions.

Open ↗

Make it yours.

Install the editable source and its dependencies into your React + Tailwind project.

Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/password-reset-form.json

Use it

Scroll horizontally for long lines
"use client";
import { PasswordResetForm } from "@/components/jez-ui/blocks/password-reset-form";

export default function Example() {
  return (
    <>
      <PasswordResetForm />
    </>
  );
}

Compose your own

These styled parts are included in the same install. Use children to build your layout; add className only when you want an override.

Scroll horizontally for long lines
"use client";
import {
  PasswordResetForm,
  PasswordResetFormIntro,
  PasswordResetFormEmailField,
  PasswordResetFormSubmit,
  PasswordResetFormStatus,
} from "@/components/jez-ui/blocks/password-reset-form";

export default function Example() {
  return (
    <PasswordResetForm>
      <PasswordResetFormIntro />
      <PasswordResetFormEmailField />
      <PasswordResetFormSubmit />
      <PasswordResetFormStatus />
    </PasswordResetForm>
  );
}
Scroll horizontally for long lines
import { PasswordResetForm, PasswordResetFormContent, PasswordResetFormTitle, PasswordResetFormIntro, PasswordResetFormEmailField, PasswordResetFormSubmit, PasswordResetFormPasswordFields, PasswordResetFormStatus } from "@/components/jez-ui/blocks/password-reset-form";
Read the composition guide →

Props & types

Scroll horizontally for long lines
export type PasswordResetFormOptions = {
  className?: string;
  onSubmit?: (data: Record<string, string>) => Promise<void>;
  heading?: React.ReactNode;
  mode?: "request" | "new-password";
};

export type PasswordResetFormProps = Omit<
  React.ComponentProps<"form">,
  keyof PasswordResetFormOptions
> &
  PasswordResetFormOptions;

PasswordResetFormProps

Source

View 5 source files

registry/blocks/password-reset-form.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "../ui/utils";
import { LockKeyhole } from "lucide-react";
import { Input } from "../ui/input";
import { FormField } from "../ui/form-field";
import { Button } from "../ui/button";
export type PasswordResetFormOptions = {
  className?: string;
  onSubmit?: (data: Record<string, string>) => Promise<void>;
  heading?: React.ReactNode;
  mode?: "request" | "new-password";
};
export type PasswordResetFormProps = Omit<
  React.ComponentProps<"form">,
  keyof PasswordResetFormOptions
> &
  PasswordResetFormOptions;

function usePasswordResetFormModel({
  heading,
  mode = "request",
  className,
  onSubmit,
  children,
  ...rootProps
}: PasswordResetFormProps) {
  const [status, setStatus] = React.useState("");
  const [error, setError] = React.useState(false);
  const [busy, setBusy] = React.useState(false);
  return {
    heading:
      heading ??
      (mode === "new-password"
        ? "Choose a new password."
        : "Forgot your password?"),
    mode,
    className,
    onSubmit,
    children,
    rootProps,
    status,
    setStatus,
    error,
    setError,
    busy,
    setBusy,
  };
}
const PasswordResetFormCompositionContext = React.createContext<ReturnType<
  typeof usePasswordResetFormModel
> | null>(null);
function usePasswordResetFormComposition() {
  const context = React.useContext(PasswordResetFormCompositionContext);
  if (!context)
    throw new Error(
      "PasswordResetForm parts must be inside PasswordResetForm.",
    );
  return context;
}
export function PasswordResetForm(props: PasswordResetFormProps) {
  const model = usePasswordResetFormModel(props);
  const {
    className,
    onSubmit,
    rootProps,
    setStatus,
    setError,
    setBusy,
    children,
    mode,
  } = model;
  return (
    <PasswordResetFormCompositionContext.Provider value={model}>
      <form
        {...rootProps}
        className={cn(
          "grid w-full max-w-md gap-5 rounded-2xl border border-border bg-background p-7 sm:p-9",
          className,
        )}
        onSubmit={async (e) => {
          e.preventDefault();
          const data = Object.fromEntries(
            new FormData(e.currentTarget),
          ) as Record<string, string>;
          if (
            mode === "new-password" &&
            data.password !== data.passwordConfirmation
          ) {
            setError(true);
            setStatus(
              "Passwords do not match. Check both fields and try again.",
            );
            return;
          }
          setBusy(true);
          setError(false);
          try {
            await onSubmit?.(data);
            setStatus(
              onSubmit
                ? mode === "new-password"
                  ? "Password updated."
                  : "Request complete."
                : mode === "new-password"
                  ? "Demo complete. No password was changed."
                  : "Demo complete. No account or email was created.",
            );
          } catch {
            setError(true);
            setStatus("Unable to continue. Check your details and try again.");
          } finally {
            setBusy(false);
          }
        }}
      >
        {children !== undefined ? (
          children
        ) : (
          <>
            <PasswordResetFormIntro />
            {mode === "request" ? (
              <PasswordResetFormEmailField />
            ) : (
              <PasswordResetFormPasswordFields />
            )}
            <PasswordResetFormSubmit />
            <PasswordResetFormStatus />
          </>
        )}
      </form>
    </PasswordResetFormCompositionContext.Provider>
  );
}

export function PasswordResetFormContent({
  className,
  ...props
}: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="password-reset-form-content"
      className={cn("mb-2", className)}
      {...props}
    />
  );
}
export function PasswordResetFormTitle({
  className,
  ...props
}: React.ComponentProps<"h2">) {
  return (
    <h2
      data-slot="password-reset-form-title"
      className={cn("font-display text-2xl", className)}
      {...props}
    />
  );
}

export function PasswordResetFormIntro({
  children,
  ...props
}: Partial<React.ComponentProps<typeof PasswordResetFormContent>> & {
  children?: React.ReactNode;
}) {
  const { heading, mode } = usePasswordResetFormComposition();
  return (
    <PasswordResetFormContent {...props}>
      {children === undefined ? (
        <>
          <span className="mb-5 grid size-10 place-items-center rounded-xl border border-border bg-muted/30">
            <LockKeyhole size={18} />
          </span>
          <PasswordResetFormTitle>{heading}</PasswordResetFormTitle>
          <p className="mt-2 text-sm leading-relaxed text-muted-foreground">
            {mode === "new-password"
              ? "Choose a new password for your account."
              : "Enter the email address associated with your account."}
          </p>
        </>
      ) : (
        children
      )}
    </PasswordResetFormContent>
  );
}
export function PasswordResetFormEmailField({
  children,
  ...props
}: Partial<React.ComponentProps<typeof FormField>> & {
  children?: React.ReactNode;
}) {
  const {} = usePasswordResetFormComposition();
  return (
    <FormField label="Email" {...props}>
      {children === undefined ? (
        <Input
          name="email"
          type="email"
          required
          autoComplete="email"
          placeholder="you@company.com"
        />
      ) : (
        children
      )}
    </FormField>
  );
}
export function PasswordResetFormSubmit({
  children,
  ...props
}: Partial<React.ComponentProps<typeof Button>> & {
  children?: React.ReactNode;
}) {
  const { busy, mode } = usePasswordResetFormComposition();
  return (
    <Button type="submit" loading={busy} {...props}>
      {children === undefined
        ? mode === "new-password"
          ? "Update password"
          : "Send reset link"
        : children}
    </Button>
  );
}
export function PasswordResetFormPasswordFields({
  children,
  className,
  ...props
}: React.ComponentProps<"div"> & {
  children?: React.ReactNode;
}) {
  return (
    <div
      data-slot="password-reset-form-password-fields"
      className={cn("grid gap-4", className)}
      {...props}
    >
      {children === undefined ? (
        <>
          <FormField label="New password">
            <Input
              name="password"
              type="password"
              autoComplete="new-password"
              required
            />
          </FormField>
          <FormField label="Confirm new password">
            <Input
              name="passwordConfirmation"
              type="password"
              autoComplete="new-password"
              required
            />
          </FormField>
        </>
      ) : (
        children
      )}
    </div>
  );
}
export function PasswordResetFormStatus({ children }: React.PropsWithChildren) {
  const { status, error } = usePasswordResetFormComposition();
  return children === undefined
    ? status && (
        <p
          role={error ? "alert" : "status"}
          className={cn("text-sm", error && "text-danger")}
        >
          {status}
        </p>
      )
    : children;
}

registry/ui/utils.ts

Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
  return twMerge(clsx(values));
}

registry/ui/input.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
export function Input({ className, ...props }: React.ComponentProps<"input">) {
  return (
    <input
      className={cn(
        "block h-10 w-full min-w-0 rounded-lg border border-border bg-background px-3 text-sm leading-normal shadow-[0_1px_2px_#00000004] transition-[border-color,box-shadow] placeholder:text-muted-foreground/75 hover:border-foreground/25 focus:border-primary focus:outline-none focus:ring-3 focus:ring-primary/10 aria-invalid:border-danger aria-invalid:ring-danger/10 disabled:cursor-not-allowed disabled:bg-muted/50 disabled:opacity-50",
        className,
      )}
      {...props}
    />
  );
}

registry/ui/form-field.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
export function FormField({
  label,
  hint,
  error,
  children,
  className,
  ...rootProps
}: Omit<
  React.ComponentProps<"div">,
  keyof {
    label: string;
    hint?: string;
    error?: string;
    children: React.ReactElement<{
      id?: string;
      "aria-describedby"?: string;
      "aria-invalid"?: boolean;
    }>;
    className?: string;
  }
> & {
  label: string;
  hint?: string;
  error?: string;
  children: React.ReactElement<{
    id?: string;
    "aria-describedby"?: string;
    "aria-invalid"?: boolean;
  }>;
  className?: string;
}) {
  const generatedId = React.useId();
  const id = children.props.id ?? generatedId;
  return (
    <div {...rootProps} className={cn("grid gap-2", className)}>
      <label htmlFor={id} className="text-sm font-medium">
        {label}
      </label>
      {React.cloneElement(children, {
        id,
        "aria-describedby":
          [
            children.props["aria-describedby"],
            hint || error ? id + "-help" : undefined,
          ]
            .filter(Boolean)
            .join(" ") || undefined,
        "aria-invalid": error ? true : children.props["aria-invalid"],
      })}
      {(hint || error) && (
        <p
          id={id + "-help"}
          role={error ? "alert" : undefined}
          className={cn(
            "text-xs",
            error ? "text-danger" : "text-muted-foreground",
          )}
        >
          {error || hint}
        </p>
      )}
    </div>
  );
}
export function Field({ className, ...props }: React.ComponentProps<"div">) {
  return <div className={cn("grid gap-2", className)} {...props} />;
}
export function FieldGroup({
  className,
  ...props
}: React.ComponentProps<"div">) {
  return <div className={cn("grid gap-6", className)} {...props} />;
}
export function FieldSet({
  className,
  ...props
}: React.ComponentProps<"fieldset">) {
  return (
    <fieldset className={cn("grid min-w-0 gap-5", className)} {...props} />
  );
}
export function FieldLegend({
  className,
  ...props
}: React.ComponentProps<"legend">) {
  return <legend className={cn("mb-3 font-medium", className)} {...props} />;
}
export function FieldLabel({
  className,
  ...props
}: React.ComponentProps<"label">) {
  return <label className={cn("text-sm font-medium", className)} {...props} />;
}
export function FieldDescription({
  className,
  ...props
}: React.ComponentProps<"p">) {
  return (
    <p
      className={cn("text-xs leading-relaxed text-muted-foreground", className)}
      {...props}
    />
  );
}
export function FieldError({ className, ...props }: React.ComponentProps<"p">) {
  return (
    <p
      role="alert"
      className={cn("text-xs leading-relaxed text-danger", className)}
      {...props}
    />
  );
}

export function FieldContent({
  className,
  ...props
}: React.ComponentProps<"div">) {
  return <div className={cn("grid min-w-0 gap-1", className)} {...props} />;
}
export function FieldRow({
  className,
  ...props
}: React.ComponentProps<"label">) {
  return (
    <label
      className={cn("flex items-center gap-3 text-sm font-medium", className)}
      {...props}
    />
  );
}

registry/ui/button.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { Slot } from "radix-ui";
import { LoaderCircle } from "lucide-react";
import { cn } from "./utils";
export type ButtonProps = React.ComponentProps<"button"> & {
  variant?: "primary" | "secondary" | "outline" | "ghost" | "danger";
  size?: "sm" | "md" | "lg";
  loading?: boolean;
  asChild?: boolean;
};
export function Button({
  variant = "primary",
  size = "md",
  loading,
  asChild,
  className,
  children,
  disabled,
  ...props
}: ButtonProps) {
  const Comp = asChild ? Slot.Root : "button";
  return (
    <Comp
      type={asChild ? undefined : "button"}
      disabled={asChild ? undefined : disabled || loading}
      aria-disabled={disabled || loading || undefined}
      aria-busy={loading || undefined}
      className={cn(
        "relative inline-flex shrink-0 items-center justify-center gap-2 whitespace-nowrap rounded-lg border border-transparent text-sm font-medium leading-none transition-[background-color,border-color,box-shadow,transform] duration-150 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary active:scale-[.98] disabled:pointer-events-none disabled:opacity-45 aria-disabled:pointer-events-none [&_svg]:shrink-0",
        {
          "bg-primary text-primary-foreground shadow-[inset_0_1px_0_#ffffff26,0_1px_2px_#00000012] hover:bg-primary/90":
            variant === "primary",
          "border-border/60 bg-muted text-foreground hover:bg-muted/70":
            variant === "secondary",
          "border-border bg-background text-foreground shadow-sm hover:bg-muted/60":
            variant === "outline",
          "text-muted-foreground hover:bg-muted hover:text-foreground":
            variant === "ghost",
          "bg-danger text-danger-foreground hover:bg-danger/90":
            variant === "danger",
        },
        {
          "h-8 px-3 text-xs": size === "sm",
          "h-10 px-4": size === "md",
          "h-12 px-6": size === "lg",
        },
        className,
      )}
      {...props}
    >
      {asChild ? (
        children
      ) : (
        <>
          <span
            className={cn(
              "inline-flex min-w-0 flex-1 items-center justify-center gap-2",
              loading && "invisible",
            )}
          >
            {children}
          </span>
          {loading && (
            <LoaderCircle
              aria-hidden="true"
              className="absolute size-4 animate-spin motion-reduce:animate-none"
            />
          )}
        </>
      )}
    </Comp>
  );
}

Dependencies

lucide-react@0.577.0 · clsx@2.1.1 · tailwind-merge@3.5.0 · radix-ui@1.6.7

Accessibility & behaviour

Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.

Read the accessibility and performance guide →