Form Section

A long settings form laid out as titled sections.

Each section’s title and description sit beside its fields, stacking on small screens. FormActions holds the buttons after the last section.

Preview
basic.tsx
import { FormActions, FormSection } from "~/components/form-section"
import { Button } from "~/components/ui/button"
import { Field, FieldDescription, FieldLabel } from "~/components/ui/field"
import { Input } from "~/components/ui/input"
import { Switch } from "~/components/ui/switch"

export default function Example() {
  return (
    <form className="w-full max-w-3xl" onSubmit={(event) => event.preventDefault()}>
      <FormSection title="Paper" description="Trim size and margins for every page of the product.">
        <div className="grid grid-cols-2 gap-lg">
          <Field>
            <FieldLabel>Width (mm)</FieldLabel>
            <Input defaultValue="289" inputMode="decimal" />
          </Field>
          <Field>
            <FieldLabel>Height (mm)</FieldLabel>
            <Input defaultValue="380" inputMode="decimal" />
          </Field>
        </div>
      </FormSection>
      <FormSection title="Checks" description="What production checks run before export.">
        <Field orientation="horizontal">
          <Switch defaultChecked />
          <FieldLabel>Block export on errors</FieldLabel>
        </Field>
        <Field>
          <FieldLabel>Overflow tolerance (lines)</FieldLabel>
          <Input defaultValue="0" inputMode="numeric" />
          <FieldDescription>
            Stories may run this many lines long before it's an error.
          </FieldDescription>
        </Field>
      </FormSection>
      <FormActions>
        <Button variant="outline" type="reset">
          Reset
        </Button>
        <Button type="submit">Save defaults</Button>
      </FormActions>
    </form>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesTextprimitive
Used byNothing yet.
Packages None.

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/form-section.json

Manually

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/form-section.tsx
import type * as React from "react"

import { Text, textVariants } from "~/components/ui/text"
import { cn } from "~/lib/utils"

/*
 * A long settings form laid out as titled sections: the title and description
 * on the left, the fields on the right, stacking on small screens.
 *
 * <form>
 *   <FormSection title="…" description="…">…fields…</FormSection>
 *   <FormSection title="…">…</FormSection>
 *   <FormActions>…buttons…</FormActions>
 * </form>
 */

type FormSectionProps = Omit<React.ComponentProps<"section">, "title"> & {
  title: React.ReactNode
  description?: React.ReactNode
}

/** One titled section of fields, ruled off from the next. */
function FormSection({ title, description, className, children, ...props }: FormSectionProps) {
  return (
    <section
      data-slot="form-section"
      className={cn(
        "grid gap-x-3xl gap-y-lg border-b border-border py-2xl first:pt-0 md:grid-cols-[minmax(0,16rem)_minmax(0,1fr)]",
        className,
      )}
      {...props}
    >
      <div className="grid content-start gap-2xs">
        <h2 className={textVariants({ size: "md", weight: "medium", family: "heading" })}>
          {title}
        </h2>
        {description !== undefined && (
          <Text size="sm" tone="muted">
            {description}
          </Text>
        )}
      </div>
      <div className="grid min-w-0 content-start gap-xl">{children}</div>
    </section>
  )
}

/** The form's buttons, after its last section. */
function FormActions({ className, children, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="form-actions"
      className={cn("flex flex-col-reverse gap-sm pt-xl sm:flex-row sm:justify-end", className)}
      {...props}
    >
      {children}
    </div>
  )
}

export { FormActions, FormSection }
export type { FormSectionProps }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

FormActions

The form's buttons, after its last section.

Takes the props of a native div.

FormSection

One titled section of fields, ruled off from the next.

descriptionours

Type ReactNode

title*ours

Type ReactNode