Contact Form
A complete contact form section, ready to adapt to your product.
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/contact-form.jsonUse it
Scroll horizontally for long lines
"use client";
import { ContactForm } from "@/components/jez-ui/blocks/contact-form";
export default function Example() {
return (
<>
<ContactForm />
</>
);
}
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 {
ContactForm,
ContactFormIntro,
ContactFormContactFields,
ContactFormProjectField,
ContactFormSubmit,
ContactFormStatus,
} from "@/components/jez-ui/blocks/contact-form";
export default function Example() {
return (
<ContactForm>
<ContactFormIntro />
<ContactFormContactFields />
<ContactFormProjectField />
<ContactFormSubmit />
<ContactFormStatus />
</ContactForm>
);
}
Scroll horizontally for long lines
import { ContactForm, ContactFormContent, ContactFormTitle, ContactFormIntro, ContactFormContactFields, ContactFormProjectField, ContactFormSubmit, ContactFormStatus } from "@/components/jez-ui/blocks/contact-form";Props & types
Scroll horizontally for long lines
export type ContactFormOptions = {
className?: string;
onSubmit?: (data: {
name: string;
email: string;
message: string;
}) => Promise<void>;
heading?: React.ReactNode;
};
export type ContactFormProps = Omit<
React.ComponentProps<"form">,
keyof ContactFormOptions
> &
ContactFormOptions;
ContactFormPropsSource
View 6 source files
registry/blocks/contact-form.tsx
Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "../ui/utils";
import { ArrowUpRight } from "lucide-react";
import { Input } from "../ui/input";
import { Textarea } from "../ui/textarea";
import { FormField } from "../ui/form-field";
import { Button } from "../ui/button";
export type ContactFormOptions = {
className?: string;
onSubmit?: (data: {
name: string;
email: string;
message: string;
}) => Promise<void>;
heading?: React.ReactNode;
};
export type ContactFormProps = Omit<
React.ComponentProps<"form">,
keyof ContactFormOptions
> &
ContactFormOptions;
function useContactFormModel({
heading = "What are you working on?",
className,
onSubmit,
children,
...rootProps
}: ContactFormProps) {
const [status, setStatus] = React.useState("");
const [busy, setBusy] = React.useState(false);
return {
heading,
className,
onSubmit,
children,
rootProps,
status,
setStatus,
busy,
setBusy,
};
}
const ContactFormCompositionContext = React.createContext<ReturnType<
typeof useContactFormModel
> | null>(null);
function useContactFormComposition() {
const context = React.useContext(ContactFormCompositionContext);
if (!context)
throw new Error("ContactForm parts must be inside ContactForm.");
return context;
}
export function ContactForm(props: ContactFormProps) {
const model = useContactFormModel(props);
const { className, onSubmit, rootProps, setStatus, setBusy, children } =
model;
return (
<ContactFormCompositionContext.Provider value={model}>
<form
{...rootProps}
className={cn(
"grid w-full gap-7 rounded-2xl border border-border bg-background p-6 sm:p-9",
className,
)}
onSubmit={async (e) => {
e.preventDefault();
const f = new FormData(e.currentTarget);
setBusy(true);
try {
await onSubmit?.({
name: String(f.get("name")),
email: String(f.get("email")),
message: String(f.get("message")),
});
setStatus(
onSubmit
? "Message sent. Thank you."
: "Your enquiry is ready. This demo does not send messages.",
);
} catch {
setStatus("Message could not be sent. Please try again.");
} finally {
setBusy(false);
}
}}
>
{children !== undefined ? (
children
) : (
<>
<ContactFormIntro />
<ContactFormContactFields />
<ContactFormProjectField />
<ContactFormSubmit />
<ContactFormStatus />
</>
)}
</form>
</ContactFormCompositionContext.Provider>
);
}
export function ContactFormContent({
className,
...props
}: React.ComponentProps<"div">) {
return (
<div
data-slot="contact-form-content"
className={cn("border-b border-border pb-6", className)}
{...props}
/>
);
}
export function ContactFormTitle({
className,
...props
}: React.ComponentProps<"h2">) {
return (
<h2
data-slot="contact-form-title"
className={cn("font-display text-3xl", className)}
{...props}
/>
);
}
export function ContactFormIntro({
children,
...props
}: Partial<React.ComponentProps<typeof ContactFormContent>> & {
children?: React.ReactNode;
}) {
const { heading } = useContactFormComposition();
return (
<ContactFormContent {...props}>
{children === undefined ? (
<>
<p className="mb-3 text-xs font-medium uppercase tracking-widest text-muted-foreground">
Start a conversation
</p>
<ContactFormTitle>{heading}</ContactFormTitle>
<p className="mt-3 max-w-md text-sm leading-relaxed text-muted-foreground">
Tell us about the project, the challenge, or the idea you can’t stop
thinking about.
</p>
</>
) : (
children
)}
</ContactFormContent>
);
}
export function ContactFormContactFields({
children,
...props
}: Partial<React.ComponentProps<"div">> & { children?: React.ReactNode }) {
return (
<div
{...props}
className={cn("grid gap-5 sm:grid-cols-2", props.className)}
>
{children === undefined ? (
<>
<FormField label="Your name">
<Input
name="name"
autoComplete="name"
required
minLength={2}
placeholder="Alex Morgan"
/>
</FormField>
<FormField label="Email address">
<Input
name="email"
type="email"
autoComplete="email"
required
placeholder="alex@studio.com"
/>
</FormField>
</>
) : (
children
)}
</div>
);
}
export function ContactFormProjectField({
children,
...props
}: Partial<React.ComponentProps<typeof FormField>> & {
children?: React.ReactNode;
}) {
return (
<FormField
label="About your project"
hint="A rough outline is a good place to start."
{...props}
>
{children === undefined ? (
<Textarea
name="message"
required
minLength={10}
placeholder="We’re building…"
className="min-h-40"
/>
) : (
children
)}
</FormField>
);
}
export function ContactFormSubmit({
children,
...props
}: Partial<React.ComponentProps<typeof Button>> & {
children?: React.ReactNode;
}) {
const { busy } = useContactFormComposition();
return (
<Button
type="submit"
loading={busy}
{...props}
className={cn("justify-self-start", props.className)}
>
{children === undefined ? (
<>
Send enquiry <ArrowUpRight size={16} />
</>
) : (
children
)}
</Button>
);
}
export function ContactFormStatus({ children }: React.PropsWithChildren) {
const { status } = useContactFormComposition();
return children === undefined
? status && (
<p role="status" className="text-sm">
{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/textarea.tsx
Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
export function Textarea({
className,
...props
}: React.ComponentProps<"textarea">) {
return (
<textarea
className={cn(
"block min-h-32 w-full min-w-0 resize-y rounded-lg border border-border bg-background px-3.5 py-3 text-sm leading-relaxed shadow-[0_1px_2px_#00000004] transition-[border-color,box-shadow] placeholder:text-muted-foreground/70 hover:border-foreground/25 focus:border-primary focus:outline-none focus:ring-3 focus:ring-primary/10 aria-invalid:border-danger 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 →