-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·304 lines (296 loc) · 11.6 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import React from 'react';
import ReactDOM from 'react-dom';
import { Home, Color, Smoothie, NotFound, Overlay } from './components/Routes';
import { Options } from './components/Options';
import { generateRandomColor } from './utils/generateRandomColor';
import { SmoothrIcon } from './components/SmoothrIcon';
// Import the smoothr packages!
import { Smoothr, SmoothRoutes, Route, Link } from 'smoothr';
// Generate some colors for the overlay Routes
const randomColor1 = generateRandomColor();
const randomColor2 = generateRandomColor();
const randomColor3 = generateRandomColor();
const randomColor4 = generateRandomColor();
const randomColor5 = generateRandomColor();
export default class App extends React.Component {
state = {
animating: false,
showOverlay: false,
animation: 'Fade',
animationIn: [{ opacity: 0 }, { opacity: 1 }],
animationOut: [{ opacity: 1 }, { opacity: 0 }],
reverseAnimationIn: null,
reverseAnimationOut: null,
duration: 750,
cssAnimation: false,
easing: 'ease-in-out'
};
/*
This method allows changing of local state
in order to conditionally set the upcoming animation.
It is a Promise, so it will be completed before the animating begins.
*/
beforeAnimation = ({
// Url's are the exact urls
outgoingUrl,
incomingUrl,
// Routes are the original matching routes, as set in the <Route /> components
outgoingRoute,
incomingRoute,
// If the user nagivated with the "back" button in the browser
backNavigation
}) => {
// Disable all navigation during animation in this app
this.setState({ animating: true });
};
/*
This method will happen right before the animation begins.
Use it to imperitively kick off transition animations,
including on the initial page load, which is passed in as a bool.
*/
onAnimationStart = () => {};
/*
This takes place after the animation is finished.
Reset your animations, or do something else...
*/
onAnimationEnd = () => {
this.setState({ animating: false });
};
render() {
return (
/*
The <Smoothr /> component just needs to be used
up the tree of any <Link />s or <SmoothRoutes />
*/
<Smoothr
/*
originPath is the path after the domain to the origin of this single page app.
Include the beginning backslash, but not the trailing backslash.
All of the <Link /> href properties will be relative to that origin path.
For hash routing, set this to "/#" or something else ending with a hash, and that's it!
Example: "/smoothr-app", and <Link href="/page1" /> will link to "/smoothr-app/page1".
This defaults to an empty string, which signifies the document root.
*/
originPath="/"
beforeAnimation={this.beforeAnimation}
onAnimationStart={this.onAnimationStart}
onAnimationEnd={this.onAnimationEnd}
>
<div className="app-container">
<header>
<h1>
Smoothr
<div className="smoothie-logo">
<SmoothrIcon />
</div>
Demo
</h1>
<Options
setState={newState => this.setState(newState)}
showOverlay={this.state.showOverlay}
animation={this.state.animation}
easing={this.state.easing}
duration={this.state.duration}
cssAnimation={this.state.cssAnimation}
animating={this.state.animating}
/>
<div className="links-container">
<div className="links">
{/*
Links are wrappers around anchor (`<a>`) tags.
The library adds the prop `data-smoothr-current-link="true"`
when the `href` matches the current URL,
and the `data-smoothr-visited-link` property
to simulate the css `:visited` rule.
*/}
<Link href="/">Home</Link>
<Link href="/color/255/209/102" className="yellow-link">
Yellow
</Link>
<Link href="/color/117/219/205" className="turquoise-link">
Turquoise
</Link>
<Link href="/smoothie">Smoothie</Link>
{/* This will redirect to the set `notFound` url */}
<Link href="/show404page">Invalid Link</Link>
</div>
</div>
</header>
<div className="main">
{/*
Animations are set by the props of the INCOMING <Route>.
`animationIn` will be performed on the same, incoming <Route>.
`animationOut` will be performed on the other, outgoing <Route>.
`animationOpts` will be applied to both the <Route>s.
*/}
<SmoothRoutes>
<Route
path="/"
component={Home}
/*
`animationIn` and `animationOut` accept 1 of the following:
1. An array of objects - first argument of Element.animate()
2. A CSS class name that will be added during the animation
*/
animationIn={this.state.animationIn}
animationOut={this.state.animationOut}
/*
`animationOpts` is an optional prop and is only used
if passing an array of objects to `animationIn` or `animationOut`.
It corresponds to the second argument of Element.animate()
*/
animationOpts={{
duration: this.state.duration,
easing: this.state.easing
}}
/*
The reverse animations props are called when
the user navigates with the back button in the browser.
If these are null, the regular animation props will be used.
*/
reverseAnimationIn={this.state.reverseAnimationIn}
reverseAnimationOut={this.state.reverseAnimationOut}
reverseAnimationOpts={{
duration: this.state.duration,
easing: this.state.easing
}}
/*
Any other props (including className and style) get
passed down to the rendered <Route />
*/
data-some-other-prop={true}
/>
<Route
/*
Variables in routes.
This example will pass props for `red`, `green`, and `blue` to the component.
*/
path="/color/:red/:green/:blue"
/*
Validate and modify the variables passed when this <Route> is navigated to.
The return value must match the pattern of the path. If it doesn't, the
`notFound` path will be used. This will only work if the `path` component
has variables.
*/
pathResolve={({ red, green, blue }) => {
// Validate that they're all numbers
if (isNaN(red) || isNaN(green) || isNaN(blue)) {
// Return anything else not matching the `path` pattern to trigger a 404
return false;
}
// Make sure they're all valid RGB values
red = Math.min(Math.abs(parseInt(red)), 255);
green = Math.min(Math.abs(parseInt(green)), 255);
blue = Math.min(Math.abs(parseInt(blue)), 255);
// Return the resolved url
return `/color/${red}/${green}/${blue}`;
}}
component={Color}
animationIn={this.state.animationIn}
animationOut={this.state.animationOut}
animationOpts={{
duration: this.state.duration,
easing: this.state.easing
}}
reverseAnimationIn={this.state.reverseAnimationIn}
reverseAnimationOut={this.state.reverseAnimationOut}
reverseAnimationOpts={{
duration: this.state.duration,
easing: this.state.easing
}}
/>
<Route
path="/smoothie"
component={Smoothie}
animationIn={this.state.animationIn}
animationOut={this.state.animationOut}
animationOpts={{
duration: this.state.duration,
easing: this.state.easing
}}
reverseAnimationIn={this.state.reverseAnimationIn}
reverseAnimationOut={this.state.reverseAnimationOut}
reverseAnimationOpts={{
duration: this.state.duration,
easing: this.state.easing
}}
/>
<Route
/* notFound prop designates a route, path, and component for 404 page */
notFound
path="/notfound"
component={NotFound}
/>
</SmoothRoutes>
</div>
<footer>
Smoothr was written by{' '}
<a href="https://github.com/n8jadams">Nate Adams</a>.{' '}
<a href="https://github.com/n8jadams/smoothr">
View in Github.
</a>
.
</footer>
</div>
{/*
You can have as many Route groups as you want on the page,
and they don't have to match up! If the current route doesn't
match up with any of the SmoothRoutes, then it just won't
render anything.
*/
this.state.showOverlay ? (
<div className="overlay-container">
<SmoothRoutes
/*
Instead of passing the same animation props to all the <Route>s,
you can pass it to the <SmoothRoutes>.
The animation logic will favor the <Route> animation prop
over the <SmoothRoute> one.
*/
animationIn={this.state.animationIn}
animationOut={this.state.animationOut}
animationOpts={{
duration: this.state.duration,
easing: this.state.easing
}}
reverseAnimationIn={this.state.reverseAnimationIn}
reverseAnimationOut={this.state.reverseAnimationOut}
reverseAnimationOpts={{
duration: this.state.duration,
easing: this.state.easing
}}
>
<Route
path="/"
component={Overlay}
style={{ backgroundColor: randomColor1 }}
/>
<Route
path="/color/255/209/102"
component={Overlay}
style={{ backgroundColor: randomColor2 }}
/>
<Route
path="/color/117/219/205"
component={Overlay}
style={{ backgroundColor: randomColor3 }}
/>
<Route
path="/smoothie"
component={Overlay}
style={{ backgroundColor: randomColor4 }}
/>
<Route
notFound
path="/notfound"
component={Overlay}
style={{ backgroundColor: randomColor5 }}
/>
</SmoothRoutes>
</div>
) : null}
</Smoothr>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));