Label

Label form elements with accessibility

Label associates text with form controls and preserves that relationship for assistive technology.

Features

  • Nested and custom control support

  • Inline sizing and styling

  • Web accessibility with aria-labelledby

Installation

Label is already installed in tamagui, or you can install it independently:

yarn add @tamagui/label

Usage

import { Label } from 'tamagui'
export default () => (
<>
<Label htmlFor="name">Name</Label>
<Input id="name" defaultValue="Nate Wienert" />
</>
)

Accessibility

Use with Input or other form elements to automatically get correct labelling by id and aria-labelledby. You can also use the provided useLabelContext hook to build your own controls.

Label supports all standard ARIA attributes for form accessibility. These work on both web and React Native. See the React Native Accessibility docs for native behavior.

Required fields

<Label htmlFor="email" aria-required>
Email
</Label>
<Input id="email" aria-required />

Invalid state

<Label htmlFor="email" aria-invalid={hasError}>
Email
</Label>
<Input id="email" aria-invalid={hasError} aria-errormessage="email-error" />
{hasError && <html.span id="email-error">Please enter a valid email</html.span>}

With description

<Label htmlFor="password" aria-describedby="password-hint">
Password
</Label>
<Input id="password" aria-describedby="password-hint" />
<html.span id="password-hint">Must be at least 8 characters</html.span>

API reference

Label

Labels extend SizableText inheriting all the Tamagui standard props, plus:

Props

  • htmlFor (required)

    string

    Matches the `id` of a form element to associate with.

  • unstyled

    boolean

    Removes all default Tamagui styles.

  • Accessibility props

    Label passes through all ARIA attributes. These are the most commonly used for form labeling:

    Props

  • aria-required

    boolean

    Indicates user input is required before form submission.

  • aria-invalid

    boolean

    Indicates the associated field has a validation error.

  • aria-disabled

    boolean

    Indicates the associated control is disabled.

  • aria-describedby

    string

    References an element providing additional description such as hint text or error messages.

  • aria-labelledby

    string

    References another element that labels this one. `Label` sets this automatically on the target input.

  • aria-details

    string

    References an element providing extended description or instructions.

  • These props work cross-platform. On web they render as standard ARIA attributes. On React Native they map to the native accessibility system. See the React Native Accessibility docs for details.