-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathApp.js
More file actions
145 lines (134 loc) · 3.71 KB
/
App.js
File metadata and controls
145 lines (134 loc) · 3.71 KB
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
import React, { useState, useEffect, useRef } from "react";
import { Switch, Route, useLocation } from "react-router-dom";
import Reminders from "./Reminders";
import { About } from "./About";
import { Link } from "./UI";
import { AnimatePresence, motion, AnimateSharedLayout } from "framer-motion";
export default function App() {
let location = useLocation();
let aboutIsActive = location.pathname.match("/about");
let remindersIsActive = !aboutIsActive;
return (
<div className="pt-12">
<header className="max-w-md mx-auto">
<nav className="mt-4 space-x-5">
<Link
className={`pb-px font-medium text-sm ${
remindersIsActive
? "text-cool-gray-900 border-b-2 border-cool-gray-600"
: "text-cool-gray-500 hover:text-cool-gray-900"
}`}
to={`/${location.search}`}
>
Reminders
</Link>
<Link
className={`pb-px font-medium text-sm ${
aboutIsActive
? "text-cool-gray-900 border-b-2 border-cool-gray-600"
: "text-cool-gray-500 hover:text-cool-gray-900"
}`}
to={`/about${location.search}`}
>
About
</Link>
</nav>
</header>
<main className="mt-10">
<Switch>
<Route path="/about">
<About />
</Route>
<Route path="/test">
<Test />
</Route>
<Route path="/:listId?" exact>
<Reminders />
</Route>
</Switch>
</main>
</div>
);
}
let id = 1;
const makeItem = () => {
let newId = id++;
return { id: newId, name: `baz ${newId}` };
};
const spring = {
duration: 0.3,
// type: "spring",
// damping: 10,
// mass: 1,
// stiffness: 50,
};
const Test = () => {
let didRenderRef = useRef(false);
useEffect(() => {
didRenderRef.current = true;
}, []);
let [array, set] = useState(() => [makeItem(), makeItem(), makeItem()]);
// let [array, set] = useState([]);
const addItem = () => {
set((array) => [...array, makeItem()]);
};
const removeItem = (id) => {
set((array) => array.filter((el) => el.id !== id));
};
return (
<div className="max-w-md mx-auto">
<button
className="px-3 py-2 text-white bg-blue-500 rounded"
onClick={addItem}
>
Add
</button>
<AnimateSharedLayout>
<motion.ul
layout
initial="hidden"
animate="visible"
variants={{
hidden: {
height: 0,
},
visible: {
height: "auto",
transition: {
delay: 0.1,
},
},
}}
className="mt-4 overflow-hidden bg-white rounded shadow"
>
<AnimatePresence>
{array.map((obj, i) => (
<motion.li
layout
custom={i}
variants={{
hidden: {
opacity: 0,
},
visible: (i) => ({
opacity: 1,
transition: didRenderRef.current
? { delay: 0.15, ...spring }
: { delay: 0.15 + i * 0.05, ...spring },
}),
}}
initial="hidden"
animate="visible"
exit="hidden"
key={obj.id}
className="p-4"
>
<button onClick={() => removeItem(obj.id)}>{obj.name}</button>
</motion.li>
))}
</AnimatePresence>
</motion.ul>
</AnimateSharedLayout>
</div>
);
};