57 lines
1.5 KiB
TypeScript
Raw Normal View History

import React from "react"
import { Box } from "@chakra-ui/react"
import { RainbowText } from "./rainbowText"
export const TextAnimation1 = (props) => {
const texts = React.useMemo(() => {
if (props?.texts?.length) {
return props?.texts
}
return [
"thingtime",
"vibrant",
"infinite",
"creative",
"powerful",
"magical",
"inspiring",
"love",
2023-06-28 07:53:34 +00:00
// 'tt'
]
}, [props?.texts])
const [titleText, setTitleText] = React.useState(texts[0])
2023-06-28 07:53:34 +00:00
const [usedTexts, setUsedTexts] = React.useState(["thingtime"])
const state = React.useRef({})
React.useEffect(() => {
2023-06-28 07:53:34 +00:00
state.current = { titleText, texts, usedTexts }
}, [titleText, texts, usedTexts])
React.useEffect(() => {
const newTimeout =
Math.random() * 1500 + 2500 + (titleText === "thingtime" ? 750 : 0)
2023-06-27 08:09:21 +00:00
setTimeout(() => {
2023-06-28 07:53:34 +00:00
// choose an unused text or pick texts[0]
const usedTexts = state.current.usedTexts
const unusedTexts = texts.filter((text) => !usedTexts.includes(text))
2023-06-28 07:53:34 +00:00
// pick a random unused text
const randomIdx = Math.floor(Math.random() * unusedTexts.length)
const newText = unusedTexts.length > 0 ? unusedTexts[randomIdx] : texts[0]
if (!unusedTexts.length) {
setUsedTexts(["thingtime"])
} else {
setUsedTexts([...usedTexts, newText])
}
setTitleText(newText)
2023-06-27 08:09:21 +00:00
}, newTimeout)
2023-06-28 07:53:34 +00:00
}, [titleText])
return <RainbowText ce={props?.ce}>{titleText}</RainbowText>
}