-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
162 lines (143 loc) · 4.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* External dependencies
*/
import { useSpring } from '@react-spring/web';
/**
* WordPress dependencies
*/
import {
useState,
useLayoutEffect,
useReducer,
useMemo,
useRef,
} from '@wordpress/element';
import { useReducedMotion } from '@wordpress/compose';
import { getScrollContainer } from '@wordpress/dom';
/**
* Simple reducer used to increment a counter.
*
* @param {number} state Previous counter value.
* @return {number} New state value.
*/
const counterReducer = ( state ) => state + 1;
const getAbsolutePosition = ( element ) => {
return {
top: element.offsetTop,
left: element.offsetLeft,
};
};
/**
* Hook used to compute the styles required to move a div into a new position.
*
* The way this animation works is the following:
* - It first renders the element as if there was no animation.
* - It takes a snapshot of the position of the block to use it
* as a destination point for the animation.
* - It restores the element to the previous position using a CSS transform
* - It uses the "resetAnimation" flag to reset the animation
* from the beginning in order to animate to the new destination point.
*
* @param {Object} $1 Options
* @param {boolean} $1.isSelected Whether it's the current block or not.
* @param {boolean} $1.adjustScrolling Adjust the scroll position to the current block.
* @param {boolean} $1.enableAnimation Enable/Disable animation.
* @param {*} $1.triggerAnimationOnChange Variable used to trigger the animation if it changes.
* @param {*} $1.scale Scale of the element
*/
function useMovingAnimation( {
isSelected,
adjustScrolling,
enableAnimation,
triggerAnimationOnChange,
scale = 1,
} ) {
const ref = useRef();
const prefersReducedMotion = useReducedMotion() || ! enableAnimation;
const [ triggeredAnimation, triggerAnimation ] = useReducer(
counterReducer,
0
);
const [ finishedAnimation, endAnimation ] = useReducer( counterReducer, 0 );
const [ transform, setTransform ] = useState( { x: 0, y: 0, scale: 1 } );
const previous = useMemo(
() => ( ref.current ? getAbsolutePosition( ref.current ) : null ),
[ triggerAnimationOnChange ]
);
// Calculate the previous position of the block relative to the viewport and
// return a function to maintain that position by scrolling.
const preserveScrollPosition = useMemo( () => {
if ( ! adjustScrolling || ! ref.current ) {
return () => {};
}
const scrollContainer = getScrollContainer( ref.current );
if ( ! scrollContainer ) {
return () => {};
}
const prevRect = ref.current.getBoundingClientRect();
return () => {
const blockRect = ref.current.getBoundingClientRect();
const diff = blockRect.top - prevRect.top;
if ( diff ) {
scrollContainer.scrollTop += diff;
}
};
}, [ triggerAnimationOnChange, adjustScrolling ] );
useLayoutEffect( () => {
if ( triggeredAnimation ) {
endAnimation();
}
}, [ triggeredAnimation ] );
useLayoutEffect( () => {
if ( ! previous ) {
return;
}
if ( prefersReducedMotion ) {
// If the animation is disabled and the scroll needs to be adjusted,
// just move directly to the final scroll position.
preserveScrollPosition();
return;
}
ref.current.style.transform = `scale( ${ scale } )`;
const destination = getAbsolutePosition( ref.current );
triggerAnimation();
setTransform( {
x: Math.round( previous.left - destination.left ),
y: Math.round( previous.top - destination.top ),
scale,
} );
}, [ triggerAnimationOnChange ] );
function onChange( { value } ) {
if ( ! ref.current ) {
return;
}
let { x, y, scale: currentScale } = value;
x = Math.round( x );
y = Math.round( y );
const finishedMoving = x === 0 && y === 0 && currentScale === scale;
ref.current.style.transformOrigin = 'center center';
ref.current.style.transform = finishedMoving
? `scale( ${ scale } )`
: `translate3d(${ x }px,${ y }px,0) scale(${ currentScale })`;
ref.current.style.zIndex = isSelected ? '1' : '';
preserveScrollPosition();
}
useSpring( {
from: {
x: transform.x,
y: transform.y,
scale: transform.scale,
},
to: {
x: 0,
y: 0,
scale,
},
reset: triggeredAnimation !== finishedAnimation,
config: { mass: 5, tension: 2000, friction: 200 },
immediate: prefersReducedMotion,
onChange,
} );
return ref;
}
export default useMovingAnimation;