Call-to-action band with email capture

A closing section on a full field of your primary colour: a large headline and one sentence beside a labelled email form. It validates on the client (empty and malformed addresses, with a specific message tied to the field by aria-describedby), shows a sending state, then swaps to a success message with a way back. Pass onSubmit to call your own API; a rejected promise shows a retry message.

MarketingBlockTailwind v4

Source

"use client";

import { useId, useRef, useState, type FormEvent } from "react";
import { ArrowRight, CircleAlert, CircleCheck, LoaderCircle } from "lucide-react";
import { cn } from "@/lib/utils";

export type CtaBandProps = {
  title?: string;
  description?: string;
  label?: string;
  placeholder?: string;
  buttonLabel?: string;
  /** Called with a valid email. Throw or reject to show an error. Defaults to a short simulated request. */
  onSubmit?: (email: string) => Promise<void> | void;
  className?: string;
};

const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;

export function CtaBand({
  title = "Bring your team's docs into one place",
  description = "Free for up to five people. Importing a whole wiki takes about ten minutes.",
  label = "Work email",
  placeholder = "you@company.com",
  buttonLabel = "Get started",
  onSubmit = () => new Promise((resolve) => setTimeout(resolve, 900)),
  className,
}: CtaBandProps) {
  const id = useId();
  const inputRef = useRef<HTMLInputElement>(null);
  const [email, setEmail] = useState("");
  const [error, setError] = useState("");
  const [status, setStatus] = useState<"idle" | "sending" | "sent">("idle");

  const validate = (value: string) => {
    if (!value.trim()) return "Enter your work email.";
    if (!EMAIL.test(value.trim())) return "That email looks incomplete. Check for a missing @ or domain.";
    return "";
  };

  const submit = async (event: FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    const problem = validate(email);
    setError(problem);
    if (problem) {
      inputRef.current?.focus();
      return;
    }
    setStatus("sending");
    try {
      await onSubmit(email.trim());
      setStatus("sent");
    } catch {
      setStatus("idle");
      setError("We could not send that just now. Try again in a moment.");
    }
  };

  return (
    <section aria-labelledby={`${id}-title`} className={cn("bg-background px-4 py-16 sm:px-6 lg:px-8 lg:py-24", className)}>
      <div className="mx-auto grid max-w-6xl gap-10 rounded-xl bg-primary px-6 py-12 text-primary-foreground sm:px-10 sm:py-16 lg:grid-cols-[1.15fr_1fr] lg:items-end lg:gap-16 lg:px-16 lg:py-20">
        <div>
          <h2 id={`${id}-title`} className="text-balance text-4xl font-semibold leading-[1.04] tracking-[-0.035em] sm:text-5xl">
            {title}
          </h2>
          <p className="mt-5 max-w-md text-lg leading-relaxed text-primary-foreground/75">{description}</p>
        </div>

        <div aria-live="polite">
          {status === "sent" ? (
            <div className="flex gap-4 rounded-lg bg-primary-foreground/10 p-5">
              <CircleCheck className="mt-0.5 size-6 shrink-0" aria-hidden="true" />
              <div className="min-w-0">
                <p className="text-lg font-semibold">Check your inbox</p>
                <p className="mt-1 break-words text-sm text-primary-foreground/75">
                  We sent a sign-in link to <span className="font-medium text-primary-foreground">{email.trim()}</span>. It works for 30 minutes.
                </p>
                <button
                  type="button"
                  onClick={() => {
                    setStatus("idle");
                    setEmail("");
                    requestAnimationFrame(() => inputRef.current?.focus());
                  }}
                  className="mt-4 rounded-sm text-sm font-medium underline underline-offset-4 outline-none hover:no-underline focus-visible:ring-2 focus-visible:ring-primary-foreground"
                >
                  Use a different email
                </button>
              </div>
            </div>
          ) : (
            <form noValidate onSubmit={submit}>
              <label htmlFor={`${id}-email`} className="text-sm font-medium text-primary-foreground/85">
                {label}
              </label>
              <div className="mt-2 flex flex-col gap-2 sm:flex-row">
                <input
                  ref={inputRef}
                  id={`${id}-email`}
                  type="email"
                  inputMode="email"
                  autoComplete="email"
                  value={email}
                  placeholder={placeholder}
                  onChange={(event) => {
                    setEmail(event.target.value);
                    if (error) setError(validate(event.target.value));
                  }}
                  aria-invalid={error ? true : undefined}
                  aria-describedby={error ? `${id}-error` : undefined}
                  className={cn(
                    "h-12 w-full min-w-0 rounded-md bg-primary-foreground/10 sm:flex-1 px-4 text-base text-primary-foreground transition-shadow",
                    "placeholder:text-primary-foreground/50 outline-none focus-visible:ring-2 focus-visible:ring-primary-foreground",
                    error ? "ring-2 ring-red-400 dark:ring-red-600" : "ring-1 ring-primary-foreground/20",
                  )}
                />
                <button
                  type="submit"
                  disabled={status === "sending"}
                  className="inline-flex h-12 shrink-0 items-center justify-center gap-2 rounded-md bg-primary-foreground px-6 text-sm font-semibold text-primary transition-opacity hover:opacity-90 disabled:opacity-70 outline-none focus-visible:ring-2 focus-visible:ring-primary-foreground focus-visible:ring-offset-2 focus-visible:ring-offset-primary"
                >
                  {status === "sending" ? (
                    <>
                      <LoaderCircle className="size-4 motion-safe:animate-spin" aria-hidden="true" />
                      Sending
                    </>
                  ) : (
                    <>
                      {buttonLabel}
                      <ArrowRight className="size-4" aria-hidden="true" />
                    </>
                  )}
                </button>
              </div>
              {error && (
                <p id={`${id}-error`} className="mt-3 flex items-start gap-2 text-sm text-primary-foreground">
                  <CircleAlert className="mt-0.5 size-4 shrink-0 text-red-400 dark:text-red-600" aria-hidden="true" />
                  {error}
                </p>
              )}
            </form>
          )}
        </div>
      </div>
    </section>
  );
}