Toggle Group (토글 그룹)
토글 그룹은 두 가지 상태를 가진 버튼들의 집합으로, 각 버튼을 켜짐(pressed) 또는 꺼짐(not pressed) 상태로 토글할 수 있어요. 텍스트 편집기의 굵게·기울임·밑줄 같은 옵션 선택에 유용합니다.
출처: 문서
본문
Import (불러오기)
import { ToggleGroup } from "@kobalte/core/toggle-group";
// or
import { Root, Item, ... } from "@kobalte/core/toggle-group";
// or (deprecated)
import { ToggleGroup } from "@kobalte/core";
Features (기능)
- 가로/세로 방향(orientation)을 지원해요.
- Space 및 Enter 키에 대한 키보드 이벤트를 지원해요.
- 제어 방식(controlled) 또는 비제어 방식(uncontrolled)으로 사용할 수 있어요.
Anatomy (구조)
토글 그룹은 다음의 부분들로 이루어져요:
- ToggleGroup: 토글 그룹의 루트 컨테이너예요.
토글 항목은 다음의 부분들로 이루어져요:
- ToggleGroup.Item: 토글 버튼의 루트 컨테이너예요.
<ToggleGroup>
<ToggleGroup.Item />
</ToggleGroup>
Example (예제)
기본 예제를 통해 토글 그룹의 구조와 스타일링 방식을 확인해 보세요. 인터랙티브한 미리보기는 출처 문서에서 직접 확인할 수 있어요.
Usage (사용법)
Default pressed (기본 눌림 상태)
defaultValue prop을 사용하면 초기의 비제어 값을 지정할 수 있어요.
<ToggleGroup defaultValue="underline">
<ToggleGroup.Item value="bold" aria-label="Bold">
<BoldIcon />
</ToggleGroup.Item>
<ToggleGroup.Item value="italic" aria-label="Italic">
<ItalicIcon />
</ToggleGroup.Item>
<ToggleGroup.Item value="underline" aria-label="Underline">
<UnderlineIcon />
</ToggleGroup.Item>
</ToggleGroup>
Controlled pressed (제어 방식 눌림 상태)
value prop을 사용하면 눌림 상태를 제어할 수 있어요. 사용자가 버튼을 토글하면 onChange 이벤트가 발생하고 새 값이 전달되답니다.
import { createSignal } from "solid-js";
function ControlledExample() {
const [value, setValue] = createSignal("underline");
return (
<>
<ToggleGroup value={value()} onChange={setValue}>
...
</ToggleGroup>
<p>Your text style is: {value()}.</p>
</>
);
}
Multiple selection (다중 선택)
multiple prop을 사용하면 여러 항목을 선택할 수 있는 토글 그룹을 만들 수 있어요.
import { createSignal } from "solid-js";
function MultipleSelectionExample() {
const [values, setValues] = createSignal(["bold", "underline"]);
return (
<ToggleGroup class="toggle-group" value={values()} onChange={setValues}>
<ToggleGroup.Item class="toggle-group__item" value="bold" aria-label="Bold">
<BoldIcon />
</ToggleGroup.Item>
<ToggleGroup.Item class="toggle-group__item" value="italic" aria-label="Italic">
<ItalicIcon />
</ToggleGroup.Item>
<ToggleGroup.Item class="toggle-group__item" value="underline" aria-label="Underline">
<UnderlineIcon />
</ToggleGroup.Item>
</ToggleGroup>
);
}
API Reference (API 참조)
ToggleGroup
ToggleGroup은 @kobalte/core/toggle-group의 Root 임포트(그리고 더 이상 사용되지 않는 ToggleGroup.Root)와 동일해요.
| Prop | Description |
|---|---|
| value | string | string[] The controlled pressed state of the toggle button. |
| defaultValue | string | string[] The default pressed state when initially rendered. Useful when you do not need to control the pressed state. |
| onChange | (value: string | string[]) => void Event handler called when the pressed state of an item changes. |
| multiple | boolean Whether the toggle group allows multi-selection. |
| orientation | 'horizontal' | 'vertical' The orientation of the toggle group. |
| disabled | boolean Whether toggle group should be disabled. |
| Data attribute | Description |
|---|---|
| data-orientation='horizontal' | Present when the separator has horizontal orientation. |
| data-orientation='vertical' | Present when the separator has vertical orientation. |
ToggleGroup.Item
| Prop | Description |
|---|---|
| value | string A unique value for the item. |
| disabled | boolean Whether the item is disabled. |
| children | JSX.Element | (state: ToggleButtonState) => JSX.Element The children of the item. Can be a JSX.Element or a render prop for having access to the internal state. |
| Render Prop | Description |
|---|---|
| pressed | Accessor<boolean> Whether the toggle button is on (pressed) or off (not pressed). |
| Data attribute | Description |
|---|---|
| data-orientation='horizontal' | Present when the separator has horizontal orientation. |
| data-orientation='vertical' | Present when the separator has vertical orientation. |
| data-disabled | Present when the accordion item is disabled. |
| data-pressed | Present when the toggle button is on (pressed). |
Rendered elements (렌더링되는 요소)
| Component | Default rendered element |
|---|---|
ToggleGroup |
div |
ToggleGroup.Item |
button |
Accessibility (접근성)
Keyboard Interactions (키보드 상호작용)
| Key | Description |
|---|---|
| Tab | Move focus to either the pressed item or the first item in the group. |
| ArrowDown | If orientation is vertical, moves focus to the next item. |
| ArrowRight | If orientation is horizontal, Moves focus to the next item. |
| ArrowUp | If orientation is vertical, moves focus to the previous item. |
| ArrowLeft | If orientation is vertical, moves focus to the previous item. |
| Home | Moves focus to the first item. |
| End | Moves focus to the last item. |
| Enter | Activates/deactivates the item. |
| Space | Activates/deactivates the item. |
더 알아보기 (Learn more)
- Toggle Button 컴포넌트 — 개별 토글 버튼
- Kobalte 시작하기 — 프로젝트 설정