2022-06-03 16:36:24 +00:00
|
|
|
import React, { Fragment, useEffect, useState } from 'react';
|
|
|
|
import { gsap } from "gsap";
|
2022-01-06 05:27:13 +00:00
|
|
|
|
|
|
|
let timer = 0;
|
|
|
|
let keyNum = 0;
|
|
|
|
let textTimeout;
|
2022-06-03 16:36:24 +00:00
|
|
|
// let chars = ("abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-;.,/?><][}{:`~").split("");
|
2022-01-06 05:27:13 +00:00
|
|
|
|
2022-06-03 16:36:24 +00:00
|
|
|
const TextShuffler = ({ textInput, delay }) => {
|
2022-01-06 05:27:13 +00:00
|
|
|
|
|
|
|
let [text, setText] = useState(textInput);
|
|
|
|
let [refresh, setRefresh] = useState(0);
|
2022-06-03 16:36:24 +00:00
|
|
|
let [readyState, setReadyState] = useState(false);
|
|
|
|
|
|
|
|
const spanRef = React.useRef();
|
2022-01-06 05:27:13 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-06-03 16:36:24 +00:00
|
|
|
const spanObserver = new IntersectionObserver((entries, observer) => {
|
|
|
|
if (entries[0].isIntersecting) {
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
if (delay) {
|
|
|
|
setTimeout(() => {
|
|
|
|
gsap.to(spanRef.current, {
|
|
|
|
opacity: 1,
|
|
|
|
duration: 2
|
|
|
|
});
|
|
|
|
|
|
|
|
setReadyState(true);
|
|
|
|
}, delay);
|
|
|
|
} else {
|
|
|
|
gsap.to(spanRef.current, {
|
|
|
|
opacity: 1,
|
|
|
|
duration: 2
|
|
|
|
});
|
|
|
|
|
|
|
|
setReadyState(true);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
observer.unobserve(spanRef.current)
|
|
|
|
}
|
|
|
|
|
|
|
|
}, {
|
|
|
|
rootMargin: "0px 0px 0px 0px"
|
|
|
|
});
|
|
|
|
|
|
|
|
spanObserver.observe(spanRef.current)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!readyState) return;
|
|
|
|
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
textTimeout = setTimeout(() => {
|
|
|
|
if (timer === 7) {
|
|
|
|
setText(textInput);
|
|
|
|
return window.clearTimeout(textTimeout);
|
|
|
|
};
|
2022-01-06 05:27:13 +00:00
|
|
|
|
2022-06-03 16:36:24 +00:00
|
|
|
setRefresh(prev => prev + 1);
|
|
|
|
timer++
|
|
|
|
}, 300);
|
|
|
|
})
|
2022-01-06 05:27:13 +00:00
|
|
|
}, [refresh])
|
|
|
|
|
2022-06-03 16:36:24 +00:00
|
|
|
return (
|
|
|
|
<span className="shuffled-text-span" ref={ spanRef } style={ { opacity: 0 } }>
|
|
|
|
{ text.split("").map(char => <span key={ keyNum++ } style={ { animationDelay: Math.random() * 1.5 + "s", animationDuration: (Math.random() * 2) + "s" } }>{ char }</span>) }
|
|
|
|
</span>
|
|
|
|
)
|
2022-01-06 05:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default TextShuffler
|