Label
A label for a form control.
There is no WAI-ARIA APG pattern for labels. Label renders a native <label>, which names the control it is associated with.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Label } from '@leve-ui/label';
import styles from './default.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<div class="label-default-field">
<Label.Root for="email" class="label-default">
Email
</Label.Root>
<input id="email" type="email" placeholder="[email protected]" class="label-default-input" />
</div>
);
});.label-default-field {
display: flex;
flex-direction: column;
gap: 6px;
width: 280px;
font-family: inherit;
font-size: 14px;
}
.label-default {
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
}
.label-default-input {
height: 36px;
padding: 0 12px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
font: inherit;
font-size: 14px;
}
.label-default-input::placeholder {
color: light-dark(#475569, #c9ddf0);
opacity: 0.7;
}
.label-default-input:focus-visible {
border-color: #009dfd;
outline: 2px solid #009dfd;
outline-offset: 2px;
}Installation#
Install the individual package, or use @leve-ui/core to get every component at once:
pnpm add @leve-ui/labelnpm install @leve-ui/labelyarn add @leve-ui/labelbun add @leve-ui/labelpnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/coreAnatomy#
import { Label } from '@leve-ui/label'; // or '@leve-ui/core'
<Label.Root />
| Part | Element | Description |
|---|---|---|
Label.Root | <label> | The label. Associate it with a control with for, or by wrapping the control. |
Examples#
Wrapping#
Wrapping a control in Label.Root associates them without for and id.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Label } from '@leve-ui/label';
import styles from './wrapping.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Label.Root class="label-wrapping-inline">
<input type="checkbox" class="label-wrapping-checkbox" />
Accept terms
</Label.Root>
);
});/* The label wraps the checkbox, so clicking the text toggles it */
.label-wrapping-inline {
display: inline-flex;
align-items: center;
gap: 8px;
font-family: inherit;
font-size: 14px;
color: light-dark(#0f172a, #ffffff);
cursor: pointer;
}
.label-wrapping-checkbox {
width: 16px;
height: 16px;
margin: 0;
accent-color: #009dfd;
cursor: pointer;
}
.label-wrapping-checkbox:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}API Reference#
Root#
No props of its own. Accepts all <label> attributes.
| Data attribute | Value |
|---|---|
data-leve-label | Always present |
All props, including for, are passed to the native <label>. Inside a Field, use Field.Label instead: it is associated with the control automatically.
Accessibility#
- The label becomes the accessible name of the associated control. Set
forto the control'sid, or wrap the control in the label. - Clicking the label focuses the control, or toggles it when it is a checkbox or radio button.
- Label has no keyboard interaction.