Skip to content

Commit 7478979

Browse files
committed
Appends line content without delay instead of character by character for reduced motion setting.
1 parent f98bd8b commit 7478979

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const loaded: Promise<unknown> = (function () {
1111
class TypingEffectElement extends HTMLElement {
1212
async connectedCallback(): Promise<void> {
1313
await loaded
14-
if (this.content) await typeLines(this.lines, this.content, this.characterDelay, this.lineDelay)
14+
if (this.content)
15+
await typeLines(this.lines, this.content, this.characterDelay, this.lineDelay, this.prefersReducedMotion)
1516
if (this.cursor) this.cursor.hidden = true
1617
this.dispatchEvent(
1718
new CustomEvent('typing:complete', {
@@ -93,12 +94,17 @@ async function typeLines(
9394
lines: string[],
9495
contentElement: HTMLElement,
9596
characterDelay: number,
96-
lineDelay: number
97+
lineDelay: number,
98+
reduceMotion: boolean
9799
): Promise<void> {
98100
for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
99-
for (const character of lines[lineIndex].split('')) {
100-
await wait(characterDelay)
101-
contentElement.innerHTML += character
101+
if (reduceMotion) {
102+
contentElement.append(lines[lineIndex])
103+
} else {
104+
for (const character of lines[lineIndex].split('')) {
105+
await wait(characterDelay)
106+
contentElement.innerHTML += character
107+
}
102108
}
103109

104110
await wait(lineDelay)

0 commit comments

Comments
 (0)