import { useEffect, useRef, useState } from "https://esm.sh/react";
const rand = (min, max) => Math.floor(Math.random() * (max - min) + min);
export const useTypewriter = (text: string, speed: number = 16, off: number = 12) => {
const [i, setI] = useState<number>(1);
useEffect(() => {
if (i > text.length) return;
let delay = rand(speed, speed + off);
const id = setTimeout(() => {
setI(i => i + 1);
}, delay);
return () => {
clearTimeout(id);
};
}, [text, i]);
return text.slice(0, i);
};
export function Typewriter({ text }) {
const res = useTypewriter(text);
return <>{res}</>;
}