Number Animation
Digit Swap
DigitSwap.vue<script setup lang="ts">
import { computed, ref } from 'vue'
import { DigitSwap } from 'beui-vue'
const CARD_NUMBER = ['4242', '4242', '4242', '0806'].join(' ')
const MASKED_NUMBER = '•••• •••• •••• 0806'
const revealed = ref(false)
const value = computed(() => (revealed.value ? CARD_NUMBER : MASKED_NUMBER))
</script>
<template>
<div class="swap-demo">
<div class="swap-field">
<span class="swap-label">Card number</span>
<DigitSwap
:value="value"
:animation-key="revealed ? 'revealed' : 'masked'"
:direction="revealed ? 'up' : 'down'"
:suffix-length="4"
:glyph-class-name="revealed ? 'swap-on' : 'swap-off'"
suffix-class-name="swap-on"
class-name="swap-number"
/>
</div>
<button type="button" :aria-label="revealed ? 'Animate masked number' : 'Animate card number'" :aria-pressed="revealed" @click="revealed = !revealed">Animate</button>
</div>
</template>
<style scoped>
.swap-demo { display: flex; width: 20rem; flex-direction: column; gap: 1.25rem; }
.swap-field { display: flex; flex-direction: column; gap: .5rem; }
.swap-label { font-size: .75rem; line-height: 1rem; font-weight: 500; color: var(--beui-muted-foreground); }
:deep(.swap-number) { font-family: var(--font-mono, 'Geist Mono Variable', ui-monospace, monospace); font-size: 1.125rem; line-height: 1.75rem; letter-spacing: .08em; font-variant-numeric: tabular-nums; }
:deep(.swap-off) { color: var(--beui-muted-foreground); }
/* After swap-off so the suffix (which carries both classes) stays foreground, like tailwind-merge upstream. */
:deep(.swap-on) { color: var(--beui-foreground); }
button { height: 2.5rem; align-self: flex-start; padding: 0 .75rem; border: 1px solid var(--beui-border); border-radius: .5rem; background: transparent; color: var(--beui-foreground); font: inherit; font-size: .875rem; font-weight: 500; cursor: pointer; outline: none; transition: background-color .15s; }
button:hover { background: var(--beui-muted); }
button:focus-visible { box-shadow: 0 0 0 2px var(--beui-ring); }
</style><script setup lang="ts">
import { AnimatePresence, motion } from 'motion-v'
import { useReducedMotion } from '../core/reduced-motion'
import { computed } from 'vue'
import { easeOut } from '../core/motion'
export type DigitSwapDirection = 'up' | 'down'
const props = defineProps<{
/** Numeric or masked value rendered in fixed character slots. */
value: string | number
/** Replays every glyph when the value itself contains unchanged characters. */
animationKey?: string | number
/** Direction the next glyph enters from. */
direction?: DigitSwapDirection
/** Per-glyph transition duration in seconds. */
duration?: number
/** Delay in seconds between neighbouring glyphs. */
stagger?: number
/** Number of final characters that receive `suffixClassName`. */
suffixLength?: number
className?: string
glyphClassName?: string
suffixClassName?: string
}>()
const { duration = .18, stagger = .006, suffixLength = 0 } = props
const reduceMotion = useReducedMotion() ?? computed(() => false)
const reduce = computed(() => Boolean(reduceMotion.value))
const text = computed(() => String(props.value))
const suffixStart = computed(() => Math.max(0, text.value.length - Math.max(0, suffixLength)))
const glyphs = computed(() =>
Array.from(text.value, (character, position) => ({ character, id: `glyph-${position}`, position })),
)
/**
* The slots clip with `clip-path`, which — unlike `overflow: hidden` — leaves
* the text baseline alone. An invisible in-flow copy of each character gives
* the slot its natural width (punctuation) or a fixed 1ch cell (digits).
*/
type GlyphMotionContext = { direction: DigitSwapDirection; reduceMotion: boolean }
// The leaving glyph is kept from its last render, so its direction travels as `custom`: the exit
// then follows the direction at the time of the change, not the one it was rendered with.
const GLYPH_VARIANTS = {
enter: ({ direction, reduceMotion }: GlyphMotionContext) => ({
opacity: 0,
transform: reduceMotion ? 'none' : `translateY(${direction === 'up' ? '45%' : '-45%'})`,
}),
visible: { opacity: 1, transform: 'translateY(0%)' },
exit: ({ direction, reduceMotion }: GlyphMotionContext) => ({
opacity: 0,
transform: reduceMotion ? 'none' : `translateY(${direction === 'up' ? '-45%' : '45%'})`,
}),
}
const motionContext = computed<GlyphMotionContext>(() => ({ direction: props.direction ?? 'up', reduceMotion: reduce.value }))
function glyphKey(glyph: { id: string; character: string }) {
return props.animationKey === undefined ? `${glyph.id}-${glyph.character}` : `${glyph.id}-${glyph.character}-${props.animationKey}`
}
function glyphTransition(glyph: { position: number }) {
return {
duration: reduce.value ? Math.min(duration, .12) : duration,
delay: reduce.value ? 0 : glyph.position * stagger,
ease: easeOut,
}
}
</script>
<template>
<span class="be-digit-swap" :class="className" :data-direction="direction ?? 'up'">
<span class="be-digit-swap-spoken">{{ text }}</span>
<span aria-hidden="true" class="be-digit-swap-row">
<template v-for="glyph in glyphs" :key="glyph.id">
<span v-if="glyph.character === ' '" class="be-digit-swap-space" />
<span
v-else
class="be-digit-swap-glyph"
:class="/\d/.test(glyph.character) && 'be-digit-swap-digit'"
>
<span class="be-digit-swap-reserve">{{ glyph.character }}</span>
<AnimatePresence :initial="false" :custom="motionContext">
<motion.span
:key="glyphKey(glyph)"
class="be-digit-swap-glyph-text"
:class="[glyphClassName, glyph.position >= suffixStart ? suffixClassName : undefined]"
:custom="motionContext"
:variants="GLYPH_VARIANTS"
initial="enter"
animate="visible"
exit="exit"
:transition="glyphTransition(glyph)"
>
{{ glyph.character }}
</motion.span>
</AnimatePresence>
</span>
</template>
</span>
</span>
</template>
<style scoped>
.be-digit-swap { display: inline-flex; align-items: baseline; white-space: nowrap; }
.be-digit-swap-spoken { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.be-digit-swap-row { display: inline-flex; align-items: baseline; }
.be-digit-swap-space { display: inline-block; width: .7ch; }
.be-digit-swap-glyph { position: relative; display: inline-block; height: 1.1em; flex: none; line-height: 1.1em; clip-path: inset(0); }
.be-digit-swap-digit { width: 1ch; text-align: center; }
.be-digit-swap-reserve { visibility: hidden; }
.be-digit-swap-glyph-text { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; line-height: 1; }
</style>Install
Run it in a project set up with shadcn-vue. The source is copied to components/beui and its dependencies are added to your package.json. Read the shadcn-vue guide
$ bunx --bun shadcn-vue@latest add @beui-vue/digit-swap$ bun add beui-vueDownload the release archive to your project root, then install the local file.
$ bun add ./beui-vue-0.1.0.tgzImport the shared styles once in your application.
import DigitSwap from '@/components/beui/numeric/DigitSwap.vue'
import '@/components/beui/styles.css'API Reference
valuestring | numberNumeric or masked value rendered in fixed character slots.
—animationKey?string | numberReplays every glyph when the value itself contains unchanged characters.
—direction?DigitSwapDirectionDirection the next glyph enters from.
—duration?numberPer-glyph transition duration in seconds.
—stagger?numberDelay in seconds between neighbouring glyphs.
—suffixLength?numberNumber of final characters that receive `suffixClassName`.
—className?string—glyphClassName?string—suffixClassName?string—Related components
Action Swap
CTA button and slot primitives for swapping text and icons with blur motion.
Text Animation
Animated text primitives for spring reveals, chromatic sweeps, shimmer loading states, letter-cascade swaps and character scrambles.
Loader
Loading indicator with seventeen variants: spinner, dots, bars, dot-matrix, dither, morph, comet, scramble, metaballs, newton, helix, percent, and five terminal-style ascii spinners. Scales from one size prop, uses currentColor, and reduced-motion swaps every transform for a calm opacity pulse.
Updated