Skip to content

Commit

Permalink
Asteroids are Deleted On Click
Browse files Browse the repository at this point in the history
  • Loading branch information
IndiSnacks committed Apr 5, 2024
1 parent 6aa6c41 commit d1a3662
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 33 deletions.
20 changes: 0 additions & 20 deletions src/assets/AsteroidsSVG/Large-Asteroid-2.svg

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/Asteroid/Asteroid.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
.asteroid-container {
position: absolute;
pointer-events: none;
height: 100%;
width: 100%;
display: grid;
align-items: start;

.asteroid{
z-index: 100;
position: absolute;
top: 0;
left: 0;
Expand Down
56 changes: 44 additions & 12 deletions src/components/Asteroid/Asteroid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export default function Asteroid({homeRef}: AsteroidProps) {

const [numAsteroids, setNumAsteroids] = useState(0);
const [contentInfo, setContentInfo] = useState({paddingLeft: 0, width: 0});
const [asteroidAnimations, setAsteroidAnimations] = useState<animationProps[]>([]); // [ {x: number, y: number}
const [asteroidVisibility, setAsteroidVisibility] = useState<boolean[]>([]);

type animationProps = {
initial: Point;
animate: Point;
transition: {duration: number, ease: number[]}
}

type Point = {
x: number;
Expand Down Expand Up @@ -41,7 +49,7 @@ export default function Asteroid({homeRef}: AsteroidProps) {
}

if(endX > contentMidPoint) {
endX = Math.max(contentWidth - (Math.random() * 500), endX)
endX = Math.max(contentWidth - 500 - (Math.random() * 100), endX)
}

const endPT = {x: endX,
Expand Down Expand Up @@ -69,29 +77,53 @@ export default function Asteroid({homeRef}: AsteroidProps) {
}
}

const onButtonClick = (index : number) => {
console.log('button clicked');
console.log(index);

// Update the visibility state to hide the clicked asteroid
setAsteroidVisibility(prevVisibility => {
const newVisibility = [...prevVisibility];
newVisibility[index] = false;
return newVisibility;
});
}

// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => {
const homeElement = homeRef.current;
if (homeElement) {
const style = window.getComputedStyle(homeElement);
const paddingLeftValue = style.paddingLeft;
setContentInfo({paddingLeft: parseFloat(paddingLeftValue), width: homeElement.getBoundingClientRect().width});

const numAsteroids = 5;
setNumAsteroids(numAsteroids);

setAsteroidVisibility(Array(numAsteroids).fill(true));

setNumAsteroids(5);
// Generate animations for each asteroid
const newAnimations = Array.from({ length: numAsteroids }, () => generateAnimationProps());
setAsteroidAnimations(newAnimations);
}
}, [])

Check warning on line 109 in src/components/Asteroid/Asteroid.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has missing dependencies: 'generateAnimationProps' and 'homeRef'. Either include them or remove the dependency array

return (
<div className='asteroid-container'>
<section className='asteroid-container'>
{Array.from({ length: numAsteroids }, (_, index) => (
<motion.div
key={index}
className='asteroid'
{...generateAnimationProps()}
>
<LargeAsteroid2 />
</motion.div>
))}
</div>
asteroidVisibility[index] && (
<motion.div
key={index}
className='asteroid'
onClick={() => onButtonClick(index)}
initial={asteroidAnimations[index]?.initial}
animate={asteroidAnimations[index]?.animate}
transition={asteroidAnimations[index]?.transition}
>
<LargeAsteroid2 />
</motion.div>
)
))}
</section>
);
}
1 change: 1 addition & 0 deletions src/styles/App.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.main-content {
height: 100dvh;
overflow-y: scroll;
overflow-x: hidden;
scroll-snap-type: y mandatory;
margin-inline: auto;
position: relative;
Expand Down

0 comments on commit d1a3662

Please sign in to comment.