Skip to content

Commit 6bb7ac6

Browse files
committed
Fix #12: Animate items when moved or removed
1 parent cfc87ca commit 6bb7ac6

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

src/components/containers.js

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,68 @@ export function GroupTitle(props) {
1111
}
1212

1313

14+
function animate(e, animation, callback) {
15+
let el = e.target.parentElement.parentElement;
16+
let prevEl = el.previousElementSibling;
17+
let nextEl = el.nextElementSibling;
18+
19+
el.classList.add('rjf-animate', 'rjf-' + animation);
20+
21+
if (animation === 'move-up') {
22+
let {y, height} = prevEl.getBoundingClientRect();
23+
let y1 = y, h1 = height;
24+
25+
({y, height} = el.getBoundingClientRect());
26+
let y2 = y, h2 = height;
27+
28+
prevEl.classList.add('rjf-animate');
29+
30+
prevEl.style.opacity = 0;
31+
prevEl.style.transform = 'translateY(' + (y2 - y1) + 'px)';
32+
33+
el.style.opacity = 0;
34+
el.style.transform = 'translateY(-' + (y2 - y1) + 'px)';
35+
36+
} else if (animation === 'move-down') {
37+
let {y, height} = el.getBoundingClientRect();
38+
let y1 = y, h1 = height;
39+
40+
({y, height} = nextEl.getBoundingClientRect());
41+
let y2 = y, h2 = height;
42+
43+
nextEl.classList.add('rjf-animate');
44+
45+
nextEl.style.opacity = 0;
46+
nextEl.style.transform = 'translateY(-' + (y2 - y1) + 'px)';
47+
48+
el.style.opacity = 0;
49+
el.style.transform = 'translateY(' + (y2 - y1) + 'px)';
50+
}
51+
52+
setTimeout(function() {
53+
callback();
54+
55+
el.classList.remove('rjf-animate', 'rjf-' + animation);
56+
el.style = null;
57+
58+
if (animation === 'move-up') {
59+
prevEl.classList.remove('rjf-animate');
60+
prevEl.style = null;
61+
}
62+
else if (animation === 'move-down') {
63+
nextEl.classList.remove('rjf-animate');
64+
nextEl.style = null;
65+
}
66+
}, 200);
67+
}
68+
1469
export function FormRowControls(props) {
1570
return (
1671
<div className="rjf-form-row-controls">
1772
{props.onMoveUp &&
1873
<Button
1974
className="move-up"
20-
onClick={props.onMoveUp}
75+
onClick={(e) => animate(e, 'move-up', props.onMoveUp)}
2176
title="Move up"
2277
>
2378
<span>&uarr;</span>
@@ -26,7 +81,7 @@ export function FormRowControls(props) {
2681
{props.onMoveDown &&
2782
<Button
2883
className="move-down"
29-
onClick={props.onMoveDown}
84+
onClick={(e) => animate(e, 'move-down', props.onMoveDown)}
3085
title="Move down"
3186
>
3287
<span>&darr;</span>
@@ -35,7 +90,7 @@ export function FormRowControls(props) {
3590
{props.onRemove &&
3691
<Button
3792
className="remove"
38-
onClick={props.onRemove}
93+
onClick={(e) => animate(e, 'remove', props.onRemove)}
3994
title="Remove"
4095
>
4196
<span>&times;</span>

0 commit comments

Comments
 (0)