summaryrefslogtreecommitdiff
path: root/components/hacktype.tsx
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2024-10-13 00:38:37 +0800
committerlittleblack111 <littleblack11111@gmail.com>2024-10-13 00:38:37 +0800
commit433e08f2f798b4c04d57cd2d61b16a58aa5196be (patch)
tree699df47962e2fc921c9ddde8a034f41af92b3c63 /components/hacktype.tsx
parent308eb96365a7cee0ebc8118673c3b6c85497ce3e (diff)
core: added home page
Diffstat (limited to 'components/hacktype.tsx')
-rw-r--r--components/hacktype.tsx55
1 files changed, 55 insertions, 0 deletions
diff --git a/components/hacktype.tsx b/components/hacktype.tsx
new file mode 100644
index 0000000..59c91b3
--- /dev/null
+++ b/components/hacktype.tsx
@@ -0,0 +1,55 @@
+"use client"
+
+import { useEffect, useRef } from "react";
+
+const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+function hackType(element: HTMLElement) {
+ let iteration = 0;
+ const interval = setInterval(() => {
+ element.innerText = element.innerText
+ .split("")
+ .map((letter, index) => {
+ if (index < iteration) {
+ return element.getAttribute('data-value')![index];
+ }
+ return letters[Math.floor(Math.random() * 26)];
+ })
+ .join("");
+
+ if (iteration >= element.getAttribute('data-value')!.length) {
+ clearInterval(interval);
+ }
+
+ iteration += 1 / 5;
+ }, 20);
+}
+
+export default function HackType({ text }: { text: string }) {
+ const elementRef = useRef<HTMLDivElement>(null);
+
+ useEffect(() => {
+ if (elementRef.current) {
+ elementRef.current.setAttribute('data-value', text);
+ hackType(elementRef.current);
+
+ const interval = setInterval(() => {
+ hackType(elementRef.current!);
+ }, 10000);
+
+ document.addEventListener("visibilitychange", () => {
+ if (!document.hidden && elementRef.current) {
+ hackType(elementRef.current);
+ }
+ });
+
+ return () => clearInterval(interval);
+ }
+ }, [text]);
+
+ return (
+ <div ref={elementRef} className="hacktype">
+ {text}
+ </div>
+ );
+} \ No newline at end of file