@tamagui/create-context 1.61.3 → 1.62.1
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.
- package/dist/cjs/create-context.js +24 -48
- package/dist/cjs/create-context.js.map +1 -1
- package/dist/cjs/create-context.native.js +114 -0
- package/dist/cjs/create-context.native.js.map +6 -0
- package/dist/cjs/index.js +4 -8
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/index.native.js +20 -0
- package/dist/cjs/index.native.js.map +6 -0
- package/dist/esm/create-context.js +17 -34
- package/dist/esm/create-context.js.map +1 -1
- package/dist/jsx/create-context.js +15 -32
- package/dist/jsx/create-context.js.map +1 -1
- package/dist/jsx/create-context.native.js +84 -0
- package/dist/jsx/create-context.native.js.map +6 -0
- package/dist/jsx/index.native.js +2 -0
- package/dist/jsx/index.native.js.map +6 -0
- package/package.json +2 -2
|
@@ -1,20 +1,15 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable:
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
9
|
+
}, __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from == "object" || typeof from == "function")
|
|
14
11
|
for (let key of __getOwnPropNames(from))
|
|
15
|
-
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
12
|
+
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
13
|
return to;
|
|
19
14
|
};
|
|
20
15
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
@@ -22,23 +17,20 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
22
17
|
// file that has been converted to a CommonJS file using a Babel-
|
|
23
18
|
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
19
|
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable:
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
|
|
26
21
|
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
|
+
)), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
29
23
|
var create_context_exports = {};
|
|
30
24
|
__export(create_context_exports, {
|
|
31
25
|
createContext: () => createContext,
|
|
32
26
|
createContextScope: () => createContextScope
|
|
33
27
|
});
|
|
34
28
|
module.exports = __toCommonJS(create_context_exports);
|
|
35
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
36
|
-
var React = __toESM(require("react"));
|
|
29
|
+
var React = __toESM(require("react")), import_jsx_runtime = require("react/jsx-runtime");
|
|
37
30
|
function createContext(rootComponentName, defaultContext) {
|
|
38
31
|
const Context = React.createContext(defaultContext);
|
|
39
32
|
function Provider(props) {
|
|
40
|
-
const { children, ...context } = props;
|
|
41
|
-
const value = React.useMemo(() => context, Object.values(context));
|
|
33
|
+
const { children, ...context } = props, value = React.useMemo(() => context, Object.values(context));
|
|
42
34
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
|
|
43
35
|
}
|
|
44
36
|
function useContext(consumerName) {
|
|
@@ -49,58 +41,44 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
49
41
|
return defaultContext;
|
|
50
42
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
51
43
|
}
|
|
52
|
-
Provider.displayName = `${rootComponentName}Provider
|
|
53
|
-
return [Provider, useContext];
|
|
44
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
54
45
|
}
|
|
55
46
|
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
56
47
|
let defaultContexts = [];
|
|
57
48
|
function createContext2(rootComponentName, defaultContext) {
|
|
58
|
-
const BaseContext = React.createContext(defaultContext);
|
|
59
|
-
const index = defaultContexts.length;
|
|
49
|
+
const BaseContext = React.createContext(defaultContext), index = defaultContexts.length;
|
|
60
50
|
defaultContexts = [...defaultContexts, defaultContext];
|
|
61
51
|
function Provider(props) {
|
|
62
|
-
const { scope, children, ...context } = props
|
|
63
|
-
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
64
|
-
const value = React.useMemo(
|
|
52
|
+
const { scope, children, ...context } = props, Context = scope?.[scopeName][index] || BaseContext, value = React.useMemo(
|
|
65
53
|
() => context,
|
|
66
54
|
Object.values(context)
|
|
67
55
|
);
|
|
68
56
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
|
|
69
57
|
}
|
|
70
58
|
function useContext(consumerName, scope, options) {
|
|
71
|
-
const Context =
|
|
72
|
-
const context = React.useContext(Context);
|
|
59
|
+
const Context = scope?.[scopeName][index] || BaseContext, context = React.useContext(Context);
|
|
73
60
|
if (context)
|
|
74
61
|
return context;
|
|
75
62
|
if (defaultContext !== void 0)
|
|
76
63
|
return defaultContext;
|
|
77
64
|
const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
|
|
78
|
-
if (options
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
return options.fallback;
|
|
83
|
-
} else {
|
|
84
|
-
throw new Error(missingContextMessage);
|
|
85
|
-
}
|
|
65
|
+
if (options?.fallback)
|
|
66
|
+
return options?.warn !== !1 && console.warn(missingContextMessage), options.fallback;
|
|
67
|
+
throw new Error(missingContextMessage);
|
|
86
68
|
}
|
|
87
|
-
Provider.displayName = `${rootComponentName}Provider
|
|
88
|
-
return [Provider, useContext];
|
|
69
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
89
70
|
}
|
|
90
71
|
const createScope = () => {
|
|
91
|
-
const scopeContexts = defaultContexts.map((defaultContext) =>
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return function useScope(scope) {
|
|
95
|
-
const contexts = (scope == null ? void 0 : scope[scopeName]) || scopeContexts;
|
|
72
|
+
const scopeContexts = defaultContexts.map((defaultContext) => React.createContext(defaultContext));
|
|
73
|
+
return function(scope) {
|
|
74
|
+
const contexts = scope?.[scopeName] || scopeContexts;
|
|
96
75
|
return React.useMemo(
|
|
97
76
|
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
98
77
|
[scope, contexts]
|
|
99
78
|
);
|
|
100
79
|
};
|
|
101
80
|
};
|
|
102
|
-
createScope.scopeName = scopeName
|
|
103
|
-
return [
|
|
81
|
+
return createScope.scopeName = scopeName, [
|
|
104
82
|
createContext2,
|
|
105
83
|
composeContextScopes(createScope, ...createContextScopeDeps)
|
|
106
84
|
];
|
|
@@ -114,10 +92,9 @@ function composeContextScopes(...scopes) {
|
|
|
114
92
|
useScope: createScope2(),
|
|
115
93
|
scopeName: createScope2.scopeName
|
|
116
94
|
}));
|
|
117
|
-
return function
|
|
95
|
+
return function(overrideScopes) {
|
|
118
96
|
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
119
|
-
const
|
|
120
|
-
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
97
|
+
const currentScope = useScope(overrideScopes)[`__scope${scopeName}`];
|
|
121
98
|
return { ...nextScopes2, ...currentScope };
|
|
122
99
|
}, {});
|
|
123
100
|
return React.useMemo(
|
|
@@ -126,8 +103,7 @@ function composeContextScopes(...scopes) {
|
|
|
126
103
|
);
|
|
127
104
|
};
|
|
128
105
|
};
|
|
129
|
-
createScope.scopeName = baseScope.scopeName;
|
|
130
|
-
return createScope;
|
|
106
|
+
return createScope.scopeName = baseScope.scopeName, createScope;
|
|
131
107
|
}
|
|
132
108
|
// Annotate the CommonJS export names for ESM import in node:
|
|
133
109
|
0 && (module.exports = {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/create-context.tsx"],
|
|
4
|
-
"mappings": "
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,YAAuB,2BAeZ;AAXJ,SAAS,cACd,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UAAM,EAAE,UAAU,GAAG,QAAQ,IAAI,OAG3B,QAAQ,MAAM,QAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,WAAO,4CAAC,QAAQ,UAAR,EAAiB,OAAe,UAAS;AAAA,EACnD;AAEA,WAAS,WAAW,cAA0D;AAC5E,UAAM,UAAU,MAAM,WAAW,OAAO;AACxC,QAAI;AAAS,aAAO;AACpB,QAAI,mBAAmB;AAAW,aAAO;AAEzC,UAAM,IAAI,MAAM,KAAK,YAAY,4BAA4B,iBAAiB,IAAI;AAAA,EACpF;AAEA,kBAAS,cAAc,GAAG,iBAAiB,YACpC,CAAC,UAAU,UAAU;AAC9B;AAeO,SAAS,mBACd,WACA,yBAAwC,CAAC,GACzC;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aAAa,GAAG,sBAAsB;AAAA,EAC7D;AACF;AAMA,SAAS,wBAAwB,QAAuB;AACtD,QAAM,YAAY,OAAO,CAAC;AAC1B,MAAI,OAAO,WAAW;AAAG,WAAO;AAEhC,QAAM,cAA2B,MAAM;AACrC,UAAM,aAAa,OAAO,IAAI,CAACC,kBAAiB;AAAA,MAC9C,UAAUA,aAAY;AAAA,MACtB,WAAWA,aAAY;AAAA,IACzB,EAAE;AAEF,WAAO,SAA2B,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAACC,aAAY,EAAE,UAAU,UAAU,MAAM;AAK5E,cAAM,eADa,SAAS,cAAc,EACV,UAAU,SAAS,EAAE;AACrD,eAAO,EAAE,GAAGA,aAAY,GAAG,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,UAAU,SAAS,EAAE,GAAG,WAAW;AAAA,QACvD,CAAC,UAAU;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAEA,qBAAY,YAAY,UAAU,WAC3B;AACT;",
|
|
5
5
|
"names": ["createContext", "createScope", "nextScopes"]
|
|
6
6
|
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
10
|
+
}, __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from == "object" || typeof from == "function")
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
|
|
22
|
+
mod
|
|
23
|
+
)), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
24
|
+
var create_context_exports = {};
|
|
25
|
+
__export(create_context_exports, {
|
|
26
|
+
createContext: () => createContext,
|
|
27
|
+
createContextScope: () => createContextScope
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(create_context_exports);
|
|
30
|
+
var React = __toESM(require("react")), import_jsx_runtime = require("react/jsx-runtime");
|
|
31
|
+
function createContext(rootComponentName, defaultContext) {
|
|
32
|
+
const Context = React.createContext(defaultContext);
|
|
33
|
+
function Provider(props) {
|
|
34
|
+
const { children, ...context } = props, value = React.useMemo(() => context, Object.values(context));
|
|
35
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
|
|
36
|
+
}
|
|
37
|
+
function useContext(consumerName) {
|
|
38
|
+
const context = React.useContext(Context);
|
|
39
|
+
if (context)
|
|
40
|
+
return context;
|
|
41
|
+
if (defaultContext !== void 0)
|
|
42
|
+
return defaultContext;
|
|
43
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
44
|
+
}
|
|
45
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
46
|
+
}
|
|
47
|
+
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
48
|
+
let defaultContexts = [];
|
|
49
|
+
function createContext2(rootComponentName, defaultContext) {
|
|
50
|
+
const BaseContext = React.createContext(defaultContext), index = defaultContexts.length;
|
|
51
|
+
defaultContexts = [...defaultContexts, defaultContext];
|
|
52
|
+
function Provider(props) {
|
|
53
|
+
const { scope, children, ...context } = props, Context = scope?.[scopeName][index] || BaseContext, value = React.useMemo(
|
|
54
|
+
() => context,
|
|
55
|
+
Object.values(context)
|
|
56
|
+
);
|
|
57
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
|
|
58
|
+
}
|
|
59
|
+
function useContext(consumerName, scope, options) {
|
|
60
|
+
const Context = scope?.[scopeName][index] || BaseContext, context = React.useContext(Context);
|
|
61
|
+
if (context)
|
|
62
|
+
return context;
|
|
63
|
+
if (defaultContext !== void 0)
|
|
64
|
+
return defaultContext;
|
|
65
|
+
const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
|
|
66
|
+
if (options?.fallback)
|
|
67
|
+
return options?.warn !== !1 && console.warn(missingContextMessage), options.fallback;
|
|
68
|
+
throw new Error(missingContextMessage);
|
|
69
|
+
}
|
|
70
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
71
|
+
}
|
|
72
|
+
const createScope = () => {
|
|
73
|
+
const scopeContexts = defaultContexts.map((defaultContext) => React.createContext(defaultContext));
|
|
74
|
+
return function(scope) {
|
|
75
|
+
const contexts = scope?.[scopeName] || scopeContexts;
|
|
76
|
+
return React.useMemo(
|
|
77
|
+
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
78
|
+
[scope, contexts]
|
|
79
|
+
);
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
return createScope.scopeName = scopeName, [
|
|
83
|
+
createContext2,
|
|
84
|
+
composeContextScopes(createScope, ...createContextScopeDeps)
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
function composeContextScopes(...scopes) {
|
|
88
|
+
const baseScope = scopes[0];
|
|
89
|
+
if (scopes.length === 1)
|
|
90
|
+
return baseScope;
|
|
91
|
+
const createScope = () => {
|
|
92
|
+
const scopeHooks = scopes.map((createScope2) => ({
|
|
93
|
+
useScope: createScope2(),
|
|
94
|
+
scopeName: createScope2.scopeName
|
|
95
|
+
}));
|
|
96
|
+
return function(overrideScopes) {
|
|
97
|
+
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
98
|
+
const currentScope = useScope(overrideScopes)[`__scope${scopeName}`];
|
|
99
|
+
return { ...nextScopes2, ...currentScope };
|
|
100
|
+
}, {});
|
|
101
|
+
return React.useMemo(
|
|
102
|
+
() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),
|
|
103
|
+
[nextScopes]
|
|
104
|
+
);
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
return createScope.scopeName = baseScope.scopeName, createScope;
|
|
108
|
+
}
|
|
109
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
110
|
+
0 && (module.exports = {
|
|
111
|
+
createContext,
|
|
112
|
+
createContextScope
|
|
113
|
+
});
|
|
114
|
+
//# sourceMappingURL=create-context.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/create-context.tsx"],
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,YAAuB,2BAeZ;AAXJ,SAAS,cACd,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UAAM,EAAE,UAAU,GAAG,QAAQ,IAAI,OAG3B,QAAQ,MAAM,QAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,WAAO,4CAAC,QAAQ,UAAR,EAAiB,OAAe,UAAS;AAAA,EACnD;AAEA,WAAS,WA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,OAAc;AACrC,YAAM,WAAW,QAAQ,SAAS,KAAK;AACvC,aAAO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,SAAS,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,EAAE;AAAA,QACtE,CAAC,OAAO,QAAQ;AAAA,MAClB;AAAA,IACF;AAAA,EACF;AAEA,qBAAY,YAAY,WACjB;AAAA,IACLA;AAAA,IACA,qBAAqB,aAAa,GAAG,sBAAsB;AAAA,EAC7D;AACF;AAMA,SAAS,wBAAwB,QAAuB;AACtD,QAAM,YAAY,OAAO,CAAC;AAC1B,MAAI,OAAO,WAAW;AAAG,WAAO;AAEhC,QAAM,cAA2B,MAAM;AACrC,UAAM,aAAa,OAAO,IAAI,CAACC,kBAAiB;AAAA,MAC9C,UAAUA,aAAY;AAAA,MACtB,WAAWA,aAAY;AAAA,IACzB,EAAE;AAEF,WAAO,SAA2B,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAACC,aAAY,EAAE,UAAU,UAAU,MAAM;AAK5E,cAAM,eADa,SAAS,cAAc,EACV,UAAU,SAAS,EAAE;AACrD,eAAO,EAAE,GAAGA,aAAY,GAAG,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,UAAU,SAAS,EAAE,GAAG,WAAW;AAAA,QACvD,CAAC,UAAU;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAEA,qBAAY,YAAY,UAAU,WAC3B;AACT;",
|
|
5
|
+
"names": ["createContext", "createScope", "nextScopes"]
|
|
6
|
+
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __defProp = Object.defineProperty;
|
|
3
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
5
|
var __copyProps = (to, from, except, desc) => {
|
|
7
|
-
if (from && typeof from
|
|
6
|
+
if (from && typeof from == "object" || typeof from == "function")
|
|
8
7
|
for (let key of __getOwnPropNames(from))
|
|
9
|
-
|
|
10
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
-
}
|
|
8
|
+
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
12
9
|
return to;
|
|
13
|
-
};
|
|
14
|
-
var
|
|
15
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
10
|
+
}, __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
11
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
16
12
|
var src_exports = {};
|
|
17
13
|
module.exports = __toCommonJS(src_exports);
|
|
18
14
|
__reExport(src_exports, require("./create-context"), module.exports);
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from == "object" || typeof from == "function")
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
return to;
|
|
11
|
+
}, __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
12
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
13
|
+
var src_exports = {};
|
|
14
|
+
module.exports = __toCommonJS(src_exports);
|
|
15
|
+
__reExport(src_exports, require("./create-context"), module.exports);
|
|
16
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
17
|
+
0 && (module.exports = {
|
|
18
|
+
...require("./create-context")
|
|
19
|
+
});
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { jsx } from "react/jsx-runtime";
|
|
2
1
|
import * as React from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
3
|
function createContext(rootComponentName, defaultContext) {
|
|
4
4
|
const Context = React.createContext(defaultContext);
|
|
5
5
|
function Provider(props) {
|
|
6
|
-
const { children, ...context } = props;
|
|
7
|
-
const value = React.useMemo(() => context, Object.values(context));
|
|
6
|
+
const { children, ...context } = props, value = React.useMemo(() => context, Object.values(context));
|
|
8
7
|
return /* @__PURE__ */ jsx(Context.Provider, { value, children });
|
|
9
8
|
}
|
|
10
9
|
function useContext(consumerName) {
|
|
@@ -15,58 +14,44 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
15
14
|
return defaultContext;
|
|
16
15
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
17
16
|
}
|
|
18
|
-
Provider.displayName = `${rootComponentName}Provider
|
|
19
|
-
return [Provider, useContext];
|
|
17
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
20
18
|
}
|
|
21
19
|
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
22
20
|
let defaultContexts = [];
|
|
23
21
|
function createContext2(rootComponentName, defaultContext) {
|
|
24
|
-
const BaseContext = React.createContext(defaultContext);
|
|
25
|
-
const index = defaultContexts.length;
|
|
22
|
+
const BaseContext = React.createContext(defaultContext), index = defaultContexts.length;
|
|
26
23
|
defaultContexts = [...defaultContexts, defaultContext];
|
|
27
24
|
function Provider(props) {
|
|
28
|
-
const { scope, children, ...context } = props
|
|
29
|
-
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
30
|
-
const value = React.useMemo(
|
|
25
|
+
const { scope, children, ...context } = props, Context = scope?.[scopeName][index] || BaseContext, value = React.useMemo(
|
|
31
26
|
() => context,
|
|
32
27
|
Object.values(context)
|
|
33
28
|
);
|
|
34
29
|
return /* @__PURE__ */ jsx(Context.Provider, { value, children });
|
|
35
30
|
}
|
|
36
31
|
function useContext(consumerName, scope, options) {
|
|
37
|
-
const Context =
|
|
38
|
-
const context = React.useContext(Context);
|
|
32
|
+
const Context = scope?.[scopeName][index] || BaseContext, context = React.useContext(Context);
|
|
39
33
|
if (context)
|
|
40
34
|
return context;
|
|
41
35
|
if (defaultContext !== void 0)
|
|
42
36
|
return defaultContext;
|
|
43
37
|
const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
|
|
44
|
-
if (options
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
return options.fallback;
|
|
49
|
-
} else {
|
|
50
|
-
throw new Error(missingContextMessage);
|
|
51
|
-
}
|
|
38
|
+
if (options?.fallback)
|
|
39
|
+
return options?.warn !== !1 && console.warn(missingContextMessage), options.fallback;
|
|
40
|
+
throw new Error(missingContextMessage);
|
|
52
41
|
}
|
|
53
|
-
Provider.displayName = `${rootComponentName}Provider
|
|
54
|
-
return [Provider, useContext];
|
|
42
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
55
43
|
}
|
|
56
44
|
const createScope = () => {
|
|
57
|
-
const scopeContexts = defaultContexts.map((defaultContext) =>
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return function useScope(scope) {
|
|
61
|
-
const contexts = (scope == null ? void 0 : scope[scopeName]) || scopeContexts;
|
|
45
|
+
const scopeContexts = defaultContexts.map((defaultContext) => React.createContext(defaultContext));
|
|
46
|
+
return function(scope) {
|
|
47
|
+
const contexts = scope?.[scopeName] || scopeContexts;
|
|
62
48
|
return React.useMemo(
|
|
63
49
|
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
64
50
|
[scope, contexts]
|
|
65
51
|
);
|
|
66
52
|
};
|
|
67
53
|
};
|
|
68
|
-
createScope.scopeName = scopeName
|
|
69
|
-
return [
|
|
54
|
+
return createScope.scopeName = scopeName, [
|
|
70
55
|
createContext2,
|
|
71
56
|
composeContextScopes(createScope, ...createContextScopeDeps)
|
|
72
57
|
];
|
|
@@ -80,10 +65,9 @@ function composeContextScopes(...scopes) {
|
|
|
80
65
|
useScope: createScope2(),
|
|
81
66
|
scopeName: createScope2.scopeName
|
|
82
67
|
}));
|
|
83
|
-
return function
|
|
68
|
+
return function(overrideScopes) {
|
|
84
69
|
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
85
|
-
const
|
|
86
|
-
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
70
|
+
const currentScope = useScope(overrideScopes)[`__scope${scopeName}`];
|
|
87
71
|
return { ...nextScopes2, ...currentScope };
|
|
88
72
|
}, {});
|
|
89
73
|
return React.useMemo(
|
|
@@ -92,8 +76,7 @@ function composeContextScopes(...scopes) {
|
|
|
92
76
|
);
|
|
93
77
|
};
|
|
94
78
|
};
|
|
95
|
-
createScope.scopeName = baseScope.scopeName;
|
|
96
|
-
return createScope;
|
|
79
|
+
return createScope.scopeName = baseScope.scopeName, createScope;
|
|
97
80
|
}
|
|
98
81
|
export {
|
|
99
82
|
createContext,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/create-context.tsx"],
|
|
4
|
-
"mappings": "
|
|
4
|
+
"mappings": "AAGA,YAAY,WAAW;AAeZ;AAXJ,SAAS,cACd,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cA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|
}
|
|
@@ -2,8 +2,7 @@ import * as React from "react";
|
|
|
2
2
|
function createContext(rootComponentName, defaultContext) {
|
|
3
3
|
const Context = React.createContext(defaultContext);
|
|
4
4
|
function Provider(props) {
|
|
5
|
-
const { children, ...context } = props;
|
|
6
|
-
const value = React.useMemo(() => context, Object.values(context));
|
|
5
|
+
const { children, ...context } = props, value = React.useMemo(() => context, Object.values(context));
|
|
7
6
|
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
8
7
|
}
|
|
9
8
|
function useContext(consumerName) {
|
|
@@ -14,49 +13,36 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
14
13
|
return defaultContext;
|
|
15
14
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
16
15
|
}
|
|
17
|
-
Provider.displayName = `${rootComponentName}Provider
|
|
18
|
-
return [Provider, useContext];
|
|
16
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
19
17
|
}
|
|
20
18
|
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
21
19
|
let defaultContexts = [];
|
|
22
20
|
function createContext2(rootComponentName, defaultContext) {
|
|
23
|
-
const BaseContext = React.createContext(defaultContext);
|
|
24
|
-
const index = defaultContexts.length;
|
|
21
|
+
const BaseContext = React.createContext(defaultContext), index = defaultContexts.length;
|
|
25
22
|
defaultContexts = [...defaultContexts, defaultContext];
|
|
26
23
|
function Provider(props) {
|
|
27
|
-
const { scope, children, ...context } = props
|
|
28
|
-
const Context = scope?.[scopeName][index] || BaseContext;
|
|
29
|
-
const value = React.useMemo(
|
|
24
|
+
const { scope, children, ...context } = props, Context = scope?.[scopeName][index] || BaseContext, value = React.useMemo(
|
|
30
25
|
() => context,
|
|
31
26
|
Object.values(context)
|
|
32
27
|
);
|
|
33
28
|
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
34
29
|
}
|
|
35
30
|
function useContext(consumerName, scope, options) {
|
|
36
|
-
const Context = scope?.[scopeName][index] || BaseContext;
|
|
37
|
-
const context = React.useContext(Context);
|
|
31
|
+
const Context = scope?.[scopeName][index] || BaseContext, context = React.useContext(Context);
|
|
38
32
|
if (context)
|
|
39
33
|
return context;
|
|
40
34
|
if (defaultContext !== void 0)
|
|
41
35
|
return defaultContext;
|
|
42
36
|
const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
|
|
43
|
-
if (options?.fallback)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
return options.fallback;
|
|
48
|
-
} else {
|
|
49
|
-
throw new Error(missingContextMessage);
|
|
50
|
-
}
|
|
37
|
+
if (options?.fallback)
|
|
38
|
+
return options?.warn !== !1 && console.warn(missingContextMessage), options.fallback;
|
|
39
|
+
throw new Error(missingContextMessage);
|
|
51
40
|
}
|
|
52
|
-
Provider.displayName = `${rootComponentName}Provider
|
|
53
|
-
return [Provider, useContext];
|
|
41
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
54
42
|
}
|
|
55
43
|
const createScope = () => {
|
|
56
|
-
const scopeContexts = defaultContexts.map((defaultContext) =>
|
|
57
|
-
|
|
58
|
-
});
|
|
59
|
-
return function useScope(scope) {
|
|
44
|
+
const scopeContexts = defaultContexts.map((defaultContext) => React.createContext(defaultContext));
|
|
45
|
+
return function(scope) {
|
|
60
46
|
const contexts = scope?.[scopeName] || scopeContexts;
|
|
61
47
|
return React.useMemo(
|
|
62
48
|
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
@@ -64,8 +50,7 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
64
50
|
);
|
|
65
51
|
};
|
|
66
52
|
};
|
|
67
|
-
createScope.scopeName = scopeName
|
|
68
|
-
return [
|
|
53
|
+
return createScope.scopeName = scopeName, [
|
|
69
54
|
createContext2,
|
|
70
55
|
composeContextScopes(createScope, ...createContextScopeDeps)
|
|
71
56
|
];
|
|
@@ -79,10 +64,9 @@ function composeContextScopes(...scopes) {
|
|
|
79
64
|
useScope: createScope2(),
|
|
80
65
|
scopeName: createScope2.scopeName
|
|
81
66
|
}));
|
|
82
|
-
return function
|
|
67
|
+
return function(overrideScopes) {
|
|
83
68
|
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
84
|
-
const
|
|
85
|
-
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
69
|
+
const currentScope = useScope(overrideScopes)[`__scope${scopeName}`];
|
|
86
70
|
return { ...nextScopes2, ...currentScope };
|
|
87
71
|
}, {});
|
|
88
72
|
return React.useMemo(
|
|
@@ -91,8 +75,7 @@ function composeContextScopes(...scopes) {
|
|
|
91
75
|
);
|
|
92
76
|
};
|
|
93
77
|
};
|
|
94
|
-
createScope.scopeName = baseScope.scopeName;
|
|
95
|
-
return createScope;
|
|
78
|
+
return createScope.scopeName = baseScope.scopeName, createScope;
|
|
96
79
|
}
|
|
97
80
|
export {
|
|
98
81
|
createContext,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/create-context.tsx"],
|
|
4
|
-
"mappings": "AAGA,YAAY,WAAW;AAIhB,SAAS,cACd,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UAAM,EAAE,UAAU,GAAG,QAAQ,IAAI
|
|
4
|
+
"mappings": "AAGA,YAAY,WAAW;AAIhB,SAAS,cACd,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UAAM,EAAE,UAAU,GAAG,QAAQ,IAAI,OAG3B,QAAQ,MAAM,QAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,WAAO,CAAC,QAAQ,SAAS,OAAO,QAAQ,SAAS,EAAzC,QAAQ;AAAA,EAClB;AAEA,WAAS,WAAW,cAA0D;AAC5E,UAAM,UAAU,MAAM,WAA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AAQ,SAAS,KAAK;AACvC,aAAO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,SAAS,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,EAAE;AAAA,QACtE,CAAC,OAAO,QAAQ;AAAA,MAClB;AAAA,IACF;AAAA,EACF;AAEA,qBAAY,YAAY,WACjB;AAAA,IACLA;AAAA,IACA,qBAAqB,aAAa,GAAG,sBAAsB;AAAA,EAC7D;AACF;AAMA,SAAS,wBAAwB,QAAuB;AACtD,QAAM,YAAY,OAAO,CAAC;AAC1B,MAAI,OAAO,WAAW;AAAG,WAAO;AAEhC,QAAM,cAA2B,MAAM;AACrC,UAAM,aAAa,OAAO,IAAI,CAACC,kBAAiB;AAAA,MAC9C,UAAUA,aAAY;AAAA,MACtB,WAAWA,aAAY;AAAA,IACzB,EAAE;AAEF,WAAO,SAA2B,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAACC,aAAY,EAAE,UAAU,UAAU,MAAM;AAK5E,cAAM,eADa,SAAS,cAAc,EACV,UAAU,SAAS,EAAE;AACrD,eAAO,EAAE,GAAGA,aAAY,GAAG,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,UAAU,SAAS,EAAE,GAAG,WAAW;AAAA,QACvD,CAAC,UAAU;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAEA,qBAAY,YAAY,UAAU,WAC3B;AACT;",
|
|
5
5
|
"names": ["createContext", "createScope", "nextScopes"]
|
|
6
6
|
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
function createContext(rootComponentName, defaultContext) {
|
|
3
|
+
const Context = React.createContext(defaultContext);
|
|
4
|
+
function Provider(props) {
|
|
5
|
+
const { children, ...context } = props, value = React.useMemo(() => context, Object.values(context));
|
|
6
|
+
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
7
|
+
}
|
|
8
|
+
function useContext(consumerName) {
|
|
9
|
+
const context = React.useContext(Context);
|
|
10
|
+
if (context)
|
|
11
|
+
return context;
|
|
12
|
+
if (defaultContext !== void 0)
|
|
13
|
+
return defaultContext;
|
|
14
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
15
|
+
}
|
|
16
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
17
|
+
}
|
|
18
|
+
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
19
|
+
let defaultContexts = [];
|
|
20
|
+
function createContext2(rootComponentName, defaultContext) {
|
|
21
|
+
const BaseContext = React.createContext(defaultContext), index = defaultContexts.length;
|
|
22
|
+
defaultContexts = [...defaultContexts, defaultContext];
|
|
23
|
+
function Provider(props) {
|
|
24
|
+
const { scope, children, ...context } = props, Context = scope?.[scopeName][index] || BaseContext, value = React.useMemo(
|
|
25
|
+
() => context,
|
|
26
|
+
Object.values(context)
|
|
27
|
+
);
|
|
28
|
+
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
29
|
+
}
|
|
30
|
+
function useContext(consumerName, scope, options) {
|
|
31
|
+
const Context = scope?.[scopeName][index] || BaseContext, context = React.useContext(Context);
|
|
32
|
+
if (context)
|
|
33
|
+
return context;
|
|
34
|
+
if (defaultContext !== void 0)
|
|
35
|
+
return defaultContext;
|
|
36
|
+
const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
|
|
37
|
+
if (options?.fallback)
|
|
38
|
+
return options?.warn !== !1 && console.warn(missingContextMessage), options.fallback;
|
|
39
|
+
throw new Error(missingContextMessage);
|
|
40
|
+
}
|
|
41
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
42
|
+
}
|
|
43
|
+
const createScope = () => {
|
|
44
|
+
const scopeContexts = defaultContexts.map((defaultContext) => React.createContext(defaultContext));
|
|
45
|
+
return function(scope) {
|
|
46
|
+
const contexts = scope?.[scopeName] || scopeContexts;
|
|
47
|
+
return React.useMemo(
|
|
48
|
+
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
49
|
+
[scope, contexts]
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
return createScope.scopeName = scopeName, [
|
|
54
|
+
createContext2,
|
|
55
|
+
composeContextScopes(createScope, ...createContextScopeDeps)
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
function composeContextScopes(...scopes) {
|
|
59
|
+
const baseScope = scopes[0];
|
|
60
|
+
if (scopes.length === 1)
|
|
61
|
+
return baseScope;
|
|
62
|
+
const createScope = () => {
|
|
63
|
+
const scopeHooks = scopes.map((createScope2) => ({
|
|
64
|
+
useScope: createScope2(),
|
|
65
|
+
scopeName: createScope2.scopeName
|
|
66
|
+
}));
|
|
67
|
+
return function(overrideScopes) {
|
|
68
|
+
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
69
|
+
const currentScope = useScope(overrideScopes)[`__scope${scopeName}`];
|
|
70
|
+
return { ...nextScopes2, ...currentScope };
|
|
71
|
+
}, {});
|
|
72
|
+
return React.useMemo(
|
|
73
|
+
() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),
|
|
74
|
+
[nextScopes]
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
return createScope.scopeName = baseScope.scopeName, createScope;
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
createContext,
|
|
82
|
+
createContextScope
|
|
83
|
+
};
|
|
84
|
+
//# sourceMappingURL=create-context.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/create-context.tsx"],
|
|
4
|
+
"mappings": "AAGA,YAAY,WAAW;AAIhB,SAAS,cACd,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UA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ED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/create-context",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.62.1",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"types": "./types/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"react": "*"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@tamagui/build": "1.
|
|
33
|
+
"@tamagui/build": "1.62.1",
|
|
34
34
|
"react": "^18.2.0"
|
|
35
35
|
},
|
|
36
36
|
"publishConfig": {
|