Primitives
Field
Connects a control to its label, help text, and error. Use FieldSet and FieldLegend to group related controls.
MotionStatic
Install
Install Kineto and follow the theme setup once in your app.
bash
npm install kinetotsx
import {
Field,
FieldSet,
FieldLegend,
FieldGroup,
FieldDescription,
FieldError,
FieldLabel,
FieldContent,
FieldTitle,
FieldSeparator,
} from "kineto/field";Preview
tsx
import * as React from "react";
import { cn } from "@/lib/cn";
type FieldControlProps = Pick<
React.InputHTMLAttributes<HTMLInputElement>,
"id" | "aria-describedby" | "aria-invalid"
>;
export interface FieldProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "children"> {
label: React.ReactNode;
description?: React.ReactNode;
error?: React.ReactNode;
children: React.ReactElement<FieldControlProps>;
}
export function Field({ label, description, error, children, className, ...props }: FieldProps) {
const id = React.useId();
const controlId = children.props.id ?? id;
const descriptionId = description == null ? undefined : `${id}-description`;
const errorId = error == null ? undefined : `${id}-error`;
const describedBy = [children.props["aria-describedby"], descriptionId, errorId]
.filter(Boolean)
.join(" ");
return (
<div className={cn("grid gap-2", className)} {...props}>
<FieldLabel htmlFor={controlId}>{label}</FieldLabel>
{React.cloneElement(children, {
id: controlId,
"aria-describedby": describedBy || undefined,
"aria-invalid": errorId ? true : children.props["aria-invalid"],
})}
{descriptionId && <FieldDescription id={descriptionId}>{description}</FieldDescription>}
{errorId && <FieldError id={errorId}>{error}</FieldError>}
</div>
);
}
export function FieldSet({ className, ...props }: React.FieldsetHTMLAttributes<HTMLFieldSetElement>) {
return <fieldset className={cn("min-w-0 space-y-4", className)} {...props} />;
}
export function FieldLegend({ className, ...props }: React.HTMLAttributes<HTMLLegendElement>) {
return <legend className={cn("mb-3 font-medium text-foreground", className)} {...props} />;
}
export function FieldGroup({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) {
return <div className={cn("grid gap-4", className)} {...props} />;
}
export function FieldDescription({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) {
return <div className={cn("text-sm text-muted-foreground", className)} {...props} />;
}
export function FieldError({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) {
return <div className={cn("text-sm text-destructive", className)} {...props} />;
}
export function FieldLabel({ className, ...props }: React.LabelHTMLAttributes<HTMLLabelElement>) {
return <label className={cn("text-sm font-medium text-foreground", className)} {...props} />;
}
export function FieldContent({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) {
return <div className={cn("grid min-w-0 gap-1", className)} {...props} />;
}
export function FieldTitle({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) {
return <div className={cn("text-sm font-medium text-foreground", className)} {...props} />;
}
export function FieldSeparator({ className, ...props }: React.HTMLAttributes<HTMLHRElement>) {
return <hr className={cn("border-border", className)} {...props} />;
}src/components/ui/field.tsx
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| label | ReactNode | — | Visible label for the control. |
| description | ReactNode | — | Help text announced with the control. |
| error | ReactNode | — | Error message. Also marks the control invalid. |
| children | ReactElement | — | One labelable control that forwards id and ARIA props. |