-
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.
- Loading branch information
1 parent
d10813f
commit a003f00
Showing
2 changed files
with
80 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
const TextFlip = ({ elementId }) => { | ||
const el = useRef(null); | ||
const [currentStep, setCurrentStep] = useState(0); | ||
|
||
useEffect(() => { | ||
el.current = document.getElementById(elementId); | ||
console.log("Created new Flip"); | ||
|
||
const nextButton = document.querySelector('.next'); | ||
if (nextButton) { | ||
nextButton.addEventListener('click', next); | ||
} | ||
|
||
const intervalId = setInterval(next, 1500); | ||
|
||
return () => { | ||
if (nextButton) { | ||
nextButton.removeEventListener('click', next); | ||
} | ||
clearInterval(intervalId); | ||
}; | ||
}, []); | ||
|
||
const next = (event) => { | ||
if (event) { | ||
event.preventDefault(); | ||
} | ||
|
||
const nextStepNum = currentStep + 1; | ||
const currentStepEl = el.current.querySelector(`.step${currentStep}`); | ||
const nextStepEl = el.current.querySelector(`.step${nextStepNum}`); | ||
|
||
if (nextStepEl) { | ||
console.log('we found the next step', nextStepEl); | ||
|
||
currentStepEl.previousElementSibling?.classList.remove('down'); | ||
|
||
currentStepEl.classList.remove('set'); | ||
currentStepEl.classList.add('down'); | ||
|
||
nextStepEl.classList.add('set'); | ||
nextStepEl.classList.remove('down'); | ||
|
||
nextStepEl.nextElementSibling?.classList.remove('down'); | ||
|
||
setCurrentStep(currentStep + 1); | ||
} else { | ||
// reset to 0 | ||
el.current.querySelectorAll(".step").forEach(step => step.classList.remove('set')); | ||
el.current.querySelector(`.step${currentStep}`)?.classList.add('down'); | ||
el.current.querySelectorAll(`.step:not(.step${currentStep})`).forEach(step => step.classList.remove('down')); | ||
|
||
setCurrentStep(0); | ||
next(); | ||
} | ||
}; | ||
|
||
return null; // This component does not render anything itself | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div id="flipper"> | ||
<div className="step0 set">Step 0</div> | ||
<div className="step1">Step 1</div> | ||
<div className="step2">Step 2</div> | ||
{/* Add more steps as needed */} | ||
</div> | ||
<button className="next">Next</button> | ||
<Flip elementId="flipper" /> | ||
</div> | ||
); | ||
|
||
|
||
export default TextFlip; |
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