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

Switch 마이그레이션 가이드

원문 보기 위키 갱신

이 문서는 HeroUI v2에서 v3로 Switch 컴포넌트를 옮길 때 어떻게 바꿔야 하는지 설명해요.

참고: v3 Switch 문서에서 완전한 API 레퍼런스, 스타일링 가이드, 고급 예제를 확인할 수 있어요. 이 가이드는 HeroUI v2에서 마이그레이션하는 데만 집중해요.

출처: HeroUI

본문

구조 변경

v2에서는 Switch가 children을 라벨로 사용하는 단순한 구조였어요:

import { Switch } from "@heroui/react";

export default function App() {
  return <Switch>Enable notifications</Switch>;
}

v3에서는 Switch가 복합 컴포넌트를 요구해요:

import { Switch, Label } from "@heroui/react";

export default function App() {
  return (
    <Switch>
      <Switch.Content>
        <Switch.Control>
          <Switch.Thumb />
        </Switch.Control>
        Enable notifications
      </Switch.Content>
    </Switch>
  );
}

주요 변경 사항

1. 컴포넌트 구조

v2: children을 라벨로 사용하는 단순 Switch
v3: Label 컴포넌트와 함께 복합 컴포넌트(Switch.Content, Switch.Control, Switch.Thumb)

2. Prop 변경 사항

v2 Prop v3 위치 비고
onValueChange onChange 이벤트 핸들러 이름 변경
size size 루트에 여전히 존재 (sm | md | lg)
label — Label 컴포넌트 사용
color — 제거됨 (Tailwind CSS 사용)
thumbIcon — Switch.Thumb 안에서 Switch.Icon 사용
startContent — 컨트롤을 직접 커스터마이즈
endContent — 컨트롤을 직접 커스터마이즈
classNames — 개별 컴포넌트에 className prop 사용
disableAnimation — 제거됨 (애니메이션은 다르게 처리)

3. 새 컴포넌트

  • SwitchGroup - 여러 스위치를 그룹으로 묶기
  • Switch.Content - 컨트롤과 Label을 감싸는 클릭 가능한 라벨
  • Switch.Icon - 썸(thumb) 안의 아이콘용

마이그레이션 예제

제어된 Switch

import { useState } from "react";

const [isSelected, setIsSelected] = useState(true);

<Switch isSelected={isSelected} onValueChange={setIsSelected}>
  Airplane mode
</Switch>
import { useState } from "react";

const [isSelected, setIsSelected] = useState(true);

<Switch isSelected={isSelected} onChange={setIsSelected}>
  <Switch.Content>
    <Switch.Control>
      <Switch.Thumb />
    </Switch.Control>
    Airplane mode
  </Switch.Content>
</Switch>

라벨 없이 쓰기

<Switch defaultSelected aria-label="Automatic updates" />
<Switch defaultSelected aria-label="Automatic updates">
  <Switch.Content>
    <Switch.Control>
      <Switch.Thumb />
    </Switch.Control>
  </Switch.Content>
</Switch>

썸 아이콘과 함께 쓰기

<Switch thumbIcon={<CheckIcon />}>Enable notifications</Switch>
<Switch>
  <Switch.Content>
    <Switch.Control>
      <Switch.Thumb>
        <Switch.Icon>
          <CheckIcon />
        </Switch.Icon>
      </Switch.Thumb>
    </Switch.Control>
    Enable notifications
  </Switch.Content>
</Switch>

시작/끝 콘텐츠와 함께 쓰기

<Switch
  startContent={<SunIcon />}
  endContent={<MoonIcon />}
>
  Dark mode
</Switch>
<Switch>
  <Switch.Content>
    <Switch.Control className="flex items-center gap-2">
      <SunIcon />
      <Switch.Thumb />
      <MoonIcon />
    </Switch.Control>
    Dark mode
  </Switch.Content>
</Switch>

라벨과 설명과 함께 쓰기

<Switch description="You will receive notifications for all activity">
  Enable notifications
</Switch>
import { Switch, Label, Description } from "@heroui/react";

<Switch>
  <Switch.Content>
    <Switch.Control>
      <Switch.Thumb />
    </Switch.Control>
    Enable notifications
  </Switch.Content>
  <Description>You will receive notifications for all activity</Description>
</Switch>

크기와 색상

{/* Sizes */}
<div className="flex gap-4">
  <Switch size="sm">Small</Switch>
  <Switch size="md">Medium</Switch>
  <Switch size="lg">Large</Switch>
</div>

{/* Colors */}
<Switch color="primary">Primary</Switch>
<Switch color="success">Success</Switch>
<Switch color="danger">Danger</Switch>
{/* Sizes */}
<div className="flex gap-4">
  <Switch size="sm">
    <Switch.Content>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      Small
    </Switch.Content>
  </Switch>
  <Switch size="md">
    <Switch.Content>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      Medium
    </Switch.Content>
  </Switch>
  <Switch size="lg">
    <Switch.Content>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      Large
    </Switch.Content>
  </Switch>
</div>

{/* Colors - Use Tailwind CSS classes */}
<Switch>
  <Switch.Content>
    <Switch.Control className="data-[selected=true]:bg-accent">
      <Switch.Thumb />
    </Switch.Control>
    Primary
  </Switch.Content>
</Switch>
<Switch>
  <Switch.Content>
    <Switch.Control className="data-[selected=true]:bg-success">
      <Switch.Thumb />
    </Switch.Control>
    Success
  </Switch.Content>
</Switch>
<Switch>
  <Switch.Content>
    <Switch.Control className="data-[selected=true]:bg-danger">
      <Switch.Thumb />
    </Switch.Control>
    Danger
  </Switch.Content>
</Switch>

Switch 그룹

{/* No built-in group component in v2 */}
<div className="flex flex-col gap-2">
  <Switch name="notifications">Allow Notifications</Switch>
  <Switch name="marketing">Marketing emails</Switch>
</div>
import { SwitchGroup } from "@heroui/react";

<SwitchGroup>
  <Switch name="notifications">
    <Switch.Content>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      Allow Notifications
    </Switch.Content>
  </Switch>
  <Switch name="marketing">
    <Switch.Content>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      Marketing emails
    </Switch.Content>
  </Switch>
</SwitchGroup>

컴포넌트 구조 (Anatomy)

v3 Switch는 이런 구조를 따르고 있어요:

Switch (Root)
  ├── Switch.Content (the clickable label)
  │   ├── Switch.Control
  │   │   └── Switch.Thumb
  │   │       └── Switch.Icon (optional)
  │   └── Label
  ├── Description (optional, sibling)
  └── FieldError (optional, sibling)

그룹은 이렇게 구성돼요:

SwitchGroup
  ├── Switch
  │   ├── Switch.Content
  │   │   ├── Switch.Control
  │   │   │   └── Switch.Thumb
  │   │   └── Label
  │   └── Description (optional)
  └── Switch
      ├── Switch.Content
      │   ├── Switch.Control
      │   │   └── Switch.Thumb
      │   └── Label
      └── Description (optional)

중요 참고 사항

이벤트 핸들러

  • v2: onValueChange prop
  • v3: onChange prop (시그니처 동일: (isSelected: boolean) => void)

라벨 처리

  • v2: children을 라벨로 사용
  • v3: 라벨은 Switch.Content(클릭 가능한 라벨) 안에 들어가고, Description/FieldError는 Switch.Content의 형제로 위치

아이콘

  • v2: thumbIcon prop으로 썸 안의 아이콘, startContent/endContent로 바깥 아이콘
  • v3: 썸 아이콘은 Switch.Thumb 안에서 Switch.Icon 사용, 시작/끝 콘텐츠는 Switch.Control을 직접 커스터마이즈

요약

  1. 컴포넌트 구조: 반드시 복합 컴포넌트(Switch.Content, Switch.Control, Switch.Thumb)를 사용해야 해요.
  2. 클릭 가능한 라벨: Switch.Content로 Switch.Control과 Label을 감싸고, Description/FieldError는 Switch.Content의 형제로 배치해요.
  3. 라벨 처리: children을 더 이상 라벨로 사용하지 않아요 - Switch.Content 안에서 Label 컴포넌트를 사용해요.
  4. 이벤트 핸들러: onValueChange → onChange
  5. 스타일링 Props 제거: color - Tailwind CSS 사용
  6. 아이콘 Props 제거: thumbIcon, startContent, endContent - 컴포넌트를 사용하거나 직접 커스터마이즈
  7. ClassNames 제거: 개별 컴포넌트에 className prop 사용
  8. 새 컴포넌트: 스위치 그룹용 SwitchGroup, 컨트롤과 Label을 감싸는 클릭 가능한 라벨 Switch.Content

더 알아보기 (Learn more)