-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a component for random text hover effect
- Loading branch information
1 parent
02c8622
commit 1e2f3b9
Showing
5 changed files
with
66 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
const letters = | ||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%"; | ||
|
||
const ScrambleText = ({ text }) => { | ||
const intervalRef = useRef(null); | ||
const h1Ref = useRef(null); | ||
|
||
useEffect(() => { | ||
const handleMouseOver = (event) => { | ||
let iteration = 0; | ||
|
||
clearInterval(intervalRef.current); | ||
|
||
intervalRef.current = setInterval(() => { | ||
event.target.innerText = event.target.innerText | ||
.split("") | ||
.map((letter, index) => { | ||
if (index < iteration) { | ||
return event.target.dataset.value[index]; | ||
} | ||
|
||
return letters[Math.floor(Math.random() * letters.length)]; | ||
}) | ||
.join(""); | ||
|
||
if (iteration >= event.target.dataset.value.length) { | ||
clearInterval(intervalRef.current); | ||
} | ||
|
||
iteration += 1 / 3; | ||
}, 30); | ||
}; | ||
|
||
const h1Element = h1Ref.current; | ||
h1Element.addEventListener("mouseover", handleMouseOver); | ||
|
||
return () => { | ||
h1Element.removeEventListener("mouseover", handleMouseOver); | ||
clearInterval(intervalRef.current); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<h1 ref={h1Ref} data-value={text}> | ||
{text} | ||
</h1> | ||
); | ||
}; | ||
|
||
export default ScrambleText; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as Header } from "./Header"; | ||
export { default as Footer } from "./Footer"; | ||
export { default as ScrambleText } from "./ScrambleText"; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import { ScrambleText } from "../components"; | ||
|
||
const About = () => { | ||
return <section id="about">About</section>; | ||
return ( | ||
<section id="about" className="h-96"> | ||
<ScrambleText text="Kiran Mahajan" /> | ||
</section> | ||
); | ||
}; | ||
|
||
export default About; |