Text that swaps to a new value letter by letter, each changed letter sliding out as the new one slides in.
Installation
Usage
<script setup lang="ts">
import { SwappingText } from '@animate-ui/vue/primitives/texts/swapping';
defineProps<{ isPublic: boolean }>();
</script>
<template>
<SwappingText :text="isPublic ? 'Public' : 'Private'" />
</template>Given an array, it swaps from one text to the next on its own, every duration milliseconds, like Rotating Text:
<SwappingText :text="['Design', 'Develop', 'Deploy']" />Animation
- When
textchanges (or, given an array, everydurationmilliseconds), the letters are compared position by position. A letter that stays the same doesn't move; every other one swaps in place. - The old letter slides up by half a line, fading and blurring out, while the new one slides up from below, unblurring in, in 0.2s.
direction="down"reverses both. - Changed letters start one after another, from left to right,
staggerseconds apart (0.04s). - Each letter's width glides from the old letter's to the new one's, so the word grows or shrinks smoothly. Extra letters fade out to nothing.
<AnimateUIProvider :tempo>paces the default transition.- Given an array, it loops back to the first text after the last, and waits while off screen or while the tab is hidden.
- Under reduced motion, the text changes at once.
- Screen readers read the current text once.
API Reference
SwappingText
| Prop | Type | Default |
|---|---|---|
text | string | string[] | - |
duration? | number | 2000 |
delay? | number | 0 |
direction? | 'up' | 'down' | 'up' |
stagger? | number | 0.04 |
transition? | Transition | { duration: 0.2, ease: [0.4, 0, 0.2, 1] } |
...props? | HTMLAttributes | - |