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.

Controls are stretched to fill their column, except fixed-size ones — input[type=checkbox] (including inputSwitch), input[type=radio] and input[type=color] keep their own width and are start-aligned, so a switch dropped into a formGroup does not render as a full-column-wide pill.

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,
  themeWeight,
} 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: null }], $: [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`);
      }
    },
    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: themeWeight("semibold"),
        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",
      },
      // Fixed-size controls are EXCLUDED from the stretch rather than
      // overriding it afterwards: a checkbox / switch / radio / colour swatch
      // dropped into a formGroup rendered as a full-column-wide pill
      // (observed in Chromium), and `width: auto` on an appearance:none
      // input collapses it to 0 instead of restoring its declared size.
      "& > :not(legend, label, p, input[type=checkbox], input[type=radio], input[type=color])":
        {
          gridColumn: isVertical ? "1" : "2",
          minWidth: 0,
          width: "100%",
          boxSizing: "border-box",
        },
      "& > input[type=checkbox], & > input[type=radio], & > input[type=color]":
        {
          gridColumn: isVertical ? "1" : "2",
          justifySelf: "start",
        },
      "& > 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 };