(Notes for myself - you can check out the effect at 8bitoracle.ai)
Getting a typing effect working properly is pretty hard. I have a few paragraphs of text and wanted to have a typing effect over it, and allow the user to skip animation. If a user presses a spacebar or clicks on a paragraph, the current paragraph completes and the next one begins typing.
A few tips:
1) DOM Manipulation is frequently buggy, so you are best off updating the html elements themselves.
2) It’s easier to simulate a typing effect using hidden/visible rather than trying to actually generate characters or copy them from a hidden data element.
Here are the key lessons learned from the provided code for implementing a typing effect in React, applied to mulitple paragraphs of text with a way to skip animation at any time:
- State Management:
- Use the
useState
hook to manage the state of the current paragraph index (currentParagraphIndex
) and the number of visible characters (visibleCharacters
). - Update the state variables accordingly to control the typing effect and track the progress.
- Use the
- Parsing Paragraphs:
- Extract the paragraphs from the
backstory
object based on the selected language usingObject.entries
andmap
. - Create an array of paragraph objects with
id
andtext
properties for easier rendering and tracking.
- Extract the paragraphs from the
- Typing Effect:
- Use the
useEffect
hook to control the typing effect based on thevisibleCharacters
andcurrentParagraphIndex
state variables. - Set a timeout to gradually increase the number of visible characters for the current paragraph.
- Move to the next paragraph after a delay when the current paragraph is fully typed.
- Clear the timeout when the component unmounts or the dependencies change to avoid memory leaks.
- Use the
- Skipping Animation:
- Implement a
skipTypingAnimation
function to allow the user to skip the typing animation and immediately reveal all characters of the current paragraph. - Use the
useCallback
hook to memoize the function and optimize performance.
- Implement a
- Event Handlers:
- Add event handlers for keyboard and touch events to trigger the
skipTypingAnimation
function. - Use
useEffect
hooks to attach and remove the event listeners based on the relevant dependencies.
- Add event handlers for keyboard and touch events to trigger the
- Rendering:
- Map over the
paragraphs
array to render each paragraph as a separate<p>
element. - Determine the visible and hidden parts of each paragraph based on the
currentParagraphIndex
andvisibleCharacters
state variables. - Render the visible text, the typing cursor for the current paragraph, and optionally, the hidden text with a ‘hidden’ class.
- Add click and touch event handlers to each paragraph to allow skipping the animation.
- Map over the
- CSS Styling:
- Apply appropriate CSS classes to style the text content, cursor, and hidden text.
- Use CSS animations or transitions to create a blinking effect for the typing cursor.
- Performance Optimization:
- Use
useCallback
anduseMemo
hooks to memoize functions and values that don’t need to be recomputed on every render. - Optimize the rendering by only updating the necessary parts of the component based on state changes.
- Use
- Internationalization:
- Utilize the
useTranslation
hook from thereact-i18next
library to handle internationalization. - Extract the appropriate translations from the
backstory
object based on the selected language.
- Utilize the
These are the main lessons learned from the provided code. By understanding and applying these concepts, you can create a typing effect component in React that is efficient, interactive, and supports internationalization.