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

Toolbar (툴바)

원문 보기 위키 갱신

Toolbar는 자주 쓰는 동작이나 도구를 컴팩트한 막대에 모아 보여 주는 컴포넌트예요. 버튼·링크·토글 그룹을 한데 배치해 워드 프로세서나 편집 도구 같은 화면의 공용 컨트롤 바를 만들 때 적합합니다. 키보드 탐색까지 함께 다룹니다.

출처: 문서

본문

<script lang="ts">
  import { Separator, Toolbar } from "bits-ui";
  import Sparkle from "phosphor-svelte/lib/Sparkle";
  import TextAlignCenter from "phosphor-svelte/lib/TextAlignCenter";
  import TextAlignLeft from "phosphor-svelte/lib/TextAlignLeft";
  import TextAlignRight from "phosphor-svelte/lib/TextAlignRight";
  import TextB from "phosphor-svelte/lib/TextB";
  import TextItalic from "phosphor-svelte/lib/TextItalic";
  import TextStrikethrough from "phosphor-svelte/lib/TextStrikethrough";
  let text = $state(["bold"]);
  let align = $state("");
</script>
<Toolbar.Root
  class="rounded-10px border-border bg-background-alt shadow-mini flex h-12 min-w-max items-center justify-center border px-[4px] py-1"
>
  <Toolbar.Group
    bind:value={text}
    type="multiple"
    class="flex items-center gap-x-0.5"
  >
    <Toolbar.GroupItem
      aria-label="toggle bold"
      value="bold"
      class="rounded-9px bg-background-alt text-foreground/60 hover:bg-muted active:bg-dark-10 data-[state=on]:bg-muted data-[state=on]:text-foreground/80 active:data-[state=on]:bg-dark-10 inline-flex size-10 items-center justify-center transition-all active:scale-[0.98]"
    >
      <TextB class="size-6" />
    </Toolbar.GroupItem>
    <Toolbar.GroupItem
      aria-label="toggle italic"
      value="italic"
      class="rounded-9px bg-background-alt text-foreground/60 hover:bg-muted active:bg-dark-10 data-[state=on]:bg-muted data-[state=on]:text-foreground/80 active:data-[state=on]:bg-dark-10 inline-flex size-10 items-center justify-center transition-all active:scale-[0.98]"
    >
      <TextItalic class="size-6" />
    </Toolbar.GroupItem>
    <Toolbar.GroupItem
      aria-label="toggle strikethrough"
      value="strikethrough"
      class="rounded-9px bg-background-alt text-foreground/60 hover:bg-muted active:bg-dark-10 data-[state=on]:bg-muted data-[state=on]:text-foreground/80 active:data-[state=on]:bg-dark-10 inline-flex size-10 items-center justify-center transition-all active:scale-[0.98]"
    >
      <TextStrikethrough class="size-6" />
    </Toolbar.GroupItem>
  </Toolbar.Group>
  <Separator.Root class="bg-dark-10 -my-1 mx-1 w-[1px] self-stretch" />
  <Toolbar.Group
    bind:value={align}
    type="single"
    class="flex items-center gap-x-0.5"
  >
    <Toolbar.GroupItem
      aria-label="align left"
      value="left"
      class="rounded-9px bg-background-alt text-foreground/60 hover:bg-muted active:bg-dark-10 data-[state=on]:bg-muted data-[state=on]:text-foreground/80 active:data-[state=on]:bg-dark-10 inline-flex size-10 items-center justify-center transition-all active:scale-[0.98]"
    >
      <TextAlignLeft class="size-6" />
    </Toolbar.GroupItem>
    <Toolbar.GroupItem
      aria-label="align center"
      value="center"
      class="rounded-9px bg-background-alt text-foreground/60 hover:bg-muted active:bg-dark-10 data-[state=on]:bg-muted data-[state=on]:text-foreground/80 active:data-[state=on]:bg-dark-10 inline-flex size-10 items-center justify-center transition-all active:scale-[0.98]"
    >
      <TextAlignCenter class="size-6" />
    </Toolbar.GroupItem>
    <Toolbar.GroupItem
      aria-label="align right"
      value="right"
      class="rounded-9px bg-background-alt text-foreground/60 hover:bg-muted active:bg-dark-10 data-[state=on]:bg-muted data-[state=on]:text-foreground/80 active:data-[state=on]:bg-dark-10 inline-flex size-10 items-center justify-center transition-all active:scale-[0.98]"
    >
      <TextAlignRight class="size-6" />
    </Toolbar.GroupItem>
  </Toolbar.Group>
  <Separator.Root class="bg-dark-10 -my-1 mx-1 w-[1px] self-stretch" />
  <div class="flex items-center">
    <Toolbar.Button
      class="rounded-9px text-foreground/80 hover:bg-muted active:bg-dark-10 inline-flex items-center justify-center  px-3 py-2 text-sm font-medium transition-all active:scale-[0.98]"
    >
      <Sparkle class="mr-2 size-6" />
      <span> Ask AI </span>
    </Toolbar.Button>
  </div>
</Toolbar.Root>

구조

<script lang="ts">
  import { Toolbar } from "bits-ui";
</script>
<Toolbar.Root>
  <Toolbar.Group>
    <Toolbar.GroupItem />
  </Toolbar.Group>
  <Toolbar.Link />
  <Toolbar.Button />
</Toolbar.Root>

value 상태 다루기

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

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

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

<script lang="ts">
  import { Toolbar } from "bits-ui";
  let myValue = $state("");
</script>
<button onclick={() => (myValue = "item-1")}> Press item 1 </button>
<Toolbar.Root>
  <Toolbar.Group type="single" bind:value={myValue}>
    <!-- ... -->
  </Toolbar.Group>
</Toolbar.Root>

완전 제어 (Fully Controlled)

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

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

API 참조

Toolbar.Root

툴바를 담고 있는 루트 컴포넌트예요.

Property Type Description Details
loop boolean Whether or not the toolbar should loop when navigating.Default: true
orientation enum - 'horizontal' | 'vertical' The orientation of the toolbar.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 - 'vertical' | 'horizontal' The orientation of the component.
data-toolbar-root '' Present on the root element.

Toolbar.Button

툴바 안의 버튼이에요.

Property Type Description Details
disabled boolean Whether or not the button 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-toolbar-button '' Present on the button element.

툴바 안의 링크예요.

Property Type Description Details
ref $bindable HTMLAnchorElement 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-toolbar-link '' Present on the link element.

Toolbar.Group

툴바 안의 토글 항목 묶음이에요.

Property Type Description Details
type required enum - 'single' | 'multiple' The type of the component, used to determine the type of the value, when 'multiple' the value will be an array.Default:  —— undefined
value $bindable union - string | string[] The value of the toggle group. If the type is multiple, this will be an array of strings, otherwise it will be a string.Default:  —— undefined
onValueChange function - (value: string) => void | (value: string[]) => void A callback function called when the value changes.Default:  —— undefined
disabled boolean Whether or not the switch is disabled.Default: false
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-toolbar-group '' Present on the group element.

Toolbar.GroupItem

툴바 토글 그룹 안의 토글 항목이에요.

Property Type Description Details
value required string The value of the toolbar toggle group item. When the toolbar toggle group item is selected, toolbar the toggle group's value will be set to this value if in single mode, or this value will be pushed to the toggle group's array value if in multiple mode.Default:  —— undefined
disabled boolean Whether or not the item 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 - 'on' | 'off' Whether the toolbar toggle item is in the on or off state.
data-value '' The value of the toolbar toggle item.
data-disabled '' Present when the toolbar toggle item is disabled.
data-toolbar-item '' Present on the toolbar toggle item.

더 알아보기 (Learn more)

Toolbar는 버튼·링크·토글 그룹을 한데 묶어 공용 컨트롤 바를 만드는 데 특화되어 있어요. 도구 모음에서 각 컨트롤의 상태를 설명하는 프롬프트가 필요하다면 Tooltip을 함께 쓰면 자연스럽고, 정렬 범위 선택은 Toggle Group으로 처리할 수 있습니다.