Skip to content
Domphy

Form Group

Use formGroup on a fieldset element. It defines a grid layout contract for its children: legend spans the full width, label elements occupy the first column, controls (any non-legend/label/p child) occupy the second column, and help text p elements appear below their control. Set layout to "vertical" to stack labels above controls instead of placing them side by side (default "horizontal"). The color prop controls the legend, text, and surface tone.

Props

PropTypeDefaultDescription
colorValueOrState<ThemeColor>"neutral"Color tone for the legend, text, and surface.
layout"horizontal" | "vertical""horizontal"Whether labels are placed side by side (horizontal) or stacked above controls (vertical).
Customization

Must see the source of patch at the bottom of each patch page to understand the structure then code it still code as html native element.

There are four levels of customization, in increasing order of effort:

  1. Patch props. Each patch exposes a small, stable set of props—typically fewer than five. Lowest friction.
  2. Context attributes. Use dataTone, dataSize, and dataDensity on a container to shift tone, size, or density for an entire subtree without touching individual elements.
  3. Inline override. Native-wins merge strategy: any property set directly on the element overrides the patch value.
  4. Create a variant. Clone a similar patch and edit it. Use this only when you need a reusable custom version.
Formulas

Unit - U = fontSize / 4 - convert final values with themeSpacing(n).

Size - n = intrinsic text lines, w = wrapping level, d = density factor:

height        = (n * 6 + 2 * d * w) * U
paddingBlock  = d * w * U
paddingInline = ceil(3 / w) * d * w * U
radius        = d * w * U

Base density d = 1.5:

Uw=0w=1w=2w=3
height (n = 1)691215
paddingBlock01.534.5
paddingInline34.564.5
radius01.534.5

Tone - K = N / 2 where N is the palette length. For N = 18, K = 9.

RoleShiftn=0
Backgroundparent +/- n0
Textbg + K6
Borderbg + K/23
Hoverbg + 2K/34
Selected / Focusabove +/- K/32-4

State shift range: K/3 <= delta <= 2K/3.

import { type PartialElement, toState, type ValueOrState } from "@domphy/core";
import {
  type ThemeColor,
  themeColor,
  themeDensity,
  themeSize,
  themeSpacing,
} from "@domphy/theme";

/**
 * Layout patch for a group of form fields. Arranges a `<legend>`, `<label>`s,
 * controls, and helper `<p>`s in a grid — labels beside controls (horizontal)
 * or stacked above them (vertical). Apply to a `<fieldset>` element.
 *
 * @hostTag fieldset
 * @param props.color - Theme color tone (`ValueOrState<ThemeColor>`) for legend/text/surface. Defaults to "neutral".
 * @param props.layout - Field arrangement, "horizontal" (label beside control) | "vertical" (label above). Defaults to "horizontal".
 * @example { fieldset: [{ legend: "Profile" }, { label: "Name" }, { input: "" }], $: [formGroup({ layout: "vertical" })] }
 */
function formGroup(
  props: {
    color?: ValueOrState<ThemeColor>;
    layout?: "horizontal" | "vertical";
  } = {},
): PartialElement {
  const { layout = "horizontal" } = props;
  const color = toState(props.color ?? "neutral", "color");

  const isVertical = layout === "vertical";

  return {
    _onInsert: (node) => {
      if (node.tagName !== "fieldset") {
        console.warn(`"formGroup" patch must use fieldset tag`);
      }
    },
    // Legend weight is design-system chrome for field groups.
    _doctorDisable: "inline-typography",
    style: {
      margin: 0,
      paddingInline: (listener) => themeSpacing(themeDensity(listener) * 3),
      paddingBlock: (listener) => themeSpacing(themeDensity(listener) * 3),
      border: "none",
      borderRadius: (listener) => themeSpacing(themeDensity(listener) * 2),
      fontSize: (listener) => themeSize(listener, "inherit"),
      backgroundColor: (listener) =>
        themeColor(listener, "inherit", color.get(listener)),
      color: (listener) => themeColor(listener, "text", color.get(listener)),
      display: "grid",
      gridTemplateColumns: isVertical
        ? `minmax(0, 1fr)`
        : `max-content minmax(0, 1fr)`,
      columnGap: themeSpacing(4),
      rowGap: themeSpacing(3),
      alignItems: "start",
      "& > legend": {
        gridColumn: "1 / -1",
        margin: 0,
        fontSize: (listener) => themeSize(listener, "inherit"),
        fontWeight: 600,
        paddingBlock: (listener) => themeSpacing(themeDensity(listener) * 1),
        borderRadius: (listener) => themeSpacing(themeDensity(listener) * 2),
        color: (listener) => themeColor(listener, "text", color.get(listener)),
        backgroundColor: (listener) =>
          themeColor(listener, "inherit", color.get(listener)),
      },
      "& > label": {
        gridColumn: "1",
        alignSelf: "start",
        margin: 0,
        paddingBlock: (listener) =>
          isVertical ? 0 : themeSpacing(themeDensity(listener) * 1),
      },
      "& > label:has(+ :not(legend, label, p) + p)": {
        gridRow: isVertical ? "auto" : "span 2",
      },
      "& > :not(legend, label, p)": {
        gridColumn: isVertical ? "1" : "2",
        minWidth: 0,
        width: "100%",
        boxSizing: "border-box",
      },
      "& > p": {
        gridColumn: isVertical ? "1" : "2",
        minWidth: 0,
        margin: 0,
        marginBlockStart: `calc(${themeSpacing(2)} * -1)`,
        fontSize: (listener) => themeSize(listener, "decrease-1"),
        color: (listener) => themeColor(listener, "text", color.get(listener)),
      },
    },
  };
}

export { formGroup };