Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
import React , { useRef , useState } from 'react'
import anime from 'animejs'
import Transition from 'react-transition-group/Transition'
import TransitionGroup from 'react-transition-group/TransitionGroup'
// we will trigger an event on the grid node after the exit animation completes
// to let the transitiongroup know that it can be removed from the DOM
// this is the only way to let react-transition-group delegate timing
// to the JavaScript animation
const ANIMATION_DONE_EVENT = 'animation::done'
const triggerAnimationDoneEvent = node =>
node . dispatchEvent ( new Event ( ANIMATION_DONE_EVENT ) )
const createOpacityAnimationConfig = animatingIn => ( {
value : animatingIn ? [ , 1 ] : ,
easing : 'linear' ,
duration : 300
} )
const easing = 'spring(1, 150, 10)'
const animateGridIn = gridContainer =>
anime
. timeline ( )
. add ( {
targets : gridContainer ,
translateX : [ - 1000 , ] ,
opacity : createOpacityAnimationConfig ( true ) ,
easing
} )
. add (
{
targets : gridContainer . querySelectorAll ( '.card' ) ,
easing ,
opacity : createOpacityAnimationConfig ( true ) ,
translateY : [ - 30 , ] ,
delay : anime . stagger ( 70 )
} ,
'-=500'
)
const animateGridOut = gridContainer =>
anime
. timeline ( )
. add ( {
targets : gridContainer . querySelectorAll ( '.card' ) ,
easing ,
opacity : createOpacityAnimationConfig ( false ) ,
translateY : - 30 ,
delay : anime . stagger ( 50 )
} )
. add (
{
targets : gridContainer ,
translateX : 1000 ,
opacity : createOpacityAnimationConfig ( false ) ,
easing ,
complete : ( ) => triggerAnimationDoneEvent ( gridContainer )
} ,
'-=1400'
)
const animateCardIn = card =>
anime ( {
targets : card ,
opacity : createOpacityAnimationConfig ( true ) ,
translateY : [ 50 , ] ,
easing
} )
const animateCardOut = card =>
anime ( {
targets : card ,
translateY : - 10 ,
opacity : createOpacityAnimationConfig ( false ) ,
easing
} )
const addEndListener = ( node , done ) =>
node . addEventListener ( ANIMATION_DONE_EVENT , done )
const TransitionGrid = ( { items , visible , removeItem } ) => {
// the code below makes sure we force re-mount the transition component
// every time it enters, to avoid the transition caching old state
// if someone rapidly toggles the component in and out
const transitionKey = useRef ( 1 )
const [ prevVisible , setPrevVisible ] = useState ( visible )
if ( visible !== prevVisible ) setPrevVisible ( visible )
if ( visible && ! prevVisible ) {
transitionKey . current += 1
}
return (
< Transition
unmountOnExit
appear
addEndListener = { addEndListener }
onEnter = { animateGridIn }
onExit = { animateGridOut }
in = { visible }
key = { transitionKey . current }
>
< ul className = "grid animated-grid" >
< TransitionGroup component = { null } >
{ items . map ( item => (
< Transition
key = { item }
onEnter = { animateCardIn }
onExit = { animateCardOut }
addEndListener = { addEndListener }
>
< li className = "card" onClick = { ( ) => removeItem ( item ) } >
< div className = "close-card" > < / div >
< div > { item } < / div >
< / li >
< / Transition >
) ) }
< / TransitionGroup >
< / ul >
< / Transition >
)
}
export default TransitionGrid

友情链: IM·体育网上直播_IM·体育赛事最新版_im体育官网在线官网在线 | im体育app平台下载|IM体育v2.3 安卓版|IM体育今天官网赛表 | im体育推荐官网_im体育今晚高清_im体育买软件 | 2022im体育网网址_分析观看比赛_2022im体育在线软件 | IM体育开户网址-Im体育官方网站-IM体育期胜负彩专家分析 | im电竞体育登录入口_IM·体育比分分析在线_IM·体育v8.5 IOS版 | im体育app官网下载|IM体育最新今天观看|IM体育赛事注册 |