1
1
import React , { useState , useEffect } from 'react'
2
- import { useTransition , animated as a , config } from 'react-spring'
2
+ import { useTransition , animated as a , config , interpolate } from 'react-spring'
3
3
import shuffle from 'lodash/shuffle'
4
4
import { useMeasure , useMedia } from './helpers'
5
5
import data from './data'
6
6
import './styles.css'
7
7
8
+ const FAST_MODE = true
9
+
8
10
export default function App ( ) {
9
11
const columns = 6
10
12
const [ bind , { width } ] = useMeasure ( )
11
13
const [ items , set ] = useState ( data )
12
14
13
- let heights = new Array ( columns ) . fill ( 0 )
14
- let gridItems = 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
- } )
15
+ const heights = new Array ( columns ) . fill ( 0 )
16
+ const gridItems = width
17
+ ? items . map ( ( child , i ) => {
18
+ const column = heights . indexOf ( Math . min ( ...heights ) )
19
+ const height = child . height / 2
20
+ return {
21
+ ...child ,
22
+ xy : [
23
+ ( width / columns ) * column ,
24
+ ( heights [ column ] += height ) - height ,
25
+ ] ,
26
+ width : width / columns ,
27
+ height,
28
+ }
29
+ } )
30
+ : [ ]
22
31
32
+ const trail = FAST_MODE ? 25 : 80
23
33
const transitions = useTransition ( gridItems , item => item . css , {
24
- from : ( { xy, width, height } ) => ( { xy, width, height, 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 } ,
34
+ from : ( { xy, width, height } ) => ( {
35
+ xy,
36
+ width,
37
+ height,
38
+ opacity : 0 ,
39
+ scale : 0.8 ,
40
+ } ) ,
41
+ enter : ( _ , i ) => ( { scale : 1 , opacity : 1 , delay : i * trail } ) ,
42
+ update : ( { xy } ) => ( { xy } ) ,
43
+ leave : { scale : 0.8 , opacity : 0 } ,
28
44
config : config . stiff ,
29
- trail : 25 ,
30
45
} )
31
46
32
- useEffect ( ( ) => void setInterval ( ( ) => set ( shuffle ) , 2000 ) , [ ] )
47
+ useEffect ( ( ) => every ( FAST_MODE ? 1000 : 2500 , ( ) => set ( shuffle ) ) , [
48
+ gridItems . length ,
49
+ ] )
33
50
34
51
return (
35
- < div className = "mgrid" >
52
+ < div
53
+ className = "mgrid"
54
+ onClick = { ( ) => {
55
+ set ( items . length ? [ ] : data )
56
+ } } >
36
57
< div { ...bind } className = "mgrid-list" >
37
- { transitions . map ( ( { item, props : { xy, ...rest } , key } ) => (
58
+ { transitions . map ( ( { item, props : { xy, scale , ...rest } , key } ) => (
38
59
< a . div
39
60
key = { key }
40
61
style = { {
41
- transform : xy . interpolate (
42
- ( x , y ) => `translate3d(${ x } px,${ y } px,0)`
62
+ transform : interpolate (
63
+ [ xy , scale ] ,
64
+ ( [ x , y ] , s ) => `translate3d(${ x } px,${ y } px,0) scale(${ s } )`
43
65
) ,
44
66
...rest ,
45
67
} } >
@@ -50,3 +72,8 @@ export default function App() {
50
72
</ div >
51
73
)
52
74
}
75
+
76
+ function every ( ms , cb ) {
77
+ const id = setInterval ( cb , ms )
78
+ return ( ) => clearInterval ( id )
79
+ }
0 commit comments