one
Version:
One is a new React Framework that makes Vite serve both native and web.
54 lines (53 loc) • 2.15 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: !0 });
}, __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
var useMatches_exports = {};
__export(useMatches_exports, {
setClientMatches: () => setClientMatches,
useMatch: () => useMatch,
useMatches: () => useMatches,
usePageMatch: () => usePageMatch
});
module.exports = __toCommonJS(useMatches_exports);
var import_react = require("react"), import_one_server_only = require("./vite/one-server-only");
let clientMatches = [];
const clientMatchesListeners = /* @__PURE__ */ new Set();
function subscribeToClientMatches(callback) {
return clientMatchesListeners.add(callback), () => clientMatchesListeners.delete(callback);
}
function getClientMatchesSnapshot() {
return clientMatches;
}
function setClientMatches(matches) {
clientMatches = matches;
for (const listener of clientMatchesListeners)
listener();
}
function useMatches() {
const serverContext = (0, import_one_server_only.useServerContext)();
return process.env.VITE_ENVIRONMENT === "ssr" ? serverContext?.matches ?? [] : (0, import_react.useSyncExternalStore)(
subscribeToClientMatches,
getClientMatchesSnapshot,
// server snapshot for hydration - must match what SSR rendered
() => serverContext?.matches ?? []
);
}
function useMatch(routeId) {
return useMatches().find((m) => m.routeId === routeId);
}
function usePageMatch() {
const matches = useMatches();
return matches[matches.length - 1];
}
//# sourceMappingURL=useMatches.js.map