Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0805dd7
commit c91f698
Showing
4 changed files
with
136 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useEffect, useRef } from 'react' | ||
|
||
export const Typewriter = (props: { words: string[] }) => { | ||
const { words } = props | ||
const typingElement = useRef<HTMLSpanElement>(null) | ||
const wordIndex = useRef(0) | ||
const charIndex = useRef(0) | ||
const deleteTimeout = 2000 | ||
const typeTimeout = 100 | ||
|
||
useEffect(() => { | ||
typeWords() | ||
}, []) | ||
|
||
const typeWords = () => { | ||
if (typingElement.current) { | ||
if (charIndex.current < words[wordIndex.current].length) { | ||
typingElement.current.textContent = | ||
words[wordIndex.current].substring(0, charIndex.current) + '_' | ||
charIndex.current++ | ||
setTimeout(typeWords, typeTimeout) | ||
} else { | ||
typingElement.current.textContent = words[wordIndex.current] | ||
setTimeout(eraseWords, deleteTimeout) | ||
} | ||
} | ||
} | ||
|
||
const eraseWords = () => { | ||
if (typingElement.current) { | ||
if (charIndex.current > 0) { | ||
typingElement.current.textContent = | ||
words[wordIndex.current].substring(0, charIndex.current - 1) + '_' | ||
charIndex.current-- | ||
setTimeout(eraseWords, 50) | ||
} else { | ||
wordIndex.current++ | ||
if (wordIndex.current >= words.length) { | ||
wordIndex.current = 0 | ||
} | ||
charIndex.current = 0 | ||
setTimeout(typeWords, 500) | ||
} | ||
} | ||
} | ||
|
||
return <span className={'min-h-5'} ref={typingElement}></span> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters