Skip to content

Commit f1d10d4

Browse files
committed
v1.1.5
1 parent 300bb29 commit f1d10d4

File tree

9 files changed

+116
-64
lines changed

9 files changed

+116
-64
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# react-scrollsy v1.1.3 ![](https://img.badgesize.io/olarsson/react-scrollsy/master/dist/react-scrollsy.es.js)
1+
# react-scrollsy v1.1.5 ![](https://img.badgesize.io/olarsson/react-scrollsy/master/dist/react-scrollsy.es.js)
22

33
An ambitious light-weight react module written in TypeScript for tracking scroll progress in a performant way. Developed for use with spring based animation libraries such as react-spring, but can be used with or without any library.
44

@@ -178,8 +178,25 @@ Creates a function which returns a `scrollObject` object as such:
178178
</ScrollTracker>
179179
```
180180

181+
### Next.js
182+
If you load react-scrollsy as a dynamic component it will work out of the box, if you want it to work with SSR then you need to change your next config accordingly:
183+
```js
184+
const nextConfig = {
185+
transpilePackages: ['react-scrollsy']
186+
};
187+
```
188+
If it still doesnt work then change the import string in the following fashion:
189+
`import { ScrollTrackerDocument, ScrollTracker } from "node_modules/react-scrollsy/dist/react-scrollsy.es";`
190+
191+
181192
### Whats new
182193

194+
##### Version 1.1.5
195+
- [x] Updated the readme with use examples for Next.js
196+
197+
##### Version 1.1.4
198+
- [x] Added server side rendering for use with Next.js and such
199+
183200
##### Version 1.1.3
184201
- [x] Bugfix, incorrect height calculation after adding triggers
185202

@@ -200,5 +217,6 @@ Creates a function which returns a `scrollObject` object as such:
200217

201218
### Todo
202219

220+
- [ ] Implement the scroll logic for horizontal scrolling
203221
- [ ] Write (more) tests
204222
- [ ] Refactor the ScrollTrackerCustom?

dist/config.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { IScrollTrackerObject } from "./types";
22
export declare const defaultConfig: {
33
resizeThrottle: number;
4+
trigger: string;
45
};
56
export declare const emptyScrollObject: IScrollTrackerObject;

dist/react-scrollsy.es.js

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { memo as R, useState as p, useEffect as g } from "react";
2-
import { jsx as k } from "react/jsx-runtime";
1+
import { memo as L, useState as h, useEffect as g } from "react";
2+
import { jsx as M } from "react/jsx-runtime";
33
var y = typeof globalThis < "u" ? globalThis : typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {};
44
function z(e) {
55
return e && e.__esModule && Object.prototype.hasOwnProperty.call(e, "default") ? e.default : e;
66
}
77
function H(e) {
88
throw new Error('Could not dynamically require "' + e + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');
99
}
10-
var C = { exports: {} };
10+
var j = { exports: {} };
1111
(function(e, n) {
1212
(function(i) {
1313
e.exports = i();
@@ -26,9 +26,9 @@ var C = { exports: {} };
2626
throw d.code = "MODULE_NOT_FOUND", d;
2727
}
2828
var m = t[l] = { exports: {} };
29-
r[l][0].call(m.exports, function(h) {
30-
var w = r[l][1][h];
31-
return s(w || h);
29+
r[l][0].call(m.exports, function(p) {
30+
var w = r[l][1][p];
31+
return s(w || p);
3232
}, m, m.exports, i, r, t, o);
3333
}
3434
return t[l].exports;
@@ -58,11 +58,11 @@ var C = { exports: {} };
5858
}();
5959
}, {}] }, {}, [1])(1);
6060
});
61-
})(C);
62-
var F = C.exports;
61+
})(j);
62+
var F = j.exports;
6363
const I = /* @__PURE__ */ z(F);
64-
var O = {}, j = {};
65-
Object.defineProperty(j, "__esModule", { value: !0 });
64+
var O = {}, R = {};
65+
Object.defineProperty(R, "__esModule", { value: !0 });
6666
var b = {};
6767
Object.defineProperty(b, "__esModule", { value: !0 });
6868
b.throttle = void 0;
@@ -87,17 +87,17 @@ b.throttle = A;
8787
for (var o in r)
8888
o !== "default" && !Object.prototype.hasOwnProperty.call(t, o) && n(t, r, o);
8989
};
90-
Object.defineProperty(e, "__esModule", { value: !0 }), i(j, e), i(b, e);
90+
Object.defineProperty(e, "__esModule", { value: !0 }), i(R, e), i(b, e);
9191
})(O);
92-
const M = (e, n) => {
92+
const k = (e, n) => {
9393
let i, r;
9494
return function(...t) {
9595
const o = this;
9696
r ? (i && clearTimeout(i), i = setTimeout(() => {
9797
Date.now() - r >= n && (e.apply(o, t), r = Date.now());
9898
}, n - (Date.now() - r))) : (e.apply(o, t), r = Date.now());
9999
};
100-
}, N = () => window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight, E = (e) => e.clientHeight;
100+
}, N = () => window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight, T = (e) => e.clientHeight;
101101
function U(e) {
102102
return new Promise((n) => {
103103
if (document.querySelector(e))
@@ -111,22 +111,22 @@ function U(e) {
111111
});
112112
});
113113
}
114-
const x = (e, n) => {
114+
const _ = (e, n) => {
115115
if (!e || !n)
116116
throw "element/container is not defined.";
117117
const i = e.getBoundingClientRect(), r = n.scrollTop - n.offsetTop;
118118
return i.top + r;
119-
}, V = () => /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream, T = () => V() ? I() : N(), W = function(e, n, i, r, t, o) {
119+
}, V = () => /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream, P = () => V() ? I() : N(), D = function(e, n, i, r, t, o) {
120120
let s = 0, u = 0, c = 0, l = 0, a = 0;
121121
switch (o.basedOn) {
122122
case "doc":
123123
s = n.scrollHeight - n.containerHeight;
124124
break;
125125
case "vp":
126-
s = n.containerHeight, u = x(e, n.element), i === "onEnter" && (u -= n.containerHeight);
126+
s = n.containerHeight, u = _(e, n.element), i === "onEnter" && (u -= n.containerHeight);
127127
break;
128128
case "elem":
129-
u = x(e, n.element), i === "onEnter" ? (s = e.scrollHeight, u -= n.containerHeight) : i === "onLeave" && (s = e.scrollHeight);
129+
u = _(e, n.element), i === "onEnter" ? (s = e.scrollHeight, u -= n.containerHeight) : i === "onLeave" && (s = e.scrollHeight);
130130
break;
131131
}
132132
if (r) {
@@ -180,10 +180,10 @@ const x = (e, n) => {
180180
start: f,
181181
end: d
182182
};
183+
}, S = {
184+
resizeThrottle: 150,
185+
trigger: "onEnter"
183186
}, q = {
184-
resizeThrottle: 150
185-
// scrollThrottle: 200
186-
}, L = {
187187
progress: 0,
188188
scrollData: {
189189
containerHeight: 0,
@@ -194,19 +194,19 @@ const x = (e, n) => {
194194
},
195195
start: 0,
196196
end: 0
197-
}, _ = (e, n = L) => typeof e == "function" ? e({ scrollObject: n, children: e }) : e, K = R(({ scrollData: e, children: n, elem: i, settings: r, onStart: t, onEnd: o }) => {
198-
const { trigger: s = "onEnter", offsetTop: u, offsetBottom: c, duration: l } = r, [a, f] = p(!1), [d, m] = p(!1), [h, w] = p(!1);
197+
}, C = (e, n = q) => typeof e == "function" ? e({ scrollObject: n, children: e }) : e, K = L(({ scrollData: e, children: n, elem: i, settings: r, onStart: t, onEnd: o }) => {
198+
const { trigger: s = S.trigger, offsetTop: u, offsetBottom: c, duration: l } = r, [a, f] = h(!1), [d, m] = h(!1), [p, w] = h(!1);
199199
if (g(() => {
200200
a && typeof t == "function" && t();
201201
}, [a]), g(() => {
202202
d && typeof o == "function" && o();
203203
}, [d]), g(() => {
204204
i != null && i.current && w(!0);
205-
}, [i]), !h)
206-
return _(n, L);
207-
const S = W(i.current, e, s, u, c, l), { progress: v } = S;
208-
return v > 0 && v < 1 && a === !1 && typeof t == "function" && f(!0), v <= 0 && a === !0 && typeof t == "function" && f(!1), v >= 1 && d === !1 && typeof o == "function" && m(!0), v < 1 && d === !0 && typeof o == "function" && m(!1), _(n, S);
209-
}), P = ({ scrollData: e, children: n }) => typeof n == "function" ? n({
205+
}, [i]), !p)
206+
return C(n, q);
207+
const x = D(i.current, e, s, u, c, l), { progress: v } = x;
208+
return v > 0 && v < 1 && a === !1 && typeof t == "function" && f(!0), v <= 0 && a === !0 && typeof t == "function" && f(!1), v >= 1 && d === !1 && typeof o == "function" && m(!0), v < 1 && d === !0 && typeof o == "function" && m(!1), C(n, x);
209+
}), E = ({ scrollData: e, children: n }) => typeof n == "function" ? n({
210210
scrollData: {
211211
scrollTop: e.scrollTop,
212212
scrollHeight: e.scrollHeight,
@@ -215,32 +215,44 @@ const x = (e, n) => {
215215
element: e.element
216216
},
217217
children: n
218-
}) : n, Q = ({ children: e, scrollThrottle: n, resizeThrottle: i = q.resizeThrottle }) => {
218+
}) : n, Q = ({ children: e, scrollThrottle: n, resizeThrottle: i }) => {
219+
const [r, t] = h(!1);
220+
return g(() => {
221+
t(!0);
222+
}, []), r ? /* @__PURE__ */ M(W, { scrollThrottle: n, resizeThrottle: i, children: e }) : E({
223+
scrollData: {
224+
scrollTop: 0,
225+
scrollHeight: 0,
226+
containerHeight: 0,
227+
percentProgress: 0,
228+
element: void 0
229+
},
230+
children: e
231+
});
232+
}, W = ({ children: e, scrollThrottle: n, resizeThrottle: i = S.resizeThrottle }) => {
219233
const r = document == null ? void 0 : document.documentElement;
220234
if (!r)
221235
throw new Error("No document.documentElement found.");
222-
if (typeof window > "u")
223-
throw new Error("No window found.");
224236
let t = null;
225-
const [o, s] = p(T()), [u, c] = p(0), l = () => {
237+
const [o, s] = h(P()), [u, c] = h(0), l = () => {
226238
t && window.cancelAnimationFrame(t), t = window.requestAnimationFrame(() => {
227-
const { scrollTop: m, scrollHeight: h } = r;
228-
c(m / (h - o));
239+
const { scrollTop: m, scrollHeight: p } = r;
240+
c(m / (p - o));
229241
});
230242
}, a = () => {
231-
s(T());
243+
s(P());
232244
}, f = O.throttle(() => {
233245
a();
234-
}, i), d = n ? M(() => {
246+
}, i), d = n ? k(() => {
235247
l();
236248
}, n) : () => {
237249
l();
238250
};
239-
return g(() => (window.addEventListener("resize", f), s(T()), a(), () => {
251+
return g(() => (window.addEventListener("resize", f), s(P()), a(), () => {
240252
window.removeEventListener("resize", f);
241253
}), []), g(() => (document.addEventListener("scroll", d, { passive: !0 }), d(), () => {
242254
document.removeEventListener("scroll", d);
243-
}), [o]), P({
255+
}), [o]), E({
244256
scrollData: {
245257
scrollTop: r.scrollTop,
246258
scrollHeight: r.scrollHeight,
@@ -251,52 +263,52 @@ const x = (e, n) => {
251263
children: e
252264
});
253265
}, X = ({ children: e, scrollThrottle: n, scrollingElement: i, resizeThrottle: r }) => {
254-
const [t, o] = p(!1), [s, u] = p(null), c = P({
266+
const [t, o] = h(!1), [s, u] = h(void 0), c = E({
255267
scrollData: {
256268
scrollTop: 0,
257269
scrollHeight: 0,
258270
containerHeight: 0,
259271
percentProgress: 0,
260-
element: document.documentElement
272+
element: s
261273
},
262274
children: e
263275
});
264276
return U(i).then((l) => {
265277
s || (u(l), o(!0));
266-
}), t ? /* @__PURE__ */ k($, { scrollThrottle: n, resizeThrottle: r, customScrollingElement: s, children: e }) : c;
278+
}), t ? /* @__PURE__ */ M($, { scrollThrottle: n, resizeThrottle: r, customScrollingElement: s, children: e }) : c;
267279
}, $ = ({
268280
children: e,
269281
customScrollingElement: n,
270282
scrollThrottle: i,
271-
resizeThrottle: r = q.resizeThrottle
283+
resizeThrottle: r = S.resizeThrottle
272284
}) => {
273285
const t = n;
274286
if (!t)
275287
throw new Error("No custom scrolling element found.");
276288
if (typeof window > "u")
277289
throw new Error("No window found.");
278290
let o = null;
279-
const [s, u] = p(E(t)), [c, l] = p(0), a = () => {
291+
const [s, u] = h(T(t)), [c, l] = h(0), a = () => {
280292
o && window.cancelAnimationFrame(o), o = window.requestAnimationFrame(() => {
281-
const { scrollTop: h, scrollHeight: w } = t;
282-
l(h / (w - s));
293+
const { scrollTop: p, scrollHeight: w } = t;
294+
l(p / (w - s));
283295
});
284296
}, f = () => {
285-
u(E(t));
297+
u(T(t));
286298
}, d = O.throttle(() => {
287299
f();
288-
}, r), m = i ? M(() => {
300+
}, r), m = i ? k(() => {
289301
a();
290302
}, i) : () => {
291303
a();
292304
};
293-
return g(() => (window.addEventListener("resize", d), u(E(t)), f(), () => {
305+
return g(() => (window.addEventListener("resize", d), u(T(t)), f(), () => {
294306
window.removeEventListener("resize", d);
295307
}), []), g(() => (t.addEventListener("scroll", m, {
296308
passive: !0
297309
}), m(), () => {
298310
t.removeEventListener("scroll", m);
299-
}), [s]), P({
311+
}), [s]), E({
300312
scrollData: {
301313
scrollTop: t.scrollTop,
302314
scrollHeight: t.scrollHeight,

0 commit comments

Comments
 (0)