Skip to main content
← All Demos

Label

Label provides a text caption for form controls and connects to inputs using htmlFor.

1. Basic Label + Input

Clicking the label focuses the associated input. Screen readers read the label when the input is focused.

Test: Click the label text — it should focus the input field.
<Label htmlFor='basic-input'>Username</Label>
<input id='basic-input' type='text' />

2. Required Field Label

Label with required prop. Exposes data-required for styling hooks. The control itself should also have required or aria-required.

<Label htmlFor='required-input' required>Email *</Label>
<input id='required-input' type='email' required />

3. Optional Field Label

Label with optional prop for visual indicator. Exposes data-optional.

<Label htmlFor='optional-input' optional>Phone</Label>
<input id='optional-input' type='tel' />

4. Disabled Field Label

Label with disabled prop. Exposes data-disabled. The associated control should also be disabled.

Expected: Clicking the label should NOT focus the disabled input.
<Label htmlFor='disabled-input' disabled>Organization</Label>
<input id='disabled-input' disabled value='Acme Corp' />

5. Read-only Field Label

Label with readOnly prop. Exposes data-readonly.

<Label htmlFor='readonly-input' readOnly>Account ID</Label>
<input id='readonly-input' readOnly value='ACC-12345' />

6. Invalid Field Label

Label with invalid prop. Exposes data-invalid. The control should have aria-invalid="true".

Screen reader: Should announce invalid state and error message.
<Label htmlFor='invalid-input' invalid>Password</Label>
<input id='invalid-input' aria-invalid='true' aria-describedby='invalid-error' />
<div id='invalid-error' role='alert'>Password must be at least 8 characters</div>

7. Polymorphic Rendering

Label can render as different HTML elements using the as prop.

As <span>
As <div>
<Label as='span' htmlFor='label-span'>As span</Label>
<input id='label-span' type='text' />

<Label as='div' htmlFor='label-div'>As div</Label>
<input id='label-div' type='text' />
← All Demos