import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import {Field はラベル、コントロール、ヘルパーテキスト、エラーメッセージを1つの揃ったブロックにまとめ、FieldGroup と FieldSet でグループ化したセクションにも拡張できます。下層にプリミティブはなく、プレーンな fieldset、legend、role="group" のセマンティクスを Label と Separator と組み合わせています。レジェンドとラベルは太字の見出しタイポグラフィ、包むコントロール側が太い枠線と硬い影を担います。
次のような場面で使います:
- 設定ページ —
FieldSetのレジェンドとグループ間のFieldSeparatorで、プロフィール、通知、請求を積み重ねます。 - チェックアウトとサインアップ — 入力、セレクト、スイッチでラベル・説明・エラーの揃えを統一します。
- 選択カード — ラジオ、チェックボックス、スイッチを選択可能な
FieldLabelカードで包み、プラン選択や設定パネルに使います。
インストール#
pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/field.jsonnpx shadcn@latest add https://neobrutalism.com/r/base/field.jsonyarn dlx shadcn@latest add https://neobrutalism.com/r/base/field.jsonbunx --bun shadcn@latest add https://neobrutalism.com/r/base/field.json
以下のコードをコピーして、プロジェクトに貼り付けます。
"use client"
import { useMemo } from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"
import { Label } from "@/components/ui/label"
import { Separator } from "@/components/ui/separator"
function FieldSet({ className, ...props }: React.ComponentProps<"fieldset">) {
return (
<fieldset
data-slot="field-set"
className={cn(
"flex flex-col gap-4 has-[>[data-slot=checkbox-group]]:gap-3 has-[>[data-slot=radio-group]]:gap-3",
className
)}
{...props}
/>
)
}
function FieldLegend({
className,
variant = "legend",
...props
}: React.ComponentProps<"legend"> & { variant?: "legend" | "label" }) {
return (
<legend
data-slot="field-legend"
data-variant={variant}
className={cn(
"mb-1.5 font-head font-medium data-[variant=label]:text-sm data-[variant=legend]:text-base",
className
)}
{...props}
/>
)
}
function FieldGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="field-group"
className={cn(
"group/field-group @container/field-group flex w-full flex-col gap-5 data-[slot=checkbox-group]:gap-3 *:data-[slot=field-group]:gap-4",
className
)}
{...props}
/>
)
}
const fieldVariants = cva(
"group/field flex w-full gap-2 data-[invalid=true]:text-destructive",
{
variants: {
orientation: {
vertical: "flex-col *:w-full [&>.sr-only]:w-auto",
horizontal:
"flex-row items-center has-[>[data-slot=field-content]]:items-start *:data-[slot=field-label]:flex-auto has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
responsive:
"flex-col *:w-full @md/field-group:flex-row @md/field-group:items-center @md/field-group:*:w-auto @md/field-group:has-[>[data-slot=field-content]]:items-start @md/field-group:*:data-[slot=field-label]:flex-auto [&>.sr-only]:w-auto @md/field-group:has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
},
},
defaultVariants: {
orientation: "vertical",
},
}
)
function Field({
className,
orientation = "vertical",
...props
}: React.ComponentProps<"div"> & VariantProps<typeof fieldVariants>) {
return (
<div
role="group"
data-slot="field"
data-orientation={orientation}
className={cn(fieldVariants({ orientation }), className)}
{...props}
/>
)
}
function FieldContent({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="field-content"
className={cn(
"group/field-content flex flex-1 flex-col gap-0.5 leading-snug",
className
)}
{...props}
/>
)
}
function FieldLabel({
className,
...props
}: React.ComponentProps<typeof Label>) {
return (
<Label
data-slot="field-label"
className={cn(
"group/field-label peer/field-label flex w-fit gap-2 leading-snug group-data-[disabled=true]/field:opacity-50 has-data-checked:border-primary/30 has-data-checked:bg-primary/5 has-[>[data-slot=field]]:rounded has-[>[data-slot=field]]:border-2 *:data-[slot=field]:p-2.5 dark:has-data-checked:border-primary/20 dark:has-data-checked:bg-primary/10",
"has-[>[data-slot=field]]:w-full has-[>[data-slot=field]]:flex-col",
className
)}
{...props}
/>
)
}
function FieldTitle({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="field-label"
className={cn(
"flex w-fit items-center gap-2 text-sm font-head font-medium group-data-[disabled=true]/field:opacity-50",
className
)}
{...props}
/>
)
}
function FieldDescription({ className, ...props }: React.ComponentProps<"p">) {
return (
<p
data-slot="field-description"
className={cn(
"text-left text-sm leading-normal font-normal text-muted-foreground group-has-data-horizontal/field:text-balance [[data-variant=legend]+&]:-mt-1.5",
"last:mt-0 nth-last-2:-mt-1",
"[&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-primary",
className
)}
{...props}
/>
)
}
function FieldSeparator({
children,
className,
...props
}: React.ComponentProps<"div"> & {
children?: React.ReactNode
}) {
return (
<div
data-slot="field-separator"
data-content={!!children}
className={cn(
"relative -my-2 h-5 text-sm group-data-[variant=outline]/field-group:-mb-2",
className
)}
{...props}
>
<Separator className="absolute inset-0 top-1/2" />
{children && (
<span
className="relative mx-auto block w-fit bg-background px-2 text-muted-foreground"
data-slot="field-separator-content"
>
{children}
</span>
)}
</div>
)
}
function FieldError({
className,
children,
errors,
...props
}: React.ComponentProps<"div"> & {
errors?: Array<{ message?: string } | undefined>
}) {
const content = useMemo(() => {
if (children) {
return children
}
if (!errors?.length) {
return null
}
const uniqueErrors = [
...new Map(errors.map((error) => [error?.message, error])).values(),
]
if (uniqueErrors?.length == 1) {
return uniqueErrors[0]?.message
}
return (
<ul className="ml-4 flex list-disc flex-col gap-1">
{uniqueErrors.map(
(error, index) =>
error?.message && <li key={index}>{error.message}</li>
)}
</ul>
)
}, [children, errors])
if (!content) {
return null
}
return (
<div
role="alert"
data-slot="field-error"
className={cn("text-sm font-normal text-destructive", className)}
{...props}
>
{content}
</div>
)
}
export {
Field,
FieldLabel,
FieldDescription,
FieldError,
FieldGroup,
FieldLegend,
FieldSeparator,
FieldSet,
FieldContent,
FieldTitle,
}
インポートのパスをプロジェクトの構成に合わせて更新します。
使い方#
import {
Field,
FieldContent,
FieldDescription,
FieldError,
FieldGroup,
FieldLabel,
FieldLegend,
FieldSeparator,
FieldSet,
FieldTitle,
} from "@/components/ui/field"<FieldSet>
<FieldLegend>Profile</FieldLegend>
<FieldDescription>This appears on invoices and emails.</FieldDescription>
<FieldGroup>
<Field>
<FieldLabel htmlFor="name">Full name</FieldLabel>
<Input id="name" autoComplete="off" placeholder="Evil Rabbit" />
<FieldDescription>This appears on invoices and emails.</FieldDescription>
</Field>
<Field>
<FieldLabel htmlFor="username">Username</FieldLabel>
<Input id="username" autoComplete="off" aria-invalid />
<FieldError>Choose another username.</FieldError>
</Field>
<Field orientation="horizontal">
<Switch id="newsletter" />
<FieldLabel htmlFor="newsletter">Subscribe to the newsletter</FieldLabel>
</Field>
</FieldGroup>
</FieldSet>コンポジション#
Field#
ラベル、ヘルプテキスト、バリデーションを備えた単一のコントロールです。
Field
├── FieldLabel
├── Input / Textarea / Switch / Select
├── FieldDescription
└── FieldErrorFieldGroup#
関連するフィールドを 1 つのグループにまとめます。必要に応じて、セクションの間に FieldSeparator を挟みます。
FieldGroup
├── Field
│ ├── FieldLabel
│ ├── Input / Textarea / Switch / Select
│ ├── FieldDescription
│ └── FieldError
├── FieldSeparator
└── Field
├── FieldLabel
└── Input / Textarea / Switch / SelectFieldSet#
凡例と説明を備えたセマンティックなグループで、通常は FieldGroup を含みます。
FieldSet
├── FieldLegend
├── FieldDescription
└── FieldGroup
├── Field
│ ├── FieldLabel
│ ├── Input / Textarea / Switch / Select
│ ├── FieldDescription
│ └── FieldError
└── Field
├── FieldLabel
└── Input / Textarea / Switch / Select構成#
Field ファミリーは、アクセシブルなフォームを構成するために設計されています。一般的なフィールドは、次のような構造になります。
<Field>
<FieldLabel htmlFor="input-id">Label</FieldLabel>
{/* Input, Select, Switch, etc. */}
<FieldDescription>Optional helper text.</FieldDescription>
<FieldError>Validation message.</FieldError>
</Field>Fieldは、単一のフィールドの中核となるラッパーです。FieldContentは、ラベルと説明をまとめる flex カラムです。説明がない場合は不要です。- 関連するフィールドは
FieldGroupでまとめ、セマンティックなグループ化にはFieldSetとFieldLegendを使用します。
フォーム#
Field コンポーネントと React Hook Form、Tanstack Form、Formisch を使ってフォームを作成する方法は、Form のドキュメントをご覧ください。
使用例#
Input#
import {
Field,
FieldDescription,Textarea#
import {
Field,
FieldDescription,Select#
import {
Field,
FieldDescription,Slider#
"use client"
import * as React from "react"Fieldset#
import {
Field,
FieldDescription,Checkbox#
import { Checkbox } from "@/components/ui/checkbox"
import {
Field,Radio#
import {
Field,
FieldDescription,Switch#
import { Field, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
選択カード#
Field コンポーネントを FieldLabel で包んで、選択可能なフィールドグループを作成します。これは RadioItem、Checkbox、Switch コンポーネントで機能します。
import {
Field,
FieldContent,フィールドグループ#
FieldGroup で Field コンポーネントを積み重ねます。区切るには FieldSeparator を追加します。
import { Checkbox } from "@/components/ui/checkbox"
import {
Field,RTL#
Neobrutalism で RTL を有効にする方法は、RTL 設定ガイドをご覧ください。
"use client"
import * as React from "react"レスポンシブレイアウト#
- 縦方向のフィールド: デフォルトの向きでは、ラベル、コントロール、ヘルプテキストが縦に積み重なります。モバイルファーストのレイアウトに最適です。
- 横方向のフィールド:
Fieldにorientation="horizontal"を設定すると、ラベルとコントロールが横並びになります。FieldContentと組み合わせると、説明の位置をそろえられます。 - レスポンシブなフィールド: コンテナを認識する親要素の内側で列レイアウトを自動化するには、
orientation="responsive"を設定します。特定のブレークポイントで向きを切り替えるには、FieldGroupに@container/field-groupクラスを適用します。
import { Button } from "@/components/ui/button"
import {
Field,バリデーションとエラー#
Fieldにdata-invalidを追加すると、ブロック全体がエラー状態に切り替わります。- 支援技術のために、入力欄そのものに
aria-invalidを追加します。 - エラーメッセージをフィールドに合わせて配置するために、
FieldErrorはコントロールの直後、またはFieldContentの内側に表示します。
<Field data-invalid>
<FieldLabel htmlFor="email">Email</FieldLabel>
<Input id="email" type="email" aria-invalid />
<FieldError>Enter a valid email address.</FieldError>
</Field>アクセシビリティ#
FieldSetとFieldLegendは、キーボードや支援技術を利用する人のために、関連するコントロールをグループ化したままにします。Fieldはrole="group"を出力するため、組み合わせたときに入れ子のコントロールがFieldLabelとFieldLegendのラベル付けを継承します。- スクリーンリーダーが明確なセクションの境界を認識できるよう、
FieldSeparatorは控えめに使用します。
アクセシビリティ#
Field ファミリーはセマンティクス優先です。FieldSet と FieldLegend はネイティブの fieldset/legend、Field は role="group"、FieldError は role="alert" を描画し、検証メッセージは表示された瞬間に読み上げられます。
- すべての
FieldLabelをhtmlFor/idでコントロールと結び付けます。ラベルをクリックするとフォーカスが移り、スクリーンリーダーも関連を読み上げます。 - 無効な入力ではコントロール自体に
aria-invalidを付けます。Fieldのdata-invalidはエラー用スタイルだけを駆動します。 - キーボード挙動は内側のコントロール側のものです。ラッパーはフォーカスストップもキーバインドも追加しません。
APIリファレンス#
FieldSet#
間隔のプリセットを備えたセマンティックな fieldset をレンダリングするコンテナです。
| Prop | Type | Default |
|---|---|---|
className | string |
<FieldSet>
<FieldLegend>Delivery</FieldLegend>
<FieldGroup>{/* Fields */}</FieldGroup>
</FieldSet>FieldLegend#
FieldSet の凡例要素です。ラベルのサイズに合わせるには、label バリアントに切り替えます。
| Prop | Type | Default |
|---|---|---|
variant | "legend" | "label" | "legend" |
className | string |
<FieldLegend variant="label">Notification Preferences</FieldLegend>FieldLegend には legend と label の 2 つのバリアントがあります。label バリアントは、ラベルのサイズと配置を適用します。FieldSet を入れ子にする場合に便利です。
FieldGroup#
Field コンポーネントを積み重ね、レスポンシブな向きのためにコンテナクエリを有効にするレイアウトラッパーです。
| Prop | Type | Default |
|---|---|---|
className | string |
<FieldGroup className="@container/field-group flex flex-col gap-6">
<Field>{/* ... */}</Field>
<Field>{/* ... */}</Field>
</FieldGroup>Field#
単一のフィールドの中核となるラッパーです。向きの制御、エラー状態のスタイル、間隔を提供します。
| Prop | Type | Default |
|---|---|---|
orientation | "vertical" | "horizontal" | "responsive" | "vertical" |
className | string | |
data-invalid | boolean |
<Field orientation="horizontal">
<FieldLabel htmlFor="remember">Remember me</FieldLabel>
<Switch id="remember" />
</Field>FieldContent#
ラベルがコントロールの横に配置されるときに、コントロールと説明をまとめる flex カラムです。説明がない場合は不要です。
| Prop | Type | Default |
|---|---|---|
className | string |
<Field>
<Checkbox id="notifications" />
<FieldContent>
<FieldLabel htmlFor="notifications">Notifications</FieldLabel>
<FieldDescription>Email, SMS, and push options.</FieldDescription>
</FieldContent>
</Field>FieldLabel#
直接の入力欄にも、入れ子になった Field の子要素にも対応したスタイルのラベルです。
| Prop | Type | Default |
|---|---|---|
className | string | |
asChild | boolean | false |
<FieldLabel htmlFor="email">Email</FieldLabel>FieldTitle#
FieldContent の内側で、ラベルのスタイルを適用したタイトルをレンダリングします。
| Prop | Type | Default |
|---|---|---|
className | string |
<FieldContent>
<FieldTitle>Enable Touch ID</FieldTitle>
<FieldDescription>Unlock your device faster.</FieldDescription>
</FieldContent>FieldDescription#
横方向のレイアウトで長い行を自動的に調整するヘルプテキストのスロットです。
| Prop | Type | Default |
|---|---|---|
className | string |
<FieldDescription>We never share your email with anyone.</FieldDescription>FieldSeparator#
FieldGroup の内側でセクションを区切る視覚的な仕切りです。インラインのコンテンツを任意で受け取れます。
| Prop | Type | Default |
|---|---|---|
className | string |
<FieldSeparator>Or continue with</FieldSeparator>FieldError#
子要素、または(react-hook-form などからの)errors 配列を受け取る、アクセシブルなエラーコンテナです。
| Prop | Type | Default |
|---|---|---|
errors | Array<{ message?: string } | undefined> | |
className | string |
<FieldError errors={errors.username} />errors 配列に複数のメッセージが含まれる場合、コンポーネントは自動的にリストをレンダリングします。
FieldError は、Zod、Valibot、ArkType など、Standard Schema を実装する任意のバリデーターが生成する issue も受け取ります。スキーマの結果の issues 配列をそのまま渡すと、ライブラリをまたいで統一されたエラーリストをレンダリングできます。