Hooks

useFormField

Access the context provided by a parent FormField component. Useful for building custom form inputs that integrate with FormField's label, error, size, and accessibility features.

Basic Usage

The useFormField hook reads context values from a parent FormField. Below, see how the context is provided to child components.

Please enter a valid email

FormField Context Values

name:
email
size:
md
error:
Please enter a valid email
ariaId:
form-field-email
<script lang="ts">
  import { FormField, Input, Card } from 'sv5ui';
  import { useFormField } from 'sv5ui';

  // Inside a custom component wrapped by FormField:
  const ctx = useFormField();
</script>

<!-- Parent usage -->
<FormField label="Email" name="email" error="Invalid email" size="sm">
  <Input placeholder="you@example.com" />
</FormField>

<!-- Reading context values -->
{#if ctx}
  <Card variant="soft">
    <p><strong>Name:</strong> {ctx.name}</p>
    <p><strong>Size:</strong> {ctx.size}</p>
    <p><strong>Error:</strong> {ctx.error}</p>
    <p><strong>Aria ID:</strong> {ctx.ariaId}</p>
  </Card>
{/if}

Custom Component

Use useFormField inside custom components to inherit FormField context automatically.

Bio is required

<!-- CustomInput.svelte -->
<script lang="ts">
  import { useFormField } from 'sv5ui';

  const ctx = useFormField();
  // ctx?.name, ctx?.size, ctx?.error, ctx?.ariaId
  // Use these to sync with the parent FormField
</script>

<input
  id={ctx?.ariaId}
  name={ctx?.name}
  aria-describedby={ctx?.describedBy || undefined}
  aria-invalid={ctx?.error ? true : undefined}
  aria-required={ctx?.required ? true : undefined}
  class="..."
/>

<!-- Usage -->
<FormField label="Username" name="username" error="Required">
  <CustomInput />
</FormField>

Controls a Label Cannot Target

<label for> only names form elements such as input, select, textarea and button. A control drawn as a div with a role needs the label's id in aria-labelledby instead. Report its id with labelable set to false, and the field keeps the label's id but leaves out for, so the label does not point at an element it cannot name. Rating, Slider, SelectMenu, RadioGroup, CheckboxGroup and ColorPicker report themselves this way.

StarPicker.svelte
<script lang="ts">
  import { useFormField } from 'sv5ui';

  let { id = 'stars', value = $bindable(3) } = $props();
  const ctx = useFormField();

  // A div cannot be the target of <label for>, so report it with labelable: false.
  // The label keeps its id and the control names itself through aria-labelledby.
  $effect(() => {
    ctx?.setControlId?.(id, false);
    return () => ctx?.setControlId?.(undefined);
  });
</script>

<div
  {id}
  role="slider"
  tabindex="0"
  aria-valuemin={0}
  aria-valuemax={5}
  aria-valuenow={value}
  aria-labelledby={ctx?.labelId}
  aria-describedby={ctx?.describedBy || undefined}
>
  <!-- stars -->
</div>

<!-- Usage -->
<FormField label="Rating" description="How was your stay?">
  <StarPicker />
</FormField>

Return Values

NameTypeDefault
namestring-
sizestring-
errorstring | boolean-
ariaIdstring-
describedBystring-
labelIdstring-
requiredboolean-
setControlId(id: string | undefined, labelable?: boolean) => void-