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

스왑

원문 보기 위키 갱신

스왑 (Swap)

부드러운 전환으로 두 시각적 상태 사이를 애니메이션하는 유틸리티예요. 흔히 아이콘이나 상태 표시를 페이드, 플립, 회전, 스케일 효과로 바꿔 보여줍니다.

출처: 문서

본문

해부 (Anatomy)

<Swap.Root>
  <Swap.Indicator />
</Swap.Root>

예제 (Examples)

페이드 (Fade)

페이드 애니메이션으로 두 아이콘 사이를 전환해 보세요. swap prop을 설정해 on과 off 인디케이터를 토글합니다.

import { Swap } from '@ark-ui/react/swap'
import { CheckIcon, XIcon } from 'lucide-react'
import { useState } from 'react'
import styles from 'styles/swap.module.css'

export const Fade = () => {
  const [swapped, setSwapped] = useState(false)

  return (
    <button type="button" className={styles.Button} onClick={() => setSwapped((prev) => !prev)}>
      <Swap.Root swap={swapped}>
        <Swap.Indicator type="on" className={styles.FadeIndicator}>
          <CheckIcon />
        </Swap.Indicator>
        <Swap.Indicator type="off" className={styles.FadeIndicator}>
          <XIcon />
        </Swap.Indicator>
      </Swap.Root>
    </button>
  )
}

플립 (Flip)

루트에 perspective를 설정하고 인디케이터에 rotateY 키프레임을 사용하면 3D 플립 효과를 추가할 수 있어요.

import { Swap } from '@ark-ui/react/swap'
import { PauseIcon, PlayIcon } from 'lucide-react'
import { useState } from 'react'
import styles from 'styles/swap.module.css'

export const Flip = () => {
  const [swapped, setSwapped] = useState(false)

  return (
    <button type="button" className={styles.Button} onClick={() => setSwapped((prev) => !prev)}>
      <Swap.Root swap={swapped} style={{ perspective: '200px' }}>
        <Swap.Indicator type="on" className={styles.FlipIndicator}>
          <PlayIcon />
        </Swap.Indicator>
        <Swap.Indicator type="off" className={styles.FlipIndicator}>
          <PauseIcon />
        </Swap.Indicator>
      </Swap.Root>
    </button>
  )
}

회전 (Rotate)

스핀 전환으로 인디케이터를 회전시키며 전환해 보세요.

import { Swap } from '@ark-ui/react/swap'
import { MoonIcon, SunIcon } from 'lucide-react'
import { useState } from 'react'
import styles from 'styles/swap.module.css'

export const Rotate = () => {
  const [swapped, setSwapped] = useState(false)

  return (
    <button type="button" className={styles.Button} onClick={() => setSwapped((prev) => !prev)}>
      <Swap.Root swap={swapped}>
        <Swap.Indicator type="on" className={styles.RotateIndicator}>
          <SunIcon />
        </Swap.Indicator>
        <Swap.Indicator type="off" className={styles.RotateIndicator}>
          <MoonIcon />
        </Swap.Indicator>
      </Swap.Root>
    </button>
  )
}

스케일 (Scale)

인디케이터를 확대/축소하며 팝인 효과를 만들어 보세요.

import { Swap } from '@ark-ui/react/swap'
import { Volume2Icon, VolumeXIcon } from 'lucide-react'
import { useState } from 'react'
import styles from 'styles/swap.module.css'

export const Scale = () => {
  const [swapped, setSwapped] = useState(false)

  return (
    <button type="button" className={styles.Button} onClick={() => setSwapped((prev) => !prev)}>
      <Swap.Root swap={swapped}>
        <Swap.Indicator type="on" className={styles.ScaleIndicator}>
          <Volume2Icon />
        </Swap.Indicator>
        <Swap.Indicator type="off" className={styles.ScaleIndicator}>
          <VolumeXIcon />
        </Swap.Indicator>
      </Swap.Root>
    </button>
  )
}

가이드 (Guides)

작동 방식 (How It Works)

Swap은 1x1 CSS 그리드 안에 두 개의 인디케이터를 서로 겹쳐 렌더링합니다. swap prop이 어떤 인디케이터를 보여줄지 제어합니다. 각 인디케이터는 프레즌스 시스템을 사용하므로 CSS 애니메이션을 구동하는 데 쓸 data-state="open"과 data-state="closed" 속성을 얻을 수 있어요.

인디케이터 애니메이션 (Animating Indicators)

각 인디케이터의 data-state를 타겟으로 진입·퇴장 애니메이션을 정의할 수 있어요.

.indicator[data-state='open'] {
  animation: fade-in 200ms ease-out;
}

.indicator[data-state='closed'] {
  animation: fade-out 100ms ease-in;
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fade-out {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

더 풍부한 효과를 위해 애니메이션을 결합할 수도 있어요. 예를 들어 스케일과 페이드를 함께 쓰는 경우:

.indicator[data-state='open'] {
  animation:
    scale-in 200ms ease-out,
    fade-in 200ms ease-out;
}

.indicator[data-state='closed'] {
  animation:
    scale-out 100ms ease-in,
    fade-out 100ms ease-in;
}

3D 플립 애니메이션 (3D Flip Animation)

플립 효과를 위해 루트에 perspective를 설정하고 인디케이터에 backface-visibility: hidden을 사용하세요.

.flip-indicator {
  backface-visibility: hidden;
}

.flip-indicator[data-state='open'] {
  animation: flip-in 400ms ease;
}

.flip-indicator[data-state='closed'] {
  animation: flip-out 200ms ease;
}

@keyframes flip-in {
  from {
    transform: rotateY(180deg);
  }
  to {
    transform: rotateY(0deg);
  }
}

@keyframes flip-out {
  from {
    transform: rotateY(0deg);
  }
  to {
    transform: rotateY(180deg);
  }
}

지연 마운트 (Lazy Mount)

lazyMount와 unmountOnExit을 사용하면 인디케이터가 언제 마운트되고 언마운트될지 제어할 수 있어요. 이렇게 하면 인디케이터가 보이지 않을 때 DOM을 깨끗하게 유지할 수 있습니다.

<Swap.Root swap={swapped} lazyMount unmountOnExit>
  <Swap.Indicator type="on">...</Swap.Indicator>
  <Swap.Indicator type="off">...</Swap.Indicator>
</Swap.Root>

API 레퍼런스 (API Reference)

Props

Root

Props

asChild Type: boolean Required: false Default Value: undefined Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하여 그들의 props와 동작을 결합

hideMode Type: HideMode Required: false Default Value: 'display-none' Description: 마운트되었지만 존재하지 않을 때 콘텐츠를 숨기는 방법

  • 'display-none': HTML hidden 속성. 이펙트는 살아있게 유지됩니다.
  • 'activity': React 19 <Activity mode="hidden">. 이펙트가 일시 중지됩니다. React 19+가 필요합니다.

lazyMount Type: boolean Required: false Default Value: false Description: 지연 마운팅을 활성화할지 여부

swap Type: boolean Required: false Default Value: false Description: 스왑이 "on" 상태인지 여부

unmountOnExit Type: boolean Required: false Default Value: false Description: 종료 시 언마운트할지 여부

Indicator

Props

type Type: 'on' | 'off' Required: true Default Value: undefined Description: undefined

asChild Type: boolean Required: false Default Value: undefined Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하여 그들의 props와 동작을 결합

RootProvider

Props

value Type: UseSwapReturn Required: true Default Value: undefined Description: undefined

asChild Type: boolean Required: false Default Value: undefined Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하여 그들의 props와 동작을 결합

Context

더 알아보기 (Learn more)