UNPKG

@flanksource/clicky-ui

Version:

Flanksource Clicky UI — React component library built on shadcn/ui with light/dark and density theming.

32 lines (31 loc) 1.09 kB
import { useState, useEffect, useCallback } from "react"; function useHistoryRoute(options) { const { parse, build } = options; const [route, setRoute] = useState( () => typeof window === "undefined" ? parse("/", "") : parse(window.location.pathname, window.location.search) ); useEffect(() => { const onPop = () => setRoute(parse(window.location.pathname, window.location.search)); window.addEventListener("popstate", onPop); return () => window.removeEventListener("popstate", onPop); }, [parse]); const navigate = useCallback( (next) => { setRoute((prev) => { const merged = typeof next === "function" ? next(prev) : { ...prev, ...next }; const path = build(merged); if (typeof window !== "undefined") { const current = window.location.pathname + window.location.search; if (current !== path) window.history.pushState(null, "", path); } return merged; }); }, [build] ); return [route, navigate]; } export { useHistoryRoute }; //# sourceMappingURL=use-history-route.js.map