@hanzogui/create-context 2.0.0-rc.41-hanzoai.5

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Nate Wienert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,142 @@
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;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: true
11
+ });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
16
+ get: () => from[key],
17
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
18
+ });
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
23
+ // If the importer is in node compatibility mode or this is not an ESM
24
+ // file that has been converted to a CommonJS file using a Babel-
25
+ // compatible transform (i.e. "__esModule" has not been set), then set
26
+ // "default" to the CommonJS "module.exports" for node compatibility.
27
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
28
+ value: mod,
29
+ enumerable: true
30
+ }) : target, mod));
31
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
32
+ value: true
33
+ }), mod);
34
+ var create_context_exports = {};
35
+ __export(create_context_exports, {
36
+ createContext: () => createContext,
37
+ createContextScope: () => createContextScope
38
+ });
39
+ module.exports = __toCommonJS(create_context_exports);
40
+ var React = __toESM(require("react"), 1);
41
+ var import_jsx_runtime = require("react/jsx-runtime");
42
+ function createContext(rootComponentName, defaultContext) {
43
+ const Context = React.createContext(defaultContext);
44
+ function Provider(props) {
45
+ const {
46
+ children,
47
+ ...context
48
+ } = props;
49
+ const value = React.useMemo(() => context, Object.values(context));
50
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)(Context.Provider, {
51
+ value,
52
+ children
53
+ });
54
+ }
55
+ function useContext(consumerName) {
56
+ const context = React.useContext(Context);
57
+ if (context) return context;
58
+ if (defaultContext !== void 0) return defaultContext;
59
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
60
+ }
61
+ return [Provider, useContext];
62
+ }
63
+ function createContextScope(scopeName, createContextScopeDeps = []) {
64
+ let defaultContexts = [];
65
+ function createContext2(rootComponentName, defaultContext) {
66
+ const BaseContext = React.createContext(defaultContext);
67
+ const index = defaultContexts.length;
68
+ defaultContexts = [...defaultContexts, defaultContext];
69
+ function Provider(props) {
70
+ const {
71
+ scope,
72
+ children,
73
+ ...context
74
+ } = props;
75
+ const Context = scope?.[scopeName]?.[index] || BaseContext;
76
+ const value = React.useMemo(() => context, Object.values(context));
77
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)(Context.Provider, {
78
+ value,
79
+ children
80
+ });
81
+ }
82
+ function useContext(consumerName, scope, options) {
83
+ const Context = scope?.[scopeName]?.[index] || BaseContext;
84
+ const context = React.useContext(Context);
85
+ if (context) return context;
86
+ if (defaultContext !== void 0) return defaultContext;
87
+ const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
88
+ if (options?.fallback) {
89
+ if (options?.warn !== false) {
90
+ console.warn(missingContextMessage);
91
+ }
92
+ return options.fallback;
93
+ }
94
+ throw new Error(missingContextMessage);
95
+ }
96
+ return [Provider, useContext];
97
+ }
98
+ const createScope = () => {
99
+ const scopeContexts = defaultContexts.map(defaultContext => {
100
+ return React.createContext(defaultContext);
101
+ });
102
+ return function useScope(scope) {
103
+ const contexts = scope?.[scopeName] || scopeContexts;
104
+ return React.useMemo(() => ({
105
+ [`__scope${scopeName}`]: {
106
+ ...scope,
107
+ [scopeName]: contexts
108
+ }
109
+ }), [scope, contexts]);
110
+ };
111
+ };
112
+ createScope.scopeName = scopeName;
113
+ return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
114
+ }
115
+ function composeContextScopes(...scopes) {
116
+ const baseScope = scopes[0];
117
+ if (scopes.length === 1) return baseScope;
118
+ const createScope = () => {
119
+ const scopeHooks = scopes.map(createScope2 => ({
120
+ useScope: createScope2(),
121
+ scopeName: createScope2.scopeName
122
+ }));
123
+ return function useComposedScopes(overrideScopes) {
124
+ const nextScopes = scopeHooks.reduce((nextScopes2, {
125
+ useScope,
126
+ scopeName
127
+ }) => {
128
+ const scopeProps = useScope(overrideScopes);
129
+ const currentScope = scopeProps[`__scope${scopeName}`];
130
+ return {
131
+ ...nextScopes2,
132
+ ...currentScope
133
+ };
134
+ }, {});
135
+ return React.useMemo(() => ({
136
+ [`__scope${baseScope.scopeName}`]: nextScopes
137
+ }), [nextScopes]);
138
+ };
139
+ };
140
+ createScope.scopeName = baseScope.scopeName;
141
+ return createScope;
142
+ }
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all) __defProp(target, name, {
11
+ get: all[name],
12
+ enumerable: true
13
+ });
14
+ };
15
+ var __copyProps = (to, from, except, desc) => {
16
+ if (from && typeof from === "object" || typeof from === "function") {
17
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
18
+ get: () => from[key],
19
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
20
+ });
21
+ }
22
+ return to;
23
+ };
24
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
25
+ // If the importer is in node compatibility mode or this is not an ESM
26
+ // file that has been converted to a CommonJS file using a Babel-
27
+ // compatible transform (i.e. "__esModule" has not been set), then set
28
+ // "default" to the CommonJS "module.exports" for node compatibility.
29
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
30
+ value: mod,
31
+ enumerable: true
32
+ }) : target, mod));
33
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
34
+ value: true
35
+ }), mod);
36
+ var create_context_exports = {};
37
+ __export(create_context_exports, {
38
+ createContext: () => createContext,
39
+ createContextScope: () => createContextScope
40
+ });
41
+ module.exports = __toCommonJS(create_context_exports);
42
+ var import_jsx_runtime = require("react/jsx-runtime");
43
+ var React = __toESM(require("react"), 1);
44
+ function createContext(rootComponentName, defaultContext) {
45
+ var Context = /* @__PURE__ */React.createContext(defaultContext);
46
+ function Provider(props) {
47
+ var {
48
+ children,
49
+ ...context
50
+ } = props;
51
+ var value = React.useMemo(function () {
52
+ return context;
53
+ }, Object.values(context));
54
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)(Context.Provider, {
55
+ value,
56
+ children
57
+ });
58
+ }
59
+ function useContext(consumerName) {
60
+ var context = React.useContext(Context);
61
+ if (context) return context;
62
+ if (defaultContext !== void 0) return defaultContext;
63
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
64
+ }
65
+ return [Provider, useContext];
66
+ }
67
+ function createContextScope(scopeName) {
68
+ var createContextScopeDeps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
69
+ var defaultContexts = [];
70
+ function createContext2(rootComponentName, defaultContext) {
71
+ var BaseContext = /* @__PURE__ */React.createContext(defaultContext);
72
+ var index = defaultContexts.length;
73
+ defaultContexts = [...defaultContexts, defaultContext];
74
+ function Provider(props) {
75
+ var _scope_scopeName;
76
+ var {
77
+ scope,
78
+ children,
79
+ ...context
80
+ } = props;
81
+ var Context = (scope === null || scope === void 0 ? void 0 : (_scope_scopeName = scope[scopeName]) === null || _scope_scopeName === void 0 ? void 0 : _scope_scopeName[index]) || BaseContext;
82
+ var value = React.useMemo(function () {
83
+ return context;
84
+ }, Object.values(context));
85
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)(Context.Provider, {
86
+ value,
87
+ children
88
+ });
89
+ }
90
+ function useContext(consumerName, scope, options) {
91
+ var _scope_scopeName;
92
+ var Context = (scope === null || scope === void 0 ? void 0 : (_scope_scopeName = scope[scopeName]) === null || _scope_scopeName === void 0 ? void 0 : _scope_scopeName[index]) || BaseContext;
93
+ var context = React.useContext(Context);
94
+ if (context) return context;
95
+ if (defaultContext !== void 0) return defaultContext;
96
+ var missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
97
+ if (options === null || options === void 0 ? void 0 : options.fallback) {
98
+ if ((options === null || options === void 0 ? void 0 : options.warn) !== false) {
99
+ console.warn(missingContextMessage);
100
+ }
101
+ return options.fallback;
102
+ }
103
+ throw new Error(missingContextMessage);
104
+ }
105
+ return [Provider, useContext];
106
+ }
107
+ var createScope = function () {
108
+ var scopeContexts = defaultContexts.map(function (defaultContext) {
109
+ return /* @__PURE__ */React.createContext(defaultContext);
110
+ });
111
+ return function useScope(scope) {
112
+ var contexts = (scope === null || scope === void 0 ? void 0 : scope[scopeName]) || scopeContexts;
113
+ return React.useMemo(function () {
114
+ return {
115
+ [`__scope${scopeName}`]: {
116
+ ...scope,
117
+ [scopeName]: contexts
118
+ }
119
+ };
120
+ }, [scope, contexts]);
121
+ };
122
+ };
123
+ createScope.scopeName = scopeName;
124
+ return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
125
+ }
126
+ function composeContextScopes() {
127
+ for (var _len = arguments.length, scopes = new Array(_len), _key = 0; _key < _len; _key++) {
128
+ scopes[_key] = arguments[_key];
129
+ }
130
+ var baseScope = scopes[0];
131
+ if (scopes.length === 1) return baseScope;
132
+ var createScope = function () {
133
+ var scopeHooks = scopes.map(function (createScope2) {
134
+ return {
135
+ useScope: createScope2(),
136
+ scopeName: createScope2.scopeName
137
+ };
138
+ });
139
+ return function useComposedScopes(overrideScopes) {
140
+ var nextScopes = scopeHooks.reduce(function (nextScopes2, param) {
141
+ var {
142
+ useScope,
143
+ scopeName
144
+ } = param;
145
+ var scopeProps = useScope(overrideScopes);
146
+ var currentScope = scopeProps[`__scope${scopeName}`];
147
+ return {
148
+ ...nextScopes2,
149
+ ...currentScope
150
+ };
151
+ }, {});
152
+ return React.useMemo(function () {
153
+ return {
154
+ [`__scope${baseScope.scopeName}`]: nextScopes
155
+ };
156
+ }, [nextScopes]);
157
+ };
158
+ };
159
+ createScope.scopeName = baseScope.scopeName;
160
+ return createScope;
161
+ }
162
+ //# sourceMappingURL=create-context.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","create_context_exports","__export","createContext","createContextScope","module","exports","import_jsx_runtime","require","React","__toESM","rootComponentName","defaultContext","Context","Provider","props","children","context","useMemo","Object","values","jsx","useContext","consumerName","Error","scopeName","createContextScopeDeps","arguments","length","defaultContexts","createContext2","BaseContext","index","_scope_scopeName","scope","options","missingContextMessage","fallback","warn","console","createScope","scopeContexts","map","useScope","contexts"],"sources":["../../src/create-context.tsx"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,sBAAA;AAAAC,QAAA,CAAAD,sBAAA;EAAAE,aAAA,EAAAA,CAAA,KAAAA,aAAA;EAAAC,kBAAA,EAAAA,CAAA,KAAAA;AAAA;AAGAC,MAAA,CAAAC,OAAuB,GAAAV,YAAA,CAAAK,sBAAA;AAkBZ,IAAAM,kBAAA,GAAAC,OAAA;AAdJ,IAAAC,KAAS,GAAAC,OAAA,CAAAF,OACd;AAMA,SAAML,aAAUA,CAAAQ,iBAAkD,EAAAC,cAAc;EAEhF,IAAAC,OAAS,kBAAkEJ,KAAA,CAAAN,aAAA,CAAAS,cAAA;EACzE,SAAME,QAAEA,CAAAC,KAAa;IAGrB;MAAMC,QAAQ;MAAA,GAAAC;IAAM,CAAQ,GAAAF,KAAM;IAClC,IAAAf,KAAO,GAAAS,KAAA,CAAAS,OAAA;MACT,OAAAD,OAAA;IAEA,GAAAE,MAAS,CAAAC,MAAA,CAAAH,OAAW;IAClB,OAAM,eAAgB,IAAAV,kBAAkB,CAAAc,GAAA,EAAAR,OAAA,CAAAC,QAAA;MACxCd,KAAI;MACJgB;IAEA;EACF;EAEA,SAAQM,UAAUA,CAAAC,YAAU;IAC9B,IAAAN,OAAA,GAAAR,KAAA,CAAAa,UAAA,CAAAT,OAAA;IAeO,IAAAI,OAAS,SAAAA,OACd;I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KAAA,CAAAa,UAAA,CAAAT,OAAA;MACD,IAAAI,OAAO,SAASA,OAAS;MACvB,IAAAL,cAAiB,UAAQ,UAASA,cAAK;MACvC,IAAAwB,qBAAa,QAAAb,YAAA,4BAAAZ,iBAAA;MAAA,IACXwB,OAAS,KAAC,QAAUA,OAAS,KAAK,KAAK,SAAQ,IAAAA,OAAY,CAAAE,QAAS,EAAE;QACtE,IAAC,CAAAF,OAAO,KAAQ,QAAAA,OAAA,uBAAAA,OAAA,CAAAG,IAAA;UAClBC,OAAA,CAAAD,IAAA,CAAAF,qBAAA;QACF;QACF,OAAAD,OAAA,CAAAE,QAAA;MAEA;MAEA,MAAO,IAAAb,KAAA,CAAAY,qBAAA;IACL;IACA,QACFtB,QAAA,EACFQ,UAAA,CAMA;EACE;EACA,IAAIkB,WAAO,YAAAA,CAAA,EAAc;IAEzB,IAAMC,aAAA,GAA2BZ,eAAM,CAAAa,GAAA,WAAA9B,cAAA;MACrC,OAAM,eAAaH,KAAW,CAACN,aAAA,CAAAS,cAAiB;IAAA,EAC9C;IAAsB,OACtB,SAAW+B,SAAAT,KAAY;MACvB,IAAAU,QAAA,IAAAV,KAAA,aAAAA,KAAA,uBAAAA,KAAA,CAAAT,SAAA,MAAAgB,aAAA;MAEF,OAAOhC,KAAA,CAAAS,OAAS,aAAkB;QAChC,OAAM;UAIJ,WAAMO,SAAa;YACnB,GAAMS,KAAA;YACN,CAAAT,SAAY,GAAAmB;UACV;QAEJ;MAAa,GACX,CACAV,KAAC,EACHU,QAAA,CACF;IACF;EAEA;EACAJ,WAAO,CAAAf,SAAA,GAAAA,SAAA;EACT,Q","ignoreList":[]}
@@ -0,0 +1,20 @@
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)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
8
+ get: () => from[key],
9
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
10
+ });
11
+ }
12
+ return to;
13
+ };
14
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
16
+ value: true
17
+ }), mod);
18
+ var index_exports = {};
19
+ module.exports = __toCommonJS(index_exports);
20
+ __reExport(index_exports, require("./create-context.cjs"), module.exports);
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __copyProps = (to, from, except, desc) => {
8
+ if (from && typeof from === "object" || typeof from === "function") {
9
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
10
+ get: () => from[key],
11
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
12
+ });
13
+ }
14
+ return to;
15
+ };
16
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
17
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
18
+ value: true
19
+ }), mod);
20
+ var index_exports = {};
21
+ module.exports = __toCommonJS(index_exports);
22
+ __reExport(index_exports, require("./create-context.native.js"), module.exports);
23
+ //# sourceMappingURL=index.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","index_exports","module","exports"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,aAAA;AAAAC,MAAA,CAAAC,OAAA,GAAAP,YAAc,CAAAK,aAAA","ignoreList":[]}
@@ -0,0 +1,105 @@
1
+ import * as React from "react";
2
+ import { jsx } from "react/jsx-runtime";
3
+ function createContext(rootComponentName, defaultContext) {
4
+ const Context = React.createContext(defaultContext);
5
+ function Provider(props) {
6
+ const {
7
+ children,
8
+ ...context
9
+ } = props;
10
+ const value = React.useMemo(() => context, Object.values(context));
11
+ return /* @__PURE__ */jsx(Context.Provider, {
12
+ value,
13
+ children
14
+ });
15
+ }
16
+ function useContext(consumerName) {
17
+ const context = React.useContext(Context);
18
+ if (context) return context;
19
+ if (defaultContext !== void 0) return defaultContext;
20
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
21
+ }
22
+ return [Provider, useContext];
23
+ }
24
+ function createContextScope(scopeName, createContextScopeDeps = []) {
25
+ let defaultContexts = [];
26
+ function createContext2(rootComponentName, defaultContext) {
27
+ const BaseContext = React.createContext(defaultContext);
28
+ const index = defaultContexts.length;
29
+ defaultContexts = [...defaultContexts, defaultContext];
30
+ function Provider(props) {
31
+ const {
32
+ scope,
33
+ children,
34
+ ...context
35
+ } = props;
36
+ const Context = scope?.[scopeName]?.[index] || BaseContext;
37
+ const value = React.useMemo(() => context, Object.values(context));
38
+ return /* @__PURE__ */jsx(Context.Provider, {
39
+ value,
40
+ children
41
+ });
42
+ }
43
+ function useContext(consumerName, scope, options) {
44
+ const Context = scope?.[scopeName]?.[index] || BaseContext;
45
+ const context = React.useContext(Context);
46
+ if (context) return context;
47
+ if (defaultContext !== void 0) return defaultContext;
48
+ const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
49
+ if (options?.fallback) {
50
+ if (options?.warn !== false) {
51
+ console.warn(missingContextMessage);
52
+ }
53
+ return options.fallback;
54
+ }
55
+ throw new Error(missingContextMessage);
56
+ }
57
+ return [Provider, useContext];
58
+ }
59
+ const createScope = () => {
60
+ const scopeContexts = defaultContexts.map(defaultContext => {
61
+ return React.createContext(defaultContext);
62
+ });
63
+ return function useScope(scope) {
64
+ const contexts = scope?.[scopeName] || scopeContexts;
65
+ return React.useMemo(() => ({
66
+ [`__scope${scopeName}`]: {
67
+ ...scope,
68
+ [scopeName]: contexts
69
+ }
70
+ }), [scope, contexts]);
71
+ };
72
+ };
73
+ createScope.scopeName = scopeName;
74
+ return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
75
+ }
76
+ function composeContextScopes(...scopes) {
77
+ const baseScope = scopes[0];
78
+ if (scopes.length === 1) return baseScope;
79
+ const createScope = () => {
80
+ const scopeHooks = scopes.map(createScope2 => ({
81
+ useScope: createScope2(),
82
+ scopeName: createScope2.scopeName
83
+ }));
84
+ return function useComposedScopes(overrideScopes) {
85
+ const nextScopes = scopeHooks.reduce((nextScopes2, {
86
+ useScope,
87
+ scopeName
88
+ }) => {
89
+ const scopeProps = useScope(overrideScopes);
90
+ const currentScope = scopeProps[`__scope${scopeName}`];
91
+ return {
92
+ ...nextScopes2,
93
+ ...currentScope
94
+ };
95
+ }, {});
96
+ return React.useMemo(() => ({
97
+ [`__scope${baseScope.scopeName}`]: nextScopes
98
+ }), [nextScopes]);
99
+ };
100
+ };
101
+ createScope.scopeName = baseScope.scopeName;
102
+ return createScope;
103
+ }
104
+ export { createContext, createContextScope };
105
+ //# sourceMappingURL=create-context.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["React","jsx","createContext","rootComponentName","defaultContext","Context","Provider","props","children","context","value","useMemo","Object","values","useContext","consumerName","Error","createContextScope","scopeName","createContextScopeDeps","defaultContexts","createContext2","BaseContext","index","length","scope","options","missingContextMessage","fallback","warn","console","createScope","scopeContexts","map","useScope","contexts","composeContextScopes","scopes","baseScope","scopeHooks","createScope2","useComposedScopes","overrideScopes","nextScopes","reduce","nextScopes2","scopeProps","currentScope"],"sources":["../../src/create-context.tsx"],"sourcesContent":[null],"mappings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U,QAAQ,CAClB;IACF;EACF;EAEAJ,WAAA,CAAYb,SAAA,GAAYA,SAAA;EAExB,OAAO,CACLG,cAAA,EACAe,oBAAA,CAAqBL,WAAA,EAAa,GAAGZ,sBAAsB,EAC7D;AACF;AAMA,SAASiB,qBAAA,GAAwBC,MAAA,EAAuB;EACtD,MAAMC,SAAA,GAAYD,MAAA,CAAO,CAAC;EAC1B,IAAIA,MAAA,CAAOb,MAAA,KAAW,GAAG,OAAOc,SAAA;EAEhC,MAAMP,WAAA,GAA2BA,CAAA,KAAM;IACrC,MAAMQ,UAAA,GAAaF,MAAA,CAAOJ,GAAA,CAAKO,YAAA,KAAiB;MAC9CN,QAAA,EAAUM,YAAA,CAAY;MACtBtB,SAAA,EAAWsB,YAAA,CAAYtB;IACzB,EAAE;IAEF,OAAO,SAASuB,kBAAkBC,cAAA,EAAgB;MAChD,MAAMC,UAAA,GAAaJ,UAAA,CAAWK,MAAA,CAAO,CAACC,WAAA,EAAY;QAAEX,QAAA;QAAUhB;MAAU,MAAM;QAI5E,MAAM4B,UAAA,GAAaZ,QAAA,CAASQ,cAAc;QAC1C,MAAMK,YAAA,GAAeD,UAAA,CAAW,UAAU5B,SAAS,EAAE;QACrD,OAAO;UAAE,GAAG2B,WAAA;UAAY,GAAGE;QAAa;MAC1C,GAAG,CAAC,CAAC;MAEL,OAAO/C,KAAA,CAAMW,OAAA,CACX,OAAO;QAAE,CAAC,UAAU2B,SAAA,CAAUpB,SAAS,EAAE,GAAGyB;MAAW,IACvD,CAACA,UAAU,CACb;IACF;EACF;EAEAZ,WAAA,CAAYb,SAAA,GAAYoB,SAAA,CAAUpB,SAAA;EAClC,OAAOa,WAAA;AACT","ignoreList":[]}
@@ -0,0 +1,122 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import * as React from "react";
3
+ function createContext(rootComponentName, defaultContext) {
4
+ var Context = /* @__PURE__ */React.createContext(defaultContext);
5
+ function Provider(props) {
6
+ var {
7
+ children,
8
+ ...context
9
+ } = props;
10
+ var value = React.useMemo(function () {
11
+ return context;
12
+ }, Object.values(context));
13
+ return /* @__PURE__ */_jsx(Context.Provider, {
14
+ value,
15
+ children
16
+ });
17
+ }
18
+ function useContext(consumerName) {
19
+ var context = React.useContext(Context);
20
+ if (context) return context;
21
+ if (defaultContext !== void 0) return defaultContext;
22
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
23
+ }
24
+ return [Provider, useContext];
25
+ }
26
+ function createContextScope(scopeName) {
27
+ var createContextScopeDeps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
28
+ var defaultContexts = [];
29
+ function createContext2(rootComponentName, defaultContext) {
30
+ var BaseContext = /* @__PURE__ */React.createContext(defaultContext);
31
+ var index = defaultContexts.length;
32
+ defaultContexts = [...defaultContexts, defaultContext];
33
+ function Provider(props) {
34
+ var _scope_scopeName;
35
+ var {
36
+ scope,
37
+ children,
38
+ ...context
39
+ } = props;
40
+ var Context = (scope === null || scope === void 0 ? void 0 : (_scope_scopeName = scope[scopeName]) === null || _scope_scopeName === void 0 ? void 0 : _scope_scopeName[index]) || BaseContext;
41
+ var value = React.useMemo(function () {
42
+ return context;
43
+ }, Object.values(context));
44
+ return /* @__PURE__ */_jsx(Context.Provider, {
45
+ value,
46
+ children
47
+ });
48
+ }
49
+ function useContext(consumerName, scope, options) {
50
+ var _scope_scopeName;
51
+ var Context = (scope === null || scope === void 0 ? void 0 : (_scope_scopeName = scope[scopeName]) === null || _scope_scopeName === void 0 ? void 0 : _scope_scopeName[index]) || BaseContext;
52
+ var context = React.useContext(Context);
53
+ if (context) return context;
54
+ if (defaultContext !== void 0) return defaultContext;
55
+ var missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
56
+ if (options === null || options === void 0 ? void 0 : options.fallback) {
57
+ if ((options === null || options === void 0 ? void 0 : options.warn) !== false) {
58
+ console.warn(missingContextMessage);
59
+ }
60
+ return options.fallback;
61
+ }
62
+ throw new Error(missingContextMessage);
63
+ }
64
+ return [Provider, useContext];
65
+ }
66
+ var createScope = function () {
67
+ var scopeContexts = defaultContexts.map(function (defaultContext) {
68
+ return /* @__PURE__ */React.createContext(defaultContext);
69
+ });
70
+ return function useScope(scope) {
71
+ var contexts = (scope === null || scope === void 0 ? void 0 : scope[scopeName]) || scopeContexts;
72
+ return React.useMemo(function () {
73
+ return {
74
+ [`__scope${scopeName}`]: {
75
+ ...scope,
76
+ [scopeName]: contexts
77
+ }
78
+ };
79
+ }, [scope, contexts]);
80
+ };
81
+ };
82
+ createScope.scopeName = scopeName;
83
+ return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
84
+ }
85
+ function composeContextScopes() {
86
+ for (var _len = arguments.length, scopes = new Array(_len), _key = 0; _key < _len; _key++) {
87
+ scopes[_key] = arguments[_key];
88
+ }
89
+ var baseScope = scopes[0];
90
+ if (scopes.length === 1) return baseScope;
91
+ var createScope = function () {
92
+ var scopeHooks = scopes.map(function (createScope2) {
93
+ return {
94
+ useScope: createScope2(),
95
+ scopeName: createScope2.scopeName
96
+ };
97
+ });
98
+ return function useComposedScopes(overrideScopes) {
99
+ var nextScopes = scopeHooks.reduce(function (nextScopes2, param) {
100
+ var {
101
+ useScope,
102
+ scopeName
103
+ } = param;
104
+ var scopeProps = useScope(overrideScopes);
105
+ var currentScope = scopeProps[`__scope${scopeName}`];
106
+ return {
107
+ ...nextScopes2,
108
+ ...currentScope
109
+ };
110
+ }, {});
111
+ return React.useMemo(function () {
112
+ return {
113
+ [`__scope${baseScope.scopeName}`]: nextScopes
114
+ };
115
+ }, [nextScopes]);
116
+ };
117
+ };
118
+ createScope.scopeName = baseScope.scopeName;
119
+ return createScope;
120
+ }
121
+ export { createContext, createContextScope };
122
+ //# sourceMappingURL=create-context.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["jsx","_jsx","React","createContext","rootComponentName","defaultContext","Context","Provider","props","children","context","value","useMemo","Object","values","useContext","consumerName","Error","createContextScope","scopeName","createContextScopeDeps","arguments","length","defaultContexts","createContext2","BaseContext","index","_scope_scopeName","scope","options","missingContextMessage","fallback","warn","console","createScope","scopeContexts","map","useScope","contexts"],"sources":["../../src/create-context.tsx"],"sourcesContent":[null],"mappings":"AAGA,SAAAA,GAAY,IAAAC,IAAA,QAAW;AAkBZ,YAAAC,KAAA;AAdJ,SAASC,cACdC,iBAAA,EACAC,cAAA,EAIA;EACA,IAAAC,OAAM,kBAAgBJ,KAAA,CAA4CC,aAAA,CAAcE,cAAA;EAEhF,SAASE,SAASC,KAAA,EAAyD;IACzE;MAAMC,QAAE;MAAA,GAAUC;IAAG,IAAQF,KAAI;IAGjC,IAAAG,KAAM,GAAAT,KAAQ,CAAAU,OAAM,aAAc;MAClC,OAAOF,OAAA;IACT,GAAAG,MAAA,CAAAC,MAAA,CAAAJ,OAAA;IAEA,OAAS,eAAWT,IAAA,CAAAK,OAA0D,CAAAC,QAAA;MAC5EI,KAAM;MACNF;IACA;EAEA;EACF,SAAAM,WAAAC,YAAA;IAEA,IAAAN,OAAQ,GAAAR,KAAU,CAAAa,UAAU,CAAAT,OAAA;IAC9B,IAAAI,OAAA,SAAAA,OAAA;IAeO,IAAAL,cAAS,UACd,UACAA,cAAA;IAuBA,MAAI,IAAAY,KAAA,MAA0BD,YAAA,4BAAAZ,iBAAA;EAM9B;EAIE,QACAG,QAAM,EACNQ,UAAA,CAEA;AAME;AACA,SAAAG,kBAAgBA,CAAAC,SAAQ,EAAS;EAGjC,IAAAC,sBAAoB,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,iBAAAA,SAAA;EAAA,IAAAE,eACZ;EAAA,SACNC,cAAcA,CAAApB,iBAAO,EAAAC,cAAA;IAAA,IACvBoB,WAAA,kBAAAvB,KAAA,CAAAC,aAAA,CAAAE,cAAA;IACA,IAAAqB,KAAO,GAAAH,eAAA,CAAAD,MAAC;IACVC,eAAA,IAEA,GAAAA,eACE,EAOAlB,cAAM,CACN;IACA,SAAIE,QAASA,CAAAC,KAAO;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Y,SAAAA,CAAA,EAAQ;IAC1B,IAAIC,aAAO,GAAWZ,eAAU,CAAAa,GAAA,WAAA/B,cAAA;MAEhC,OAAM,eAAiCH,KAAA,CAAAC,aAAA,CAAAE,cAAA;IACrC;IAAgD,OAC9C,SAAUgC,SAAYT,KAAA;MACtB,IAAAU,QAAW,IAAAV,KAAA,KAAY,QAAAA,KAAA,uBAAAA,KAAA,CAAAT,SAAA,MAAAgB,aAAA;MACvB,OAAAjC,KAAA,CAAAU,OAAA;QAEF,OAAO;UACL,WAAMO,SAAa;YAIjB,GAAMS,KAAA;YACN,CAAAT,SAAM,GAAAmB;UACN;QACF,CAAG;MAEH,IACEV,KAAA,EACAU,QAAC,CACH;IACF;EACF;EAEAJ,WAAA,CAAYf,SAAA,GAAYA,SAAA;EACxB,OAAO,CACTK,cAAA,E","ignoreList":[]}
@@ -0,0 +1,2 @@
1
+ export * from "./create-context.mjs";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc","ignoreList":[]}
@@ -0,0 +1,2 @@
1
+ export * from "./create-context.mjs";
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc","ignoreList":[]}
@@ -0,0 +1,2 @@
1
+ export * from "./create-context.native.js";
2
+ //# sourceMappingURL=index.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc","ignoreList":[]}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@hanzogui/create-context",
3
+ "version": "2.0.0-rc.41-hanzoai.5",
4
+ "gitHead": "a49cc7ea6b93ba384e77a4880ae48ac4a5635c14",
5
+ "source": "src/index.ts",
6
+ "files": [
7
+ "src",
8
+ "types",
9
+ "dist"
10
+ ],
11
+ "type": "module",
12
+ "sideEffects": false,
13
+ "main": "dist/cjs",
14
+ "module": "dist/esm",
15
+ "types": "./types/index.d.ts",
16
+ "exports": {
17
+ "./package.json": "./package.json",
18
+ ".": {
19
+ "types": "./types/index.d.ts",
20
+ "react-native": "./dist/esm/index.native.js",
21
+ "browser": "./dist/esm/index.mjs",
22
+ "module": "./dist/esm/index.mjs",
23
+ "import": "./dist/esm/index.mjs",
24
+ "require": "./dist/cjs/index.cjs",
25
+ "default": "./dist/esm/index.mjs"
26
+ }
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "scripts": {
32
+ "build": "hanzogui-build",
33
+ "watch": "hanzogui-build --watch",
34
+ "clean": "hanzogui-build clean",
35
+ "clean:build": "hanzogui-build clean:build"
36
+ },
37
+ "devDependencies": {
38
+ "@hanzogui/build": "2.0.0-rc.41-hanzoai.5",
39
+ "react": ">=19"
40
+ },
41
+ "peerDependencies": {
42
+ "react": ">=19"
43
+ }
44
+ }
@@ -0,0 +1,192 @@
1
+ // from radix
2
+ // https://github.com/radix-ui/primitives/blob/main/packages/react/context/src/createContext.tsx
3
+
4
+ import * as React from 'react'
5
+
6
+ export type ScopedProps<P, K extends string> = P & { [Key in `__scope${K}`]?: Scope }
7
+
8
+ export function createContext<ContextValueType extends object | null>(
9
+ rootComponentName: string,
10
+ defaultContext?: ContextValueType
11
+ ): readonly [
12
+ (props: ContextValueType & { children: React.ReactNode }) => React.JSX.Element,
13
+ (consumerName: string) => Exclude<ContextValueType | undefined, undefined>,
14
+ ] {
15
+ const Context = React.createContext<ContextValueType | undefined>(defaultContext)
16
+
17
+ function Provider(props: ContextValueType & { children: React.ReactNode }) {
18
+ const { children, ...context } = props
19
+ // Only re-memoize when prop values change
20
+
21
+ const value = React.useMemo(() => context, Object.values(context)) as ContextValueType
22
+ return <Context.Provider value={value}>{children}</Context.Provider>
23
+ }
24
+
25
+ function useContext(consumerName: string): Exclude<typeof context, undefined> {
26
+ const context = React.useContext(Context)
27
+ if (context) return context as any
28
+ if (defaultContext !== undefined) return defaultContext as any
29
+ // if a defaultContext wasn't specified, it's a required context.
30
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``)
31
+ }
32
+
33
+ return [Provider, useContext] as const
34
+ }
35
+
36
+ /* -------------------------------------------------------------------------------------------------
37
+ * createContextScope
38
+ * -----------------------------------------------------------------------------------------------*/
39
+
40
+ type ScopeHook = (scope: Scope) => { [__scopeProp: string]: Scope }
41
+
42
+ export type Scope<C = any> = { [scopeName: string]: React.Context<C>[] } | undefined
43
+
44
+ export interface CreateScope {
45
+ scopeName: string
46
+ (): ScopeHook
47
+ }
48
+
49
+ export function createContextScope(
50
+ scopeName: string,
51
+ createContextScopeDeps: CreateScope[] = []
52
+ ): readonly [
53
+ <ContextValueType extends object | null>(
54
+ rootComponentName: string,
55
+ defaultContext?: ContextValueType
56
+ ) => readonly [
57
+ (
58
+ props: ContextValueType & {
59
+ scope: Scope<ContextValueType>
60
+ children: React.ReactNode
61
+ }
62
+ ) => import('react/jsx-runtime').JSX.Element,
63
+ (
64
+ consumerName: string,
65
+ scope: Scope<ContextValueType | undefined>,
66
+ options?: {
67
+ warn?: boolean
68
+ fallback?: Partial<ContextValueType>
69
+ }
70
+ ) => ContextValueType,
71
+ ],
72
+ CreateScope,
73
+ ] {
74
+ let defaultContexts: any[] = []
75
+
76
+ /* -----------------------------------------------------------------------------------------------
77
+ * createContext
78
+ * ---------------------------------------------------------------------------------------------*/
79
+
80
+ function createContext<ContextValueType extends object | null>(
81
+ rootComponentName: string,
82
+ defaultContext?: ContextValueType
83
+ ) {
84
+ const BaseContext = React.createContext<ContextValueType | undefined>(defaultContext)
85
+ const index = defaultContexts.length
86
+ defaultContexts = [...defaultContexts, defaultContext]
87
+
88
+ function Provider(
89
+ props: ContextValueType & {
90
+ scope: Scope<ContextValueType>
91
+ children: React.ReactNode
92
+ }
93
+ ) {
94
+ const { scope, children, ...context } = props
95
+ const Context = scope?.[scopeName]?.[index] || BaseContext
96
+ // Only re-memoize when prop values change
97
+
98
+ const value = React.useMemo(
99
+ () => context,
100
+ Object.values(context)
101
+ ) as ContextValueType
102
+ return <Context.Provider value={value}>{children}</Context.Provider>
103
+ }
104
+
105
+ function useContext(
106
+ consumerName: string,
107
+ scope: Scope<ContextValueType | undefined>,
108
+ options?: {
109
+ warn?: boolean
110
+ fallback?: Partial<ContextValueType>
111
+ }
112
+ ) {
113
+ const Context = scope?.[scopeName]?.[index] || BaseContext
114
+ const context = React.useContext(Context)
115
+ if (context) return context
116
+ // if a defaultContext wasn't specified, it's a required context.
117
+ if (defaultContext !== undefined) return defaultContext
118
+ const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``
119
+ // fallback can be given per-hook as well
120
+ if (options?.fallback) {
121
+ if (options?.warn !== false) {
122
+ console.warn(missingContextMessage)
123
+ }
124
+ return options.fallback as ContextValueType
125
+ }
126
+ throw new Error(missingContextMessage)
127
+ }
128
+
129
+ return [Provider, useContext] as const
130
+ }
131
+
132
+ /* -----------------------------------------------------------------------------------------------
133
+ * createScope
134
+ * ---------------------------------------------------------------------------------------------*/
135
+
136
+ const createScope: CreateScope = () => {
137
+ const scopeContexts = defaultContexts.map((defaultContext) => {
138
+ return React.createContext(defaultContext)
139
+ })
140
+ return function useScope(scope: Scope) {
141
+ const contexts = scope?.[scopeName] || scopeContexts
142
+ return React.useMemo(
143
+ () => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
144
+ [scope, contexts]
145
+ )
146
+ }
147
+ }
148
+
149
+ createScope.scopeName = scopeName
150
+
151
+ return [
152
+ createContext,
153
+ composeContextScopes(createScope, ...createContextScopeDeps),
154
+ ] as const
155
+ }
156
+
157
+ /* -------------------------------------------------------------------------------------------------
158
+ * composeContextScopes
159
+ * -----------------------------------------------------------------------------------------------*/
160
+
161
+ function composeContextScopes(...scopes: CreateScope[]) {
162
+ const baseScope = scopes[0]
163
+ if (scopes.length === 1) return baseScope
164
+
165
+ const createScope: CreateScope = () => {
166
+ const scopeHooks = scopes.map((createScope) => ({
167
+ useScope: createScope(),
168
+ scopeName: createScope.scopeName,
169
+ }))
170
+
171
+ return function useComposedScopes(overrideScopes) {
172
+ const nextScopes = scopeHooks.reduce((nextScopes, { useScope, scopeName }) => {
173
+ // We are calling a hook inside a callback which React warns against to avoid inconsistent
174
+ // renders, however, scoping doesn't have render side effects so we ignore the rule.
175
+
176
+ const scopeProps = useScope(overrideScopes)
177
+ const currentScope = scopeProps[`__scope${scopeName}`]
178
+ return { ...nextScopes, ...currentScope }
179
+ }, {})
180
+
181
+ return React.useMemo(
182
+ () => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),
183
+ [nextScopes]
184
+ )
185
+ }
186
+ }
187
+
188
+ createScope.scopeName = baseScope.scopeName
189
+ return createScope
190
+ }
191
+
192
+ /* -----------------------------------------------------------------------------------------------*/
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './create-context'
@@ -0,0 +1,28 @@
1
+ import * as React from "react";
2
+ export type ScopedProps<
3
+ P,
4
+ K extends string
5
+ > = P & { [Key in `__scope${K}`]? : Scope };
6
+ export declare function createContext<ContextValueType extends object | null>(rootComponentName: string, defaultContext?: ContextValueType): readonly [(props: ContextValueType & {
7
+ children: React.ReactNode;
8
+ }) => React.JSX.Element, (consumerName: string) => Exclude<ContextValueType | undefined, undefined>];
9
+ type ScopeHook = (scope: Scope) => {
10
+ [__scopeProp: string]: Scope;
11
+ };
12
+ export type Scope<C = any> = {
13
+ [scopeName: string]: React.Context<C>[];
14
+ } | undefined;
15
+ export interface CreateScope {
16
+ scopeName: string;
17
+ (): ScopeHook;
18
+ }
19
+ export declare function createContextScope(scopeName: string, createContextScopeDeps?: CreateScope[]): readonly [<ContextValueType extends object | null>(rootComponentName: string, defaultContext?: ContextValueType) => readonly [(props: ContextValueType & {
20
+ scope: Scope<ContextValueType>;
21
+ children: React.ReactNode;
22
+ }) => import("react/jsx-runtime").JSX.Element, (consumerName: string, scope: Scope<ContextValueType | undefined>, options?: {
23
+ warn?: boolean;
24
+ fallback?: Partial<ContextValueType>;
25
+ }) => ContextValueType], CreateScope];
26
+ export {};
27
+
28
+ //# sourceMappingURL=create-context.d.ts.map
@@ -0,0 +1,11 @@
1
+ {
2
+ "mappings": "AAGA,YAAY,WAAW;AAEvB,YAAY;CAAY;CAAG;IAAoB,OAAO,iBAAiB,QAAO;AAE9E,OAAO,iBAAS,cAAc,wCAC5B,2BACA,iBAAiB,8BAEhB,OAAO,mBAAmB;CAAE,UAAU,MAAM;MAAgB,MAAM,IAAI,UACtE,yBAAyB,QAAQ;KA2B/B,aAAa,OAAO,UAAU;wBAAyB;;AAE5D,YAAY,MAAM,WAAW;sBAAuB,MAAM,QAAQ;;AAElE,iBAAiB,YAAY;CAC3B;KACI;;AAGN,OAAO,iBAAS,mBACd,mBACA,yBAAwB,2BAEvB,wCACC,2BACA,iBAAiB,gCAGf,OAAO,mBAAmB;CACxB,OAAO,MAAM;CACb,UAAU,MAAM;aAER,mCAEV,sBACA,OAAO,MAAM,+BACb,UAAU;CACR;CACA,WAAW,QAAQ;MAElB,mBAEP",
3
+ "names": [],
4
+ "sources": [
5
+ "src/create-context.tsx"
6
+ ],
7
+ "version": 3,
8
+ "sourcesContent": [
9
+ "// from radix\n// https://github.com/radix-ui/primitives/blob/main/packages/react/context/src/createContext.tsx\n\nimport * as React from 'react'\n\nexport type ScopedProps<P, K extends string> = P & { [Key in `__scope${K}`]?: Scope }\n\nexport function createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext?: ContextValueType\n): readonly [\n (props: ContextValueType & { children: React.ReactNode }) => React.JSX.Element,\n (consumerName: string) => Exclude<ContextValueType | undefined, undefined>,\n] {\n const Context = React.createContext<ContextValueType | undefined>(defaultContext)\n\n function Provider(props: ContextValueType & { children: React.ReactNode }) {\n const { children, ...context } = props\n // Only re-memoize when prop values change\n\n const value = React.useMemo(() => context, Object.values(context)) as ContextValueType\n return <Context.Provider value={value}>{children}</Context.Provider>\n }\n\n function useContext(consumerName: string): Exclude<typeof context, undefined> {\n const context = React.useContext(Context)\n if (context) return context as any\n if (defaultContext !== undefined) return defaultContext as any\n // if a defaultContext wasn't specified, it's a required context.\n throw new Error(`\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``)\n }\n\n return [Provider, useContext] as const\n}\n\n/* -------------------------------------------------------------------------------------------------\n * createContextScope\n * -----------------------------------------------------------------------------------------------*/\n\ntype ScopeHook = (scope: Scope) => { [__scopeProp: string]: Scope }\n\nexport type Scope<C = any> = { [scopeName: string]: React.Context<C>[] } | undefined\n\nexport interface CreateScope {\n scopeName: string\n (): ScopeHook\n}\n\nexport function createContextScope(\n scopeName: string,\n createContextScopeDeps: CreateScope[] = []\n): readonly [\n <ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext?: ContextValueType\n ) => readonly [\n (\n props: ContextValueType & {\n scope: Scope<ContextValueType>\n children: React.ReactNode\n }\n ) => import('react/jsx-runtime').JSX.Element,\n (\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n options?: {\n warn?: boolean\n fallback?: Partial<ContextValueType>\n }\n ) => ContextValueType,\n ],\n CreateScope,\n] {\n let defaultContexts: any[] = []\n\n /* -----------------------------------------------------------------------------------------------\n * createContext\n * ---------------------------------------------------------------------------------------------*/\n\n function createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext?: ContextValueType\n ) {\n const BaseContext = React.createContext<ContextValueType | undefined>(defaultContext)\n const index = defaultContexts.length\n defaultContexts = [...defaultContexts, defaultContext]\n\n function Provider(\n props: ContextValueType & {\n scope: Scope<ContextValueType>\n children: React.ReactNode\n }\n ) {\n const { scope, children, ...context } = props\n const Context = scope?.[scopeName]?.[index] || BaseContext\n // Only re-memoize when prop values change\n\n const value = React.useMemo(\n () => context,\n Object.values(context)\n ) as ContextValueType\n return <Context.Provider value={value}>{children}</Context.Provider>\n }\n\n function useContext(\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n options?: {\n warn?: boolean\n fallback?: Partial<ContextValueType>\n }\n ) {\n const Context = scope?.[scopeName]?.[index] || BaseContext\n const context = React.useContext(Context)\n if (context) return context\n // if a defaultContext wasn't specified, it's a required context.\n if (defaultContext !== undefined) return defaultContext\n const missingContextMessage = `\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``\n // fallback can be given per-hook as well\n if (options?.fallback) {\n if (options?.warn !== false) {\n console.warn(missingContextMessage)\n }\n return options.fallback as ContextValueType\n }\n throw new Error(missingContextMessage)\n }\n\n return [Provider, useContext] as const\n }\n\n /* -----------------------------------------------------------------------------------------------\n * createScope\n * ---------------------------------------------------------------------------------------------*/\n\n const createScope: CreateScope = () => {\n const scopeContexts = defaultContexts.map((defaultContext) => {\n return React.createContext(defaultContext)\n })\n return function useScope(scope: Scope) {\n const contexts = scope?.[scopeName] || scopeContexts\n return React.useMemo(\n () => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),\n [scope, contexts]\n )\n }\n }\n\n createScope.scopeName = scopeName\n\n return [\n createContext,\n composeContextScopes(createScope, ...createContextScopeDeps),\n ] as const\n}\n\n/* -------------------------------------------------------------------------------------------------\n * composeContextScopes\n * -----------------------------------------------------------------------------------------------*/\n\nfunction composeContextScopes(...scopes: CreateScope[]) {\n const baseScope = scopes[0]\n if (scopes.length === 1) return baseScope\n\n const createScope: CreateScope = () => {\n const scopeHooks = scopes.map((createScope) => ({\n useScope: createScope(),\n scopeName: createScope.scopeName,\n }))\n\n return function useComposedScopes(overrideScopes) {\n const nextScopes = scopeHooks.reduce((nextScopes, { useScope, scopeName }) => {\n // We are calling a hook inside a callback which React warns against to avoid inconsistent\n // renders, however, scoping doesn't have render side effects so we ignore the rule.\n\n const scopeProps = useScope(overrideScopes)\n const currentScope = scopeProps[`__scope${scopeName}`]\n return { ...nextScopes, ...currentScope }\n }, {})\n\n return React.useMemo(\n () => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),\n [nextScopes]\n )\n }\n }\n\n createScope.scopeName = baseScope.scopeName\n return createScope\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n"
10
+ ]
11
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./create-context";
2
+
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,11 @@
1
+ {
2
+ "mappings": "AAAA,cAAc",
3
+ "names": [],
4
+ "sources": [
5
+ "src/index.ts"
6
+ ],
7
+ "version": 3,
8
+ "sourcesContent": [
9
+ "export * from './create-context'\n"
10
+ ]
11
+ }