one
Version:
One is a new React Framework that makes Vite serve both native and web.
94 lines (93 loc) • 3.27 kB
JavaScript
import React, { createContext, useContext } from "react";
import { router } from "./router/imperative-api.mjs";
import { RouteParamsContext, useRouteNode } from "./router/Route.mjs";
import { RouteInfoContext } from "./router/RouteInfoContext.mjs";
import { navigationRef, useStoreRootState, useStoreRouteInfo } from "./router/router.mjs";
import { jsx } from "react/jsx-runtime";
function useRootNavigationState() {
return useStoreRootState();
}
function useRouteInfo() {
const routeInfoFromContext = useContext(RouteInfoContext),
routeInfoFromRootState = useStoreRouteInfo();
return useRouteNode()?.type === "layout" ? routeInfoFromRootState : routeInfoFromContext || routeInfoFromRootState;
}
function useNavigationContainerRef() {
return navigationRef;
}
const FrozeContext = createContext(!1);
function Frozen({
on = !1,
children
}) {
return typeof window > "u" ? children : /* @__PURE__ */jsx(FrozeContext.Provider, {
value: on,
children: /* @__PURE__ */jsx("div", {
...(on && {
inert: !0
}),
style: {
display: "contents"
},
children
})
});
}
function useRouter() {
return router;
}
function useUnstableGlobalHref() {
return useRouteInfo().unstable_globalHref;
}
function useSegments() {
return useRouteInfo().segments;
}
function usePathname() {
return useRouteInfo().pathname;
}
function useActiveParams() {
return useRouteInfo().params;
}
const useLocalSearchParams = useParams,
useGlobalSearchParams = useActiveParams;
function useParams() {
const params = React.useContext(RouteParamsContext) ?? {};
return React.useMemo(() => Object.fromEntries(Object.entries(params).filter(([_, value]) => value !== void 0).map(([key, value]) => {
if (Array.isArray(value)) return [key, value.map(v => {
try {
return decodeURIComponent(v);
} catch {
return v;
}
})];
try {
return [key, decodeURIComponent(value)];
} catch {
return [key, value];
}
})), [params]);
}
class ReadOnlyURLSearchParams extends URLSearchParams {
set(_name, _value) {
throw new Error("useSearchParams returns a read-only URLSearchParams object");
}
append(_name, _value) {
throw new Error("useSearchParams returns a read-only URLSearchParams object");
}
delete(_name, _value) {
throw new Error("useSearchParams returns a read-only URLSearchParams object");
}
}
function useSearchParams({
global = !1
} = {}) {
const globalRef = React.useRef(global);
process.env.NODE_ENV !== "production" && global !== globalRef.current && console.warn("useSearchParams: the 'global' option cannot change between renders");
const params = global ? useActiveParams() : useParams();
return React.useMemo(() => {
const entries = Object.entries(params).flatMap(([key, value]) => value === void 0 ? [] : Array.isArray(value) ? value.map(v => [key, String(v)]) : [[key, String(value)]]);
return new ReadOnlyURLSearchParams(entries);
}, [params]);
}
export { Frozen, useActiveParams, useGlobalSearchParams, useLocalSearchParams, useNavigationContainerRef, useParams, usePathname, useRootNavigationState, useRouteInfo, useRouter, useSearchParams, useSegments, useUnstableGlobalHref };
//# sourceMappingURL=hooks.mjs.map