@react-scrollytelling/react-spring 0.0.1 → 0.1.1
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/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.es.js +20 -81
- package/dist/index.es.js.map +1 -1
- package/dist/src/hooks/index.d.ts +1 -0
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/useActiveSectionSpring.d.ts +16 -0
- package/dist/src/hooks/useActiveSectionSpring.d.ts.map +1 -0
- package/dist/src/hooks/useSectionScrollSpring.d.ts +1 -1
- package/dist/src/hooks/useSectionScrollSpring.d.ts.map +1 -1
- package/package.json +4 -2
package/dist/index.cjs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),S=require("@react-scrollytelling/core"),l=require("@react-spring/web"),a=require("@react-scrollytelling/grouped");function g(r,e,o=!0,s){const c=t.useRef(new l.SpringValue(0)),n=t.useCallback(i=>{const{scrolledRatio:u}=i;c.current.set(u),e&&e(i)},[e]);return S.useSectionScroll(r,n,o,s),{scrolledRatioSpring:c.current}}function d(r){const e=t.useRef(new l.SpringValue(0)),[o,s]=t.useState(null),c=t.useCallback(n=>{const{trackingId:i,scrolledRatio:u}=n;s(i),e.current.set(u),r&&r(n)},[r]);return a.useActiveSection(c),{trackingId:o,scrolledRatioSpring:e.current}}exports.useActiveSectionSpring=d;exports.useSectionScrollSpring=g;
|
|
2
2
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/hooks/useSectionScrollSpring.ts","../src/hooks/useActiveSectionSpring.ts"],"sourcesContent":["import { useCallback, useRef } from 'react';\n\nimport {\n IntersectionObserverOptions,\n SectionScrollInfo,\n useSectionScroll,\n} from '@react-scrollytelling/core';\nimport { SpringValue } from '@react-spring/web';\n\n/**\n * Get the Spring value (react-spring) of the section scroll\n * @param sectionRef - The reference to the section element\n * @param shouldObserve - Whether the underlying IntersectionObserver should be active\n * @param onScroll - The callback to track the scroll ratio\n */\nexport function useSectionScrollSpring(\n sectionRef: React.RefObject<Element>,\n onScroll?: (scrollInfo: SectionScrollInfo) => void,\n shouldObserve = true,\n options?: IntersectionObserverOptions\n) {\n const scrolledRatioSpringRef = useRef(new SpringValue(0));\n\n const onSectionScroll = useCallback(\n (scrollInfo: SectionScrollInfo) => {\n const { scrolledRatio } = scrollInfo;\n scrolledRatioSpringRef.current.set(scrolledRatio);\n\n if (onScroll) {\n onScroll(scrollInfo);\n }\n },\n [onScroll]\n );\n\n useSectionScroll(sectionRef, onSectionScroll, shouldObserve, options);\n\n return { scrolledRatioSpring: scrolledRatioSpringRef.current };\n}\n","import { useCallback, useRef, useState } from 'react';\n\nimport {\n ActiveSectionScrollInfo,\n useActiveSection,\n ActiveSectionObserver,\n} from '@react-scrollytelling/grouped';\nimport { SpringValue } from '@react-spring/web';\n\n/**\n * Watches for all tracked sections to find the section closet to the bottom of the viewport\n * through ScrollytellingProvider.\n * It invokes the onActiveSectionChange when the active section changes.\n * Returns the Spring value (react-spring) of the section scroll.\n * @param onActiveSectionChange - The callback needs to be memoized\n * @returns The trackingId of the active section and the Spring value of the section scroll\n */\nexport function useActiveSectionSpring(onActiveSectionChange?: ActiveSectionObserver) {\n const scrolledRatioSpringRef = useRef(new SpringValue(0));\n const [trackingId, setTrackingId] = useState<string | null>(null);\n\n const onSectionScroll = useCallback(\n (scrollInfo: ActiveSectionScrollInfo) => {\n const { trackingId: id, scrolledRatio } = scrollInfo;\n setTrackingId(id);\n scrolledRatioSpringRef.current.set(scrolledRatio);\n\n if (onActiveSectionChange) {\n onActiveSectionChange(scrollInfo);\n }\n },\n [onActiveSectionChange]\n );\n\n useActiveSection(onSectionScroll);\n\n return { trackingId, scrolledRatioSpring: scrolledRatioSpringRef.current };\n}\n"],"names":["useSectionScrollSpring","sectionRef","onScroll","shouldObserve","options","scrolledRatioSpringRef","useRef","SpringValue","onSectionScroll","useCallback","scrollInfo","scrolledRatio","useSectionScroll","useActiveSectionSpring","onActiveSectionChange","trackingId","setTrackingId","useState","id","useActiveSection"],"mappings":"2NAeO,SAASA,EACdC,EACAC,EACAC,EAAgB,GAChBC,EACA,CACA,MAAMC,EAAyBC,EAAA,OAAO,IAAIC,EAAAA,YAAY,CAAC,CAAC,EAElDC,EAAkBC,EAAA,YACrBC,GAAkC,CAC3B,KAAA,CAAE,cAAAC,CAAkB,EAAAD,EACHL,EAAA,QAAQ,IAAIM,CAAa,EAE5CT,GACFA,EAASQ,CAAU,CAEvB,EACA,CAACR,CAAQ,CAAA,EAGMU,OAAAA,EAAAA,iBAAAX,EAAYO,EAAiBL,EAAeC,CAAO,EAE7D,CAAE,oBAAqBC,EAAuB,QACvD,CCrBO,SAASQ,EAAuBC,EAA+C,CACpF,MAAMT,EAAyBC,EAAA,OAAO,IAAIC,EAAAA,YAAY,CAAC,CAAC,EAClD,CAACQ,EAAYC,CAAa,EAAIC,WAAwB,IAAI,EAE1DT,EAAkBC,EAAA,YACrBC,GAAwC,CACvC,KAAM,CAAE,WAAYQ,EAAI,cAAAP,CAAA,EAAkBD,EAC1CM,EAAcE,CAAE,EACOb,EAAA,QAAQ,IAAIM,CAAa,EAE5CG,GACFA,EAAsBJ,CAAU,CAEpC,EACA,CAACI,CAAqB,CAAA,EAGxBK,OAAAA,EAAA,iBAAiBX,CAAe,EAEzB,CAAE,WAAAO,EAAY,oBAAqBV,EAAuB,OAAQ,CAC3E"}
|
package/dist/index.es.js
CHANGED
|
@@ -1,90 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const n = window.scrollY || document.documentElement.scrollTop, e = window.innerHeight || document.documentElement.clientHeight, o = n + e;
|
|
11
|
-
return {
|
|
12
|
-
scrollTop: n,
|
|
13
|
-
scrollBottom: o,
|
|
14
|
-
windowHeight: e
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
function p(n) {
|
|
18
|
-
return n < 0 ? 0 : n > 1 ? 1 : n;
|
|
19
|
-
}
|
|
20
|
-
const v = {};
|
|
21
|
-
function S(n, e = v, o = !0, t) {
|
|
22
|
-
const [s, i] = a(!1), c = d(null), r = l(() => {
|
|
23
|
-
c.current && c.current.disconnect();
|
|
24
|
-
}, []);
|
|
25
|
-
return w(() => {
|
|
26
|
-
if (window && IntersectionObserver && o) {
|
|
27
|
-
c.current = new IntersectionObserver(([m]) => {
|
|
28
|
-
i(m.isIntersecting), t && t(m, r);
|
|
29
|
-
}, e);
|
|
30
|
-
const u = n.current;
|
|
31
|
-
return u && c.current.observe(n.current), () => {
|
|
32
|
-
c.current && u && c.current.unobserve(u);
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
}, [n, e, o, t]), { isIntersecting: s, disconnect: r };
|
|
36
|
-
}
|
|
37
|
-
function b(n) {
|
|
38
|
-
const e = d(null), o = l(
|
|
39
|
-
(...t) => {
|
|
40
|
-
n(...t), e.current = null;
|
|
41
|
-
},
|
|
42
|
-
[n]
|
|
43
|
-
);
|
|
44
|
-
return w(() => () => {
|
|
45
|
-
e.current && cancelAnimationFrame(e.current);
|
|
46
|
-
}, []), l(
|
|
47
|
-
(...t) => {
|
|
48
|
-
const s = () => o(...t);
|
|
49
|
-
e.current || (e.current = requestAnimationFrame(s));
|
|
50
|
-
},
|
|
51
|
-
[o]
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
function R(n, e) {
|
|
55
|
-
var o;
|
|
56
|
-
const t = (o = n.current) == null ? void 0 : o.getBoundingClientRect();
|
|
57
|
-
if (!t)
|
|
58
|
-
return { scrolledRatio: 0, scrollBottom: 0, distance: 1 / 0, isIntersecting: e };
|
|
59
|
-
const { scrollTop: s, scrollBottom: i } = g(), c = t.top + s, r = i - c;
|
|
60
|
-
return { scrolledRatio: p(r / t.height), scrollBottom: i, distance: r, isIntersecting: e };
|
|
61
|
-
}
|
|
62
|
-
function I(n, e, o = !0, t) {
|
|
63
|
-
const s = l(
|
|
64
|
-
(r) => {
|
|
65
|
-
const u = R(n, r);
|
|
66
|
-
e(u);
|
|
67
|
-
},
|
|
68
|
-
[n, e]
|
|
69
|
-
), i = b(s), c = l(
|
|
70
|
-
({ isIntersecting: r }) => {
|
|
71
|
-
i(r), r ? window.addEventListener("scroll", i.bind(null, r)) : window.removeEventListener("scroll", i.bind(null, r));
|
|
1
|
+
import { useRef as u, useCallback as l, useState as p } from "react";
|
|
2
|
+
import { useSectionScroll as a } from "@react-scrollytelling/core";
|
|
3
|
+
import { SpringValue as S } from "@react-spring/web";
|
|
4
|
+
import { useActiveSection as d } from "@react-scrollytelling/grouped";
|
|
5
|
+
function k(t, r, n = !0, c) {
|
|
6
|
+
const e = u(new S(0)), o = l(
|
|
7
|
+
(i) => {
|
|
8
|
+
const { scrolledRatio: s } = i;
|
|
9
|
+
e.current.set(s), r && r(i);
|
|
72
10
|
},
|
|
73
|
-
[
|
|
11
|
+
[r]
|
|
74
12
|
);
|
|
75
|
-
|
|
13
|
+
return a(t, o, n, c), { scrolledRatioSpring: e.current };
|
|
76
14
|
}
|
|
77
|
-
function
|
|
78
|
-
const
|
|
79
|
-
(
|
|
80
|
-
const { scrolledRatio:
|
|
81
|
-
|
|
15
|
+
function w(t) {
|
|
16
|
+
const r = u(new S(0)), [n, c] = p(null), e = l(
|
|
17
|
+
(o) => {
|
|
18
|
+
const { trackingId: i, scrolledRatio: s } = o;
|
|
19
|
+
c(i), r.current.set(s), t && t(o);
|
|
82
20
|
},
|
|
83
|
-
[
|
|
21
|
+
[t]
|
|
84
22
|
);
|
|
85
|
-
return
|
|
23
|
+
return d(e), { trackingId: n, scrolledRatioSpring: r.current };
|
|
86
24
|
}
|
|
87
25
|
export {
|
|
88
|
-
|
|
26
|
+
w as useActiveSectionSpring,
|
|
27
|
+
k as useSectionScrollSpring
|
|
89
28
|
};
|
|
90
29
|
//# sourceMappingURL=index.es.js.map
|
package/dist/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["
|
|
1
|
+
{"version":3,"file":"index.es.js","sources":["../src/hooks/useSectionScrollSpring.ts","../src/hooks/useActiveSectionSpring.ts"],"sourcesContent":["import { useCallback, useRef } from 'react';\n\nimport {\n IntersectionObserverOptions,\n SectionScrollInfo,\n useSectionScroll,\n} from '@react-scrollytelling/core';\nimport { SpringValue } from '@react-spring/web';\n\n/**\n * Get the Spring value (react-spring) of the section scroll\n * @param sectionRef - The reference to the section element\n * @param shouldObserve - Whether the underlying IntersectionObserver should be active\n * @param onScroll - The callback to track the scroll ratio\n */\nexport function useSectionScrollSpring(\n sectionRef: React.RefObject<Element>,\n onScroll?: (scrollInfo: SectionScrollInfo) => void,\n shouldObserve = true,\n options?: IntersectionObserverOptions\n) {\n const scrolledRatioSpringRef = useRef(new SpringValue(0));\n\n const onSectionScroll = useCallback(\n (scrollInfo: SectionScrollInfo) => {\n const { scrolledRatio } = scrollInfo;\n scrolledRatioSpringRef.current.set(scrolledRatio);\n\n if (onScroll) {\n onScroll(scrollInfo);\n }\n },\n [onScroll]\n );\n\n useSectionScroll(sectionRef, onSectionScroll, shouldObserve, options);\n\n return { scrolledRatioSpring: scrolledRatioSpringRef.current };\n}\n","import { useCallback, useRef, useState } from 'react';\n\nimport {\n ActiveSectionScrollInfo,\n useActiveSection,\n ActiveSectionObserver,\n} from '@react-scrollytelling/grouped';\nimport { SpringValue } from '@react-spring/web';\n\n/**\n * Watches for all tracked sections to find the section closet to the bottom of the viewport\n * through ScrollytellingProvider.\n * It invokes the onActiveSectionChange when the active section changes.\n * Returns the Spring value (react-spring) of the section scroll.\n * @param onActiveSectionChange - The callback needs to be memoized\n * @returns The trackingId of the active section and the Spring value of the section scroll\n */\nexport function useActiveSectionSpring(onActiveSectionChange?: ActiveSectionObserver) {\n const scrolledRatioSpringRef = useRef(new SpringValue(0));\n const [trackingId, setTrackingId] = useState<string | null>(null);\n\n const onSectionScroll = useCallback(\n (scrollInfo: ActiveSectionScrollInfo) => {\n const { trackingId: id, scrolledRatio } = scrollInfo;\n setTrackingId(id);\n scrolledRatioSpringRef.current.set(scrolledRatio);\n\n if (onActiveSectionChange) {\n onActiveSectionChange(scrollInfo);\n }\n },\n [onActiveSectionChange]\n );\n\n useActiveSection(onSectionScroll);\n\n return { trackingId, scrolledRatioSpring: scrolledRatioSpringRef.current };\n}\n"],"names":["useSectionScrollSpring","sectionRef","onScroll","shouldObserve","options","scrolledRatioSpringRef","useRef","SpringValue","onSectionScroll","useCallback","scrollInfo","scrolledRatio","useSectionScroll","useActiveSectionSpring","onActiveSectionChange","trackingId","setTrackingId","useState","id","useActiveSection"],"mappings":";;;;AAeO,SAASA,EACdC,GACAC,GACAC,IAAgB,IAChBC,GACA;AACA,QAAMC,IAAyBC,EAAO,IAAIC,EAAY,CAAC,CAAC,GAElDC,IAAkBC;AAAA,IACtB,CAACC,MAAkC;AAC3B,YAAA,EAAE,eAAAC,EAAkB,IAAAD;AACH,MAAAL,EAAA,QAAQ,IAAIM,CAAa,GAE5CT,KACFA,EAASQ,CAAU;AAAA,IAEvB;AAAA,IACA,CAACR,CAAQ;AAAA,EAAA;AAGM,SAAAU,EAAAX,GAAYO,GAAiBL,GAAeC,CAAO,GAE7D,EAAE,qBAAqBC,EAAuB;AACvD;ACrBO,SAASQ,EAAuBC,GAA+C;AACpF,QAAMT,IAAyBC,EAAO,IAAIC,EAAY,CAAC,CAAC,GAClD,CAACQ,GAAYC,CAAa,IAAIC,EAAwB,IAAI,GAE1DT,IAAkBC;AAAA,IACtB,CAACC,MAAwC;AACvC,YAAM,EAAE,YAAYQ,GAAI,eAAAP,EAAA,IAAkBD;AAC1C,MAAAM,EAAcE,CAAE,GACOb,EAAA,QAAQ,IAAIM,CAAa,GAE5CG,KACFA,EAAsBJ,CAAU;AAAA,IAEpC;AAAA,IACA,CAACI,CAAqB;AAAA,EAAA;AAGxB,SAAAK,EAAiBX,CAAe,GAEzB,EAAE,YAAAO,GAAY,qBAAqBV,EAAuB,QAAQ;AAC3E;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { SpringValue } from '@react-spring/web';
|
|
2
|
+
import { ActiveSectionObserver } from '@react-scrollytelling/grouped';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Watches for all tracked sections to find the section closet to the bottom of the viewport
|
|
6
|
+
* through ScrollytellingProvider.
|
|
7
|
+
* It invokes the onActiveSectionChange when the active section changes.
|
|
8
|
+
* Returns the Spring value (react-spring) of the section scroll.
|
|
9
|
+
* @param onActiveSectionChange - The callback needs to be memoized
|
|
10
|
+
* @returns The trackingId of the active section and the Spring value of the section scroll
|
|
11
|
+
*/
|
|
12
|
+
export declare function useActiveSectionSpring(onActiveSectionChange?: ActiveSectionObserver): {
|
|
13
|
+
trackingId: string | null;
|
|
14
|
+
scrolledRatioSpring: SpringValue<number>;
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=useActiveSectionSpring.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useActiveSectionSpring.d.ts","sourceRoot":"","sources":["../../../src/hooks/useActiveSectionSpring.ts"],"names":[],"mappings":"AAEA,OAAO,EAGL,qBAAqB,EACtB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,qBAAqB,CAAC,EAAE,qBAAqB;;;EAoBnF"}
|
|
@@ -2,7 +2,7 @@ import { SpringValue } from '@react-spring/web';
|
|
|
2
2
|
import { IntersectionObserverOptions, SectionScrollInfo } from '@react-scrollytelling/core';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Get the
|
|
5
|
+
* Get the Spring value (react-spring) of the section scroll
|
|
6
6
|
* @param sectionRef - The reference to the section element
|
|
7
7
|
* @param shouldObserve - Whether the underlying IntersectionObserver should be active
|
|
8
8
|
* @param onScroll - The callback to track the scroll ratio
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSectionScrollSpring.d.ts","sourceRoot":"","sources":["../../../src/hooks/useSectionScrollSpring.ts"],"names":[],"mappings":";AAEA,OAAO,EACL,2BAA2B,EAC3B,iBAAiB,EAElB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,UAAU,EAAE,KAAK,CAAC,SAAS,CAAC,OAAO,CAAC,EACpC,QAAQ,CAAC,EAAE,CAAC,UAAU,EAAE,iBAAiB,KAAK,IAAI,EAClD,aAAa,UAAO,EACpB,OAAO,CAAC,EAAE,2BAA2B;;
|
|
1
|
+
{"version":3,"file":"useSectionScrollSpring.d.ts","sourceRoot":"","sources":["../../../src/hooks/useSectionScrollSpring.ts"],"names":[],"mappings":";AAEA,OAAO,EACL,2BAA2B,EAC3B,iBAAiB,EAElB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,UAAU,EAAE,KAAK,CAAC,SAAS,CAAC,OAAO,CAAC,EACpC,QAAQ,CAAC,EAAE,CAAC,UAAU,EAAE,iBAAiB,KAAK,IAAI,EAClD,aAAa,UAAO,EACpB,OAAO,CAAC,EAAE,2BAA2B;;EAmBtC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-scrollytelling/react-spring",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "@hsunpei",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,9 +43,11 @@
|
|
|
43
43
|
"vite": "^5.2.11"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
+
"@react-scrollytelling/core": "workspace:^",
|
|
47
|
+
"@react-scrollytelling/grouped": "workspace:^",
|
|
46
48
|
"react": "^18.2.0",
|
|
47
49
|
"react-dom": "^18.2.0",
|
|
48
50
|
"react-spring": "^9.7.5"
|
|
49
51
|
},
|
|
50
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "b4dc4bd312e4d1d3de0f344764b9b67dadac18a8"
|
|
51
53
|
}
|