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

Toggle Group (토글 그룹)

원문 보기 위키 갱신

ToggleGroup은 여러 토글 컨트롤을 하나로 묶어 하나 또는 여러 옵션을 선택할 수 있게 해 주는 컴포넌트예요. 단일 선택과 다중 선택 타입을 모두 지원하고, 선택된 값은 컴포넌트 상태로 관리됩니다. 세그먼트 컨트롤 같은 UI를 만들 때 특히 편리합니다.

출처: 문서

본문

<script lang="ts">
  import { ToggleGroup } from "bits-ui";
  import TextB from "phosphor-svelte/lib/TextB";
  import TextItalic from "phosphor-svelte/lib/TextItalic";
  import TextStrikethrough from "phosphor-svelte/lib/TextStrikethrough";
  let value: string[] = $state(["bold"]);
</script>
<ToggleGroup.Root
  bind:value
  type="multiple"
  class="h-input rounded-card-sm border-border bg-background-alt shadow-mini flex items-center gap-x-0.5 border px-[4px] py-1"
>
  <ToggleGroup.Item
    aria-label="toggle bold"
    value="bold"
    class="rounded-9px bg-background-alt hover:bg-muted active:bg-dark-10 data-[state=on]:bg-muted data-[state=off]:text-foreground-alt data-[state=on]:text-foreground 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" />
  </ToggleGroup.Item>
  <ToggleGroup.Item
    aria-label="toggle italic"
    value="italic"
    class="rounded-9px bg-background-alt hover:bg-muted active:bg-dark-10 data-[state=on]:bg-muted data-[state=off]:text-foreground-alt data-[state=on]:text-foreground 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" />
  </ToggleGroup.Item>
  <ToggleGroup.Item
    aria-label="toggle strikethrough"
    value="strikethrough"
    class="rounded-9px bg-background-alt hover:bg-muted active:bg-dark-10 data-[state=on]:bg-muted data-[state=off]:text-foreground-alt data-[state=on]:text-foreground 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" />
  </ToggleGroup.Item>
</ToggleGroup.Root>

구조

<script lang="ts">
  import { ToggleGroup } from "bits-ui";
</script>
<ToggleGroup.Root>
  <ToggleGroup.Item value="bold">bold</ToggleGroup.Item>
  <ToggleGroup.Item value="italic">italic</ToggleGroup.Item>
</ToggleGroup.Root>

단일 & 다중 선택

ToggleGroup 컴포넌트는 'single'과 'multiple' 두 가지 type prop을 지원해요. type이 'single'이면 한 번에 하나의 항목만 선택할 수 있고, value prop의 타입은 문자열이 됩니다.

type이 'multiple'이면 동시에 여러 항목을 선택할 수 있고, value prop의 타입은 문자열 배열이 됩니다.

value 상태 다루기

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

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

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

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

완전 제어 (Fully Controlled)

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

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

API 참조

ToggleGroup.Root

토글 그룹 항목들을 담고 있는 루트 컴포넌트예요.

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 of the toggle group changes. The type of the value is dependent on the type of the toggle group.Default:  —— undefined
disabled boolean Whether or not the switch is disabled.Default: false
loop boolean Whether or not the toggle group should loop when navigating.Default: true
orientation enum - 'horizontal' | 'vertical' The orientation of the toggle group.Default: 'horizontal'
rovingFocus boolean Whether or not the toggle group should use roving focus when navigating.Default: true
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 toggle group.
data-toggle-group-root '' Present on the root element.

ToggleGroup.Item

그룹 안의 개별 토글 항목이에요.

Property Type Description Details
value string The value of the item.Default:  —— undefined
disabled boolean Whether or not the switch 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 toggle item is in the on or off state.
data-value '' The value of the toggle item.
data-orientation enum - 'horizontal' | 'vertical' The orientation of the toggle group.
data-disabled '' Present when the toggle item is disabled.
data-toggle-group-item '' Present on the toggle group item.

더 알아보기 (Learn more)

ToggleGroup은 단일·다중 선택을 모두 지원해서, 세그먼트 컨트롤이나 도구 옵션 묶음에 잘 어울려요. 선택 방식을 다루는 type·value 패턴은 Tabs나 Select 같은 컴포넌트와 비슷하니, 함께 살펴보면 선택형 컴포넌트 전반의 흐름이 잡힙니다.