Skip to content

Label

Render nhãn có khả năng tiếp cận được liên kết với các form controls.

Cài Đặt

bash
npx @galaxy-stack/nebula-cli@latest add label
bash
pnpm dlx @galaxy-stack/nebula-cli@latest add label
bash
yarn dlx @galaxy-stack/nebula-cli@latest add label
bash
bunx @galaxy-stack/nebula-cli@latest add label

API Reference

Props

PropKiểuMặc địnhMô tảFrameworks
htmlForstring-ID của element mà label liên kết vớiReact, React Native
forstring-ID của element mà label liên kết vớiVue, Angular, Flutter
classNamestring''Tên class CSS cho labelReact, React Native
classstring''Tên class CSS cho labelVue, Angular, Flutter
childrenReactNode-Nội dung labelReact, React Native
textstring-Nội dung text labelFlutter

Sử Dụng

Form Cơ Bản

vue
<template>
  <div class="space-y-4">
    <div class="space-y-2">
      <Label for="username">Tên đăng nhập</Label>
      <Input id="username" placeholder="johndoe" />
    </div>
    <div class="space-y-2">
      <Label for="password">Mật khẩu</Label>
      <Input id="password" type="password" />
    </div>
  </div>
</template>
tsx
<div className="space-y-4">
  <div className="space-y-2">
    <Label htmlFor="username">Tên đăng nhập</Label>
    <Input id="username" placeholder="johndoe" />
  </div>
  <div className="space-y-2">
    <Label htmlFor="password">Mật khẩu</Label>
    <Input id="password" type="password" />
  </div>
</div>
html
<div class="space-y-4">
  <div class="space-y-2">
    <ui-label for="username">Tên đăng nhập</ui-label>
    <ui-input id="username" placeholder="johndoe" />
  </div>
  <div class="space-y-2">
    <ui-label for="password">Mật khẩu</ui-label>
    <ui-input id="password" type="password" />
  </div>
</div>

Với Dấu Bắt Buộc

vue
<template>
  <div class="space-y-2">
    <Label for="required-field">
      Họ tên
      <span class="text-destructive">*</span>
    </Label>
    <Input id="required-field" required />
  </div>
</template>
tsx
<div className="space-y-2">
  <Label htmlFor="required-field">
    Họ tên
    <span className="text-destructive">*</span>
  </Label>
  <Input id="required-field" required />
</div>
html
<div class="space-y-2">
  <ui-label for="required-field">
    Họ tên
    <span class="text-destructive">*</span>
  </ui-label>
  <ui-input id="required-field" required />
</div>

Với Checkbox

vue
<template>
  <div class="flex items-center space-x-2">
    <Checkbox id="terms" />
    <Label
      for="terms"
      class="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
    >
      Chấp nhận điều khoản và điều kiện
    </Label>
  </div>
</template>
tsx
<div className="flex items-center space-x-2">
  <Checkbox id="terms" />
  <Label
    htmlFor="terms"
    className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
  >
    Chấp nhận điều khoản và điều kiện
  </Label>
</div>
html
<div class="flex items-center space-x-2">
  <ui-checkbox id="terms" />
  <ui-label
    for="terms"
    class="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
  >
    Chấp nhận điều khoản và điều kiện
  </ui-label>
</div>

Tham Chiếu API

Props

PropKiểuMặc địnhMô tả
for / htmlForstring-Id của form control để liên kết với
class / classNamestring-CSS classes bổ sung

Đặc Biệt Theo Framework

Vue

  • Sử dụng component Label của Radix Vue
  • Props: for, asChild

React

  • Sử dụng component Label của Radix UI
  • Props: htmlFor, asChild

Angular

  • Implementation tùy chỉnh với HostBinding
  • Props: for, class

Khả Năng Tiếp Cận

  • Liên kết đúng label với form control sử dụng thuộc tính for
  • Hỗ trợ click để focus input liên kết
  • Hoạt động với screen readers
  • Hỗ trợ styling trạng thái disabled (peer-disabled)

Tạo Kiểu

Label component sử dụng các style cơ bản:

css
text-sm font-medium leading-none
peer-disabled:cursor-not-allowed
peer-disabled:opacity-70

Bạn có thể override hoặc mở rộng các styles này bằng cách truyền custom classes.

Components Liên Quan

Tác Giả

Bùi Trọng Hiếu (kevinbui)

Giấy Phép

MIT © 2025 Bùi Trọng Hiếu (kevinbui)

Khả năng truy cập

  • Điều hướng bàn phím: [TODO]
  • Đọc màn hình: [TODO]
  • Quản lý focus: [TODO]
  • Tuân thủ WCAG: Tuân thủ WCAG 2.1 cấp độ AA

Tác giả

Bùi Trọng Hiếu (kevinbui)

Giấy phép

MIT © 2025 Bùi Trọng Hiếu (kevinbui)

Phát hành theo Giấy phép MIT.