---
title: "Animated Toggle Icon"
description: "Used to toggle between inactive and active icons with a scale animation."
url: https://weme-ui.github.io/slim/components/animated-toggle-icon.md
---
# Animated Toggle Icon

两个图标来回切，进场出场缩一下。底下是 `Icon` 和 `motion-v`。没列出来的属性经 `$attrs` 传给 `Motion`。

## Installation

```bash
pnpm dlx @weme-ui/weme-ui add animated-toggle-icon
```

## Usage

```vue
<script lang="ts" setup>
import { ref } from 'vue'
import AnimatedToggleIcon from '../animated-toggle-icon.vue'

const active = ref(false)
</script>

<template>
  <button type="button" class="text-sm inline-flex gap-2 items-center" @click="active = !active">
    <AnimatedToggleIcon
      icon="ri:heart-line"
      active-icon="ri:heart-fill"
      :active="active"
      class="text-lg text-error"
    />
    Toggle
  </button>
</template>
```

## Props

| 属性         | 说明                                             | 类型                       | 默认值 |
| ------------ | ------------------------------------------------ | -------------------------- | ------ |
| `icon`       | 未激活态图标（Iconify）；必填                    | `IconifyIconProps['icon']` | —      |
| `activeIcon` | 激活态图标（Iconify）；未传时激活态回退到 `icon` | `IconifyIconProps['icon']` | —      |
| `active`     | 是否为激活态；切换时触发进出场动画               | `boolean`                  | —      |
| `class`      | 根节点额外 class                                 | `any`                      | —      |

## Accessibility

经 `Motion`（`as-child`）画成 `Icon`，带着 `data-slot="animated-toggle-icon"` 和 `data-active`。根节点 `pointer-events-none`。没有自己的键盘操作。有含义就在旁边写字。

## Examples

### States

```vue
<script lang="ts" setup>
import AnimatedToggleIcon from '../animated-toggle-icon.vue'
</script>

<template>
  <div class="text-lg flex flex-wrap gap-6 items-center">
    <AnimatedToggleIcon
      icon="ri:checkbox-blank-circle-line"
      active-icon="ri:checkbox-circle-fill"
      :active="false"
    />
    <AnimatedToggleIcon
      icon="ri:checkbox-blank-circle-line"
      active-icon="ri:checkbox-circle-fill"
      :active="true"
    />
  </div>
</template>
```

### Fallback Icon

```vue
<script lang="ts" setup>
import { ref } from 'vue'
import AnimatedToggleIcon from '../animated-toggle-icon.vue'

const active = ref(true)
</script>

<template>
  <button
    type="button"
    class="inline-flex items-center"
    aria-label="Toggle"
    @click="active = !active"
  >
    <AnimatedToggleIcon icon="ri:star-line" :active="active" class="text-lg text-warning" />
  </button>
</template>
```