Last active
November 3, 2020 18:26
-
-
Save blakazulu/0e5625efa3eb45c41bafe4d804fd3c09 to your computer and use it in GitHub Desktop.
type-delete - medium demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
AfterViewInit, Component, ElementRef, Input, Renderer2, ViewChild | |
} from "@angular/core"; | |
@Component({ | |
selector: "app-type-delete", | |
templateUrl: "./type-delete.component.html", | |
styleUrls: ["./type-delete.component.css"] | |
}) | |
export class TypeDeleteComponent implements AfterViewInit { | |
@ViewChild("textElement") textElement: ElementRef; | |
@ViewChild("blinkElement") blinkElement: ElementRef; | |
@Input() wordArray: string[] = [ | |
"You Can...", | |
"Write Anything You want...", | |
"SBZ 🍻 Enjoy 🍸🍻🍺🍷🍹" | |
]; | |
@Input() textColor = "black"; | |
@Input() fontSize = "20px"; | |
@Input() blinkWidth = "2px"; | |
@Input() typingSpeedMilliseconds = 300; | |
@Input() deleteSpeedMilliseconds = 300; | |
private i = 0; | |
constructor(private renderer: Renderer2) {} | |
ngAfterViewInit(): void { | |
this.initVariables(); | |
this.typingEffect(); | |
} | |
private initVariables(): void { | |
this.renderer.setStyle( | |
this.textElement.nativeElement, | |
"color", | |
this.textColor | |
); | |
this.renderer.setStyle( | |
this.textElement.nativeElement, | |
"font-size", | |
this.fontSize | |
); | |
this.renderer.setStyle(this.textElement.nativeElement, "padding", "0.1em"); | |
this.renderer.setStyle( | |
this.blinkElement.nativeElement, | |
"border-right-width", | |
this.blinkWidth | |
); | |
this.renderer.setStyle( | |
this.blinkElement.nativeElement, | |
"border-right-color", | |
this.textColor | |
); | |
this.renderer.setStyle( | |
this.blinkElement.nativeElement, | |
"font-size", | |
this.fontSize | |
); | |
} | |
private typingEffect(): void { | |
const word = this.wordArray[this.i].split(""); | |
const loopTyping = () => { | |
if (word.length > 0) { | |
this.textElement.nativeElement.innerHTML += word.shift(); | |
} else { | |
this.deletingEffect(); | |
return; | |
} | |
setTimeout(loopTyping, this.typingSpeedMilliseconds); | |
}; | |
loopTyping(); | |
} | |
private deletingEffect(): void { | |
const word = this.wordArray[this.i].split(""); | |
const loopDeleting = () => { | |
if (word.length > 0) { | |
word.pop(); | |
this.textElement.nativeElement.innerHTML = word.join(""); | |
} else { | |
this.i = this.wordArray.length > this.i + 1 ? this.i++ : 0; | |
this.typingEffect(); | |
return false; | |
} | |
setTimeout(loopDeleting, this.deleteSpeedMilliseconds); | |
}; | |
loopDeleting(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment