A React typewriter component with multiple words and customization options
npm install reactjs-typewriter-effectshell
with npm
npm i reactjs-typewriter-effect
with yarn
yarn add reactjs-typewriter-effect
`
Word Object Structure
Each object in the words array should have the following structure:
`javascript
{
text: string,
styles: [
{
start: number,
end: number,
...cssProperties
},
// ... more style objects
]
}
`
- text: The string to be typed.
- styles: An array of style objects, each defining a styled section of the text.
- start: The starting index of the styled section.
- end: The ending index of the styled section.
- ...cssProperties: Any valid CSS properties to apply to the styled section.
Usage
Import the component where you want to use it:
`jsx
import React from "react";
import Typewriter from "reactjs-typewriter-effect";
const ComponentName = () => {
return (
Welcome to tutorial of reactjs-typewriter-effect
words={[
{ text: "Welcome to my portfolio!", styles: [] },
{
text: "I'm a full-stack developer",
styles: [{ start: 6, end: 24, color: "blue", fontWeight: "bold" }],
},
{
text: "Specializing in React and Node.js",
styles: [
{ start: 16, end: 21, color: "cyan" },
{ start: 26, end: 33, color: "green" },
],
},
]}
typingInterval={150}
deletingInterval={100}
pauseTime={200}
loop={true}
color="blue"
cursorStyle="_"
fontName="Roboto"
fontSize="24px"
fontWeight={600}
textShadow="2px 2px 4px rgba(0,0,0,0.3)"
lineHeight="1.5"
customClass="opacity"
/>
);
};
export default ComponentName;
`
This example will create a typewriter effect that cycles through three phrases, with custom styling applied to specific words or sections.
Props
| Prop | Type | Default | Description |
| ------------------ | ---------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| words | Array of Objects | Required | An array of word objects to be typed. Each object should have a text property (string) and a styles property (array of style objects). |
| typingInterval | Number | 150 | The interval (in milliseconds) between typing each character. |
| deletingInterval | Number | 50 | The interval (in milliseconds) between deleting each character. |
| pauseTime | Number | 100 | The time (in milliseconds) to pause after completing a word before starting to delete it. |
| loop | Boolean | true | Whether to loop through the words array continuously. |
| color | String | 'inherit' | The default text color for the typewriter text. |
| cursorStyle | String | '\|' | The character or string to use as the cursor. |
| fontName | String | undefined | The name of the font to use for the typewriter text. If provided, it will be loaded from Google Fonts. |
| fontSize | String | undefined | The font size for the typewriter text. |
| fontWeight | String or Number | undefined | The font weight for the typewriter text. |
| textShadow | String | undefined | The text shadow CSS property for the typewriter text. |
| lineHeight | String or Number | undefined | The line height for the typewriter text. |
| customClass` | String | undefined | A custom CSS class to apply to the typewriter component. |