Skip to content
← Blocks

Profile Settings

A working profile settings 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/profile-settings.json

Use it

Scroll horizontally for long lines
"use client";
import { ProfileSettings } from "@/components/jez-ui/blocks/profile-settings";

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

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 {
  ProfileSettings,
  ProfileSettingsError,
  ProfileSettingsIntro,
  ProfileSettingsAvatar,
  ProfileSettingsNameField,
  ProfileSettingsEmailField,
  ProfileSettingsActions,
  ProfileSettingsStatus,
} from "@/components/jez-ui/blocks/profile-settings";

export default function Example() {
  return (
    <ProfileSettings>
      <ProfileSettingsError />
      <ProfileSettingsIntro />
      <ProfileSettingsAvatar />
      <ProfileSettingsNameField />
      <ProfileSettingsEmailField />
      <ProfileSettingsActions />
      <ProfileSettingsStatus />
    </ProfileSettings>
  );
}
Scroll horizontally for long lines
import { ProfileSettings, ProfileSettingsContent, ProfileSettingsTitle, ProfileSettingsError, ProfileSettingsIntro, ProfileSettingsAvatar, ProfileSettingsNameField, ProfileSettingsEmailField, ProfileSettingsActions, ProfileSettingsStatus } from "@/components/jez-ui/blocks/profile-settings";
Read the composition guide →

Props & types

Scroll horizontally for long lines
export type ProfileSettingsOptions = {
  className?: string;
  value?: typeof initial;
  defaultValue?: typeof initial;
  onValueChange?: (value: typeof initial) => void;
  heading?: React.ReactNode;
  onSave?: (value: typeof initial) => void | Promise<void>;
};

export type ProfileSettingsProps = Omit<
  React.ComponentProps<"form">,
  keyof ProfileSettingsOptions
> &
  ProfileSettingsOptions;

ProfileSettingsProps

Source

View 7 source files

registry/blocks/profile-settings.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { useAsyncAction } from "../ui/use-async-action";
import { cn } from "../ui/utils";
import { Input } from "../ui/input";
import { FormField } from "../ui/form-field";
import { Button } from "../ui/button";
import { useControllable } from "../ui/use-controllable";
const initial = { name: "Alex Morgan", email: "alex@example.com" };
export type ProfileSettingsOptions = {
  className?: string;
  value?: typeof initial;
  defaultValue?: typeof initial;
  onValueChange?: (value: typeof initial) => void;
  heading?: React.ReactNode;
  onSave?: (value: typeof initial) => void | Promise<void>;
};
export type ProfileSettingsProps = Omit<
  React.ComponentProps<"form">,
  keyof ProfileSettingsOptions
> &
  ProfileSettingsOptions;

function useProfileSettingsModel({
  heading = "Personal details",
  className,
  value: controlledValue,
  defaultValue = initial,
  onValueChange,
  onSave,
  children,
  ...rootProps
}: ProfileSettingsProps) {
  const action = useAsyncAction();
  const [profile, setProfile] = useControllable<typeof initial>(
    controlledValue,
    defaultValue,
    onValueChange,
  );
  const reset = () => setProfile(defaultValue);
  const [saved, setSaved] = React.useState(false);
  return {
    heading,
    className,
    controlledValue,
    defaultValue,
    onValueChange,
    onSave,
    children,
    rootProps,
    action,
    profile,
    setProfile,
    reset,
    saved,
    setSaved,
  };
}
const ProfileSettingsCompositionContext = React.createContext<ReturnType<
  typeof useProfileSettingsModel
> | null>(null);
function useProfileSettingsComposition() {
  const context = React.useContext(ProfileSettingsCompositionContext);
  if (!context)
    throw new Error("ProfileSettings parts must be inside ProfileSettings.");
  return context;
}
export function ProfileSettings(props: ProfileSettingsProps) {
  const model = useProfileSettingsModel(props);
  const { className, onSave, rootProps, action, profile, setSaved, children } =
    model;
  return (
    <ProfileSettingsCompositionContext.Provider value={model}>
      <form
        {...rootProps}
        className={cn(
          "grid max-w-xl gap-5 rounded-xl border border-border p-6",
          className,
        )}
        onSubmit={(e) => {
          rootProps.onSubmit?.(e);
          if (e.defaultPrevented) return;

          e.preventDefault();
          void action.run(async () => {
            await onSave?.(profile);
            setSaved(true);
          });
        }}
      >
        {children !== undefined ? (
          children
        ) : (
          <>
            <ProfileSettingsError />
            <ProfileSettingsIntro />
            <ProfileSettingsAvatar />
            <ProfileSettingsNameField />
            <ProfileSettingsEmailField />
            <ProfileSettingsActions />
            <ProfileSettingsStatus />
          </>
        )}
      </form>
    </ProfileSettingsCompositionContext.Provider>
  );
}

export function ProfileSettingsContent({
  className,
  ...props
}: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="profile-settings-content"
      className={cn("border-b border-border pb-5", className)}
      {...props}
    />
  );
}
export function ProfileSettingsTitle({
  className,
  ...props
}: React.ComponentProps<"h2">) {
  return (
    <h2
      data-slot="profile-settings-title"
      className={cn("text-lg font-semibold", className)}
      {...props}
    />
  );
}

export function ProfileSettingsError({ children }: React.PropsWithChildren) {
  const { action } = useProfileSettingsComposition();
  return children === undefined
    ? action.error && <p role="alert">{action.error}</p>
    : children;
}
export function ProfileSettingsIntro({
  children,
  ...props
}: Partial<React.ComponentProps<typeof ProfileSettingsContent>> & {
  children?: React.ReactNode;
}) {
  const { heading } = useProfileSettingsComposition();
  return (
    <ProfileSettingsContent {...props}>
      {children === undefined ? (
        <>
          <ProfileSettingsTitle>{heading}</ProfileSettingsTitle>
          <p className="mt-1 text-sm text-muted-foreground">
            How you appear to the people you work with.
          </p>
        </>
      ) : (
        children
      )}
    </ProfileSettingsContent>
  );
}
export function ProfileSettingsAvatar({
  children,
  ...props
}: Partial<React.ComponentProps<"div">> & { children?: React.ReactNode }) {
  const { profile } = useProfileSettingsComposition();
  return (
    <div
      {...props}
      className={cn("flex items-center gap-4 pb-2", props.className)}
    >
      {children === undefined ? (
        <>
          <span className="grid size-14 place-items-center rounded-full bg-primary/10 font-display text-xl text-primary">
            {profile.name
              .split(" ")
              .map((n) => n[0])
              .slice(0, 2)
              .join("")}
          </span>
          <div>
            <p className="text-sm font-medium">{profile.name}</p>
            <p className="mt-1 text-xs text-muted-foreground">
              Your initials are used as your avatar.
            </p>
          </div>
        </>
      ) : (
        children
      )}
    </div>
  );
}
export function ProfileSettingsNameField({
  children,
  ...props
}: Partial<React.ComponentProps<typeof FormField>> & {
  children?: React.ReactNode;
}) {
  const { profile, setProfile, setSaved } = useProfileSettingsComposition();
  return (
    <FormField label="Name" {...props}>
      {children === undefined ? (
        <Input
          required
          value={profile.name}
          onChange={(e) => {
            setProfile({ ...profile, name: e.target.value });
            setSaved(false);
          }}
        />
      ) : (
        children
      )}
    </FormField>
  );
}
export function ProfileSettingsEmailField({
  children,
  ...props
}: Partial<React.ComponentProps<typeof FormField>> & {
  children?: React.ReactNode;
}) {
  const { profile, setProfile, setSaved } = useProfileSettingsComposition();
  return (
    <FormField label="Email" {...props}>
      {children === undefined ? (
        <Input
          type="email"
          required
          value={profile.email}
          onChange={(e) => {
            setProfile({ ...profile, email: e.target.value });
            setSaved(false);
          }}
        />
      ) : (
        children
      )}
    </FormField>
  );
}
export function ProfileSettingsActions({
  children,
  ...props
}: Partial<React.ComponentProps<"div">> & { children?: React.ReactNode }) {
  const { action, reset, setSaved } = useProfileSettingsComposition();
  return (
    <div {...props} className={cn("flex gap-2", props.className)}>
      {children === undefined ? (
        <>
          <Button
            type="submit"
            disabled={action.pending}
            loading={action.pending}
          >
            Save profile
          </Button>
          <Button
            variant="ghost"
            onClick={() => {
              reset();
              setSaved(false);
            }}
          >
            Reset changes
          </Button>
        </>
      ) : (
        children
      )}
    </div>
  );
}
export function ProfileSettingsStatus({ children }: React.PropsWithChildren) {
  const { onSave, saved } = useProfileSettingsComposition();
  return children === undefined
    ? saved && (
        <p role="status" className="text-sm">
          {onSave ? "Changes saved." : "Changes kept in this preview."}
        </p>
      )
    : children;
}

registry/ui/use-async-action.ts

Scroll horizontally for long lines
"use client";
import * as React from "react";
/** Keeps duplicate submissions out and reports failures without committing local success. */
export function useAsyncAction() {
  const [pending, setPending] = React.useState(false);
  const [error, setError] = React.useState<string>();
  const busy = React.useRef(false);
  async function run(action: () => void | Promise<void>) {
    if (busy.current) return false;
    busy.current = true;
    setPending(true);
    setError(undefined);
    try {
      await action();
      return true;
    } catch (error) {
      setError(
        error instanceof Error
          ? error.message
          : "The action failed. Please try again.",
      );
      return false;
    } finally {
      busy.current = false;
      setPending(false);
    }
  }
  return { run, pending, error };
}

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>
  );
}

registry/ui/use-controllable.ts

Scroll horizontally for long lines
"use client";
import { useRef, useState, type SetStateAction } from "react";
/** Controlled or instance-local state. Functional updates also work before a rerender. */
export function useControllable<T>(
  value: T | undefined,
  initial: T,
  onChange?: (value: T) => void,
) {
  const [local, setLocal] = useState(initial);
  const current = useRef(value === undefined ? local : value);
  current.current = value === undefined ? local : value;
  return [
    current.current,
    (action: SetStateAction<T>) => {
      const next =
        typeof action === "function"
          ? (action as (previous: T) => T)(current.current)
          : action;
      current.current = next;
      if (value === undefined) setLocal(next);
      onChange?.(next);
    },
  ] as const;
}

Dependencies

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

Accessibility & behaviour

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

Read the accessibility and performance guide →