By michael
Published at: 7/18/2023, 9:03:43 PM
In the dynamic realm of web development, captivating user interfaces (UIs) play a vital role in engagement and user experience. One of the charming ways to create an engaging UI is by incorporating interactive elements, such as a typewriter effect. Today, we're going to explore how to build a responsive, interactive loading screen using HTML, CSS, and JavaScript within the React framework, employing a typewriter effect.
Our goal is to construct a loading screen that sequentially types out multiple messages. At a designated point, the screen clears itself, and the typing resumes. Once all messages are displayed, the user can dismiss the loading screen by clicking anywhere or pressing 'Enter'. We'll ensure our design remains fluid and responsive across all device screen sizes for a seamless user experience.
First, let's initialise the state for our loading screen. We'll do this using React's useState and useEffect hooks.
const [currentMessageIndex, setCurrentMessageIndex] = useState(0); const [text, setText] = useState(""); const [charIndex, setCharIndex] = useState(0);
The typewriter effect, a charming UI element that mimics an old-fashioned typewriter's mechanical typing, is created using a setTimeout function that increments the character index every 120 milliseconds. This gives the illusion of typing, as each character appears one after another.
useEffect(() => { if (currentMessageIndex >= messages.length) { return; } if (messages[currentMessageIndex] === 'Clear') { setText(""); setCurrentMessageIndex(currentMessageIndex + 1); setCharIndex(0); return; } if (charIndex < messages[currentMessageIndex].length) { const timeoutId = setTimeout(() => { setText((text) => text + messages[currentMessageIndex][charIndex]); setCharIndex(charIndex + 1); }, 120); return () => clearTimeout(timeoutId); } else { setText((text) => text + '<br /><br />'); setCurrentMessageIndex(currentMessageIndex + 1); setCharIndex(0); } }, [charIndex, currentMessageIndex]);
To enhance our loading screen's interactivity, we add event listeners for keyboard and mouse events. These listeners will dismiss the loading screen when the user clicks anywhere or presses 'Enter'. These event listeners ensure that our UI remains user-friendly and accessible.
useEffect(() => { const handleKeyPress = (event) => { if (event.key === 'Enter' || event.key === 'Escape') { setLoadingDone(true); } }; const handleClick = () => { setLoadingDone(true); }; window.addEventListener('keydown', handleKeyPress); window.addEventListener('click', handleClick); return () => { window.removeEventListener('keydown', handleKeyPress); window.removeEventListener('click', handleClick); } }, [setLoadingDone]);
Now let's give our loading screen some style. We'll use CSS to style the HTML elements and create the blinking cursor effect, a crucial element of the typewriter illusion.
.loading-screen { background-color: black; color: green; height: 100vh; width: 100vw; display: flex; justify-content: center; align-items: center; padding: 2rem; cursor: ${charIndex + currentMessageIndex >= messages.join('').length ? 'pointer' : 'default'}; font-size: 2rem; box-sizing: border-box; } .typewriter { text-align: center; white-space: pre-wrap; border-right: .15em solid green; animation: blink-caret .75s step-end infinite; max-width: 100%; overflow-wrap: break-word; } /* The typewriter cursor effect */ @keyframes blink-caret { from, to { border-color: transparent } 50% { border-color: green; } }
In the era of ubiquitous devices and screen sizes, responsive design is a non-negotiable aspect of web development. We'll use CSS media queries to adjust the font size based on the viewport width. Moreover, to ensure our text does not touch the screen edges, we'll add some padding to our loading screen.
@media (max-width: 600px) { .loading-screen { font-size: 1.5rem; } } @media (max-width: 400px) { .loading-screen { font-size: 1rem; } }
In this tutorial, we've learnt how to create an interactive loading screen with a typewriter effect using React, HTML, CSS, and JavaScript. We've also ensured our design is responsive, providing a great user experience across all devices.
These techniques and concepts, such as managing state with React hooks, handling user interactions, creating CSS animations, and implementing responsive design, are all fundamental skills in modern front-end development. They're applicable across a wide range of projects beyond this tutorial.
The learning doesn't stop here. To deepen your understanding and skills, consider the following:
Numerous online platforms such as Codecademy, Udemy, and Coursera offer comprehensive courses on front-end development. These resources cover a wide range of topics, including HTML, CSS, JavaScript, React, Next.js, and other essential front-end languages and frameworks. With step-by-step instructions and hands-on coding exercises, these courses provide a structured path for learning and practising front-end development.
After gaining a foundational understanding of front-end development, it's crucial to apply your knowledge to real-world projects. Whether you're enhancing the interactivity of your website or creating a new application, hands-on experience is invaluable. You might choose to refine the typewriter effect or to build entirely new features. These practical experiences will solidify your understanding and give you confidence in your front-end development skills.
Participating in coding communities such as GitHub and Stack Overflow can offer immense benefits. These platforms host forums where developers from around the globe share their knowledge, discuss new trends, and provide support. By engaging in these discussions, you can learn from experienced developers, get answers to your questions, and receive feedback on your projects.
Open-source projects provide a treasure trove of learning opportunities. You can find numerous front-end projects on platforms like GitHub, which offer real-world examples of how languages and frameworks like React and Next.js are used in practice. By analysing and contributing to these projects, you can learn best practices, encounter new challenges, and enhance your problem-solving skills.
Front-end development involves a variety of coding languages, each with its unique role.
Here are some of the key languages used in front-end development:
By following these steps, you'll continue to grow as a front-end developer, gaining the skills and knowledge needed to create engaging, responsive, and user-friendly interfaces. Remember, the journey of learning in development is ongoing, and every new project or challenge offers an opportunity to learn something new. Keep coding and keep exploring!