feat: add typewriter effect
This commit is contained in:
@@ -4,7 +4,9 @@ import { fetchBodyText } from "./api/fetchBodyText";
|
|||||||
function App() {
|
function App() {
|
||||||
const [bodyText, setBodyText] = useState("");
|
const [bodyText, setBodyText] = useState("");
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [visibleText, setVisibleText] = useState("");
|
||||||
|
|
||||||
|
// Call to load flag
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function load() {
|
async function load() {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
@@ -15,9 +17,28 @@ function App() {
|
|||||||
load();
|
load();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
// Typewriter effect
|
||||||
|
useEffect(() => {
|
||||||
|
if (!loading && bodyText) {
|
||||||
|
let index = 0;
|
||||||
|
const delay = 500; // 0.5ms per character
|
||||||
|
|
||||||
|
const type = () => {
|
||||||
|
setVisibleText(bodyText.slice(0, index));
|
||||||
|
index++;
|
||||||
|
if (index <= bodyText.length) {
|
||||||
|
setTimeout(type, delay);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
type();
|
||||||
|
}
|
||||||
|
}, [loading, bodyText]);
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div className="App">
|
||||||
{loading ? <p>Loading...</p> : <p>{bodyText}</p>}
|
{loading ? <p>Loading...</p> : <p>{visibleText}</p>}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user