enes

Label

A Label provides an accessible caption for a form control. It supports different sizes and weights, can be marked as required or disabled, and can be positioned relative to the control it wraps.

Usage

<script>
	import { Label, TextBox } from 'fluentui-svelte';
</script>

<Label label="Email"><TextBox /></Label>

Size

Use the size prop to control the label text size. Possible values: 'small', 'medium', 'large'.

Weight

Use the weight prop to switch between a regular and a semibold label.

Label Position

Use the labelPosition prop to place the label text relative to the wrapped control. Possible values: 'before', 'after', 'above', 'below'.

Required

Pass a required object to mark the label as required and render a required indicator, which defaults to an asterisk.

Tooltip

Pass tooltipContent, as text, a snippet or a component, to show more about the field in a tooltip from a badge after the label text. The badge is a button named after the label, so keyboard and screen reader users reach it too; tooltipBadge replaces its info icon, and tooltipButtonAttributes its aria-label. People cannot move into a tooltip, so keep links and buttons out of it.

Label form controls

A control inside Label needs no id or for. The own text of a Checkbox, RadioButton or ToggleSwitch inside it is not rendered: the Label names the control. A native <label for> outside a Label names a picker (Dropdown, TagPicker, DatePicker, TimePicker, CalendarDatePicker) only once the page hydrates, because the picker reads it from the page: when rendering on the server, use Label or pass aria-labelledby. A field with an id of its own keeps it: when the text comes before the field (labelPosition 'before' or 'above', the default), pass that id to for too, or the label the server writes points at no field until the page hydrates.

Form Control Attributes

Label renders its text and field as siblings, and is for the first field inside it, which takes the id of the label when it has none of its own. Pass for to point the label at another field. HTML attributes, class and ref apply to the label text element; wrapperAttributes and wrapperRef apply to the container. Size and weight affect only the label text.

Component Props

NameTypeDefaultDescription
as'div' | 'span''div'The element used to wrap the label and its field.
childrenSnippetThe field described by the label, rendered beside the label text.
wrapperRef bindableHTMLDivElement | HTMLSpanElementThe DOM reference of the wrapper element.
wrapperAttributesHTMLAttributesThe attributes to spread on the wrapper element.
labelstring'Label Element'The text of the label.
size'small' | 'medium' | 'large''medium'The size of the label text.
weight'regular' | 'semibold''regular'The weight of the label text.
labelPosition'before' | 'after' | 'above' | 'below''after'Where the label sits relative to the field it describes.
required{ abbr?: string; message: string; }Marks the field as required, and describes why through the given message.
disabledbooleanRenders the label as disabled, to match the field it describes.
ref bindableHTMLLabelElement | nullThe DOM reference of the label element.
idstringGeneratedThe id of the label element. A control inside the label is named after it, including a picker whose visible control stands in for its native field.
forstringThe field insideThe id of the field the label is for. Without it, the label is for the first field inside it, which takes a generated id when it has none of its own. With labelPosition before or above and a field that has an id of its own, pass that id here, so the server renders the association too.
tooltipContentstring | Snippet | ComponentMore about the field, in a tooltip shown from a badge after the label text, on hover and on focus. People cannot move into a tooltip, so keep links and buttons out of it.
tooltipBadgeSnippet | ComponentInfoRegularWhat the badge that shows the tooltip displays. Only rendered with tooltipContent.
tooltipPropsTooltipPropsThe props to spread on the tooltip.
tooltipButtonAttributesHTMLButtonAttributesThe attributes to spread on the button of the badge. Its name is the label text followed by its aria-label.
HTMLLabelAttributes without for
Edit this Doc View as Markdown