Skip to content

Label

Names a control, and wires the association up for you.

View source View as Markdown

Label renders the <label> for a control and points it at the right element. Inside a Field it needs no props at all; outside one, give it a for.

App.vue
<template>
  <div class="flex w-[17rem] flex-col gap-6">
    <Field.Root class="group flex flex-col gap-1.5">
      <div :class="box">
        <div :class="stack">
          <span :class="line">
            <Input v-model="workspace" :class="control" placeholder=" " />
          </span>
          <Label :class="label">Workspace</Label>
        </div>
      </div>
    </Field.Root>

    <Field.Root
      class="group flex flex-col gap-1.5"
      :options="{ disabled: true }"
    >
      <div :class="box">
        <div :class="stack">
          <span :class="line">
            <Input
              :class="control"
              model-value="eu-central-1"
              placeholder=" "
            />
          </span>
          <Label :class="label">Region</Label>
        </div>
      </div>
    </Field.Root>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Field } from '@maas/mirror/vue/Field'
import { Input } from '@maas/mirror/vue/Input'
import { Label } from '@maas/mirror/vue/Label'

const workspace = ref('')

const box = [
  'relative isolate flex h-12 w-full cursor-text items-center justify-between gap-1.5',
  'rounded-component-lg border-surface border-2 px-[0.875rem]',
  'transition-all duration-100 ease-linear [&_*]:transition-all [&_*]:duration-100 [&_*]:ease-linear',
  'outline-4 outline-transparent focus-within:focus-ring',
  'group-[[data-invalid=true]:not([data-focused=true])]:border-danger-subtle',
  'group-[[data-invalid=true]:not([data-focused=true])]:bg-danger-subtle',
  'group-data-[disabled=true]:border-disabled-subtle group-data-[disabled=true]:cursor-not-allowed',
].join(' ')

const stack = [
  'group/stack flex h-full max-h-full w-full flex-col-reverse',
  'items-center justify-center gap-0 px-1',
  'focus-within:gap-1 has-[input:not(:placeholder-shown)]:gap-1',
].join(' ')

const line = [
  'relative flex h-0 w-full items-end',
  'group-focus-within/stack:h-[0.9375rem]',
  'has-[input:not(:placeholder-shown)]:h-[0.9375rem]',
].join(' ')

const control = [
  'type-component-lg leading-[normal]! text-surface',
  'block box-content h-[1lh] w-full py-1.25 -my-1.25 appearance-none bg-transparent outline-none',
  'placeholder:text-transparent',
  'group-[[data-invalid=true]:not([data-focused=true])]:text-danger-on-muted',
  'data-[disabled=true]:text-disabled-solid',
].join(' ')

const label = [
  'type-component-lg leading-[normal]! text-surface-muted',
  'flex h-full w-full items-center [--mirror-label-cursor:text]',
  "before:absolute before:-inset-0.5 before:-z-10 before:content-['']",
  'group-focus-within/stack:h-auto group-focus-within/stack:text-[0.6875rem] group-focus-within/stack:[--mirror-label-cursor:auto]',
  'group-has-[input:not(:placeholder-shown)]/stack:h-auto group-has-[input:not(:placeholder-shown)]/stack:text-[0.6875rem] group-has-[input:not(:placeholder-shown)]/stack:[--mirror-label-cursor:auto]',
  'group-[[data-invalid=true]:not([data-focused=true])]:text-danger-muted',
  'group-data-[disabled=true]:text-disabled-muted',
].join(' ')
</script>

Usage guidelines

  • Give a second label in the same field its own id, or both take the field’s label ID and register it twice.must
  • A non-native label needs something to point at, so without a for and without a Field to inject one it throws missing_label_target.must
  • Inside a Field, a non-native label names the control from the other side: the control adds aria-labelledby for it, whether or not it could have been a <label for> target.may