|
| 1 | +import React, { useState, useEffect } from 'react' |
| 2 | +import { useTransition, animated as a, config } from 'react-spring/hooks' |
| 3 | +import shuffle from 'lodash/shuffle' |
| 4 | +import { useMeasure, useMedia } from './helpers' |
| 5 | +import data from './data' |
| 6 | +import './styles.css' |
| 7 | + |
| 8 | +export default function App() { |
| 9 | + const columns = 6 |
| 10 | + const [bind, { width }] = useMeasure() |
| 11 | + const [items, set] = useState(data) |
| 12 | + |
| 13 | + let heights = new Array(columns).fill(0) |
| 14 | + const transItems = items.map((child, i) => { |
| 15 | + const column = heights.indexOf(Math.min(...heights)) |
| 16 | + const xy = [ |
| 17 | + (width / columns) * column, |
| 18 | + (heights[column] += child.height / 2) - child.height / 2, |
| 19 | + ] |
| 20 | + return { ...child, xy, width: width / columns, height: child.height / 2 } |
| 21 | + }) |
| 22 | + |
| 23 | + const transitions = useTransition(width ? transItems : [], item => item.css, { |
| 24 | + from: ({ xy, width, height }) => ({ xy, width, height: 0, opacity: 0 }), |
| 25 | + enter: ({ xy, width, height }) => ({ xy, width, height, opacity: 1 }), |
| 26 | + update: ({ xy, width, height }) => ({ xy, width, height }), |
| 27 | + leave: { height: 0, opacity: 0 }, |
| 28 | + config: config.stiff, |
| 29 | + trail: 25, |
| 30 | + }) |
| 31 | + |
| 32 | + useEffect(() => void setInterval(() => set(shuffle), 2000), []) |
| 33 | + |
| 34 | + return ( |
| 35 | + <div className="mgrid"> |
| 36 | + <div |
| 37 | + {...bind} |
| 38 | + className="mgrid-list" |
| 39 | + style={{ height: Math.max(...heights) }}> |
| 40 | + {transitions.map(({ item, props: { xy, ...rest }, key }) => ( |
| 41 | + <a.div |
| 42 | + key={key} |
| 43 | + style={{ |
| 44 | + transform: xy.interpolate( |
| 45 | + (x, y) => `translate3d(${x}px,${y}px,0)` |
| 46 | + ), |
| 47 | + ...rest, |
| 48 | + }}> |
| 49 | + <div style={{ backgroundImage: item.css }} /> |
| 50 | + </a.div> |
| 51 | + ))} |
| 52 | + </div> |
| 53 | + </div> |
| 54 | + ) |
| 55 | +} |
0 commit comments