Skip to content

Commit 7c005a4

Browse files
committed
Implemented roll-up and done some minor chores
- added roll-up - extracted functionalities - refactor to es6 syntax - prep for testing
1 parent a414886 commit 7c005a4

8 files changed

+377
-56
lines changed

.npmignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# raw
2+
src
3+
4+
# config
5+
rollup.config.js
6+
7+
# tests
8+
test
9+
coverage

lib/vue-animate-onscroll.cjs.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/vue-animate-onscroll.es.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
"name": "vue-animate-onscroll",
33
"version": "1.0.1",
44
"description": "A simple Vue directive that animates element as they scroll into view.",
5-
"main": "src/index.js",
5+
"main": "dist/vue-animate-onscroll.cjs.js",
6+
"module": "dist/vue-animate-onscroll.es.js",
7+
"jsnext:main": "dist/vue-animate-onscroll.es.js",
8+
"scripts": {
9+
"clean": "rimraf lib",
10+
"build": "npm run clean && rollup --config rollup.config.js",
11+
"deploy": "npm build && npm version patch && npm publish"
12+
},
613
"author": "yev",
714
"repository": {
815
"type": "git",
@@ -15,5 +22,10 @@
1522
"reveal"
1623
],
1724
"license": "MIT",
18-
"dependencies": {}
25+
"dependencies": {},
26+
"devDependencies": {
27+
"rimraf": "^2.6.2",
28+
"rollup": "^0.66.4",
29+
"rollup-plugin-terser": "^3.0.0"
30+
}
1931
}

rollup.config.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { terser } from 'rollup-plugin-terser'
2+
3+
// Due to bug [Breaks with multiple outputs](https://github.com/TrySound/rollup-plugin-terser/issues/5) I had to create multiple configuration instead of a simpler one.
4+
5+
export default [
6+
{
7+
input: 'src/index.js',
8+
output: {
9+
format: 'es',
10+
file: 'lib/vue-animate-onscroll.es.js'
11+
},
12+
plugins: [ terser() ]
13+
},
14+
{
15+
input: 'src/index.js',
16+
output: {
17+
format: 'cjs',
18+
file: 'lib/vue-animate-onscroll.cjs.js'
19+
},
20+
plugins: [ terser() ]
21+
}
22+
]

src/index.js

+10-54
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,21 @@
1-
var ScrollAnimate = function(el) {
2-
3-
var oldClasses = el.className;
4-
5-
function isInScrollView(rect){
6-
return rect.top < document.documentElement.clientHeight && rect.bottom > 0;
7-
}
8-
9-
function isDirectionAgnostic(params) {
10-
return typeof params === 'string';
11-
}
12-
13-
function shouldResetAnimation(isUpwards, modifiers, params) {
14-
return modifiers.repeat &&
15-
(isUpwards && params.down || !isUpwards && params.up);
16-
}
17-
18-
return {
19-
dispatch: function(isUpwards, binding) {
20-
var params = binding.value;
21-
var modifiers = binding.modifiers;
22-
23-
if(!isInScrollView(el.getBoundingClientRect())) {
24-
if (modifiers.repeat && isDirectionAgnostic(params)) {
25-
el.className = oldClasses;
26-
}
27-
return;
28-
}
29-
30-
if (isDirectionAgnostic(params)) {
31-
el.className = params;
32-
return;
33-
}
34-
35-
if (params.down && params.up) { // implicit repeat
36-
el.className = isUpwards ? params.up : params.down;
37-
return;
38-
}
39-
40-
if(shouldResetAnimation(isUpwards, modifiers, params)) {
41-
el.className = oldClasses;
42-
return;
43-
}
44-
el.className = params.up || params.down;
45-
}
46-
}
47-
}
1+
import ScrollAnimate from './scroll-animate'
482

493
export default {
4+
ScrollAnimate,
505
install(Vue) {
51-
var scrollAnimate;
6+
let scrollAnimate;
527
Vue.directive('animate-onscroll', {
538
bind(el, binding) {
54-
scrollAnimate = ScrollAnimate(el);
9+
scrollAnimate = ScrollAnimate()
5510
},
5611
inserted(el, binding) {
12+
const currentClassName = el.className
5713
window.addEventListener('scroll', function() {
58-
var isUpwards = this.oldScroll > this.scrollY;
59-
scrollAnimate.dispatch(isUpwards, binding);
60-
this.oldScroll = this.scrollY;
61-
});
14+
const isUpwards = this.oldScroll > this.scrollY
15+
scrollAnimate.run(el, binding, {isUpwards, currentClassName})
16+
this.oldScroll = this.scrollY
17+
})
6218
}
63-
});
19+
})
6420
}
6521
}

src/scroll-animate.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export default () => {
2+
3+
const getClientHeight = () => document.documentElement.clientHeight
4+
5+
const isInScrollView = ({top, bottom}) => top < getClientHeight() && bottom > 0
6+
7+
const isDirectionAgnostic = (params) => typeof params === 'string'
8+
9+
const shouldResetAnimation = ({params, isUpwards, repeat}) => repeat &&
10+
(isUpwards && params.down || !isUpwards && params.up)
11+
12+
const applyAnimationClass = (el, current, newClass) => el.className = `${current} ${(newClass || '')}`
13+
14+
return {
15+
run(el, {value: params, modifiers}, {isUpwards, currentClassName}) {
16+
17+
if(!isInScrollView(el.getBoundingClientRect())) {
18+
if (modifiers.repeat && isDirectionAgnostic(params)) {
19+
return applyAnimationClass(el, currentClassName)
20+
}
21+
return
22+
}
23+
24+
if (isDirectionAgnostic(params)) {
25+
return applyAnimationClass(el, currentClassName, params)
26+
}
27+
28+
if (params.down && params.up) { // implicit repeat
29+
const animationClass = isUpwards ? params.up : params.down
30+
return applyAnimationClass(el, currentClassName, animationClass)
31+
}
32+
33+
if(shouldResetAnimation({params, isUpwards, ...modifiers})) {
34+
return applyAnimationClass(el, currentClassName)
35+
}
36+
return applyAnimationClass(el, currentClassName, params.up || params.down)
37+
}
38+
}
39+
40+
}
41+
42+
43+

0 commit comments

Comments
 (0)