package.cjs.react-utils.useRestoreScrollPosition.useRestoreScrollPosition.js 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.
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useSetScrollPosition = exports.useRemoveScrollPosition = exports.useGetScrollPosition = exports["default"] = void 0;
var _react = require("react");
var _ramda = require("ramda");
var _useFuncDebounce = _interopRequireDefault(require("../useFuncDebounce"));
var _withImmutableActions = _interopRequireDefault(require("../withImmutableActions"));
var _zustand = require("zustand");
/** @type {import("neetocommons/react-utils").ZustandStoreHook} */
var useScrollStore = (0, _zustand.create)((0, _withImmutableActions["default"])(function (set, get) {
return {
scrollPositions: {},
setScrollPosition: function setScrollPosition(key, value) {
return set(function (state) {
return {
scrollPositions: (0, _ramda.assoc)(key, value, state.scrollPositions)
};
});
},
removeScrollPosition: function removeScrollPosition(key) {
return set(function (state) {
return {
scrollPositions: (0, _ramda.dissoc)(key, state.scrollPositions)
};
});
},
getScrollPosition: function getScrollPosition(key) {
return get().scrollPositions[key];
}
};
}));
var useRestoreScrollPosition = function useRestoreScrollPosition(key, ref) {
var setScrollPosition = useScrollStore((0, _ramda.prop)("setScrollPosition"));
var getScrollPosition = useScrollStore((0, _ramda.prop)("getScrollPosition"));
var handleScroll = (0, _useFuncDebounce["default"])(function () {
if (!ref.current) return;
setScrollPosition(key, ref.current.scrollTop);
}, 500);
(0, _react.useEffect)(function () {
if (ref.current) {
var savedScrollPosition = getScrollPosition(key);
if (savedScrollPosition !== undefined) {
ref.current.scrollTop = savedScrollPosition;
}
ref.current.addEventListener("scroll", handleScroll);
}
return function () {
if (ref.current) {
ref.current.removeEventListener("scroll", handleScroll);
}
};
}, [key]);
};
var useSetScrollPosition = function useSetScrollPosition() {
return useScrollStore((0, _ramda.prop)("setScrollPosition"));
};
exports.useSetScrollPosition = useSetScrollPosition;
var useGetScrollPosition = function useGetScrollPosition() {
return useScrollStore((0, _ramda.prop)("getScrollPosition"));
};
exports.useGetScrollPosition = useGetScrollPosition;
var useRemoveScrollPosition = function useRemoveScrollPosition() {
return useScrollStore((0, _ramda.prop)("removeScrollPosition"));
};
exports.useRemoveScrollPosition = useRemoveScrollPosition;
var _default = useRestoreScrollPosition;
exports["default"] = _default;
//# sourceMappingURL=useRestoreScrollPosition.js.map