@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 CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("react"),d=require("@react-spring/web");function f(){if(typeof window=="undefined")return{scrollTop:0,scrollBottom:0,windowHeight:10};const e=window.scrollY||document.documentElement.scrollTop,n=window.innerHeight||document.documentElement.clientHeight,o=e+n;return{scrollTop:e,scrollBottom:o,windowHeight:n}}function g(e){return e<0?0:e>1?1:e}const w={};function m(e,n=w,o=!0,t){const[l,s]=i.useState(!1),c=i.useRef(null),r=i.useCallback(()=>{c.current&&c.current.disconnect()},[]);return i.useEffect(()=>{if(window&&IntersectionObserver&&o){c.current=new IntersectionObserver(([a])=>{s(a.isIntersecting),t&&t(a,r)},n);const u=e.current;return u&&c.current.observe(e.current),()=>{c.current&&u&&c.current.unobserve(u)}}},[e,n,o,t]),{isIntersecting:l,disconnect:r}}function b(e){const n=i.useRef(null),o=i.useCallback((...t)=>{e(...t),n.current=null},[e]);return i.useEffect(()=>()=>{n.current&&cancelAnimationFrame(n.current)},[]),i.useCallback((...t)=>{const l=()=>o(...t);n.current||(n.current=requestAnimationFrame(l))},[o])}function S(e,n){var o;const t=(o=e.current)==null?void 0:o.getBoundingClientRect();if(!t)return{scrolledRatio:0,scrollBottom:0,distance:1/0,isIntersecting:n};const{scrollTop:l,scrollBottom:s}=f(),c=t.top+l,r=s-c;return{scrolledRatio:g(r/t.height),scrollBottom:s,distance:r,isIntersecting:n}}function p(e,n,o=!0,t){const l=i.useCallback(r=>{const u=S(e,r);n(u)},[e,n]),s=b(l),c=i.useCallback(({isIntersecting:r})=>{s(r),r?window.addEventListener("scroll",s.bind(null,r)):window.removeEventListener("scroll",s.bind(null,r))},[s]);m(e,t,o,c)}function v(e,n,o=!0,t){const l=i.useRef(new d.SpringValue(0)),s=i.useCallback(c=>{const{scrolledRatio:r}=c;console.log("onSectionScroll",r),l.current.set(r),n&&n(c)},[n]);return p(e,s,o,t),{scrolledRatioSpring:l.current}}exports.useSectionScrollSpring=v;
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
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs.js","sources":["../../core/dist/index.es.js","../src/hooks/useSectionScrollSpring.ts"],"sourcesContent":["import { useState as a, useRef as d, useCallback as u, useEffect as g } from \"react\";\nfunction w() {\n if (typeof window == \"undefined\")\n return {\n scrollTop: 0,\n scrollBottom: 0,\n windowHeight: 10\n };\n const t = window.scrollY || document.documentElement.scrollTop, n = window.innerHeight || document.documentElement.clientHeight, e = t + n;\n return {\n scrollTop: t,\n scrollBottom: e,\n windowHeight: n\n };\n}\nfunction S(t) {\n return t < 0 ? 0 : t > 1 ? 1 : t;\n}\nconst m = {};\nfunction I(t, n = m, e = !0, r) {\n const [s, i] = a(!1), o = d(null), c = u(() => {\n o.current && o.current.disconnect();\n }, []);\n return g(() => {\n if (window && IntersectionObserver && e) {\n o.current = new IntersectionObserver(([f]) => {\n i(f.isIntersecting), r && r(f, c);\n }, n);\n const l = t.current;\n return l && o.current.observe(t.current), () => {\n o.current && l && o.current.unobserve(l);\n };\n }\n }, [t, n, e, r]), { isIntersecting: s, disconnect: c };\n}\nfunction R(t, n, e) {\n const [r, s] = a(!1), i = u((c) => {\n s(c.isIntersecting);\n }, []), { disconnect: o } = I(t, n, e, i);\n return { isIntersecting: r, disconnect: o };\n}\nfunction E(t, n = !1, e = m) {\n const r = d(!1), [s, i] = a(!1), o = u(\n (c, l) => {\n const f = c.isIntersecting;\n i(f), n && !r.current && f && l();\n },\n [n]\n );\n return I(t, e, !0, o), { isIntersecting: s };\n}\nfunction v(t) {\n const n = d(null), e = u(\n (...r) => {\n t(...r), n.current = null;\n },\n [t]\n );\n return g(() => () => {\n n.current && cancelAnimationFrame(n.current);\n }, []), u(\n (...r) => {\n const s = () => e(...r);\n n.current || (n.current = requestAnimationFrame(s));\n },\n [e]\n );\n}\nfunction T(t, n) {\n var l;\n const e = (l = t.current) == null ? void 0 : l.getBoundingClientRect();\n if (!e)\n return { scrolledRatio: 0, scrollBottom: 0, distance: 1 / 0, isIntersecting: n };\n const { scrollTop: r, scrollBottom: s } = w(), i = e.top + r, o = s - i;\n return { scrolledRatio: S(o / e.height), scrollBottom: s, distance: o, isIntersecting: n };\n}\nfunction b(t, n, e = !0, r) {\n const s = u(\n (c) => {\n const l = T(t, c);\n n(l);\n },\n [t, n]\n ), i = v(s), o = u(\n ({ isIntersecting: c }) => {\n i(c), c ? window.addEventListener(\"scroll\", i.bind(null, c)) : window.removeEventListener(\"scroll\", i.bind(null, c));\n },\n [i]\n );\n I(t, r, e, o);\n}\nfunction h(t, n = !0, e) {\n const [r, s] = a(0), [i, o] = a(!1), c = u(\n ({ scrolledRatio: l, isIntersecting: f }) => {\n s(l), o(f);\n },\n []\n );\n return b(t, c, n, e), { scrolledRatio: r, isIntersecting: i };\n}\nexport {\n m as DEFAULT_INTERSECTION_OBS_OPTIONS,\n S as clampScrolledRatio,\n w as getScrollPosition,\n T as getSectionScrollInfo,\n I as useIntersectionObserver,\n R as useIntersectionObserverState,\n E as useIntersectionReveal,\n v as useRafThrottle,\n b as useSectionScroll,\n h as useSectionScrollState\n};\n//# sourceMappingURL=index.es.js.map\n","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 state 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 console.log('onSectionScroll', scrolledRatio);\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"],"names":["w","t","e","S","m","I","r","s","i","a","o","d","c","u","g","f","l","v","T","b","useSectionScrollSpring","sectionRef","onScroll","shouldObserve","options","scrolledRatioSpringRef","useRef","SpringValue","onSectionScroll","useCallback","scrollInfo","scrolledRatio","useSectionScroll"],"mappings":"wIACA,SAASA,GAAI,CACX,GAAI,OAAO,QAAU,YACnB,MAAO,CACL,UAAW,EACX,aAAc,EACd,aAAc,EACpB,EACE,MAAMC,EAAI,OAAO,SAAW,SAAS,gBAAgB,UAAW,EAAI,OAAO,aAAe,SAAS,gBAAgB,aAAcC,EAAID,EAAI,EACzI,MAAO,CACL,UAAWA,EACX,aAAcC,EACd,aAAc,CAClB,CACA,CACA,SAASC,EAAEF,EAAG,CACZ,OAAOA,EAAI,EAAI,EAAIA,EAAI,EAAI,EAAIA,CACjC,CAC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"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 { useCallback as l, useState as a, useRef as d, useEffect as w } from "react";
2
- import { SpringValue as f } from "@react-spring/web";
3
- function g() {
4
- if (typeof window == "undefined")
5
- return {
6
- scrollTop: 0,
7
- scrollBottom: 0,
8
- windowHeight: 10
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
- [i]
11
+ [r]
74
12
  );
75
- S(n, t, o, c);
13
+ return a(t, o, n, c), { scrolledRatioSpring: e.current };
76
14
  }
77
- function E(n, e, o = !0, t) {
78
- const s = d(new f(0)), i = l(
79
- (c) => {
80
- const { scrolledRatio: r } = c;
81
- console.log("onSectionScroll", r), s.current.set(r), e && e(c);
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
- [e]
21
+ [t]
84
22
  );
85
- return I(n, i, o, t), { scrolledRatioSpring: s.current };
23
+ return d(e), { trackingId: n, scrolledRatioSpring: r.current };
86
24
  }
87
25
  export {
88
- E as useSectionScrollSpring
26
+ w as useActiveSectionSpring,
27
+ k as useSectionScrollSpring
89
28
  };
90
29
  //# sourceMappingURL=index.es.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.es.js","sources":["../../core/dist/index.es.js","../src/hooks/useSectionScrollSpring.ts"],"sourcesContent":["import { useState as a, useRef as d, useCallback as u, useEffect as g } from \"react\";\nfunction w() {\n if (typeof window == \"undefined\")\n return {\n scrollTop: 0,\n scrollBottom: 0,\n windowHeight: 10\n };\n const t = window.scrollY || document.documentElement.scrollTop, n = window.innerHeight || document.documentElement.clientHeight, e = t + n;\n return {\n scrollTop: t,\n scrollBottom: e,\n windowHeight: n\n };\n}\nfunction S(t) {\n return t < 0 ? 0 : t > 1 ? 1 : t;\n}\nconst m = {};\nfunction I(t, n = m, e = !0, r) {\n const [s, i] = a(!1), o = d(null), c = u(() => {\n o.current && o.current.disconnect();\n }, []);\n return g(() => {\n if (window && IntersectionObserver && e) {\n o.current = new IntersectionObserver(([f]) => {\n i(f.isIntersecting), r && r(f, c);\n }, n);\n const l = t.current;\n return l && o.current.observe(t.current), () => {\n o.current && l && o.current.unobserve(l);\n };\n }\n }, [t, n, e, r]), { isIntersecting: s, disconnect: c };\n}\nfunction R(t, n, e) {\n const [r, s] = a(!1), i = u((c) => {\n s(c.isIntersecting);\n }, []), { disconnect: o } = I(t, n, e, i);\n return { isIntersecting: r, disconnect: o };\n}\nfunction E(t, n = !1, e = m) {\n const r = d(!1), [s, i] = a(!1), o = u(\n (c, l) => {\n const f = c.isIntersecting;\n i(f), n && !r.current && f && l();\n },\n [n]\n );\n return I(t, e, !0, o), { isIntersecting: s };\n}\nfunction v(t) {\n const n = d(null), e = u(\n (...r) => {\n t(...r), n.current = null;\n },\n [t]\n );\n return g(() => () => {\n n.current && cancelAnimationFrame(n.current);\n }, []), u(\n (...r) => {\n const s = () => e(...r);\n n.current || (n.current = requestAnimationFrame(s));\n },\n [e]\n );\n}\nfunction T(t, n) {\n var l;\n const e = (l = t.current) == null ? void 0 : l.getBoundingClientRect();\n if (!e)\n return { scrolledRatio: 0, scrollBottom: 0, distance: 1 / 0, isIntersecting: n };\n const { scrollTop: r, scrollBottom: s } = w(), i = e.top + r, o = s - i;\n return { scrolledRatio: S(o / e.height), scrollBottom: s, distance: o, isIntersecting: n };\n}\nfunction b(t, n, e = !0, r) {\n const s = u(\n (c) => {\n const l = T(t, c);\n n(l);\n },\n [t, n]\n ), i = v(s), o = u(\n ({ isIntersecting: c }) => {\n i(c), c ? window.addEventListener(\"scroll\", i.bind(null, c)) : window.removeEventListener(\"scroll\", i.bind(null, c));\n },\n [i]\n );\n I(t, r, e, o);\n}\nfunction h(t, n = !0, e) {\n const [r, s] = a(0), [i, o] = a(!1), c = u(\n ({ scrolledRatio: l, isIntersecting: f }) => {\n s(l), o(f);\n },\n []\n );\n return b(t, c, n, e), { scrolledRatio: r, isIntersecting: i };\n}\nexport {\n m as DEFAULT_INTERSECTION_OBS_OPTIONS,\n S as clampScrolledRatio,\n w as getScrollPosition,\n T as getSectionScrollInfo,\n I as useIntersectionObserver,\n R as useIntersectionObserverState,\n E as useIntersectionReveal,\n v as useRafThrottle,\n b as useSectionScroll,\n h as useSectionScrollState\n};\n//# sourceMappingURL=index.es.js.map\n","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 state 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 console.log('onSectionScroll', scrolledRatio);\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"],"names":["w","t","n","e","S","m","I","r","a","o","d","c","u","g","f","l","v","T","s","i","b","useSectionScrollSpring","sectionRef","onScroll","shouldObserve","options","scrolledRatioSpringRef","useRef","SpringValue","onSectionScroll","useCallback","scrollInfo","scrolledRatio","useSectionScroll"],"mappings":";;AACA,SAASA,IAAI;AACX,MAAI,OAAO,UAAU;AACnB,WAAO;AAAA,MACL,WAAW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GAAGJ,GAAGM,CAAC;AACd;AC3EO,SAASY,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;AAClB,cAAA,IAAI,mBAAmBC,CAAa,GACrBN,EAAA,QAAQ,IAAIM,CAAa,GAE5CT,KACFA,EAASQ,CAAU;AAAA,IAEvB;AAAA,IACA,CAACR,CAAQ;AAAA,EAAA;AAGMU,SAAAA,EAAAX,GAAYO,GAAiBL,GAAeC,CAAO,GAE7D,EAAE,qBAAqBC,EAAuB;AACvD;"}
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,2 +1,3 @@
1
1
  export * from './useSectionScrollSpring';
2
+ export * from './useActiveSectionSpring';
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -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 state of the section scroll
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;;EAoBtC"}
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.0.1",
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": "0755e926c96e557be7055da7d8174fe94ffbb3de"
52
+ "gitHead": "b4dc4bd312e4d1d3de0f344764b9b67dadac18a8"
51
53
  }