UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

247 lines (244 loc) 8.83 kB
import React, { createContext, useContext } from "react"; import { router } from "./router/imperative-api.native.js"; import { RouteParamsContext, useRouteNode } from "./router/Route.native.js"; import { RouteInfoContext } from "./router/RouteInfoContext.native.js"; import { navigationRef, useStoreRootState, useStoreRouteInfo } from "./router/router.native.js"; import { getServerContext } from "./vite/one-server-only.native.js"; function _assert_this_initialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _call_super(_this, derived, args) { derived = _get_prototype_of(derived); return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args)); } function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _construct(Parent, args, Class) { if (_is_native_reflect_construct()) { _construct = Reflect.construct; } else { _construct = function construct(Parent2, args2, Class2) { var a = [null]; a.push.apply(a, args2); var Constructor = Function.bind.apply(Parent2, a); var instance = new Constructor(); if (Class2) _set_prototype_of(instance, Class2.prototype); return instance; }; } return _construct.apply(null, arguments); } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _get_prototype_of(o) { _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o2) { return o2.__proto__ || Object.getPrototypeOf(o2); }; return _get_prototype_of(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _set_prototype_of(subClass, superClass); } function _is_native_function(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; } function _possible_constructor_return(self, call) { if (call && (_type_of(call) === "object" || typeof call === "function")) { return call; } return _assert_this_initialized(self); } function _set_prototype_of(o, p) { _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o2, p2) { o2.__proto__ = p2; return o2; }; return _set_prototype_of(o, p); } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _wrap_native_super(Class) { var _cache = typeof Map === "function" ? /* @__PURE__ */new Map() : void 0; _wrap_native_super = function wrapNativeSuper(Class2) { if (Class2 === null || !_is_native_function(Class2)) return Class2; if (typeof Class2 !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class2)) return _cache.get(Class2); _cache.set(Class2, Wrapper); } function Wrapper() { return _construct(Class2, arguments, _get_prototype_of(this).constructor); } Wrapper.prototype = Object.create(Class2.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _set_prototype_of(Wrapper, Class2); }; return _wrap_native_super(Class); } function _is_native_reflect_construct() { try { var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (_) {} return (_is_native_reflect_construct = function () { return !!result; })(); } function useRootNavigationState() { return useStoreRootState(); } function useRouteInfo() { var routeInfoFromContext = useContext(RouteInfoContext); var routeInfoFromRootState = useStoreRouteInfo(); var routeNode = useRouteNode(); if ((routeNode === null || routeNode === void 0 ? void 0 : routeNode.type) === "layout") { return routeInfoFromRootState; } return routeInfoFromContext || routeInfoFromRootState; } function useNavigationContainerRef() { return navigationRef; } var FrozeContext = /* @__PURE__ */createContext(false); function useRouter() { return router; } function useUnstableGlobalHref() { return useRouteInfo().unstable_globalHref; } function useSegments() { return useRouteInfo().segments; } function usePathname() { var routeInfoPathname = useRouteInfo().pathname; if (import.meta.env.SSR) { try { var _ctx_loaderProps; var ctx = getServerContext(); if (ctx === null || ctx === void 0 ? void 0 : (_ctx_loaderProps = ctx.loaderProps) === null || _ctx_loaderProps === void 0 ? void 0 : _ctx_loaderProps.path) { return stripTrailingSlash(ctx.loaderProps.path); } } catch (unused) {} } return stripTrailingSlash(routeInfoPathname); } function stripTrailingSlash(path) { return path.length > 1 && path.endsWith("/") ? path.slice(0, -1) : path; } function useActiveParams() { return useRouteInfo().params; } var useLocalSearchParams = useParams; var useGlobalSearchParams = useActiveParams; function useParams() { var _React_useContext; var params = (_React_useContext = React.useContext(RouteParamsContext)) !== null && _React_useContext !== void 0 ? _React_useContext : {}; return React.useMemo(function () { return Object.fromEntries(Object.entries(params).filter(function (param) { var [_, value] = param; return value !== void 0; }).map(function (param) { var [key, value] = param; if (Array.isArray(value)) { return [key, value.map(function (v) { try { return decodeURIComponent(v); } catch (unused) { return v; } })]; } try { return [key, decodeURIComponent(value)]; } catch (unused) { return [key, value]; } })); }, [params]); } var ReadOnlyURLSearchParams = /* @__PURE__ */function (URLSearchParams1) { "use strict"; _inherits(ReadOnlyURLSearchParams2, URLSearchParams1); function ReadOnlyURLSearchParams2() { _class_call_check(this, ReadOnlyURLSearchParams2); return _call_super(this, ReadOnlyURLSearchParams2, arguments); } _create_class(ReadOnlyURLSearchParams2, [{ key: "set", value: function set(_name, _value) { throw new Error("useSearchParams returns a read-only URLSearchParams object"); } }, { key: "append", value: function append(_name, _value) { throw new Error("useSearchParams returns a read-only URLSearchParams object"); } }, { key: "delete", value: function _delete(_name, _value) { throw new Error("useSearchParams returns a read-only URLSearchParams object"); } }]); return ReadOnlyURLSearchParams2; }(_wrap_native_super(URLSearchParams)); function useSearchParams() { var { global = false } = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}; var globalRef = React.useRef(global); if (process.env.NODE_ENV !== "production") { if (global !== globalRef.current) { console.warn("useSearchParams: the 'global' option cannot change between renders"); } } var params = global ? useActiveParams() : useParams(); return React.useMemo(function () { var entries = Object.entries(params).flatMap(function (param) { var [key, value] = param; if (value === void 0) return []; return Array.isArray(value) ? value.map(function (v) { return [key, String(v)]; }) : [[key, String(value)]]; }); return new ReadOnlyURLSearchParams(entries); }, [params]); } export { FrozeContext, useActiveParams, useGlobalSearchParams, useLocalSearchParams, useNavigationContainerRef, useParams, usePathname, useRootNavigationState, useRouteInfo, useRouter, useSearchParams, useSegments, useUnstableGlobalHref }; //# sourceMappingURL=hooks.native.js.map