1
1
import { cubicOut , cubicInOut } from './easing' ;
2
+ import { assign , is_function } from './internal' ;
2
3
3
4
export function fade ( node , {
4
5
delay = 0 ,
@@ -79,4 +80,62 @@ export function draw(node, {
79
80
easing,
80
81
css : ( t , u ) => `stroke-dasharray: ${ t * len } ${ u * len } `
81
82
} ;
83
+ }
84
+
85
+ export function crossfade ( { fallback, ...defaults } ) {
86
+ let to_receive = new Map ( ) ;
87
+ let to_send = new Map ( ) ;
88
+
89
+ function crossfade ( from , node , params ) {
90
+ const {
91
+ delay = 0 ,
92
+ duration = d => Math . sqrt ( d ) * 30 ,
93
+ easing = cubicOut
94
+ } = assign ( assign ( { } , defaults ) , params ) ;
95
+
96
+ const to = node . getBoundingClientRect ( ) ;
97
+ const dx = from . left - to . left ;
98
+ const dy = from . top - to . top ;
99
+
100
+ const style = getComputedStyle ( node ) ;
101
+ const transform = style . transform === 'none' ? '' : style . transform ;
102
+
103
+ return {
104
+ delay,
105
+ duration : is_function ( duration ) ? duration ( d ) : duration ,
106
+ easing,
107
+ css : ( t , u ) => `
108
+ opacity: ${ t } ;
109
+ transform: ${ transform } translate(${ u * dx } px,${ u * dy } px);
110
+ `
111
+ } ;
112
+ }
113
+
114
+ function transition ( items , counterparts , intro ) {
115
+ return ( node , params ) => {
116
+ items . set ( params . key , {
117
+ rect : node . getBoundingClientRect ( )
118
+ } ) ;
119
+
120
+ return ( ) => {
121
+ if ( counterparts . has ( params . key ) ) {
122
+ const { rect } = counterparts . get ( params . key ) ;
123
+ counterparts . delete ( params . key ) ;
124
+
125
+ return crossfade ( rect , node , params ) ;
126
+ }
127
+
128
+ // if the node is disappearing altogether
129
+ // (i.e. wasn't claimed by the other list)
130
+ // then we need to supply an outro
131
+ items . delete ( params . key ) ;
132
+ return fallback ( node , params , intro ) ;
133
+ } ;
134
+ } ;
135
+ }
136
+
137
+ return [
138
+ transition ( to_send , to_receive , false ) ,
139
+ transition ( to_receive , to_send , true )
140
+ ] ;
82
141
}
0 commit comments