Skip to content

Commit 21f7c89

Browse files
committed
improved type declarations
1 parent f1d10d4 commit 21f7c89

12 files changed

+105
-100
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# react-scrollsy v1.1.5 ![](https://img.badgesize.io/olarsson/react-scrollsy/master/dist/react-scrollsy.es.js)
1+
# react-scrollsy v1.1.6 ![](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

@@ -191,6 +191,10 @@ If it still doesnt work then change the import string in the following fashion:
191191

192192
### Whats new
193193

194+
##### Version 1.1.6
195+
- [x] Improved type declarations
196+
- [x] Minor changes to some examples
197+
194198
##### Version 1.1.5
195199
- [x] Updated the readme with use examples for Next.js
196200

dist/react-scrollsy.es.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memo as L, useState as h, useEffect as g } from "react";
1+
import { memo as L, useState as p, useEffect as h } from "react";
22
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) {
@@ -26,9 +26,9 @@ var j = { exports: {} };
2626
throw d.code = "MODULE_NOT_FOUND", d;
2727
}
2828
var m = t[l] = { exports: {} };
29-
r[l][0].call(m.exports, function(p) {
30-
var w = r[l][1][p];
31-
return s(w || p);
29+
r[l][0].call(m.exports, function(g) {
30+
var w = r[l][1][g];
31+
return s(w || g);
3232
}, m, m.exports, i, r, t, o);
3333
}
3434
return t[l].exports;
@@ -194,19 +194,19 @@ const _ = (e, n) => {
194194
},
195195
start: 0,
196196
end: 0
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);
199-
if (g(() => {
197+
}, C = (e, n = q) => typeof e == "function" ? e && 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] = p(!1), [d, m] = p(!1), [g, w] = p(!1);
199+
if (h(() => {
200200
a && typeof t == "function" && t();
201-
}, [a]), g(() => {
201+
}, [a]), h(() => {
202202
d && typeof o == "function" && o();
203-
}, [d]), g(() => {
203+
}, [d]), h(() => {
204204
i != null && i.current && w(!0);
205-
}, [i]), !p)
205+
}, [i]), !g)
206206
return C(n, q);
207207
const x = D(i.current, e, s, u, c, l), { progress: v } = x;
208208
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({
209+
}), E = ({ scrollData: e, children: n }) => typeof n == "function" ? n && n({
210210
scrollData: {
211211
scrollTop: e.scrollTop,
212212
scrollHeight: e.scrollHeight,
@@ -216,8 +216,8 @@ const _ = (e, n) => {
216216
},
217217
children: n
218218
}) : n, Q = ({ children: e, scrollThrottle: n, resizeThrottle: i }) => {
219-
const [r, t] = h(!1);
220-
return g(() => {
219+
const [r, t] = p(!1);
220+
return h(() => {
221221
t(!0);
222222
}, []), r ? /* @__PURE__ */ M(W, { scrollThrottle: n, resizeThrottle: i, children: e }) : E({
223223
scrollData: {
@@ -234,10 +234,10 @@ const _ = (e, n) => {
234234
if (!r)
235235
throw new Error("No document.documentElement found.");
236236
let t = null;
237-
const [o, s] = h(P()), [u, c] = h(0), l = () => {
237+
const [o, s] = p(P()), [u, c] = p(0), l = () => {
238238
t && window.cancelAnimationFrame(t), t = window.requestAnimationFrame(() => {
239-
const { scrollTop: m, scrollHeight: p } = r;
240-
c(m / (p - o));
239+
const { scrollTop: m, scrollHeight: g } = r;
240+
c(m / (g - o));
241241
});
242242
}, a = () => {
243243
s(P());
@@ -248,9 +248,9 @@ const _ = (e, n) => {
248248
}, n) : () => {
249249
l();
250250
};
251-
return g(() => (window.addEventListener("resize", f), s(P()), a(), () => {
251+
return h(() => (window.addEventListener("resize", f), s(P()), a(), () => {
252252
window.removeEventListener("resize", f);
253-
}), []), g(() => (document.addEventListener("scroll", d, { passive: !0 }), d(), () => {
253+
}), []), h(() => (document.addEventListener("scroll", d, { passive: !0 }), d(), () => {
254254
document.removeEventListener("scroll", d);
255255
}), [o]), E({
256256
scrollData: {
@@ -263,7 +263,7 @@ const _ = (e, n) => {
263263
children: e
264264
});
265265
}, X = ({ children: e, scrollThrottle: n, scrollingElement: i, resizeThrottle: r }) => {
266-
const [t, o] = h(!1), [s, u] = h(void 0), c = E({
266+
const [t, o] = p(!1), [s, u] = p(void 0), c = E({
267267
scrollData: {
268268
scrollTop: 0,
269269
scrollHeight: 0,
@@ -288,10 +288,10 @@ const _ = (e, n) => {
288288
if (typeof window > "u")
289289
throw new Error("No window found.");
290290
let o = null;
291-
const [s, u] = h(T(t)), [c, l] = h(0), a = () => {
291+
const [s, u] = p(T(t)), [c, l] = p(0), a = () => {
292292
o && window.cancelAnimationFrame(o), o = window.requestAnimationFrame(() => {
293-
const { scrollTop: p, scrollHeight: w } = t;
294-
l(p / (w - s));
293+
const { scrollTop: g, scrollHeight: w } = t;
294+
l(g / (w - s));
295295
});
296296
}, f = () => {
297297
u(T(t));
@@ -302,9 +302,9 @@ const _ = (e, n) => {
302302
}, i) : () => {
303303
a();
304304
};
305-
return g(() => (window.addEventListener("resize", d), u(T(t)), f(), () => {
305+
return h(() => (window.addEventListener("resize", d), u(T(t)), f(), () => {
306306
window.removeEventListener("resize", d);
307-
}), []), g(() => (t.addEventListener("scroll", m, {
307+
}), []), h(() => (t.addEventListener("scroll", m, {
308308
passive: !0
309309
}), m(), () => {
310310
t.removeEventListener("scroll", m);

dist/react-scrollsy.umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/types.d.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
/// <reference types="react" />
2+
type TThrottles = {
3+
resizeThrottle?: number;
4+
scrollThrottle?: number;
5+
};
6+
type TScrollObject = {
7+
progress: number;
8+
scrollData: IScrollDataBase;
9+
start: number;
10+
end: number;
11+
};
12+
export type TChildren = ((props: IScrollDataChildren | IScrollObject) => any) | {
13+
children: React.ReactNode;
14+
};
215
export interface IScrollDataBase {
316
scrollTop: number;
417
scrollHeight: number;
@@ -11,22 +24,13 @@ export interface IScrollData {
1124
}
1225
export interface IScrollDataChildren {
1326
scrollData: IScrollDataBase;
14-
children: any;
27+
children: TChildren;
1528
}
1629
export interface IScrollObject {
17-
scrollObject: {
18-
progress: number;
19-
scrollData: IScrollDataBase;
20-
start: number;
21-
end: number;
22-
};
23-
children: any;
30+
scrollObject: TScrollObject;
31+
children: TChildren;
2432
}
25-
export interface IScrollTrackerObject {
26-
progress: number;
27-
scrollData: IScrollDataBase;
28-
start: number;
29-
end: number;
33+
export interface IScrollTrackerObject extends TScrollObject {
3034
}
3135
export interface IScrollTrackerSettingsProperties {
3236
distance: number;
@@ -43,26 +47,21 @@ export interface IScrollTrackerSettingsArray extends Array<IScrollTrackerSetting
4347
}
4448
export interface IScrollTracker {
4549
scrollData: IScrollDataBase;
46-
children: React.ReactNode | any;
50+
children: TChildren;
4751
elem?: React.RefObject<HTMLInputElement>;
4852
settings: IScrollTrackerSettings;
49-
onStart?: any;
50-
onEnd?: any;
53+
onStart?: () => void;
54+
onEnd?: () => void;
5155
}
52-
export interface IScrollTrackerDocument {
53-
resizeThrottle?: number;
54-
scrollThrottle?: number;
55-
children: any;
56+
export interface IScrollTrackerDocument extends TThrottles {
57+
children: TChildren;
5658
}
57-
export interface IScrollTrackerCustom {
58-
resizeThrottle?: number;
59-
scrollThrottle?: number;
59+
export interface IScrollTrackerCustom extends TThrottles {
6060
scrollingElement?: string;
61-
children: any;
61+
children: TChildren;
6262
}
63-
export interface IScrollTrackerCustomMain {
64-
resizeThrottle?: number;
65-
scrollThrottle?: number;
63+
export interface IScrollTrackerCustomMain extends TThrottles {
6664
customScrollingElement?: HTMLElement | null;
67-
children: any;
65+
children: TChildren;
6866
}
67+
export {};

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-scrollsy",
33
"private": false,
4-
"version": "1.1.5",
4+
"version": "1.1.6",
55
"description": "",
66
"license": "MIT",
77
"type": "module",
@@ -12,6 +12,9 @@
1212
},
1313
"keywords": [
1414
"react",
15+
"react scroll",
16+
"scroll triggered",
17+
"scrolltriggered",
1518
"scrolling",
1619
"scrolling animation",
1720
"scroll",

src/ScrollTracker.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
21
import { memo, useEffect, useState } from "react";
32
import { elementVisibility } from "./functions/elementVisibility";
4-
import { IScrollTrackerObject, IScrollTracker } from "./types";
3+
import { IScrollTrackerObject, IScrollTracker, TChildren } from "./types";
54
import { defaultConfig, emptyScrollObject } from "./config";
65

7-
const childrenAsMethod = (children: any, scrollObject: IScrollTrackerObject = emptyScrollObject) => {
6+
const childrenAsMethod = (children: TChildren, scrollObject: IScrollTrackerObject = emptyScrollObject) => {
87
if (typeof children === "function") {
8+
if (!children) return children;
99
return children({ scrollObject, children });
1010
}
1111
return children;

src/ScrollTrackerCustom.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
21
import { useState, useEffect } from "react";
32

43
import { throttle, throttleLastCall, getContainerClientHeight, waitForElm } from "./functions/utils";

src/ScrollTrackerDocument.test.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2+
// @ts-nocheck
13
import { describe, test, expect } from "vitest";
24
import { render } from "@testing-library/react";
35

@@ -8,14 +10,12 @@ describe("<ScrollTrackerDocument />", () => {
810
test("ScrollTrackerDocument mounts properly", () => {
911
const wrapper = render(
1012
<ScrollTrackerDocument>
11-
{({ scrollData }: IScrollData) => {
12-
return (
13-
<>
14-
<h1>mounted</h1>
15-
<p className='element'>{scrollData.element?.nodeName}</p>
16-
</>
17-
);
18-
}}
13+
{({ scrollData }: IScrollData) => (
14+
<>
15+
<h1>mounted</h1>
16+
<p className='element'>{scrollData.element?.nodeName}</p>
17+
</>
18+
)}
1919
</ScrollTrackerDocument>
2020
);
2121

src/ScrollTrackerDocument.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
21
import { useEffect, useState } from "react";
32

43
import { throttle, throttleLastCall, correctInnerHeight } from "./functions/utils";
@@ -38,7 +37,7 @@ const ScrollTrackerDocumentMain = ({ children, scrollThrottle, resizeThrottle =
3837
throw new Error("No document.documentElement found.");
3938
}
4039

41-
let timeout: any = null;
40+
let timeout: number | null = null;
4241

4342
const [containerHeight, setcontainerHeight] = useState<number>(correctInnerHeight());
4443
const [percentProgress, setPercentProgress] = useState<number>(0);

src/functions/childrenAsMethod.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { IScrollDataChildren } from "../types";
22

33
export const childrenAsMethod = ({ scrollData, children }: IScrollDataChildren) => {
44
if (typeof children === "function") {
5+
if (!children) return children;
56
return children({
67
scrollData: {
78
scrollTop: scrollData.scrollTop,

0 commit comments

Comments
 (0)