@tamagui/create-context 1.116.1 → 1.116.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,103 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: !0 });
9
+ }, __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from == "object" || typeof from == "function")
11
+ for (let key of __getOwnPropNames(from))
12
+ !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ return to;
14
+ };
15
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
16
+ // If the importer is in node compatibility mode or this is not an ESM
17
+ // file that has been converted to a CommonJS file using a Babel-
18
+ // compatible transform (i.e. "__esModule" has not been set), then set
19
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
21
+ mod
22
+ )), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
23
+ var create_context_exports = {};
24
+ __export(create_context_exports, {
25
+ createContext: () => createContext,
26
+ createContextScope: () => createContextScope
27
+ });
28
+ module.exports = __toCommonJS(create_context_exports);
29
+ var React = __toESM(require("react")), import_jsx_runtime = require("react/jsx-runtime");
30
+ function createContext(rootComponentName, defaultContext) {
31
+ const Context = React.createContext(defaultContext);
32
+ function Provider(props) {
33
+ const { children, ...context } = props, value = React.useMemo(() => context, Object.values(context));
34
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
35
+ }
36
+ function useContext(consumerName) {
37
+ const context = React.useContext(Context);
38
+ if (context) return context;
39
+ if (defaultContext !== void 0) return defaultContext;
40
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
41
+ }
42
+ return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
43
+ }
44
+ function createContextScope(scopeName, createContextScopeDeps = []) {
45
+ let defaultContexts = [];
46
+ function createContext2(rootComponentName, defaultContext) {
47
+ const BaseContext = React.createContext(defaultContext), index = defaultContexts.length;
48
+ defaultContexts = [...defaultContexts, defaultContext];
49
+ function Provider(props) {
50
+ const { scope, children, ...context } = props, Context = scope?.[scopeName]?.[index] || BaseContext, value = React.useMemo(
51
+ () => context,
52
+ Object.values(context)
53
+ );
54
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
55
+ }
56
+ function useContext(consumerName, scope, options) {
57
+ const Context = scope?.[scopeName]?.[index] || BaseContext, context = React.useContext(Context);
58
+ if (context) return context;
59
+ if (defaultContext !== void 0) return defaultContext;
60
+ const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
61
+ if (options?.fallback)
62
+ return options?.warn !== !1 && console.warn(missingContextMessage), options.fallback;
63
+ throw new Error(missingContextMessage);
64
+ }
65
+ return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
66
+ }
67
+ const createScope = () => {
68
+ const scopeContexts = defaultContexts.map((defaultContext) => React.createContext(defaultContext));
69
+ return function(scope) {
70
+ const contexts = scope?.[scopeName] || scopeContexts;
71
+ return React.useMemo(
72
+ () => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
73
+ [scope, contexts]
74
+ );
75
+ };
76
+ };
77
+ return createScope.scopeName = scopeName, [
78
+ createContext2,
79
+ composeContextScopes(createScope, ...createContextScopeDeps)
80
+ ];
81
+ }
82
+ function composeContextScopes(...scopes) {
83
+ const baseScope = scopes[0];
84
+ if (scopes.length === 1) return baseScope;
85
+ const createScope = () => {
86
+ const scopeHooks = scopes.map((createScope2) => ({
87
+ useScope: createScope2(),
88
+ scopeName: createScope2.scopeName
89
+ }));
90
+ return function(overrideScopes) {
91
+ const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
92
+ const currentScope = useScope(overrideScopes)[`__scope${scopeName}`];
93
+ return { ...nextScopes2, ...currentScope };
94
+ }, {});
95
+ return React.useMemo(
96
+ () => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),
97
+ [nextScopes]
98
+ );
99
+ };
100
+ };
101
+ return createScope.scopeName = baseScope.scopeName, createScope;
102
+ }
103
+ //# sourceMappingURL=create-context.js.map
@@ -0,0 +1,15 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from == "object" || typeof from == "function")
7
+ for (let key of __getOwnPropNames(from))
8
+ !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
9
+ return to;
10
+ }, __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
11
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
12
+ var src_exports = {};
13
+ module.exports = __toCommonJS(src_exports);
14
+ __reExport(src_exports, require("./create-context"), module.exports);
15
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/create-context",
3
- "version": "1.116.1",
3
+ "version": "1.116.2",
4
4
  "sideEffects": false,
5
5
  "source": "src/index.ts",
6
6
  "types": "./types/index.d.ts",
@@ -32,7 +32,7 @@
32
32
  }
33
33
  },
34
34
  "devDependencies": {
35
- "@tamagui/build": "1.116.1",
35
+ "@tamagui/build": "1.116.2",
36
36
  "react": "^18.2.0 || ^19.0.0"
37
37
  },
38
38
  "publishConfig": {
File without changes