Skip to content

Commit

Permalink
feat(TypedText): Add new TypedText component for animated typing effect
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbinoGeek committed Dec 10, 2023
1 parent 1a4ffe5 commit c05a8b9
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/components/branding/TypedText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Box from '@mui/material/Box'
import { useEffect, useState } from 'react'

type Props = {
finalText: string
initialText?: string
startDelay?: number
typingSpeed?: number
}

export default function TypedText(props: Props): JSX.Element {
const { finalText, initialText = '', startDelay = 0, typingSpeed = 25 } = props

const [typedText, setTypedText] = useState(initialText)
const [isTyping, setIsTyping] = useState(false)

useEffect(() => {
const timeout = setTimeout(() => {
setIsTyping(true)
}, startDelay)

return () => clearTimeout(timeout)
}, [startDelay])

useEffect(() => {
if (!isTyping) return

const timeout = setTimeout(() => {
const nextChar = finalText[typedText.length]
setTypedText(typedText + nextChar)
}, typingSpeed)

return () => clearTimeout(timeout)
}, [typedText, finalText, typingSpeed, isTyping])

useEffect(() => {
if (typedText === finalText) {
setIsTyping(false)
}
}, [typedText, finalText])

return <Box>
{typedText}
{isTyping && <span>&#9608;</span>}
</Box>
}

0 comments on commit c05a8b9

Please sign in to comment.