package.react-utils.useRestoreScrollPosition.useRestoreScrollPosition.js.map Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neeto-commons-frontend Show documentation
Show all versions of neeto-commons-frontend Show documentation
A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.
{"version":3,"file":"useRestoreScrollPosition.js","names":["useEffect","assoc","dissoc","prop","useFuncDebounce","withImmutableActions","create","useScrollStore","set","get","scrollPositions","setScrollPosition","key","value","state","removeScrollPosition","getScrollPosition","useRestoreScrollPosition","ref","handleScroll","current","scrollTop","savedScrollPosition","undefined","addEventListener","removeEventListener","useSetScrollPosition","useGetScrollPosition","useRemoveScrollPosition"],"sources":["../../../src/react-utils/useRestoreScrollPosition/useRestoreScrollPosition.js"],"sourcesContent":["import { useEffect } from \"react\";\n\nimport { assoc, dissoc, prop } from \"ramda\";\nimport useFuncDebounce from \"react-utils/useFuncDebounce\";\nimport withImmutableActions from \"react-utils/withImmutableActions\";\nimport { create } from \"zustand\";\n\n/** @type {import(\"neetocommons/react-utils\").ZustandStoreHook} */\nconst useScrollStore = create(\n withImmutableActions((set, get) => ({\n scrollPositions: {},\n setScrollPosition: (key, value) =>\n set(state => ({\n scrollPositions: assoc(key, value, state.scrollPositions),\n })),\n removeScrollPosition: key =>\n set(state => ({ scrollPositions: dissoc(key, state.scrollPositions) })),\n getScrollPosition: key => get().scrollPositions[key],\n }))\n);\n\nconst useRestoreScrollPosition = (key, ref) => {\n const setScrollPosition = useScrollStore(prop(\"setScrollPosition\"));\n const getScrollPosition = useScrollStore(prop(\"getScrollPosition\"));\n\n const handleScroll = useFuncDebounce(() => {\n if (!ref.current) return;\n setScrollPosition(key, ref.current.scrollTop);\n }, 500);\n\n useEffect(() => {\n if (ref.current) {\n const savedScrollPosition = getScrollPosition(key);\n if (savedScrollPosition !== undefined) {\n ref.current.scrollTop = savedScrollPosition;\n }\n ref.current.addEventListener(\"scroll\", handleScroll);\n }\n\n return () => {\n if (ref.current) {\n ref.current.removeEventListener(\"scroll\", handleScroll);\n }\n };\n }, [key]);\n};\n\nexport const useSetScrollPosition = () =>\n useScrollStore(prop(\"setScrollPosition\"));\n\nexport const useGetScrollPosition = () =>\n useScrollStore(prop(\"getScrollPosition\"));\n\nexport const useRemoveScrollPosition = () =>\n useScrollStore(prop(\"removeScrollPosition\"));\n\nexport default useRestoreScrollPosition;\n"],"mappings":"AAAA,SAASA,SAAS,QAAQ,OAAO;AAEjC,SAASC,KAAK,EAAEC,MAAM,EAAEC,IAAI,QAAQ,OAAO;AAC3C,OAAOC,eAAe;AACtB,OAAOC,oBAAoB;AAC3B,SAASC,MAAM,QAAQ,SAAS;;AAEhC;AACA,IAAMC,cAAc,GAAGD,MAAM,CAC3BD,oBAAoB,CAAC,UAACG,GAAG,EAAEC,GAAG;EAAA,OAAM;IAClCC,eAAe,EAAE,CAAC,CAAC;IACnBC,iBAAiB,EAAE,SAAnBA,iBAAiBA,CAAGC,GAAG,EAAEC,KAAK;MAAA,OAC5BL,GAAG,CAAC,UAAAM,KAAK;QAAA,OAAK;UACZJ,eAAe,EAAET,KAAK,CAACW,GAAG,EAAEC,KAAK,EAAEC,KAAK,CAACJ,eAAe;QAC1D,CAAC;MAAA,CAAC,CAAC;IAAA;IACLK,oBAAoB,EAAE,SAAtBA,oBAAoBA,CAAEH,GAAG;MAAA,OACvBJ,GAAG,CAAC,UAAAM,KAAK;QAAA,OAAK;UAAEJ,eAAe,EAAER,MAAM,CAACU,GAAG,EAAEE,KAAK,CAACJ,eAAe;QAAE,CAAC;MAAA,CAAC,CAAC;IAAA;IACzEM,iBAAiB,EAAE,SAAnBA,iBAAiBA,CAAEJ,GAAG;MAAA,OAAIH,GAAG,CAAC,CAAC,CAACC,eAAe,CAACE,GAAG,CAAC;IAAA;EACtD,CAAC;AAAA,CAAC,CACJ,CAAC;AAED,IAAMK,wBAAwB,GAAG,SAA3BA,wBAAwBA,CAAIL,GAAG,EAAEM,GAAG,EAAK;EAC7C,IAAMP,iBAAiB,G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