one
Version:
One is a new React Framework that makes Vite serve both native and web.
299 lines (295 loc) • 10.9 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var hooks_exports = {};
__export(hooks_exports, {
FrozeContext: () => FrozeContext,
useActiveParams: () => useActiveParams,
useGlobalSearchParams: () => useGlobalSearchParams,
useLocalSearchParams: () => useLocalSearchParams,
useNavigationContainerRef: () => useNavigationContainerRef,
useParams: () => useParams,
usePathname: () => usePathname,
useRootNavigationState: () => useRootNavigationState,
useRouteInfo: () => useRouteInfo,
useRouter: () => useRouter,
useSearchParams: () => useSearchParams,
useSegments: () => useSegments,
useUnstableGlobalHref: () => useUnstableGlobalHref
});
module.exports = __toCommonJS(hooks_exports);
var import_react = __toESM(require("react"), 1);
var import_imperative_api = require("./router/imperative-api.native.js");
var import_Route = require("./router/Route.native.js");
var import_RouteInfoContext = require("./router/RouteInfoContext.native.js");
var import_router = require("./router/router.native.js");
var import_one_server_only = require("./vite/one-server-only.native.js");
const import_meta = {};
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 (0, import_router.useStoreRootState)();
}
function useRouteInfo() {
var routeInfoFromContext = (0, import_react.useContext)(import_RouteInfoContext.RouteInfoContext);
var routeInfoFromRootState = (0, import_router.useStoreRouteInfo)();
var routeNode = (0, import_Route.useRouteNode)();
if ((routeNode === null || routeNode === void 0 ? void 0 : routeNode.type) === "layout") {
return routeInfoFromRootState;
}
return routeInfoFromContext || routeInfoFromRootState;
}
function useNavigationContainerRef() {
return import_router.navigationRef;
}
var FrozeContext = /* @__PURE__ */(0, import_react.createContext)(false);
function useRouter() {
return import_imperative_api.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 = (0, import_one_server_only.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 = import_react.default.useContext(import_Route.RouteParamsContext)) !== null && _React_useContext !== void 0 ? _React_useContext : {};
return import_react.default.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 = import_react.default.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 import_react.default.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]);
}
//# sourceMappingURL=hooks.native.js.map