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

BitsConfig 문서

원문 보기 위키 갱신

BitsConfig는 Bits UI 컴포넌트 전체의 기본 prop 값을 한곳에서 관리하도록 도와주는 전역 컨텍스트 제공자예요. 포털 대상이나 로캘 같은 기본값을 중앙에서 설정해 두면, 같은 prop을 매 컴포넌트에 일일이 넘기지 않아도 돼요. 실제 앱에서는 여러 컴포넌트가 함께 쓰이니까, 기본값을 한곳에 모아두는 게 훨씬 관리하기 편하죠.

출처: 문서

본문

개요 (Overview)

BitsConfig는 자기 범위 안에 있는 모든 Bits UI 컴포넌트의 기본 prop 값을 간단히 관리하도록 만들어 주는 전역 컨텍스트 제공자예요. 포털 대상이나 로캘 같은 기본값을 여기에 중앙 설정해 두면, 동일한 prop을 모든 컴포넌트에 일일이 넘길 필요가 없어져요.

주요 특징 (Key Features)

  • 범위 제한 기본값: 설정한 기본값은 해당 범위 안에 있는 컴포넌트에만 적용돼요.
  • 상속: 하위 BitsConfig 인스턴스는 상위 값을 상속받고, 필요한 값만 선택적으로 덮어쓸 수 있어요.
  • 대체값 해석: 여러 설정 계층을 따라 값을 자동으로 결정해 줘요.

기본 사용법 (Basic Usage)

최상위 레벨에서 기본 prop을 구성해 보아요:

<script lang="ts">
  import { BitsConfig, Dialog, DateField } from "bits-ui";
</script>
<!-- All components inside will use these defaults -->
<BitsConfig defaultPortalTo="#modal-root" defaultLocale="es">
  <Dialog.Root>
    <Dialog.Trigger>Open Dialog</Dialog.Trigger>
    <Dialog.Portal>
      <!-- This will portal to #modal-root by default -->
      <Dialog.Content>
        <Dialog.Title>Dialog Title</Dialog.Title>
        <Dialog.Description>Dialog content here</Dialog.Description>
        <!-- DateField will use the default locale -->
        <DateField.Root>
          <!-- ... -->
        </DateField.Root>
      </Dialog.Content>
    </Dialog.Portal>
  </Dialog.Root>
</BitsConfig>

상속과 덮어쓰기 (Inheritance & Overrides)

하위 인스턴스는 상위 값을 상속하면서 원하는 값만 덮어씁니다:

<script lang="ts">
  import { BitsConfig, Dialog, Tooltip } from "bits-ui";
</script>
<div id="main-portal"></div>
<div id="tooltip-portal"></div>
<!-- Root level config -->
<BitsConfig defaultPortalTo="#main-portal" defaultLocale="de">
  <!-- This dialog will portal to #main-portal -->
  <Dialog.Root>
    <Dialog.Portal>
      <Dialog.Content>Main dialog</Dialog.Content>
    </Dialog.Portal>
  </Dialog.Root>
  <!-- Child config overrides portal target, inherits locale -->
  <BitsConfig defaultPortalTo="#tooltip-portal">
    <!-- This tooltip will portal to #tooltip-portal -->
    <Tooltip.Root>
      <Tooltip.Trigger>Hover me</Tooltip.Trigger>
      <Tooltip.Portal>
        <Tooltip.Content>Tooltip content</Tooltip.Content>
      </Tooltip.Portal>
    </Tooltip.Root>
    <!-- This dialog will also portal to #tooltip-portal -->
    <Dialog.Root>
      <Dialog.Portal>
        <Dialog.Content>
          Nested dialog
          <!-- This date field will use the "de" locale -->
          <DateField.Root>
            <!-- ... -->
          </DateField.Root>
        </Dialog.Content>
      </Dialog.Portal>
    </Dialog.Root>
  </BitsConfig>
</BitsConfig>

실전 예시 (Real-world Examples)

전역 기본값 (Global Defaults)

앱 전체에 적용할 기본값을 루트 레이아웃에 설정해 보아요:

+layout.svelte

<script lang="ts">
  import { BitsConfig } from "bits-ui";
  import { locale } from "$lib/states/i18n.svelte.js";
  let { children } = $props();
</script>
<BitsConfig defaultPortalTo="body" defaultLocale={locale.current}>
  {@render children()}
</BitsConfig>

테마별 설정 (Theme-specific Configuration)

서로 다른 UI 영역마다 다른 포털 대상을 지정하고 싶을 때 사용해요:

<script lang="ts">
  import { BitsConfig } from "bits-ui";
  import { MyHeader, MySidebar, MyContent } from "$lib/components";
</script>
<div id="header-portals"></div>
<div id="sidebar-portals"></div>
<div id="main-portals"></div>
<header>
  <BitsConfig defaultPortalTo="#header-portals">
    <MyHeader />
  </BitsConfig>
</header>
<aside>
  <BitsConfig defaultPortalTo="#sidebar-portals">
    <MySidebar />
  </BitsConfig>
</aside>
<main>
  <BitsConfig defaultPortalTo="#main-portals">
    <MyContent />
  </BitsConfig>
</main>

라우트별 로캘 (Route-specific Locales)

특정 라우트에 해당하는 로캘을 적용할 때는 이렇게 해요:

routes/(admin)/+layout.svelte

<script lang="ts">
  import { BitsConfig } from "bits-ui";
  let { children } = $props();
</script>
<BitsConfig defaultLocale="en">
  {@render children()}
</BitsConfig>

컴포넌트 레벨 덮어쓰기 (Component-level Overrides)

개별 컴포넌트는 전역 기본값을 직접 덮어쓸 수 있어요:

<script lang="ts">
  import { BitsConfig, Dialog } from "bits-ui";
</script>
<BitsConfig defaultPortalTo="#main-portal">
  <!-- This dialog will use the config default -->
  <Dialog.Root>
    <Dialog.Portal>
      <Dialog.Content>Uses #main-portal</Dialog.Content>
    </Dialog.Portal>
  </Dialog.Root>
  <!-- This dialog overrides the config -->
  <Dialog.Root>
    <Dialog.Portal to="#special-portal">
      <Dialog.Content>Uses #special-portal</Dialog.Content>
    </Dialog.Portal>
  </Dialog.Root>
</BitsConfig>

값 결정 순서 (Value Resolution Order)

Bits UI는 기본값을 다음 우선순위로 정해요:

  1. 컴포넌트의 직접 prop (Direct Component prop)
  2. 가장 가까운 상위 BitsConfig (Nearest parent BitsConfig)
  3. 상위 BitsConfig에서 상속된 값 (Inherited from parent BitsConfig(s))
  4. 내장 컴포넌트 기본값 (Built-in component default) (예: 포털은 기본적으로 "body", 로캘은 "en")

API 레퍼런스 (API Reference)

BitsConfig

모든 Bits UI 컴포넌트의 기본 prop을 설정하는 전역 컨텍스트 제공자예요.

Property Type Description Details
defaultPortalTo union - Element | string Where to render the content when it is open. Defaults to the body.Default: document.body
defaultLocale string The default locale to use for the app.Default: en
children Snippet The children content to render.Default: —— undefined

더 알아보기 (Learn more)

BitsConfig로 설정한 기본값은 Portal 같은 하위 컴포넌트에서도 그대로 적용돼요. 특히 defaultPortalTo로 포털 대상을 바꾸는 방법은 Portal 문서에서 더 자세히 확인할 수 있어요. 키보드 접근성 상태를 추적하는 IsUsingKeyboard와 함께 보면 Bits UI의 공용 상태 관리 방식을 이해하는 데 도움이 돼요.