Label component is used as a text label for items in an interface. Most often used inside other form components.

Open in new tab
import { Label } from "@kesko/components";

export default () => (
  <Label>Default label</Label>
);
import { Label } from "@kesko/components";

const subtle = {
  "--k-label-text": "var(--k-color-text-dim)",
  "--k-label-weight": "var(--k-font-weight-medium)",
  "--k-label-required": "var(--k-color-status-error)",
};

export default () => (
  <Label required style={subtle}>
    Custom label
  </Label>
);
import { Label } from "@kesko/components";

export default () => (
  <Label required>Required label</Label>
);

Properties #

NameDescriptionTypeDefault
htmlFor

The id of the form control this label describes.

string—
required

Shows a required marker after the text. The marker is decorative, set required on the control as well.

booleanfalse

Styles #

CSS custom properties provide finer-grained control than properties. See Styling API for more details. Prefer the Properties API when it suits your need.

NameDescriptionDefault
--k-label-text

Overrides the text color.

var(--k-color-text-base)
--k-label-weight

Overrides the font weight.

var(--k-font-weight-bold)
--k-label-size

Overrides the font size.

var(--k-font-size-md)
--k-label-required

Overrides the required indicator color. Defaults to the label text color.

currentColor

Accessibility #

Label is built on a native <label>, so it behaves as expected with assistive technologies.

  • Point it at the control. Set htmlFor to the control's id. That is what makes a screen reader read the label as the control's name and what makes a click on the label focus the control. Wrapping the control inside the label works too.
  • Keep it visible. Placeholder text disappears as soon as someone types. If a label really has no room, wrap it in Visually Hidden rather than leaving it out.
  • required only draws the marker. It is hidden from assistive technology. Set required on the control as well, which is what gets announced and what blocks the form from submitting.
  • Check contrast if you override the color. --k-label-text needs 4.5:1 against the surface the label sits on. Verify with the WebAIM contrast checker.

See the accessibility guidelines and the A11Y checklist for more details.


Usage #

There are two necessary parts to using the Label component. The @kesko/components package and the CSS Framework are required. Everything else is optional:

npm install @kesko/components @kesko/css

Once installed, load the CSS Framework in your app’s entry point or root layout:

import "@kesko/css";

Then, you can import the Label:

import { Label } from "@kesko/components";

Projects without React can get the same styles from @kesko/css by importing the components stylesheet as well:

@import "@kesko/css";
@import "@kesko/css/components";

Resources #