Label 문서
Label은 연결된 UI 요소를 식별하거나 설명해 주는 컴포넌트예요. 접근성 관점에서 입력 요소에 이름을 붙이는 일은 생각보다 중요해서, Bits UI가 이 역할을 전담하는 Label 프리미티브를 제공해요.
출처: 문서
본문
아래 예시에서는 체크박스와 라벨을 연결해서 사용해요. 라벨의 for 속성을 체크박스의 id와 맞춰 주면, 라벨을 클릭했을 때 체크박스가 함께 토글되는 걸 확인할 수 있어요.
<script lang="ts">
import { Checkbox, Label } from "bits-ui";
import Check from "phosphor-svelte/lib/Check";
import Minus from "phosphor-svelte/lib/Minus";
</script>
<div class="flex items-center space-x-3">
<Checkbox.Root
id="terms"
aria-labelledby="terms-label"
class="border-muted bg-foreground data-[state=unchecked]:border-border-input data-[state=unchecked]:bg-background data-[state=unchecked]:hover:border-dark-40 peer inline-flex size-[25px] items-center justify-center rounded-md border transition-all duration-150 ease-in-out active:scale-[0.98]"
name="hello"
>
{#snippet children({ checked, indeterminate })}
<div class="text-background inline-flex items-center justify-center">
{#if indeterminate}
<Minus class="size-[15px]" weight="bold" />
{:else if checked}
<Check class="size-[15px]" weight="bold" />
{/if}
</div>
{/snippet}
</Checkbox.Root>
<Label.Root
id="terms-label"
for="terms"
class="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
Accept terms and conditions
</Label.Root>
</div>
구조 (Structure)
Label의 기본 구조는 아주 단순해요. Label.Root 하나로 시작하고, 그 안에 원하는 내용을 채워 넣으면 돼요.
<script lang="ts">
import { Label } from "bits-ui";
</script>
<Label.Root />
API Reference
Label.Root
일반 입력 요소와 함께 쓸 수 있도록 기능이 보강된 라벨 컴포넌트예요.
| Property | Type | Description | Details |
|---|---|---|---|
ref $bindable |
HTMLLabelElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-label-root |
'' |
Present on the root element. |
더 알아보기 (Learn more)
Label은 폼 입력 요소의 이름을 붙여 주는 역할을 해서, Checkbox나 Input 같은 다른 Bits UI 컴포넌트와 함께 쓰기 좋아요. 덕분에 스크린 리더가 입력 요소를 정확히 읽어 줄 수 있고, 라벨을 클릭했을 때 입력이 함께 반응하는 자연스러운 동작도 그대로 얻을 수 있어요.