rytm-webflow 2.0.6 → 2.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # rytm-webflow
2
2
  Rytm webflow pack is used to make websites rendered on the server (using a PHP or other back-end) more dynamic.
3
- A static website can be transformed into something we call a *pseudo single page application* (SPA). The pack includes **[ASwap](#markdown-header-aswap)** - a framework programmed by Rytm Digital arround 2016 which uses *AJAX* to first load and then *swap* the page's content in user's browser.
4
- Another future rytm-webflow includes is **[ShowUp](#markdown-header-showup)** - a ScrollMagic.js wrapper which can be integrated with ASwap.
3
+ A static website can be transformed into something we call a *pseudo single page application* (SPA). The pack includes [ASwap](#aswap) - a framework under developement by Rytm.Digital since 2016 which uses *AJAX* / *fetch* to first load and then *swap* the page's content in user's browser.
4
+ This package comes with a handy [WebflowView](#webflowview).
5
+ Another future rytm-webflow includes is [ShowUp](#showup) - a ScrollMagic.js wrapper which can be integrated with ASwap.
5
6
 
6
7
 
7
8
  **Install using NPM:**
@@ -14,7 +15,7 @@ $ npm install rytm-webflow --save
14
15
  - scrollmagic
15
16
  - scrollmagic-plugin-gsap
16
17
 
17
- ## ASwap
18
+ ## <a name="aswap">ASwap</a>
18
19
  ### Basic usage example
19
20
  In your HTML for each request you neeed a wrapper (default wrapper selector: ```#stage```)
20
21
  Inside a wrapper you can have multiple view instances. Each view uses a name defined in ```data-as-view``` and an unique id defined in ```data-as-id```
@@ -217,9 +218,62 @@ RytmWebflow.aswap.init(params, {
217
218
  },
218
219
  })
219
220
  ```
221
+ ## <a name="webflowview">WebflowView</a> ##
222
+ WebflowView is an universal ASwap View which can be used with the **Webflow syntax** - a minified way to describe an animation, it's duration (time), easing, delay or the trigger. Eg. `y:100,o:0` will animate an element from: `y:100px` and `opacity: 0` to it's current state. Go to [Webflow syntax](#webflowsyntax) for more.
223
+ ### Basic usage example
224
+ Define the route:
225
+ ```js
226
+ import RytmWebflow from 'rytm-webflow'
227
+
228
+ RytmWebflow.aswap.init({
229
+ routes: {
230
+ "webflow" : {
231
+ controller: RytmWebflow.Controller,
232
+ view: RytmWebflow.WebflowView
233
+ }
234
+ }
235
+ })
236
+ ```
237
+ Add data attributes to the cointainer element:
238
+ ```html
239
+ <div id="stage">
240
+ <div data-as-view="webflow" data-as-id="uid">
241
+ ...
242
+ </div>
243
+ </div>
244
+ ```
245
+ Animate any element within the *WebflowView* container using the **Webflow syntax** inside the `webview` attributes (initial, show, hide):
246
+ ```html
247
+ <div data-webview-initial="o:0" data-webview-show="o:1,t:.5" data-webview-hide="o:0,t:.4">
248
+ ...
249
+ </div>
250
+ ```
251
+ or (more complex example):
252
+ ```html
253
+ <div data-webview-initial="x:-10,y:50" data-webview-show="x:0,y:0,t:.5,d:.15,e:Power2.easeOut" data-webview-hide="x:10,y:-25,t:.4,d:.2,e:Power2.easeIn">
254
+ ...
255
+ </div>
256
+ ```
257
+ ### Stagger - animate any list of items
258
+ Animate elements nested under the WebflowView container when scrolled into viewport by using `stagger` data attributes (initial, show, hide) and the `data-webset` attribute:
259
+ ```html
260
+ <ul data-stagger-initial="o:0" data-stagger-show="o:1,t:.5,stagger:.1" data-stagger-hide="o:0,t:.4,stagger:.1" data-webset="selector:li">
261
+ <li>...</li>
262
+ <li>...</li>
263
+ </ul>
264
+ ```
265
+ ### Scrollmagic - animate elements when scrolled into viewport
266
+ Scroll-triggered-animations of any element inside the *WebflowView* container. Use the `webscroll` data attributes (initial, show, hide):
267
+ ```html
268
+ <div data-webscroll-initial="y:100" data-webscroll-show="y:0,t:.5" data-webscroll-hide="o:0,t:.4">
269
+ ...
270
+ </div>
271
+ ```
272
+
273
+
220
274
 
221
- ## ShowUp ##
222
- ShowUp makes creating gsap scroll-triggered-animations much faster. It can be used to show content while user scrolls down the page. It can also be used for parallax like animations. ShowUp comes with the **Webflow syntax** - a minified way to describe an animation, it's duration (time), easing, delay or the trigger. Eg. `y:100,o:0` will animate an element from: `y:100px` and `opacity: 0` to it's current state. Go to [Webflow syntax](#markdown-header-webflow-syntax) for more.
275
+ ## <a name="showup">ShowUp</a> ##
276
+ ShowUp makes creating gsap scroll-triggered-animations much faster. It can be used to show content while user scrolls down the page. It can also be used for parallax like animations. ShowUp comes with the **Webflow syntax**. Go to [Webflow syntax](#webflowsyntax) for more.
223
277
  ### Webflow usage example
224
278
  To enable ShowUp call the init method in your JS:
225
279
  ```js
@@ -315,11 +369,11 @@ When moving elements in the Y axis you can use the `parent` trigger hook:
315
369
  <div data-webpara="y:-100" data-webset="duration:100%,trigger:parent"></div>
316
370
  </div>
317
371
  ```
318
- ### Webflow syntax ##
372
+ ### <a name="webflowsyntax">Webflow syntax</a> ##
319
373
  | short code | prameter | example |
320
374
  |---|---|---|
321
375
  | ```d``` | delay | ```"d:.4"``` |
322
- | ```e``` | ease | ```"e:Bounce:easeOut"``` |
376
+ | ```e``` | ease | ```"e:Bounce.easeOut"``` |
323
377
  | ```h``` | height | ```"h:10"``` |
324
378
  | ```o``` | opacity | ```"o:0"``` |
325
379
  | ```r``` | rotation | ```"r:180"``` |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rytm-webflow",
3
- "version": "2.0.6",
3
+ "version": "2.0.8",
4
4
  "description": "rytm webflow pack - ASwap, ShowUp",
5
5
  "main": "scripts/index.js",
6
6
  "scripts": {
@@ -0,0 +1,286 @@
1
+ import ScrollMagic from 'scrollmagic';
2
+ import gsap from 'gsap';
3
+ import View from './View';
4
+ import scrollController from './../showup/ScrollController';
5
+ import { getWebflowAnimationProps, parseProps } from './../lib/dataTweenParser';
6
+ import { getScrollMagicSceneProps } from './../lib/dataScrollMagicParser';
7
+
8
+ // data-webview-... (aswap show / hide)
9
+ const DATA_ATTR_WEBVIEW_INIT = "webview-initial";
10
+ const DATA_ATTR_WEBVIEW_SHOW = "webview-show";
11
+ const DATA_ATTR_WEBVIEW_HIDE = "webview-hide";
12
+ // data-webscroll-... (scroll magic)
13
+ const DATA_ATTR_WEBSCROLL_INIT = "webscroll-initial";
14
+ const DATA_ATTR_WEBSCROLL_SHOW = "webscroll-show";
15
+ const DATA_ATTR_WEBSCROLL_HIDE = "webscroll-hide";
16
+ // data-stagger-... (stagger - lists)
17
+ const DATA_ATTR_STAGGER_INIT = "stagger-initial";
18
+ const DATA_ATTR_STAGGER_SHOW = "stagger-show";
19
+ const DATA_ATTR_STAGGER_HIDE = "stagger-hide";
20
+ const DATA_ATTR_SETUP = "webset";
21
+
22
+ class WebflowView extends View {
23
+
24
+ constructor(id) {
25
+ super(id);
26
+ this.scenes = [];
27
+ }
28
+ /**
29
+ * prepare (before show)
30
+ **/
31
+ prepare(container) {
32
+ super.prepare(container);
33
+ }
34
+ /**
35
+ * animate in (show)
36
+ **/
37
+ show(container) {
38
+ super.show(container);
39
+ this.container = container;
40
+ // show Aswap webviews
41
+ this.webViewsShow();
42
+ // show stagger elements
43
+ this.staggerShow();
44
+ // build scrollmagic scenes
45
+ this.buildScrollmagicScenes();
46
+ // add event listeners
47
+ this.addEventListeners();
48
+ }
49
+ /**
50
+ * animate out (hide)
51
+ **/
52
+ hide(container) {
53
+ super.hide(container);
54
+ // hide Aswap webviews
55
+ this.webViewsHide();
56
+ // hide stagger elements
57
+ this.staggerHide();
58
+ // hide scrollmagic scenes
59
+ this.hideScrollmagicScenes();
60
+ }
61
+ /**
62
+ * animate out (hide)
63
+ **/
64
+ hidden(container) {
65
+ super.hidden(container);
66
+ this.removeEventListeners();
67
+ this.destroyScenes();
68
+ }
69
+ /**
70
+ * ##################
71
+ * ### DOM events ###
72
+ * ##################
73
+ */
74
+ /**
75
+ * Class event handlers
76
+ */
77
+ handleEvent(e) {
78
+ switch (e.type) {
79
+ case 'resize':
80
+ this.onWindowUpdate(e);
81
+ break;
82
+ case 'DOMContentLoaded':
83
+ this.onWindowUpdate(e);
84
+ break;
85
+ }
86
+ }
87
+ /**
88
+ * Add event listeners
89
+ */
90
+ addEventListeners() {
91
+ window.addEventListener('resize', this);
92
+ document.addEventListener('DOMContentLoaded', this);
93
+ }
94
+ /**
95
+ * Remove event listeners
96
+ */
97
+ removeEventListeners() {
98
+ window.removeEventListener('resize', this);
99
+ document.removeEventListener('DOMContentLoaded', this);
100
+ }
101
+ onWindowUpdate(e) {
102
+ if (this.scenes.length > 0) {
103
+ scrollController.refresh();
104
+ }
105
+ }
106
+ /**
107
+ * ############
108
+ * ### SHOW ###
109
+ * ############
110
+ */
111
+ // ### ASwap Show / Hide ###
112
+ // show webviews
113
+ webViewsShow() {
114
+ if (!this.container) {
115
+ console.warn("Unknown container", this);
116
+ return;
117
+ }
118
+ const list = [...this.container.querySelectorAll('*[data-' + DATA_ATTR_WEBVIEW_SHOW + ']')];
119
+ list.forEach(this.webViewElementShow.bind(this));
120
+ }
121
+ // showe webview element
122
+ webViewElementShow(el, index) {
123
+ const propsInitial = this.getTweenProps(el, DATA_ATTR_WEBVIEW_INIT);
124
+ const propsShow = this.getTweenProps(el, DATA_ATTR_WEBVIEW_SHOW);
125
+ if (propsInitial && propsShow) {
126
+ gsap.killTweensOf(el);
127
+ gsap.set(el, {...propsInitial.tween});
128
+ gsap.to(el, {duration: propsShow.time, ...propsShow.tween});
129
+ }
130
+ }
131
+ // ### Stagger ###
132
+ // Stagger (lists) show
133
+ staggerShow() {
134
+ if (!this.container) {
135
+ console.warn("Unknown container", this);
136
+ return;
137
+ }
138
+ const list = [...this.container.querySelectorAll('*[data-' + DATA_ATTR_STAGGER_SHOW + ']')];
139
+ list.forEach(this.staggerListShow.bind(this));
140
+ }
141
+ staggerListShow(el, index) {
142
+ const { selector } = parseProps(el.dataset[DATA_ATTR_SETUP]);
143
+ if (!selector) {
144
+ console.warn("Unknown selector for webflow stagger", el);
145
+ return;
146
+ }
147
+ const items = [...el.querySelectorAll(selector)];
148
+ const propsInit = this.getTweenProps(el, DATA_ATTR_STAGGER_INIT);
149
+ const propsShow = this.getTweenProps(el, DATA_ATTR_STAGGER_SHOW);
150
+ if (items.length && propsInit && propsShow) {
151
+ gsap.killTweensOf(items);
152
+ gsap.set(items, {...propsInit.tween});
153
+ gsap.to(items, {duration: propsShow.time, ...propsShow.tween});
154
+ }
155
+ }
156
+ // ### ScrollMagic ###
157
+ /**
158
+ * Build scenes for scrollmagic
159
+ */
160
+ buildScrollmagicScenes() {
161
+ if (!this.container) {
162
+ console.warn("Unknown container", this);
163
+ return;
164
+ }
165
+ const list = [...this.container.querySelectorAll('*[data-' + DATA_ATTR_WEBSCROLL_SHOW + ']')];
166
+ list.forEach(this.buildScrollmagicScene.bind(this));
167
+ }
168
+ /**
169
+ * Build scroll magic scene
170
+ * @param {DOM} el
171
+ * @param {Int} index
172
+ */
173
+ buildScrollmagicScene(el, index) {
174
+ const propsInitial = this.getTweenProps(el, DATA_ATTR_WEBSCROLL_INIT);
175
+ const propsShow = this.getTweenProps(el, DATA_ATTR_WEBSCROLL_SHOW);
176
+ const setup = parseProps(el.dataset[DATA_ATTR_SETUP]);
177
+ if (propsInitial && propsShow) {
178
+ const smsp = getScrollMagicSceneProps(el, setup);
179
+ const scene = new ScrollMagic.Scene(smsp);
180
+ scene.on("start", (e) => {
181
+ if (e.scrollDirection === "FORWARD") {
182
+ gsap.killTweensOf(el);
183
+ gsap.set(el, {...propsInitial.tween});
184
+ gsap.to(el, {duration: propsShow.time, ...propsShow.tween});
185
+ }
186
+ });
187
+ scene.addTo(scrollController.get());
188
+ this.scenes.push(scene);
189
+ }
190
+ }
191
+ /**
192
+ * ############
193
+ * ### HIDE ###
194
+ * ############
195
+ */
196
+ // ### ASwap Show / Hide ###
197
+ // hide webviews
198
+ webViewsHide() {
199
+ if (!this.container) {
200
+ console.warn("Unknown container", this);
201
+ return;
202
+ }
203
+ const list = [...this.container.querySelectorAll('*[data-' + DATA_ATTR_WEBVIEW_HIDE + ']')];
204
+ list.forEach(this.webViewsElementHide.bind(this));
205
+ }
206
+ // showe webview element
207
+ webViewsElementHide(el, index) {
208
+ const propsHide = this.getTweenProps(el, DATA_ATTR_WEBVIEW_HIDE);
209
+ if (propsHide) {
210
+ gsap.killTweensOf(el);
211
+ gsap.to(el, {duration: propsHide.time, ...propsHide.tween});
212
+ }
213
+ }
214
+ // ### Stagger ###
215
+ // Hide stagger
216
+ staggerHide() {
217
+ if (!this.container) {
218
+ console.warn("Unknown container", this);
219
+ return;
220
+ }
221
+ const list = [...this.container.querySelectorAll('*[data-' + DATA_ATTR_STAGGER_HIDE + ']')];
222
+ list.forEach(this.staggerListHide.bind(this));
223
+ }
224
+ // Hide stagger element
225
+ staggerListHide(el, index) {
226
+ const { selector } = parseProps(el.dataset[DATA_ATTR_SETUP]);
227
+ if (!selector) {
228
+ console.warn("Unknown selector for webflow stagger", el);
229
+ return;
230
+ }
231
+ const items = [...el.querySelectorAll(selector)];
232
+ const propsHide = this.getTweenProps(el, DATA_ATTR_STAGGER_HIDE);
233
+ if (items.length && propsHide) {
234
+ gsap.killTweensOf(items);
235
+ gsap.to(items, {duration: propsHide.time, ...propsHide.tween});
236
+ }
237
+ }
238
+ // ### ScrollMagic ###
239
+ /**
240
+ * hide scenes
241
+ */
242
+ hideScrollmagicScenes() {
243
+ if (!this.container) {
244
+ console.warn("Unknown container", this);
245
+ return;
246
+ }
247
+ const list = [...this.container.querySelectorAll('*[data-' + DATA_ATTR_WEBSCROLL_HIDE + ']')];
248
+ list.forEach(this.hideScrollmagicElement.bind(this));
249
+ }
250
+ /**
251
+ * Hide element
252
+ * @param {DOM} el
253
+ * @param {Int} index
254
+ */
255
+ hideScrollmagicElement(el, index) {
256
+ const propsHide = this.getTweenProps(el, DATA_ATTR_WEBSCROLL_HIDE);
257
+ if (propsHide) {
258
+ gsap.killTweensOf(el);
259
+ gsap.to(el, { duration: propsHide.time, ...propsHide.tween});
260
+ }
261
+ }
262
+ // ## Destroy scrollmagic scenes
263
+ destroyScenes() {
264
+ this.scenes.forEach((scene) => {
265
+ scene.destroy();
266
+ scene = null;
267
+ })
268
+ this.scenes = [];
269
+ }
270
+ /**
271
+ * ###############
272
+ * ### HELPERS ###
273
+ * ###############
274
+ */
275
+ // get animation props
276
+ getTweenProps(el, name) {
277
+ const dataAttrName = this.getDataAttributeName(name);
278
+ return getWebflowAnimationProps(el.dataset[dataAttrName]);
279
+ }
280
+ // replace data-x with dataX
281
+ getDataAttributeName(name) {
282
+ return name.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });
283
+ }
284
+
285
+ }
286
+ export default WebflowView
package/scripts/index.js CHANGED
@@ -2,12 +2,13 @@ import aswapInstance from './aswap/ASwap'
2
2
  import Controller from './aswap/Controller'
3
3
  import ControllerImgLoad from './aswap/ControllerImgLoad'
4
4
  import View from './aswap/View'
5
+ import WebflowView from './aswap/WebflowView'
5
6
  // Bootstrap v5 helper
6
7
  import bootsrap5 from './bootstrap/v5/Bootstrap5'
7
8
  // showup
8
9
  import scrollController from './showup/ScrollController'
9
10
  import showUp from './showup/ShowUp'
10
- import { getWebflowProps, getParallaxProps } from './lib/dataTweenParser'
11
+ import { getWebflowAnimationProps, getWebflowProps, getParallaxProps, parseProps } from './lib/dataTweenParser'
11
12
  import { getScrollMagicSceneProps } from './lib/dataScrollMagicParser'
12
13
 
13
14
  export default {
@@ -15,13 +16,16 @@ export default {
15
16
  Controller,
16
17
  ControllerImgLoad,
17
18
  View,
19
+ WebflowView,
18
20
  // boottstrap
19
21
  bootsrap5,
20
22
  // showup
21
23
  scrollController,
22
24
  showUp,
23
25
  // lib
26
+ getWebflowAnimationProps,
24
27
  getWebflowProps,
25
28
  getParallaxProps,
29
+ parseProps,
26
30
  getScrollMagicSceneProps
27
31
  }
@@ -55,7 +55,7 @@ export const getWebflowProps = (dataStr) => {
55
55
  * @param {string} gstr (eg. y:100,o:0)
56
56
  * @return {Object}
57
57
  */
58
- const getWebflowAnimationProps = (gstr) => {
58
+ export const getWebflowAnimationProps = (gstr) => {
59
59
  let propsFromData = parseProps(gstr)
60
60
  return getValidWebflowAnimationProps(propsFromData)
61
61
  }
@@ -74,7 +74,7 @@ class ShowUp {
74
74
  if (wfp.show) {
75
75
  const smsp = getScrollMagicSceneProps(el, setup)
76
76
  const scene = new ScrollMagic.Scene(smsp)
77
- scene.setTween(TweenMax.from(el, wfp.show.time, wfp.show.tween))
77
+ scene.setTween(TweenMax.from(el, { duration: wfp.show.time, ...wfp.show.tween}))
78
78
  scene.addTo(scrollController.get())
79
79
  this.scenes.push(scene)
80
80
  }
@@ -92,7 +92,7 @@ class ShowUp {
92
92
  if (pp) {
93
93
  const smsp = getScrollMagicSceneProps(el, setup)
94
94
  const scene = new ScrollMagic.Scene(smsp)
95
- scene.setTween(TweenMax.to(el, 1, pp.parallax.tween))
95
+ scene.setTween(TweenMax.to(el, {duration: 1, ...pp.parallax.tween}))
96
96
  scene.addTo(scrollController.get())
97
97
  this.scenes.push(scene)
98
98
  }