wouter-preact
Version:
Minimalist-friendly ~1.5KB router for Preact
72 lines (58 loc) • 1.71 kB
JavaScript
import mitt from 'mitt';
import { a as useSyncExternalStore } from './preact-deps-dec5c677.js';
import 'preact/hooks';
import 'preact';
/**
* In-memory location that supports navigation
*/
const memoryLocation = ({
path = "/",
searchPath = "",
static: staticLocation,
record,
} = {}) => {
let initialPath = path;
if (searchPath) {
// join with & if path contains search query, and ? otherwise
initialPath += path.split("?")[1] ? "&" : "?";
initialPath += searchPath;
}
let [currentPath, currentSearch = ""] = initialPath.split("?");
const history = [initialPath];
const emitter = mitt();
const navigateImplementation = (path, { replace = false } = {}) => {
if (record) {
if (replace) {
history.splice(history.length - 1, 1, path);
} else {
history.push(path);
}
}
[currentPath, currentSearch = ""] = path.split("?");
emitter.emit("navigate", path);
};
const navigate = !staticLocation ? navigateImplementation : () => null;
const subscribe = (cb) => {
emitter.on("navigate", cb);
return () => emitter.off("navigate", cb);
};
const useMemoryLocation = () => [
useSyncExternalStore(subscribe, () => currentPath),
navigate,
];
const useMemoryQuery = () =>
useSyncExternalStore(subscribe, () => currentSearch);
function reset() {
// clean history array with mutation to preserve link
history.splice(0, history.length);
navigateImplementation(initialPath);
}
return {
hook: useMemoryLocation,
searchHook: useMemoryQuery,
navigate,
history: record ? history : undefined,
reset: record ? reset : undefined,
};
};
export { memoryLocation };