Renders an accessible label associated with controls.

With Checkbox
With Input
Disabled
With Textarea

Installation

Install the dependencies

pnpm add cn class-variance-authority

Copy the component

Copy the following files into src/lib/components/ui/label.

src/lib/components/ui/label/Label.svelte
<script lang="ts">
  import type { HTMLLabelAttributes } from 'svelte/elements';
  import { cn } from 'cn';
  let { children, class: classProp, ...props }: HTMLLabelAttributes = $props();
</script>
<label data-slot="label" {...props} class={cn('gap-2 text-sm leading-none font-medium group-data-[disabled=true]:opacity-50 peer-disabled:opacity-50 flex items-center select-none group-data-[disabled=true]:pointer-events-none peer-disabled:cursor-not-allowed', classProp)}>
  {@render children?.()}
</label>
src/lib/components/ui/label/index.ts
export { default as Label } from './Label.svelte';

Update the import paths

Change the imports to match your project setup.

Usage

<script lang="ts">
  import { Label } from '$lib/components/ui/label';
</script>
<Label for="email">Your email address</Label>