---
title: "Fieldset · UI SDK — StyleX Blocks"
description: "Group related fields under a legend with helper text and spacing tuned per size."
url: "https://stylexblocks.com/components/ui-sdk/fieldset/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Fieldset

Group related fields under a legend with helper text and spacing tuned per size.

- Behavior engine: UI SDK
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/fieldset.md), [Base UI](https://stylexblocks.com/components/base-ui/fieldset.md)
- Category: Form
- Sizes: sm, md, lg
- Used in blocks: [Contact Form](https://stylexblocks.com/block/ui-sdk/contact-form.md), [Two-Column Form](https://stylexblocks.com/block/ui-sdk/form-two-column.md), [Vertical Wizard](https://stylexblocks.com/block/ui-sdk/form-wizard-vertical.md)
- HTML page: https://stylexblocks.com/components/ui-sdk/fieldset/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init --style ui-sdk
```

Then add the item as source you own:

```sh
npx stylexblocks add fieldset
```

Equivalent: `npx shadcn add @stylexblocks/fieldset`. Registry item JSON: https://stylexblocks.com/r/ui-sdk/fieldset.json

## Usage

```tsx
import { Field } from "#components/ui/ui-sdk/field.tsx";
import { Fieldset } from "#components/ui/ui-sdk/fieldset.tsx";
import { Input } from "#components/ui/ui-sdk/input.tsx";

export function Example() {
  return (
    // a native <fieldset>: size resolves once on Root, the parts inherit it;
    // disabled disables every control inside. HelperText describes the set,
    // so screen readers read it as focus enters.
    <Fieldset.Root size="md">
      <Fieldset.Legend>Contact details</Fieldset.Legend>
      <Fieldset.HelperText>Used for order updates only.</Fieldset.HelperText>
      <Fieldset.Content>
        <Field.Root>
          <Field.Label>Full name</Field.Label>
          <Input size="sm" placeholder="Ada Lovelace" />
        </Field.Root>
      </Fieldset.Content>
    </Fieldset.Root>
  );
}
```

## Source

`components/ui/ui-sdk/fieldset.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import {
  FieldDescription as UiSdkFieldDescription,
  FieldGroup as UiSdkFieldGroup,
  FieldLegend as UiSdkFieldLegend,
  FieldSet as UiSdkFieldSet,
} from "@ui-sdk/react/field";
import { createSlotRecipeContext } from "#lib/ui-sdk/create-slot-recipe-context.tsx";
import { defineSlotRecipe, resolveSlotStyles } from "#lib/recipe-props.ts";
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'
import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'
import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'

const fieldsetRoot = stylex.create({
  base: {
    display: 'flex',
    flexDirection: 'column',
    width: sizes.full,
  },
})

const fieldsetRootSizes = stylex.create({
  sm: {},
  md: {},
  lg: {},
})

const fieldsetContent = stylex.create({
  base: {
    display: 'flex',
    flexDirection: 'column',
    width: sizes.full,
  },
})

/*
 * Chakra spaces the fieldset root with `spaceY` (sibling margins) because a
 * native <legend> sits outside the fieldset's anonymous flex content box, so
 * flex `gap` on the root never reaches it. StyleX has no sibling combinators,
 * so the equivalent margin-block-start is emitted per slot (helperText,
 * errorText, content) via the size variants below.
 */
const fieldsetContentSizes = stylex.create({
  sm: {
    gap: spacing._1_5,
    marginBlockStart: spacing._2,
  },
  md: {
    gap: spacing._4,
    marginBlockStart: spacing._4,
  },
  lg: {
    gap: spacing._4,
    marginBlockStart: spacing._4,
  },
})

const fieldsetLegend = stylex.create({
  base: {
    color: semanticColors.fg,
    fontWeight: fontWeights.medium,
    paddingInline: 0,
    opacity: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 0.5,
    },
  },
})

const fieldsetLegendSizes = stylex.create({
  sm: {
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
  },
  md: {
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
  },
  lg: {
    fontSize: fontSizes.md,
    lineHeight: lineHeights.md,
  },
})

const fieldsetHelperText = stylex.create({
  base: {
    color: semanticColors.fgMuted,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
  },
})

const fieldsetHelperTextSizes = stylex.create({
  sm: {
    marginBlockStart: spacing._2,
  },
  md: {
    marginBlockStart: spacing._4,
  },
  lg: {
    marginBlockStart: spacing._4,
  },
})

const fieldsetErrorText = stylex.create({
  base: {
    display: 'inline-flex',
    alignItems: 'center',
    color: semanticColors.fgError,
    gap: spacing._2,
    fontWeight: fontWeights.medium,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
  },
})

const fieldsetErrorTextSizes = stylex.create({
  sm: {
    marginBlockStart: spacing._2,
  },
  md: {
    marginBlockStart: spacing._4,
  },
  lg: {
    marginBlockStart: spacing._4,
  },
})

export type FieldsetSize = keyof typeof fieldsetRootSizes

const fieldsetRecipe = defineSlotRecipe({
  slots: {
    root: { styles: fieldsetRoot, sizes: fieldsetRootSizes },
    content: { styles: fieldsetContent, sizes: fieldsetContentSizes },
    legend: { styles: fieldsetLegend, sizes: fieldsetLegendSizes },
    helperText: { styles: fieldsetHelperText, sizes: fieldsetHelperTextSizes },
    errorText: { styles: fieldsetErrorText, sizes: fieldsetErrorTextSizes },
  },
  defaultVariants: { size: "md" },
});

const chrome = stylex.create({
  reset: { borderWidth: 0, borderStyle: "none", padding: 0, margin: 0, minWidth: 0 },
});

const { withProvider, withContext } = createSlotRecipeContext(fieldsetRecipe);

/**
 * Fieldset — UI SDK's field set (a native `<fieldset>` + `<legend>`;
 * `disabled` disables every control inside and marks the parts with
 * `data-disabled`) wearing the fieldset slot recipe. HelperText is UI SDK's
 * FieldDescription placed directly in the set, so screen readers read it as
 * focus enters; Content is its FieldGroup. `<Fieldset.Root size>` resolves
 * the recipe once; the parts inherit the size through context.
 */
export const Fieldset = {
  Root: withProvider(
    UiSdkFieldSet,
    (v) => [...resolveSlotStyles(fieldsetRecipe.slots.root, v), chrome.reset],
    "FieldsetRoot",
  ),
  Legend: withContext(UiSdkFieldLegend, "legend", "FieldsetLegend"),
  HelperText: withContext(UiSdkFieldDescription, "helperText", "FieldsetHelperText"),
  ErrorText: withContext("p", "errorText", "FieldsetErrorText"),
  Content: withContext(UiSdkFieldGroup, "content", "FieldsetContent"),
};

export type FieldsetRootProps = React.ComponentProps<typeof Fieldset.Root>;
export type FieldsetLegendProps = React.ComponentProps<typeof Fieldset.Legend>;
export type FieldsetHelperTextProps = React.ComponentProps<typeof Fieldset.HelperText>;
export type FieldsetErrorTextProps = React.ComponentProps<typeof Fieldset.ErrorText>;
export type FieldsetContentProps = React.ComponentProps<typeof Fieldset.Content>;
```
