Skip to content

Commit 1e95c41

Browse files
committed
extract HOC for animations
1 parent e05fef5 commit 1e95c41

File tree

2 files changed

+58
-36
lines changed

2 files changed

+58
-36
lines changed

src/AnimatedVisibility.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ function AnimatedVisibility({
66
visible,
77
children,
88
animationOutDuration,
9+
disappearOffset,
910
...rest
1011
}) {
1112
const [noDisplay, setNoDisplay] = useState(!visible);
1213
useEffect(() => {
1314
if (!visible) {
14-
const delay = animationOutDuration - 350;
15+
const delay = animationOutDuration - disappearOffset;
1516
setTimeout(() => setNoDisplay(true), delay);
1617
} else setNoDisplay(false);
1718
}, [visible]);
@@ -26,6 +27,7 @@ function AnimatedVisibility({
2627

2728
AnimatedVisibility.defaultProps = {
2829
animationOutDuration: 1000,
30+
disappearOffset: 350,
2931
visible: true
3032
};
3133

@@ -35,7 +37,41 @@ AnimatedVisibility.propTypes = {
3537
PropTypes.node
3638
]),
3739
animationOutDuration: PropTypes.number,
40+
disappearOffset: PropTypes.number,
3841
visible: PropTypes.bool
3942
};
4043

44+
function makeAnimated(
45+
Component,
46+
animationIn,
47+
animationOut,
48+
animationInDuration,
49+
animationOutDuration,
50+
disappearOffset
51+
) {
52+
return function({ open, className, ...props }) {
53+
return (
54+
<AnimatedVisibility
55+
visible={open}
56+
animationIn={animationIn}
57+
animationOut={animationOut}
58+
animationInDuration={animationInDuration}
59+
animationOutDuration={animationOutDuration}
60+
disappearOffset={disappearOffset}
61+
className={className}
62+
>
63+
<Component {...props} />
64+
</AnimatedVisibility>
65+
);
66+
};
67+
}
68+
69+
export function makeAnimationSlideLeft(Component) {
70+
return makeAnimated(Component, "slideInLeft", "slideOutLeft", 400, 500, 200);
71+
}
72+
73+
export function makeAnimationSlideUpDown(Component) {
74+
return makeAnimated(Component, "slideInDown", "slideOutUp", 400, 500, 200);
75+
}
76+
4177
export default AnimatedVisibility;

src/App.js

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { Fragment, useState } from "react";
2-
import AnimatedVisibility from "./AnimatedVisibility";
1+
import React, {Fragment, useState} from "react";
2+
import {makeAnimationSlideLeft, makeAnimationSlideUpDown} from "./AnimatedVisibility";
33
import Boxes from "./Boxes";
44
import "./App.css";
55

@@ -16,45 +16,31 @@ function ToggleButton({ label, isOpen, onClick }) {
1616
);
1717
}
1818

19-
function Navbar({ open }) {
19+
function Navbar() {
2020
return (
21-
<AnimatedVisibility
22-
visible={open}
23-
animationIn="slideInDown"
24-
animationOut="slideOutUp"
25-
animationInDuration={300}
26-
animationOutDuration={600}
27-
>
28-
<nav className="bar nav">
29-
<li>Item 1</li>
30-
<li>Item 2</li>
31-
<li>Item 3</li>
32-
</nav>
33-
</AnimatedVisibility>
21+
<nav className="bar nav">
22+
<li>Item 1</li>
23+
<li>Item 2</li>
24+
<li>Item 3</li>
25+
</nav>
3426
);
3527
}
3628

37-
function Sidebar({ open }) {
29+
function Sidebar() {
3830
return (
39-
<AnimatedVisibility
40-
visible={open}
41-
animationIn="slideInLeft"
42-
animationOut="slideOutLeft"
43-
animationInDuration={500}
44-
animationOutDuration={600}
45-
className="on-top"
46-
>
47-
<div className="sidebar">
48-
<ul>
49-
<li>Item 1</li>
50-
<li>Item 2</li>
51-
<li>Item 3</li>
52-
</ul>
53-
</div>
54-
</AnimatedVisibility>
31+
<div className="sidebar">
32+
<ul>
33+
<li>Item 1</li>
34+
<li>Item 2</li>
35+
<li>Item 3</li>
36+
</ul>
37+
</div>
5538
);
5639
}
5740

41+
const AnimatedSidebar = makeAnimationSlideLeft(Sidebar);
42+
const AnimatedNavbar = makeAnimationSlideUpDown(Navbar);
43+
5844
function App() {
5945
const [navIsOpen, setNavOpen] = useState(false);
6046
const [sidebarIsOpen, setSidebarOpen] = useState(false);
@@ -78,10 +64,10 @@ function App() {
7864
/>
7965
<ToggleButton label="Navbar" isOpen={navIsOpen} onClick={toggleNav} />
8066
</header>
81-
<Navbar open={navIsOpen} />
67+
<AnimatedNavbar open={navIsOpen} />
8268
<Boxes />
8369
</main>
84-
<Sidebar open={sidebarIsOpen} />
70+
<AnimatedSidebar open={sidebarIsOpen} className="on-top"/>
8571
</Fragment>
8672
);
8773
}

0 commit comments

Comments
 (0)