UNPKG

nuqs

Version:

Type-safe search params state manager for React - Like useState, but stored in the URL query string

81 lines (79 loc) 3.28 kB
import { r as createEmitter } from "./debounce-PSGthE_7.js"; import { c as debug, n as createAdapterProvider, o as renderQueryString } from "./context-C4spomkL.js"; import { n as filterSearchParams, t as applyChange } from "./key-isolation-Y_bk4Yva.js"; import { n as setQueueResetMutex } from "./reset-p1J0pPAX.js"; import { r as patchHistory, t as historyUpdateMarker } from "./patch-history-CLy38Aj7.js"; import { startTransition, useCallback, useEffect, useState } from "react"; //#region src/adapters/lib/react-router.ts function createReactRouterBasedAdapter({ adapter, useNavigate, useSearchParams }) { const emitter = createEmitter(); function useNuqsReactRouterBasedAdapter(watchKeys) { const navigate = useNavigate(); return { searchParams: useOptimisticSearchParams(watchKeys), updateUrl: useCallback((search, options) => { startTransition(() => { emitter.emit("update", search); }); const url = new URL(location.href); url.search = renderQueryString(search); debug(`[nuqs ${adapter}] Updating url: %s`, url); const updateMethod = options.history === "push" ? history.pushState : history.replaceState; setQueueResetMutex(options.shallow ? 1 : 2); updateMethod.call(history, history.state, historyUpdateMarker, url); if (options.shallow === false) navigate({ hash: url.hash, search: url.search }, { replace: true, preventScrollReset: true, state: history.state?.usr }); if (options.scroll) window.scrollTo(0, 0); }, [navigate]), autoResetQueueOnUpdate: false }; } function useOptimisticSearchParams(watchKeys = []) { const [serverSearchParams] = useSearchParams(typeof location === "undefined" ? new URLSearchParams() : new URLSearchParams(location.search)); const [searchParams, setSearchParams] = useState(() => { return typeof location === "undefined" ? filterSearchParams(serverSearchParams, watchKeys, true) : filterSearchParams(new URLSearchParams(location.search), watchKeys, false); }); useEffect(() => { function onPopState() { startTransition(() => { setSearchParams(applyChange(new URLSearchParams(location.search), watchKeys, false)); }); } function onEmitterUpdate(search) { startTransition(() => { setSearchParams(applyChange(search, watchKeys, true)); }); } emitter.on("update", onEmitterUpdate); window.addEventListener("popstate", onPopState); return () => { emitter.off("update", onEmitterUpdate); window.removeEventListener("popstate", onPopState); }; }, [watchKeys.join("&")]); return searchParams; } /** * Sync shallow updates of the URL with the useOptimisticSearchParams hook. * * By default, the useOptimisticSearchParams hook will only react to internal nuqs updates. * If third party code updates the History API directly, use this function to * enable useOptimisticSearchParams to react to those changes. * * Note: this is actually required in React Router frameworks to follow Link navigations. */ patchHistory(emitter, adapter); return { NuqsAdapter: createAdapterProvider(useNuqsReactRouterBasedAdapter), useOptimisticSearchParams }; } //#endregion export { createReactRouterBasedAdapter as t }; //# sourceMappingURL=react-router-Ed4tAlvc.js.map