This Vue component simulates a typewriter effect by typing and erasing sentences with customizable typing speed, delay, caret style, and looping options. The component is built with the Vue 3 Composition API and TypeScript.
https://dmncodes.github.io/vue-typing/
- Dynamic Typing: Type out a list of sentences with random typing speed within a specified range.
- Erasing: Erase sentences after typing, with configurable speed and delay.
- Looping: Option to loop through the sentences indefinitely.
- Customizable Caret: Customize the caret character and its appearance.
- Event Emission: Emits an event after typing each sentence, useful for triggering additional actions.
Here’s how you can use the Typewriter component in your Vue application:
With NPM Package Manager
npm i @dmncodes/vue-typing
With Yarn
yarn add @dmncodes/vue-typing
You can import the component into your Vue component:
import VueTyping from "@dmncodes/vue-typing"
<template>
<VueTyping
:sentences="['Hello World!', 'Welcome to Vue.js']"
:minTypeSpeed="50"
:maxTypeSpeed="150"
:eraseSpeed="100"
:eraseDelay="1500"
:writeDelay="0"
caret="_"
:loop="true"
@sentence:typed="onSentenceTyped"
/>
</template>
<script setup>
function onSentenceTyped(sentence: string) {
console.log(`Typed sentence: ${sentence}`);
}
</script>
Prop | Type | Default | Description |
---|---|---|---|
sentences | string[] | [] | List of sentences to type out. |
minTypeSpeed | number | 50 | Minimum typing speed in milliseconds. |
maxTypeSpeed | number | 150 | Maximum typing speed in milliseconds. |
eraseSpeed | number | 100 | Speed of erasing each character in milliseconds. |
eraseDelay | number | 1500 | Delay before starting to erase in milliseconds. |
writeDelay | number | 0 | Delay before starting to type the next sentence in milliseconds. |
caret | string | _ | Character to display as the caret. |
loop | boolean | false | Whether to loop through the sentences indefinitely. |
Event Name | Payload | Description |
---|---|---|
sentence:typed | string (sentence) | Emitted after a sentence is fully typed out. |
The component comes with some basic styles for the caret and typing animations. You can customize these styles by overriding the CSS classes:
.dmn-typing span.caret {
color: inherit;
animation: blink 1s infinite;
}
.dmn-typing span.typing {
animation: none;
}
@keyframes blink {
50% {
opacity: 0;
}
}
Unit tests are written using Vitest and Vue Test Utils. The tests cover:
- Typing the sentences correctly.
- Erasing sentences after typing.
- Emitting events after each sentence is typed.
- Correct application of the caret classes and animations.
Contributions are welcome! Please feel free to submit a Pull Request or open an issue.
This project is licensed under the MIT License.