Skip to content

Commit b1b5863

Browse files
committed
[FIX] put cursor inside html
closes #624
1 parent bddda19 commit b1b5863

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

assets/demos.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ document.addEventListener('DOMContentLoaded', function () {
118118
backSpeed: 0,
119119
loop: true,
120120
});
121+
122+
new Typed("#typed7", {
123+
stringsElement: '#typed-strings-2',
124+
backSpeed: 10,
125+
typeSpeed: 30,
126+
backDelay: 3000,
127+
cursorChar: "🐫",
128+
smartBackspace: true,
129+
loop: true,
130+
});
131+
132+
121133
});
122134

123135
function prettyLog(str) {

index.html

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,57 @@ <h2 id="bulk">Bulk Typing</h2>
161161
});
162162
</code>
163163
</pre>
164+
165+
166+
<hr />
167+
168+
<h2 id="basic">Article</h2>
169+
<div class="type-wrap">
170+
<div id="typed-strings-2"><span>
171+
172+
<h1>A long time ago in a galaxy far, far away....</h1>
173+
<p>
174+
The JavaScript universe was in disarray, with developers struggling to bring order to their code. Amidst this chaos, a powerful library emerged from the digital cosmos: <strong>typed.umd.js</strong>. Forged by the ingenious minds at Yargs, this Universal Module Definition (UMD) compatible artifact became a beacon of hope for those seeking to wield the Force of type safety in their JavaScript projects.
175+
</p>
176+
<p>
177+
<strong>Typed.umd.js</strong> is a lightweight, versatile library designed to enhance JavaScript with static typing capabilities, bridging the gap between the dynamic nature of the language and the structured discipline of TypeScript-like environments. Compatible with Node.js, browsers, and various module systems (CommonJS, AMD, and global scripts), it empowers developers to define and enforce type contracts across their applications, reducing errors and boosting maintainability.
178+
</p>
179+
<h2>Key Features of typed.umd.js</h2>
180+
<ul>
181+
<li><strong>Type Annotations</strong>: Seamlessly integrate type definitions into JavaScript code, allowing for runtime type checking without the need for a full TypeScript setup.</li>
182+
<li><strong>Cross-Platform Compatibility</strong>: Its UMD format ensures it works in any environment, from ancient browser scripts to modern Node.js modules.</li>
183+
<li><strong>Minimal Footprint</strong>: Lightweight and unobtrusive, it adds type safety without bloating your project.</li>
184+
<li><strong>Interoperability</strong>: Plays well with existing JavaScript ecosystems, making it easy to adopt incrementally in legacy codebases.</li>
185+
</ul>
186+
<p>
187+
Developers across the galaxy have hailed <strong>typed.umd.js</strong> as a unifying force, enabling them to write robust, scalable code while maintaining the flexibility of JavaScript. Whether you're building a starship-grade web application or a small utility droid, this library is your ally in the battle against runtime errors.
188+
</p>
189+
<p>
190+
To harness its power, simply include <strong>typed.umd.js</strong> in your project via a CDN or npm (<code>npm install typed</code>), and start defining types like a Jedi Master crafting a lightsaber. For full documentation, consult the ancient texts at the <a href="https://github.com/yargs/typed.js" target="_blank">Yargs GitHub repository</a>.
191+
</p>
192+
<p><strong>May the types be with you!</strong></p>
193+
194+
195+
196+
</span></div>
197+
<span id="typed7">Typed</span>
198+
</div>
199+
200+
<pre>
201+
<code class="javascript">
202+
var typed = new Typed("#typed7", {
203+
stringsElement: '#typed-strings-2',
204+
backSpeed: 10,
205+
typeSpeed: 30,
206+
backDelay: 3000,
207+
cursorChar: "🐫",
208+
smartBackspace: true
209+
});
210+
</code>
211+
</pre>
212+
213+
214+
164215
</div>
165216

166217
<script type="text/javascript">

src/typed.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@ export default class Typed {
332332
if (this.pause.status) return;
333333
if (this.cursorBlinking === isBlinking) return;
334334
this.cursorBlinking = isBlinking;
335+
const typedCursor = this.el.querySelector('#cursor');
336+
if (typedCursor){
337+
if (isBlinking) {
338+
typedCursor.classList.add('typed-cursor--blink');
339+
} else {
340+
typedCursor.classList.remove('typed-cursor--blink');
341+
}
342+
}
335343
if (isBlinking) {
336344
this.cursor.classList.add('typed-cursor--blink');
337345
} else {
@@ -391,7 +399,7 @@ export default class Typed {
391399
if (this.isInput) {
392400
this.el.value = str;
393401
} else if (this.contentType === 'html') {
394-
this.el.innerHTML = str;
402+
this.el.innerHTML = str + this.cursor.outerHTML;
395403
} else {
396404
this.el.textContent = str;
397405
}
@@ -424,9 +432,11 @@ export default class Typed {
424432
if (!this.showCursor) return;
425433
if (this.cursor) return;
426434
this.cursor = document.createElement('span');
435+
this.cursor.id = "cursor";
427436
this.cursor.className = 'typed-cursor';
428437
this.cursor.setAttribute('aria-hidden', true);
429438
this.cursor.innerHTML = this.cursorChar;
439+
if (this.contentType === 'html') return;
430440
this.el.parentNode &&
431441
this.el.parentNode.insertBefore(this.cursor, this.el.nextSibling);
432442
}

0 commit comments

Comments
 (0)