Skip to content

Instantly share code, notes, and snippets.

@natew
Last active March 8, 2024 00:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natew/e773fa3bdc99f75a3b28f21db168a449 to your computer and use it in GitHub Desktop.
Save natew/e773fa3bdc99f75a3b28f21db168a449 to your computer and use it in GitHub Desktop.
AnimatePresence number ticker
const AnimatedNumbers = () => {
const [numbers, setNumbers] = useState(100_000)
const len = `${numbers}`.length
return (
<YStack gap="$5">
<XStack gap="$2">
<Button
onPress={() => {
setNumbers(Math.ceil(Math.random() * 1_000_000))
}}
>
Next
</Button>
<Button
onPress={() => {
setNumbers(+`${numbers}1`)
}}
>
Add
</Button>
<Button
onPress={() => {
setNumbers(+`${numbers}`.slice(0, -1))
}}
>
Remove
</Button>
</XStack>
<XStack x={-50}>
<AnimatePresence
initial={false}
debug
enterVariant="fromTop"
exitVariant="toBottom"
>
{`${numbers}`
// .slice(0, 1)
.split('')
.map((num, i) => {
// we do every other iteration so we can avoid enter/exit of same thing
// ${iteration % 3 == 0}
const key = `${i}${num}`
return (
<AnimatedNumber
animation="medium"
animateOnly={['transform', 'opacity']}
x={-len * 10 + 60 * i}
key={key}
>
{num}
</AnimatedNumber>
)
})}
</AnimatePresence>
</XStack>
</YStack>
)
}
const AnimatedNumber = styled(Text, {
fontSize: '$12',
fontFamily: '$silkscreen',
color: '$color',
pos: 'absolute',
t: 0,
l: 0,
y: 0,
o: 1,
variants: {
fromTop: {
true: {
y: -50,
o: 0,
},
},
toBottom: {
true: {
y: 50,
o: 0,
},
},
} as const,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment