Skip to content

Animations

BetterTUI includes a tween-based animation engine.

import { useAnimation } from '@bettertui/react';
function App() {
const { value, start } = useAnimation({
from: 0,
to: 100,
duration: 1000,
easing: 'easeInOut',
});
return (
<Box>
<Text>Progress: {value}%</Text>
<Button onPress={start}>Animate</Button>
</Box>
);
}
Easing Description
linear Constant speed
easeIn Slow start
easeOut Slow end
easeInOut Slow start and end
spring Spring physics
import { useKeyframes } from '@bettertui/react';
function App() {
const { value } = useKeyframes({
keyframes: [
{ offset: 0, opacity: 0 },
{ offset: 0.5, opacity: 1 },
{ offset: 1, opacity: 0 },
],
duration: 2000,
iterations: Infinity,
});
return <Text style={{ opacity: value }}>Fading text</Text>;
}
import { useSpinner } from '@bettertui/react';
function LoadingSpinner() {
const frame = useSpinner();
return <Text>{frame}</Text>;
}
import { useProgressBar } from '@bettertui/react';
function App() {
const [progress, setProgress] = useState(0);
useEffect(() => {
const interval = setInterval(() => {
setProgress(p => Math.min(p + 1, 100));
}, 50);
return () => clearInterval(interval);
}, []);
return (
<Box>
<Text>
[{''.repeat(progress / 5)}{''.repeat(20 - progress / 5)}]
{' '}{progress}%
</Text>
</Box>
);
}