"Make it blink!"is an app that creates a blinking effect for the text entered by the user.
To implement the blinking effect, the app uses setInterval to update the color of each character every blinkSpeed milliseconds. The color of each character is determined by cycling through an array of colors.
useEffect(() => { const intervalId = setInterval(() => {
setColorIndex((prevColorIndex) => (prevColorIndex + 1) % 6); }, blinkSpeed);
return () => clearInterval(intervalId);
}
, [blinkSpeed]
);This code sets up an effect to run after the component has rendered using the useEffect hook. It creates an interval using setInterval, which will run the callback function every blinkSpeed milliseconds. Inside the callback, the setColorIndex function is called with a callback that increments the colorIndex state variable by 1 and then takes the remainder when divided by 6. This creates a cycling effect through an array of six colors. Finally, the interval is cleared when the component is unmounted using clearInterval. The useEffect hook takes the blinkSpeed state variable as a dependency, which means that the effect will be re-run if blinkSpeed changes
const coloredString = inputValue.split("").map((char, index) => (
<span key={index} style={{ color: colors[(index + colorIndex) % 6] }}> {char} </span>
));This code defines a variable named coloredString using the map function to transform each character in the inputValue string into a <span> element.
-
The
mapfunction iterates through each character ofinputValueusingsplit(""), which returns an array of each character. -
For each character, a
<span>element is created with akeyattribute set to theindexparameter passed to the callback function. -
The
styleattribute of each<span>element is set using an object literal that sets thecolorattribute to the color at the(index + colorIndex) % 6position in thecolorsarray. -
This expression creates a cycling effect through the six colors in the array based on the index of the character and the current value of the
colorIndexstate variable.
The expression
(index + colorIndex) % 6is used to cycle through the six colors in thecolorsarray based on the index of each character and the current value of thecolorIndexstate variable.
The
%(modulo) operator calculates the remainder of dividing the sum of theindexandcolorIndexvalues by6. This results in a value between0and5, which is used as the index to access the corresponding color in thecolorsarray.
By adding the current value of
colorIndexto the index of each character, the colors will cycle through the array as the value ofcolorIndexis incremented. WhencolorIndexis0, the first color in the array will be used for all characters. WhencolorIndexis1, the second color will be used, and so on. WhencolorIndexis6or higher, it will reset back to0, so the cycle can repeat.
-
Finally, the text of each
<span>element is set to the corresponding character in theinputValuestring using thecharparameter. -
The resulting
coloredStringvariable is a React element that can be rendered to display the input text with the cycling colors.
- User can enter text to create a blinking effect
- User can adjust the speed of the blinking effect using a range input
- Text blinks with a variety of colors
| Color Code | Color Name |
|---|---|
| #333333 | dark gray |
| #666666 | gray |
| #999999 | light gray |
| #FFA500 | orange |
| #FFA07A | light salmon |
| #FF8C00 | dark orange |
The font family and font sizes are defined usingΒ Google Fonts and TypeScale.
- "Permanent Marker" , cursive
- "Raleway", sans-serif
To run this app on your local machine:
- Clone the repository using
git clone https://github.com/z-bj/Make-it-blink.git. - Navigate to the project directory using `cd Make-it-blink.
- Install the dependencies using
npm install. - Start the development server using
npm start.
Contributions are welcome! To contribute to this project:
- Fork the repository.
- Make your changes.
- Submit a pull request.
This project is licensed under the MIT License.
