All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.cjs.react-utils.useRestoreScrollPosition.useRestoreScrollPosition.js.map Maven / Gradle / Ivy

Go to download

A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.

There is a newer version: 4.12.3
Show newest version
{"version":3,"file":"useRestoreScrollPosition.js","names":["_react","require","_ramda","_useFuncDebounce","_interopRequireDefault","_withImmutableActions","_zustand","useScrollStore","create","withImmutableActions","set","get","scrollPositions","setScrollPosition","key","value","state","assoc","removeScrollPosition","dissoc","getScrollPosition","useRestoreScrollPosition","ref","prop","handleScroll","useFuncDebounce","current","scrollTop","useEffect","savedScrollPosition","undefined","addEventListener","removeEventListener","useSetScrollPosition","exports","useGetScrollPosition","useRemoveScrollPosition","_default"],"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,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,gBAAA,GAAAC,sBAAA,CAAAH,OAAA;AACA,IAAAI,qBAAA,GAAAD,sBAAA,CAAAH,OAAA;AACA,IAAAK,QAAA,GAAAL,OAAA;AAEA;AACA,IAAMM,cAAc,GAAG,IAAAC,eAAM,EAC3B,IAAAC,gCAAoB,EAAC,UAACC,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,EAAE,IAAAK,YAAK,EAACH,GAAG,EAAEC,KAAK,EAAEC,KAAK,CAACJ,eAAe;QAC1D,CAAC;MAAA,CAAC,CAAC;IAAA;IACLM,oBAAoB,EAAE,SAAtBA,oBAAoBA,CAAEJ,GAAG;MAAA,OACvBJ,GAAG,CAAC,UAAAM,KAAK;QAAA,OAAK;UAAEJ,eAAe,EAAE,IAAAO,aAAM,EAACL,GAAG,EAAEE,KAAK,CAACJ,eAAe;QAAE,CAAC;MAAA,CAAC,CAAC;IAAA;IACzEQ,iBAAiB,EAAE,SAAnBA,iBAAiBA,CAAEN,GAAG;MAAA,OAAIH,GAAG,CAAC,CAAC,CAACC,eAAe,CAACE,GAAG,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