-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypify.js
More file actions
148 lines (124 loc) · 5.46 KB
/
typify.js
File metadata and controls
148 lines (124 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
MIT License
Copyright (c) 2023 Sam K Thampan <github@devsk18> <https://devsk18.github.io/samkthampan>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
* Typify - A utility function to create an auto-typing effect with a cursor for an HTML element.
* Author - Sam K Thampan <github@devsk18> <https://devsk18.github.io/samkthampan>
* License - MIT License
* @param {string} selector - Required: The CSS selector for the HTML element where the typing effect will be applied.
* @param {object} options - Required: An object with configuration options for the typing effect.
* @param {string[]} options.text - Required: An array of strings to be typed and erased in sequence.
* @param {number} [options.delay=100] - Optional: The delay in milliseconds between typing each character.
* @param {boolean} [options.loop=true] - Optional: Whether to loop through the 'text' array continuously or stop after one iteration.
* @param {boolean} [options.cursor=true] - Optional: Whether to show the cursor during the typing effect.
* @param {number} [options.stringDelay=1000] - Optional: Time in milliseconds to pause before typing each string.
* @returns {{ stop: function }} - An object with a stop() method to cancel the typing effect.
*/
// Track running instances per element
const _instances = new WeakMap();
function Typify(selector, options) {
// Select the HTML element where the typing effect will be applied
const element = document.querySelector(selector);
// Check if the element exists
if (!element) {
console.warn(`Typify: no element found for selector "${selector}"`);
return { stop: () => {} };
}
// Stop any existing instance for this element
if (_instances.has(element)) {
_instances.get(element).stop();
}
// Default options for the typing effect
const defaultOptions = {
text: [], // An array of strings to be typed and erased in sequence
delay: 100, // The delay in milliseconds between typing each character
stringDelay: 1000, // The delay in milliseconds between typing each string
loop: true, // Whether to loop through the 'text' array continuously or stop after one iteration
cursor: true, // Whether to show the cursor during the typing effect
};
// Merge user-provided options with the default options
const config = { ...defaultOptions, ...options };
let cursorVisible = true;
let stopped = false;
element.textContent = '|'; // Set initial cursor (visible)
// Function to type a single string
async function type(text) {
toggleCursor();
for (const char of text) {
if (stopped) return;
element.textContent = element.textContent.slice(0, -1); // Remove cursor
element.textContent += char + '|'; // Add new character with cursor
await delay(config.delay);
}
toggleCursor();
}
// Function to erase a single string
async function erase() {
toggleCursor();
for (let i = element.textContent.length; i >= 0; i--) {
if (stopped) return;
element.textContent = element.textContent.slice(0, -1); // Remove cursor
element.textContent = element.textContent.slice(0, i) + '|'; // Add cursor during erasing
await delay(config.delay);
}
toggleCursor();
}
// Utility function to introduce a delay
async function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Function to toggle the cursor visibility
function toggleCursor() {
if (config.cursor) {
cursorVisible = !cursorVisible;
element.textContent = element.textContent.replace(/\|/g, cursorVisible ? '|' : ' ');
}
}
// Create and return an instance object with a stop method
const instance = {
stop: () => {
stopped = true;
element.textContent = config.text[0] || '';
_instances.delete(element);
}
};
// Register this instance
_instances.set(element, instance);
// Automatically start the typing effect when options are passed and text array is not empty
if (config.text.length > 0) {
(async function () {
while (true) {
if (stopped) return;
for (const text of config.text) {
await type(text); // Type the string
await delay(config.stringDelay); // Pause between strings
await erase(); // Erase the typed string
}
if (!config.loop) {
await type(config.text[0]);
toggleCursor();
return;
}
}
})();
}
// Return the instance so users can stop it if needed
return instance;
}
export default Typify;