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

Toggle (토글)

원문 보기 위키 갱신

Toggle은 두 상태 사이를 오가게 해 주는 토글 컨트롤이에요. 눌렸는지 여부를 pressed 상태로 관리하고, 양방향 바인딩이나 함수 바인딩으로 자유롭게 제어할 수 있습니다. 간단한 버튼형 토글이 필요할 때 바로 쓸 수 있는 컴포넌트입니다.

출처: 문서

본문

<script lang="ts">
  import { Toggle } from "bits-ui";
  import LockKeyOpen from "phosphor-svelte/lib/LockKeyOpen";
  let unlocked = $state(false);
  const code = $derived(unlocked ? "B1T5" : "••••");
</script>
<div
  class="min-h-input rounded-card-sm border-border bg-background-alt shadow-mini flex h-full w-[176px] items-center gap-2 border py-1 pl-[18px] pr-1.5"
>
  <div
    class="font-alt text-end text-[19px] tracking-[13.87px] {unlocked
      ? 'text-foreground'
      : 'text-muted-foreground'}"
  >
    {code}
  </div>
  <Toggle.Root
    aria-label="toggle code visibility"
    class="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 rounded-[9px] transition-all active:scale-[0.98]"
    bind:pressed={unlocked}
  >
    <LockKeyOpen class="size-6" />
  </Toggle.Root>
</div>

구조

<script lang="ts">
  import { Toggle } from "bits-ui";
</script>
<Toggle.Root />

pressed 상태 다루기

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

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

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

<script lang="ts">
  import { Toggle } from "bits-ui";
  let myPressed = $state(true);
</script>
<button onclick={() => (myPressed = false)}> un-press </button>
<Toggle.Root bind:pressed={myPressed} />

완전 제어 (Fully Controlled)

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

<script lang="ts">
  import { Toggle } from "bits-ui";
  let myPressed = $state(false);
  function getPressed() {
    return myPressed;
  }
  function setPressed(newPressed: boolean) {
    myPressed = newPressed;
  }
</script>
<Toggle.Root bind:pressed={getPressed, setPressed}>
  <!-- ... -->
</Toggle.Root>

API 참조

Toggle.Root

토글 버튼이에요.

Property Type Description Details
pressed $bindable boolean Whether or not the toggle button is pressed.Default: false
onPressedChange function - (pressed: boolean) => void A callback function called when the pressed state of the toggle changes.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 is in the on or off state.
data-disabled '' Present when the toggle is disabled.
data-toggle-root '' Present on the root element.

더 알아보기 (Learn more)

Toggle은 가장 단순한 토글 형태라 시작점으로 좋아요. pressed 상태를 양방향 바인딩이나 함수 바인딩으로 다루는 방식은 Switch·Toggle Group과 동일하니, 한 번 익혀 두면 여러 토글 계열 컴포넌트에 그대로 쓸 수 있습니다.