one
Version:
One is a new React Framework that makes Vite serve both native and web.
105 lines (104 loc) • 3.9 kB
JavaScript
import { useEffect } from "react";
import { setLastAction } from "../router/lastAction.mjs";
import { subscribeToLoadingState, subscribeToRootState } from "../router/router.mjs";
const KEY = "one-sr",
GROUP_KEY = "one-sr-groups",
getState = () => JSON.parse(sessionStorage.getItem(KEY) || "{}"),
getGroupState = () => JSON.parse(sessionStorage.getItem(GROUP_KEY) || "{}");
let isFirstLoad = !0,
activeGroups = /* @__PURE__ */new Set();
function registerScrollGroup(groupId) {
return activeGroups.add(groupId), () => {
activeGroups.delete(groupId);
};
}
function getGroupKey(pathname) {
let longestMatch = null;
for (const group of activeGroups) pathname.startsWith(group) && (!longestMatch || group.length > longestMatch.length) && (longestMatch = group);
return longestMatch;
}
function restorePosition() {
try {
const saved = getState()[window.location.pathname];
typeof saved == "number" && setTimeout(() => {
window.scrollTo(0, saved);
});
} catch (error) {
console.error("Error restoring scroll position", error), sessionStorage.removeItem(KEY);
}
}
function restoreGroupPosition(groupId) {
try {
const saved = getGroupState()[groupId];
typeof saved == "number" && setTimeout(() => {
window.scrollTo(0, saved);
});
} catch (error) {
console.error(`Error restoring scroll position for group ${groupId}`, error), sessionStorage.removeItem(GROUP_KEY);
}
}
let didPop = !1,
previousPathname = null;
function rememberScrollPosition() {
didPop = !1;
const pathname = window.location.pathname,
state = getState();
state[pathname] = window.scrollY, sessionStorage.setItem(KEY, JSON.stringify(state));
const groupKey = getGroupKey(pathname);
if (groupKey) {
const groupState = getGroupState();
groupState[groupKey] = window.scrollY, sessionStorage.setItem(GROUP_KEY, JSON.stringify(groupState));
}
previousPathname = pathname;
}
let disable = null;
function configure(props) {
if (typeof window > "u" || !window.addEventListener) return;
disable?.();
const popStateController = new AbortController();
window.addEventListener("popstate", () => {
didPop = !0, setLastAction();
}, {
signal: popStateController.signal
});
const disposeOnLoadState = subscribeToLoadingState(state => {
state === "loading" && rememberScrollPosition();
}),
disposeOnRootState = subscribeToRootState(state => {
if (isFirstLoad) {
isFirstLoad = !1, previousPathname = window.location.pathname;
return;
}
if (state.linkOptions?.scroll === !1) return;
const {
hash
} = state,
currentPathname = window.location.pathname;
if (hash) setTimeout(() => {
scrollToHash(hash);
});else if (didPop) props.disable !== "restore" && restorePosition();else {
const prevGroup = previousPathname ? getGroupKey(previousPathname) : null,
currentGroup = getGroupKey(currentPathname);
prevGroup && currentGroup && prevGroup === currentGroup ? restoreGroupPosition(currentGroup) : state.linkOptions?.scrollGroup ? restoreGroupPosition(state.linkOptions.scrollGroup) : window.scrollTo(0, 0);
}
previousPathname = currentPathname;
});
return disable = () => {
popStateController.abort(), disposeOnLoadState(), disposeOnRootState();
}, disable;
}
function scrollToHash(hash) {
if (!hash || !hash.startsWith("#")) return;
const id = hash.slice(1),
el = document.getElementById(id);
el && el.scrollIntoView({
behavior: "instant"
});
}
function ScrollBehavior(props) {
return process.env.VITE_ENVIRONMENT === "client" && (useEffect(() => {
window.location.hash && scrollToHash(window.location.hash);
}, []), useEffect(() => configure(props), [props.disable])), null;
}
export { ScrollBehavior, registerScrollGroup };
//# sourceMappingURL=ScrollBehavior.mjs.map