// // Copyright 2026 DXOS.org // // @import-as-namespace import { Field as FieldPrimitive, useFieldContext } from '@ark-ui/react/field'; import { useFieldsetContext } from '@ark-ui/react/fieldset'; import React, { Children, type ComponentPropsWithoutRef, forwardRef, isValidElement } from 'react'; import { mx } from '@dxos/ui-theme'; import { type MessageValence, type ThemedClassName } from '@dxos/ui-types'; import { recipes } from '../../recipes.ts'; import { type Size } from '../../sizes.ts'; import { type Level, type Span, spanAttributes } from '../Container/Container.tsx'; // // Root // /** The current Field's validation tones; `error` is the one that makes the field invalid. */ type FieldValence = MessageValence; type FieldRootProps = ThemedClassName & { /** * Tones the control's border and focus ring and the HelperText; `error` also sets `invalid` (unless given), so * ErrorText shows and the control reports `aria-invalid`. */ validationValence?: FieldValence; /** * `stack` (default) puts the label above the control (decision 13). `row` joins the parent's `columns` as a subgrid * row (Phase 4 decision 3): the Header and HelperText take the tracks before the parent's interior `control` line, * every other child the tracks after it; below the pane's collapse width the row stacks. `inline` puts the label * beside the control, in a `--dx-label-width` column, so a run of inline fields lines its controls up. */ layout?: 'stack' | 'row' | 'inline'; /** A surface rung for a `row` field, which then draws a separator border around itself (a settings card row). */ level?: Level; /** Tracks the field spans in its parent Container (e.g. two columns of a multi-column form). */ span?: Span; /** The control's text colour, as Typography's `tone`. */ tone?: 'muted'; }; /** A part, not a container (decision 13): a flex stack in the content track with the label above its control. */ const FieldRoot = forwardRef( ({ classNames, invalid, validationValence, layout, level, span, tone, style, ...props }, forwardedRef) => { // Ark inherits only `disabled` from an enclosing Fieldset; an invalid set marks its fields invalid too. const fieldset = useFieldsetContext(); const { style: spanStyle, ...spanAttrs } = spanAttributes(span); return ( ); }, ); FieldRoot.displayName = 'Field.Root'; // // Header // type FieldHeaderProps = ThemedClassName> & { /** Overrides the field's size; by default the row shares it, so its actions share the control's end column. */ size?: Size; }; /** The label row: a Label followed by optional trailing Icons or icon-only Buttons, aligned to the control's edges. */ const FieldHeader = forwardRef(({ classNames, size, ...props }, forwardedRef) => (
)); FieldHeader.displayName = 'Field.Header'; // // Label // type FieldLabelProps = ThemedClassName & { /** Visually hidden but still names the control, like `srOnly` on a standalone label. */ srOnly?: boolean; }; /** * Marks the part a label click focuses in a control whose labelled element is a hidden input (DateInput's first * segment, PinInput's first cell), since the browser cannot focus the hidden input the label points at. */ const LABEL_TARGET_ATTRIBUTE = 'data-label-target'; /** * Names the control; while the root is `required` it ends with the required indicator, unless a * `RequiredIndicator` among its children places the mark itself. */ const FieldLabel = forwardRef( ({ classNames, srOnly, onClick, children, ...props }, forwardedRef) => { const field = useFieldContext(); const placed = Children.toArray(children).some( (child) => isValidElement(child) && child.type === FieldRequiredIndicator, ); return ( { onClick?.(event); const control = field && event.currentTarget.ownerDocument.getElementById(field.ids.control); const target = control?.parentElement?.querySelector(`[${LABEL_TARGET_ATTRIBUTE}]`); if (!event.defaultPrevented && target) { event.preventDefault(); target.focus(); } }} data-sr-only={srOnly ? '' : undefined} className={mx(recipes.label(), classNames)} ref={forwardedRef} > {children} {field?.required && !placed && } ); }, ); FieldLabel.displayName = 'Field.Label'; // // RequiredIndicator // type FieldRequiredIndicatorProps = ThemedClassName; /** Ark's mark for a `required` root (`*` by default); `Field.Label` renders one, so use this only to place it yourself. */ const FieldRequiredIndicator = forwardRef( ({ classNames, ...props }, forwardedRef) => ( ), ); FieldRequiredIndicator.displayName = 'Field.RequiredIndicator'; // // HelperText // type FieldHelperTextProps = ThemedClassName; const FieldHelperText = forwardRef(({ classNames, ...props }, forwardedRef) => ( )); FieldHelperText.displayName = 'Field.HelperText'; // // ErrorText // type FieldErrorTextProps = ThemedClassName; /** Rendered only while the root is `invalid`. */ const FieldErrorText = forwardRef(({ classNames, ...props }, forwardedRef) => ( )); FieldErrorText.displayName = 'Field.ErrorText'; export { LABEL_TARGET_ATTRIBUTE }; export type { FieldErrorTextProps as ErrorTextProps, FieldHeaderProps as HeaderProps, FieldHelperTextProps as HelperTextProps, FieldLabelProps as LabelProps, FieldRequiredIndicatorProps as RequiredIndicatorProps, FieldRootProps as RootProps, FieldValence as Valence, }; export { FieldErrorText as ErrorText, FieldHeader as Header, FieldHelperText as HelperText, FieldLabel as Label, FieldRequiredIndicator as RequiredIndicator, FieldRoot as Root, };