-
-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Animate card wall loading transition
- Loading branch information
1 parent
e812bd5
commit 4e4e5f3
Showing
5 changed files
with
306 additions
and
115 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,12 @@ | ||
@keyframes skeleton-loading { | ||
0% { | ||
background-color: hsla(200, 100%, 100%, 0.1); | ||
} | ||
100% { | ||
background-color: hsla(200, 100%, 100%, 0.3); | ||
} | ||
} | ||
|
||
.skeletonPulse { | ||
animation: skeleton-loading 1s linear infinite alternate; | ||
} |
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,149 @@ | ||
import { useMeasure, usePrevious } from "react-use"; | ||
import { useEffect, useState } from "react"; | ||
import { animated, useSpring, useSprings } from "react-spring"; | ||
import DevCard, { DevCardProps } from "components/molecules/DevCard/dev-card"; | ||
import styles from "./dev-card-wall.module.css"; | ||
|
||
interface DevCardWallProps { | ||
cards: DevCardProps[]; | ||
isLoadingUsernames?: boolean; | ||
} | ||
|
||
const LOADING_TILES_COUNT = 20; | ||
|
||
const cellHeight = 348; | ||
const cellWidth = 245; | ||
const gapSize = 20; | ||
|
||
const coordinatesForIndex = (height: number) => (index: number) => { | ||
const extraYOffset = cellHeight * 0.4; | ||
const tilesPerColumn = Math.floor((height - gapSize) / (cellHeight + gapSize)) + 1; | ||
const columnRow = index % tilesPerColumn; | ||
const columnIsOdd = Math.floor(index / tilesPerColumn) % 2 === 1; | ||
const yOffsetForCell = columnIsOdd ? 0 : extraYOffset; | ||
const y = columnRow * (cellHeight + gapSize) - yOffsetForCell; | ||
const x = Math.floor(index / tilesPerColumn) * (cellWidth + gapSize); | ||
return { | ||
x, | ||
y, | ||
}; | ||
}; | ||
|
||
export default function DevCardWall({ isLoadingUsernames = false, cards }: DevCardWallProps) { | ||
const [containerRef, { height }] = useMeasure<HTMLDivElement>(); | ||
const previousHeight = usePrevious(height); | ||
const [activeCardIndex, setActiveCardIndex] = useState<number | null>(null); | ||
|
||
const loadingOpacity = useSpring({ | ||
opacity: isLoadingUsernames ? 1 : 0, | ||
config: { | ||
duration: 1000, | ||
}, | ||
}); | ||
|
||
const [cardTrails, api] = useSprings( | ||
cards.length, | ||
(index) => ({ | ||
opacity: 0, | ||
x: 0, | ||
y: 0, | ||
}), | ||
[cards.length] | ||
); | ||
|
||
useEffect(() => { | ||
if (!height) { | ||
return; | ||
} | ||
let stagger = 0; | ||
if (previousHeight === 0) { | ||
stagger = 100; | ||
} | ||
api.start((i) => ({ | ||
...coordinatesForIndex(height)(i), | ||
delay: i * 100 + stagger, | ||
})); | ||
}, [height, previousHeight, api, cards]); | ||
|
||
// useEffect(() => { | ||
// if(cards.length) { | ||
// api.start((i) => ({ | ||
// ...coordinatesForIndex(height)(i), | ||
// delay: i * 100, | ||
// })); | ||
// } | ||
|
||
// }, [cards]); | ||
|
||
const loadingSkeleton = Array.from({ length: LOADING_TILES_COUNT }, (_, i) => { | ||
const { x, y } = coordinatesForIndex(height)(i); | ||
return ( | ||
<animated.div | ||
className={`grid absolute rounded-3xl ${styles.skeletonPulse}`} | ||
key={i} | ||
style={{ | ||
width: `${cellWidth}px`, | ||
height: `${cellHeight}px`, | ||
left: `${x}px`, | ||
top: `${y}px`, | ||
opacity: loadingOpacity.opacity, | ||
// ...style, | ||
}} | ||
></animated.div> | ||
); | ||
}); | ||
|
||
const cardElements = cardTrails.map((style, i) => { | ||
const cardProps = cards[i]; | ||
const { x, y } = style; | ||
console.log("card props", x, y); | ||
return ( | ||
<animated.div | ||
className="grid absolute" | ||
key={i} | ||
style={{ | ||
width: `${cellWidth}px`, | ||
height: `${cellHeight}px`, | ||
left: x.to((x) => `${x}px`), | ||
top: y.to((y) => `${y}px`), | ||
}} | ||
> | ||
<DevCard isInteractive={false} {...cardProps} /> | ||
</animated.div> | ||
); | ||
}); | ||
|
||
return ( | ||
<div className="relative overflow-hidden" ref={containerRef}> | ||
{/* card wall surface area, should extend beyond the top and bottom */} | ||
{/* {isLoadingUsernames ? loadingSkeleton : cardElements} */} | ||
{loadingSkeleton} | ||
{cardElements} | ||
<div | ||
style={{ | ||
width: "100%", | ||
position: "absolute", | ||
top: 0, | ||
left: 0, | ||
bottom: 0, | ||
right: 0, | ||
zIndex: 2, | ||
pointerEvents: "none", | ||
background: "linear-gradient(90deg, #000 0%, rgba(0, 0, 0, 0.00) 100%)", | ||
}} | ||
></div> | ||
<div | ||
style={{ | ||
width: "100%", | ||
position: "absolute", | ||
top: 0, | ||
left: 0, | ||
zIndex: 2, | ||
pointerEvents: "none", | ||
height: "72.42614145%", | ||
background: "linear-gradient(180deg, #000 0%, rgba(0, 0, 0, 0.00) 40%)", | ||
}} | ||
></div> | ||
</div> | ||
); | ||
} |
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
Oops, something went wrong.