Skip to Content
CodeUI

Field helpers

@codeui/solid v0.3.0

Helpers that wire labels, descriptions, and errors into custom fields.

The field helpers give your own control the look of TextField: the same box, the same label, and the same error text. You need them only when you build a field the kit does not ship, such as a native select, a color input, or a third-party editor. For text, numbers, and choices, use the ready components. They already wire the label, the hint, the error, and the ARIA attributes.

SVG keeps the text selectable.
field-helpers/custom-field.tsx
import {
	type BaseFieldProps,
	createBaseFieldProps,
	createFieldErrorMessageProps,
	createFieldLabelProps,
	type FieldWithErrorMessageSupport,
	fieldStyles,
	textFieldStyles,
} from "@codeui/solid";
import { createSignal, For, Show } from "solid-js";
import { type JSX } from "@solidjs/web";

type ExportFormatFieldProps = BaseFieldProps &
	FieldWithErrorMessageSupport & {
		label: JSX.Element;
		description?: JSX.Element;
		value: string;
		onChange: (value: string) => void;
	};

/** A native select that looks and reads like the kit fields. */
function ExportFormatField(props: ExportFormatFieldProps) {
	const field = createBaseFieldProps(props, () => props);
	const labelProps = createFieldLabelProps<"label">({
		get for() {
			return field.ids.input();
		},
		get id() {
			return field.ids.label();
		},
	});
	const errorProps = createFieldErrorMessageProps(props);
	const describedBy = () =>
		[props.description && field.ids.description(), props.errorMessage && field.ids.errorMessage()]
			.filter(Boolean)
			.join(" ") || undefined;

	return (
		<div class={textFieldStyles.baseFieldContainer} data-field-size={field.attributes["data-size"]}>
			<label {...labelProps}>{props.label}</label>
			<select
				id={field.ids.input()}
				{...field.attributes}
				class={`${field.baseStyle()} ${textFieldStyles.textField}`}
				value={props.value}
				data-invalid={props.errorMessage ? "" : undefined}
				aria-invalid={props.errorMessage ? "true" : undefined}
				aria-describedby={describedBy()}
				onChange={event => props.onChange(event.currentTarget.value)}
			>
				<For each={["png", "jpeg", "svg"]}>{format => <option value={format}>{format.toUpperCase()}</option>}</For>
			</select>
			<Show when={props.description}>
				<div
					id={field.ids.description()}
					style={{ color: fieldStyles.baseFieldVars.descriptionColor, "font-size": "var(--cui-font-size-xs)" }}
				>
					{props.description}
				</div>
			</Show>
			<Show when={errorProps.errorMessage}>
				<div id={field.ids.errorMessage()} class={errorProps.class}>
					{errorProps.errorMessage}
				</div>
			</Show>
		</div>
	);
}

export default function CustomField() {
	const [format, setFormat] = createSignal("png");
	return (
		<div style={{ width: "320px" }}>
			<ExportFormatField
				label="Export format"
				description="SVG keeps the text selectable."
				value={format()}
				onChange={setFormat}
				errorMessage={format() === "jpeg" ? "JPEG drops the transparent background." : undefined}
			/>
		</div>
	);
}

The demo builds an export format field from a native select. It uses all three helpers and the field style exports. The helpers provide styling attributes, classes, and IDs. Your component renders the elements, links them with for and aria-describedby, and decides when the field is invalid.

createBaseFieldProps

const field = createBaseFieldProps({ size: "sm", variant: "filled" });
return <input {...field.attributes} class={field.baseStyle()} />;

BaseFieldProps supplies size and variant. Pass the component props directly to keep the attributes reactive.

Prop

Type

baseStyle() returns the base class. Spread field.attributes to set the current data-size and data-variant.

Pass the field accessor to also get ids. The helper reads only id and inputId from it. ids holds four accessors that return linked ids:

AccessorReturns
ids.input()inputId, or else ${id}-input. Without id, a generated id.
ids.label()${ids.input()}-label
ids.description()${ids.input()}-description
ids.errorMessage()${ids.input()}-error

Without the accessor, ids is undefined. The generated id comes from createUniqueId, so it matches between the server and the client.

field-helpers/base-style.tsx
import { createBaseFieldProps, fieldStyles, textFieldStyles } from "@codeui/solid";

export default function BaseStyle() {
	const field = createBaseFieldProps({ size: "sm", variant: "filled" });
	return (
		<div class={fieldStyles.baseFieldTheme} style={{ width: "240px" }}>
			<input
				type="search"
				aria-label="Search snippets"
				placeholder="Search snippets"
				{...field.attributes}
				class={`${field.baseStyle()} ${textFieldStyles.textField}`}
			/>
		</div>
	);
}

The box reads its colors from variables that fieldStyles.baseFieldTheme defines. Put that class, or textFieldStyles.baseFieldContainer, on an ancestor. Without it the box has no fill and no text color. The box sets no padding. Add textFieldStyles.textField for the horizontal padding of a single-line input.

The invalid and disabled styles key on the data-invalid and data-disabled attributes of the control, not on :invalid or :disabled. Set those attributes yourself, as the custom field demo does with data-invalid.

createFieldLabelProps

import { createFieldLabelProps } from "@codeui/solid";

function createFieldLabelProps<T extends ValidComponent = "label">(
	props: PolymorphicProps<T>,
): PolymorphicProps<T>;

The helper returns your props with the kit label class added after props.class. Spread the result on a label. It sets no for and no id. Pass them as getters, as the demo does, so they stay reactive:

const labelProps = createFieldLabelProps<"label">({
	get for() {
		return field.ids.input();
	},
	get id() {
		return field.ids.label();
	},
});

The label font size follows the nearest data-field-size attribute, from 12px at xs to 16px at lg. Set data-field-size on the field root. Without it, the label inherits the font size of its parent.

createFieldErrorMessageProps

import { createFieldErrorMessageProps, type FieldWithErrorMessageSupport } from "@codeui/solid";

function createFieldErrorMessageProps<T extends FieldWithErrorMessageSupport>(
	props: T,
): { errorMessage: JSX.Element; class: string };

The helper picks errorMessage from your props and adds class, the kit error style. It ignores every other prop.

Prop

Type

The helper renders nothing and sets no id or role. Render the message only while the field is invalid, give it ids.errorMessage(), and add that id to the control's aria-describedby. Also set aria-invalid on the control. Extend FieldWithErrorMessageSupport in your props type so your field takes errorMessage like the ready fields.

styleFieldMessage

import { styleFieldMessage } from "@codeui/solid";

function styleFieldMessage(
	sizesMap: Record<"xs" | "sm" | "md" | "lg", "xs" | "sm" | "md" | "lg" | "xl">,
): { selectors: StyleRule["selectors"] };

The helper builds a vanilla-extract style rule. For each field size, it maps [data-field-size=<size>] & to the font size you pick for that size. Call it in a .css.ts file. It needs vanilla-extract at build time and does nothing at runtime.

Use it to size a custom field message:

export const errorMessage = style([
	styleFieldMessage({
		xs: "xs",
		sm: "xs",
		md: "xs",
		lg: "sm",
	}),
	{
		userSelect: "none",
		color: tokens.text.danger,
	},
]);

Use it when you add a text part to a custom field, such as a character counter, and want it to scale with the field size like the label and the hint.

Field style exports

These namespaces hold the classes and variables that the helpers and the ready fields use. Import them from @codeui/solid.

ExportHolds
fieldStyles.baseFieldThemeClass that defines the field variables. Put it on the field root.
fieldStyles.baseFieldVarsThe field variables: background, borderColor, color, descriptionColor, inputHeight, fontSize, disabledBackground.
fieldStyles.baseFieldVariantsThe base class helper behind baseStyle().
fieldStyles.FieldSizesThe four size names, xs to lg.
fieldStyles.inputHeight, fieldStyles.fontSizeRaw variables for the control height and font size.
fieldLabelStyles.labelThe label class that createFieldLabelProps adds.
fieldErrorStyles.errorMessageThe error class that createFieldErrorMessageProps adds.
textFieldStyles.baseFieldContainerbaseFieldTheme plus a column layout with a 12px gap. The TextField root class.
textFieldStyles.textFieldHorizontal padding and no outline. The TextField input class.

The kit exports no class for the hint. The demo colors its hint with fieldStyles.baseFieldVars.descriptionColor and --cui-font-size-xs.

FieldClasses is the type of the classes prop on the ready fields, with the keys root, input, label, description, and errorMessage. Reuse it so your field takes the same classes shape.

All the helpers run during server rendering. They render no DOM, use no portal, and add no client effects.

On this page