본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Tabs (탭)

원문 보기 위키 갱신

Tabs는 콘텐츠를 탭 섹션으로 정리해 주는 컴포넌트예요. 트리거로 탭을 전환하고, 키보드 방향키·활성화 방식·패널 포커스까지 접근성을 고려해 설계되어 있습니다. 데이터 속성으로 각 상태를 구분해 스타일을 입힐 수 있습니다.

출처: 문서

본문

<script lang="ts">
  import { Tabs } from "bits-ui";
  import Airplane from "phosphor-svelte/lib/Airplane";
</script>
<div class="pt-6">
  <Tabs.Root
    value="outbound"
    class="rounded-card border-muted bg-background-alt shadow-card w-[390px] border p-3"
  >
    <Tabs.List
      class="rounded-9px bg-dark-10 shadow-mini-inset dark:bg-background grid w-full grid-cols-2 gap-1 p-1 text-sm font-semibold leading-[0.01em] dark:border dark:border-neutral-600/30"
    >
      <Tabs.Trigger
        value="outbound"
        class="data-[state=active]:shadow-mini dark:data-[state=active]:bg-muted h-8 rounded-[7px] bg-transparent py-2 data-[state=active]:bg-white"
        >Outbound</Tabs.Trigger
      >
      <Tabs.Trigger
        value="inbound"
        class="data-[state=active]:shadow-mini dark:data-[state=active]:bg-muted h-8 rounded-[7px] bg-transparent py-2 data-[state=active]:bg-white"
        >Inbound</Tabs.Trigger
      >
    </Tabs.List>
    <Tabs.Content value="outbound" class="select-none pt-3">
      <div class="grid grid-cols-3 grid-rows-2 gap-0 p-4 pb-1">
        <div class="text-left">
          <h4
            class="mb-2 text-[20px] font-semibold leading-none tracking-[-0.01em]"
          >
            Prague
          </h4>
          <p class="text-muted-foreground text-sm font-medium">06:05</p>
        </div>
        <div class="self-end text-center">
          <p class="text-muted-foreground text-sm font-medium">3h 30m</p>
        </div>
        <div class="text-right">
          <h4
            class="mb-2 text-[20px] font-semibold leading-none tracking-[-0.01em]"
          >
            Malaga
          </h4>
          <p class="text-muted-foreground text-sm font-medium">06:05</p>
        </div>
        <div class="relative col-span-3">
          <hr
            class="border-border-input border-1 relative top-4 h-px border-dashed"
          />
          <div class="bg-background-alt absolute left-1/2 -translate-x-1/2 p-1">
            <Airplane class="text-muted-foreground size-6 rotate-90" />
          </div>
        </div>
      </div>
    </Tabs.Content>
    <Tabs.Content value="inbound" class="select-none pt-3">
      <div class="grid grid-cols-3 grid-rows-2 gap-0 p-4 pb-1">
        <div class="text-left">
          <h4
            class="mb-2 text-[20px] font-semibold leading-none tracking-[-0.01em]"
          >
            Malaga
          </h4>
          <p class="text-muted-foreground text-sm font-medium">07:25</p>
        </div>
        <div class="self-end text-center">
          <p class="text-muted-foreground text-sm font-medium">3h 20m</p>
        </div>
        <div class="text-right">
          <h4
            class="mb-2 text-[20px] font-semibold leading-none tracking-[-0.01em]"
          >
            Prague
          </h4>
          <p class="text-muted-foreground text-sm font-medium">10:45</p>
        </div>
        <div class="relative col-span-3">
          <hr
            class="border-border-input border-1 relative top-4 h-px border-dashed"
          />
          <div class="bg-background-alt absolute left-1/2 -translate-x-1/2 p-1">
            <Airplane class="text-muted-foreground size-6 rotate-90" />
          </div>
        </div>
      </div>
    </Tabs.Content>
  </Tabs.Root>
</div>

구조

<script lang="ts">
  import { Tabs } from "bits-ui";
</script>
<Tabs.Root>
  <Tabs.List>
    <Tabs.Trigger />
  </Tabs.List>
  <Tabs.Content />
</Tabs.Root>

value 상태 다루기

이번 절에서는 컴포넌트의 value 상태를 어떻게 관리하는지 다뤄요.

양방향 바인딩 (Two-Way Binding)

간단하고 자동적인 상태 동기화를 위해 bind:value를 사용해요:

<script lang="ts">
  import { Tabs } from "bits-ui";
  let myValue = $state("");
</script>
<button onclick={() => (myValue = "tab-1")}> Activate tab 1 </button>
<Tabs.Root bind:value={myValue}>
  <!-- -->
</Tabs.Root>

완전 제어 (Fully Controlled)

상태의 읽기와 쓰기를 완전히 통제하고 싶다면 Function Binding을 사용해요.

<script lang="ts">
  import { Tabs } from "bits-ui";
  let myValue = $state("");
  function getValue() {
    return myValue;
  }
  function setValue(newValue: string) {
    myValue = newValue;
  }
</script>
<Tabs.Root bind:value={getValue, setValue}>
  <!-- ... -->
</Tabs.Root>

방향 (Orientation)

orientation prop은 Tabs 컴포넌트의 방향을 결정하는 데 쓰여요. 이 값은 키보드 탐색 방식에도 영향을 줍니다.

orientation이 'horizontal'이면 ArrowLeft와 ArrowRight 키가 각각 이전·다음 탭으로 포커스를 옮겨요. 'vertical'이면 ArrowUp과 ArrowDown 키가 각각 이전·다음 탭으로 포커스를 옮깁니다.

<Tabs.Root orientation="horizontal">
  <!-- ... -->
</Tabs.Root>
<Tabs.Root orientation="vertical">
  <!-- ... -->
</Tabs.Root>

활성화 모드 (Activation Mode)

기본적으로 Tabs 컴포넌트는 트리거에 포커스하면 그 트리거와 연결된 탭을 자동으로 활성화해요. 이 동작을 끄려면 activationMode prop을 'manual'로 설정하면 됩니다.

'manual'로 설정하면 사용자가 트리거를 눌러야만 탭이 활성화돼요.

<Tabs.Root activationMode="manual">
  <!-- ... -->
</Tabs.Root>

패널 포커스

Tabs.Content는 기본적으로 tabindex="0"이라 키보드 사용자가 패널에 포커스할 수 있어요. 패널에 포커스 가능한 요소가 없거나 첫 의미 있는 콘텐츠가 포커스 불가능할 땐 — 나중에 버튼이나 링크가 나오더라도 — 이 기본값을 유지하는 게 좋습니다.

첫 의미 있는 콘텐츠가 포커스 가능하면 tabindex={-1}로 설정해서 Tab 키가 바로 그 요소로 이동하게 해요. WAI-ARIA tabs 패턴을 따라 콘텐츠에 맞게 선택하세요.

<Tabs.Content value="settings" tabindex={-1}>
  <button>Update settings</button>
</Tabs.Content>

API 참조

Tabs.Root

다른 탭 컴포넌트들을 담고 있는 루트 탭 컴포넌트예요.

Property Type Description Details
value $bindable string The active tab value.Default:  —— undefined
onValueChange function - (value: string) => void A callback function called when the active tab value changes.Default:  —— undefined
activationMode enum - 'automatic' | 'manual' How the activation of tabs should be handled. If set to 'automatic', the tab will be activated when the trigger is focused. If set to 'manual', the tab will be activated when the trigger is pressed.Default: 'automatic'
disabled boolean Whether or not the tabs are disabled.Default: false
loop boolean Whether or not the tabs should loop when navigating with the keyboard.Default: true
orientation enum - 'horizontal' | 'vertical' The orientation of the tabs.Default: 'horizontal'
ref $bindable HTMLDivElement 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-orientation enum - 'horizontal' | 'vertical' The orientation of the tabs.
data-tabs-root '' Present on the root element.

Tabs.List

탭 트리거들을 담고 있는 컴포넌트예요.

Property Type Description Details
ref $bindable HTMLDivElement 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-orientation enum - 'horizontal' | 'vertical' The orientation of the tabs.
data-tabs-list '' Present on the list element.

Tabs.Trigger

탭 하나의 트리거예요.

Property Type Description Details
value required string The value of the tab this trigger represents.Default:  —— undefined
disabled boolean Whether or not the tab is disabled.Default: false
ref $bindable HTMLButtonElement 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-state enum - 'active' | 'inactive' The state of the tab trigger.
data-value '' The value of the tab this trigger represents.
data-orientation enum - 'horizontal' | 'vertical' The orientation of the tabs.
data-disabled '' Present when the tab trigger is disabled.
data-tabs-trigger '' Present on the trigger elements.

Tabs.Content

탭의 콘텐츠를 담고 있는 패널이에요.

Property Type Description Details
value required string The value of the tab this content represents.Default:  —— undefined
ref $bindable HTMLDivElement 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-tabs-content '' Present on the content elements.

더 알아보기 (Learn more)

Tabs는 키보드 탐색과 활성 상태를 다루기 때문에, 접근성 고려가 많은 폼·내비게이션 구조에서 자주 쓰여요. value 상태를 bind:value로 다루는 방식은 다른 선택형 컴포넌트와 같아서, 상태 관리 패턴을 함께 익혀 두면 유용합니다.