@tamagui/create-context 1.0.1-beta.21 → 1.0.1-beta.210
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 +29 -57
- package/dist/cjs/create-context.js.map +2 -2
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/create-context.js +21 -25
- package/dist/esm/create-context.js.map +2 -2
- package/dist/esm/index.js +0 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/jsx/create-context.js +16 -22
- package/dist/jsx/create-context.js.map +7 -0
- package/dist/jsx/index.js +1 -0
- package/dist/jsx/index.js.map +7 -0
- package/package.json +7 -4
- package/src/create-context.tsx +139 -0
- package/src/index.ts +1 -0
- package/types/create-context.d.ts +0 -0
- package/types/index.d.ts +0 -0
- package/types/create-context.d.ts.map +0 -1
- package/types/index.d.ts.map +0 -1
|
@@ -1,39 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
|
-
var __defProps = Object.defineProperties;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
9
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
-
var __spreadValues = (a, b) => {
|
|
13
|
-
for (var prop in b || (b = {}))
|
|
14
|
-
if (__hasOwnProp.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
if (__getOwnPropSymbols)
|
|
17
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
-
if (__propIsEnum.call(b, prop))
|
|
19
|
-
__defNormalProp(a, prop, b[prop]);
|
|
20
|
-
}
|
|
21
|
-
return a;
|
|
22
|
-
};
|
|
23
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
25
|
-
var __objRest = (source, exclude) => {
|
|
26
|
-
var target = {};
|
|
27
|
-
for (var prop in source)
|
|
28
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
29
|
-
target[prop] = source[prop];
|
|
30
|
-
if (source != null && __getOwnPropSymbols)
|
|
31
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
32
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
33
|
-
target[prop] = source[prop];
|
|
34
|
-
}
|
|
35
|
-
return target;
|
|
36
|
-
};
|
|
37
8
|
var __export = (target, all) => {
|
|
38
9
|
for (var name in all)
|
|
39
10
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -46,7 +17,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
46
17
|
}
|
|
47
18
|
return to;
|
|
48
19
|
};
|
|
49
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
50
24
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
51
25
|
var create_context_exports = {};
|
|
52
26
|
__export(create_context_exports, {
|
|
@@ -54,17 +28,18 @@ __export(create_context_exports, {
|
|
|
54
28
|
createContextScope: () => createContextScope
|
|
55
29
|
});
|
|
56
30
|
module.exports = __toCommonJS(create_context_exports);
|
|
31
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
57
32
|
var React = __toESM(require("react"));
|
|
58
33
|
function createContext(rootComponentName, defaultContext) {
|
|
59
34
|
const Context = React.createContext(defaultContext);
|
|
60
35
|
function Provider(props) {
|
|
61
|
-
const
|
|
36
|
+
const { children, ...context } = props;
|
|
62
37
|
const value = React.useMemo(() => context, Object.values(context));
|
|
63
|
-
return /* @__PURE__ */
|
|
64
|
-
value
|
|
65
|
-
|
|
38
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, {
|
|
39
|
+
value,
|
|
40
|
+
children
|
|
41
|
+
});
|
|
66
42
|
}
|
|
67
|
-
__name(Provider, "Provider");
|
|
68
43
|
function useContext(consumerName) {
|
|
69
44
|
const context = React.useContext(Context);
|
|
70
45
|
if (context)
|
|
@@ -73,11 +48,9 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
73
48
|
return defaultContext;
|
|
74
49
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
75
50
|
}
|
|
76
|
-
__name(useContext, "useContext");
|
|
77
51
|
Provider.displayName = rootComponentName + "Provider";
|
|
78
52
|
return [Provider, useContext];
|
|
79
53
|
}
|
|
80
|
-
__name(createContext, "createContext");
|
|
81
54
|
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
82
55
|
let defaultContexts = [];
|
|
83
56
|
function createContext2(rootComponentName, defaultContext) {
|
|
@@ -85,14 +58,14 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
85
58
|
const index = defaultContexts.length;
|
|
86
59
|
defaultContexts = [...defaultContexts, defaultContext];
|
|
87
60
|
function Provider(props) {
|
|
88
|
-
const
|
|
61
|
+
const { scope, children, ...context } = props;
|
|
89
62
|
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
90
63
|
const value = React.useMemo(() => context, Object.values(context));
|
|
91
|
-
return /* @__PURE__ */
|
|
92
|
-
value
|
|
93
|
-
|
|
64
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, {
|
|
65
|
+
value,
|
|
66
|
+
children
|
|
67
|
+
});
|
|
94
68
|
}
|
|
95
|
-
__name(Provider, "Provider");
|
|
96
69
|
function useContext(consumerName, scope) {
|
|
97
70
|
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
98
71
|
const context = React.useContext(Context);
|
|
@@ -102,46 +75,45 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
102
75
|
return defaultContext;
|
|
103
76
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
104
77
|
}
|
|
105
|
-
__name(useContext, "useContext");
|
|
106
78
|
Provider.displayName = rootComponentName + "Provider";
|
|
107
79
|
return [Provider, useContext];
|
|
108
80
|
}
|
|
109
|
-
|
|
110
|
-
const createScope = /* @__PURE__ */ __name(() => {
|
|
81
|
+
const createScope = () => {
|
|
111
82
|
const scopeContexts = defaultContexts.map((defaultContext) => {
|
|
112
83
|
return React.createContext(defaultContext);
|
|
113
84
|
});
|
|
114
|
-
return
|
|
85
|
+
return function useScope(scope) {
|
|
115
86
|
const contexts = (scope == null ? void 0 : scope[scopeName]) || scopeContexts;
|
|
116
|
-
return React.useMemo(
|
|
117
|
-
|
|
118
|
-
|
|
87
|
+
return React.useMemo(
|
|
88
|
+
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
89
|
+
[scope, contexts]
|
|
90
|
+
);
|
|
91
|
+
};
|
|
92
|
+
};
|
|
119
93
|
createScope.scopeName = scopeName;
|
|
120
94
|
return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
|
|
121
95
|
}
|
|
122
|
-
__name(createContextScope, "createContextScope");
|
|
123
96
|
function composeContextScopes(...scopes) {
|
|
124
97
|
const baseScope = scopes[0];
|
|
125
98
|
if (scopes.length === 1)
|
|
126
99
|
return baseScope;
|
|
127
|
-
const createScope =
|
|
100
|
+
const createScope = () => {
|
|
128
101
|
const scopeHooks = scopes.map((createScope2) => ({
|
|
129
102
|
useScope: createScope2(),
|
|
130
103
|
scopeName: createScope2.scopeName
|
|
131
104
|
}));
|
|
132
|
-
return
|
|
105
|
+
return function useComposedScopes(overrideScopes) {
|
|
133
106
|
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
134
107
|
const scopeProps = useScope(overrideScopes);
|
|
135
108
|
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
136
|
-
return
|
|
109
|
+
return { ...nextScopes2, ...currentScope };
|
|
137
110
|
}, {});
|
|
138
111
|
return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
112
|
+
};
|
|
113
|
+
};
|
|
141
114
|
createScope.scopeName = baseScope.scopeName;
|
|
142
115
|
return createScope;
|
|
143
116
|
}
|
|
144
|
-
__name(composeContextScopes, "composeContextScopes");
|
|
145
117
|
// Annotate the CommonJS export names for ESM import in node:
|
|
146
118
|
0 && (module.exports = {
|
|
147
119
|
createContext,
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/create-context.tsx"],
|
|
4
4
|
"sourcesContent": ["// 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) {\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 // eslint-disable-next-line react-hooks/exhaustive-deps\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) {\n const context = React.useContext(Context)\n if (context) return context\n if (defaultContext !== undefined) return defaultContext\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 Provider.displayName = rootComponentName + 'Provider'\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(scopeName: string, createContextScopeDeps: CreateScope[] = []) {\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 & { scope: Scope<ContextValueType>; children: React.ReactNode }\n ) {\n const { scope, children, ...context } = props\n const Context = scope?.[scopeName][index] || BaseContext\n // Only re-memoize when prop values change\n // eslint-disable-next-line react-hooks/exhaustive-deps\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, scope: Scope<ContextValueType | undefined>) {\n const Context = scope?.[scopeName][index] || BaseContext\n const context = React.useContext(Context)\n if (context) return context\n if (defaultContext !== undefined) return defaultContext\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 Provider.displayName = rootComponentName + 'Provider'\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 return [createContext, composeContextScopes(createScope, ...createContextScopeDeps)] 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 // eslint-disable-next-line react-hooks/rules-of-hooks\n const scopeProps = useScope(overrideScopes)\n const currentScope = scopeProps[`__scope${scopeName}`]\n return { ...nextScopes, ...currentScope }\n }, {})\n\n return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes])\n }\n }\n\n createScope.scopeName = baseScope.scopeName\n return createScope\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBW;AAfX,YAAuB;AAIhB,SAAS,cACd,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UAAM,EAAE,aAAa,QAAQ,IAAI;AAGjC,UAAM,Q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M,cAAc,cAAc;AAAA,IAC3C,CAAC;AACD,WAAO,SAAS,SAAS,OAAc;AACrC,YAAM,YAAW,+BAAQ,eAAc;AACvC,aAAO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,cAAc,EAAE,GAAG,OAAO,CAAC,YAAY,SAAS,EAAE;AAAA,QACtE,CAAC,OAAO,QAAQ;AAAA,MAClB;AAAA,IACF;AAAA,EACF;AAEA,cAAY,YAAY;AACxB,SAAO,CAACA,gBAAe,qBAAqB,aAAa,GAAG,sBAAsB,CAAC;AACrF;AAMA,SAAS,wBAAwB,QAAuB;AACtD,QAAM,YAAY,OAAO;AACzB,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,SAAS,kBAAkB,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAACC,aAAY,EAAE,UAAU,UAAU,MAAM;AAI5E,cAAM,aAAa,SAAS,cAAc;AAC1C,cAAM,eAAe,WAAW,UAAU;AAC1C,eAAO,EAAE,GAAGA,aAAY,GAAG,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM,QAAQ,OAAO,EAAE,CAAC,UAAU,UAAU,cAAc,WAAW,IAAI,CAAC,UAAU,CAAC;AAAA,IAC9F;AAAA,EACF;AAEA,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;",
|
|
6
|
+
"names": ["createContext", "createScope", "nextScopes"]
|
|
7
7
|
}
|
package/dist/cjs/index.js
CHANGED
package/dist/cjs/index.js.map
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
2
|
import * as React from "react";
|
|
4
3
|
function createContext(rootComponentName, defaultContext) {
|
|
5
4
|
const Context = React.createContext(defaultContext);
|
|
6
5
|
function Provider(props) {
|
|
7
6
|
const { children, ...context } = props;
|
|
8
7
|
const value = React.useMemo(() => context, Object.values(context));
|
|
9
|
-
return /* @__PURE__ */
|
|
10
|
-
value
|
|
11
|
-
|
|
8
|
+
return /* @__PURE__ */ jsx(Context.Provider, {
|
|
9
|
+
value,
|
|
10
|
+
children
|
|
11
|
+
});
|
|
12
12
|
}
|
|
13
|
-
__name(Provider, "Provider");
|
|
14
13
|
function useContext(consumerName) {
|
|
15
14
|
const context = React.useContext(Context);
|
|
16
15
|
if (context)
|
|
@@ -19,11 +18,9 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
19
18
|
return defaultContext;
|
|
20
19
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
21
20
|
}
|
|
22
|
-
__name(useContext, "useContext");
|
|
23
21
|
Provider.displayName = rootComponentName + "Provider";
|
|
24
22
|
return [Provider, useContext];
|
|
25
23
|
}
|
|
26
|
-
__name(createContext, "createContext");
|
|
27
24
|
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
28
25
|
let defaultContexts = [];
|
|
29
26
|
function createContext2(rootComponentName, defaultContext) {
|
|
@@ -34,11 +31,11 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
34
31
|
const { scope, children, ...context } = props;
|
|
35
32
|
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
36
33
|
const value = React.useMemo(() => context, Object.values(context));
|
|
37
|
-
return /* @__PURE__ */
|
|
38
|
-
value
|
|
39
|
-
|
|
34
|
+
return /* @__PURE__ */ jsx(Context.Provider, {
|
|
35
|
+
value,
|
|
36
|
+
children
|
|
37
|
+
});
|
|
40
38
|
}
|
|
41
|
-
__name(Provider, "Provider");
|
|
42
39
|
function useContext(consumerName, scope) {
|
|
43
40
|
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
44
41
|
const context = React.useContext(Context);
|
|
@@ -48,46 +45,45 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
48
45
|
return defaultContext;
|
|
49
46
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
50
47
|
}
|
|
51
|
-
__name(useContext, "useContext");
|
|
52
48
|
Provider.displayName = rootComponentName + "Provider";
|
|
53
49
|
return [Provider, useContext];
|
|
54
50
|
}
|
|
55
|
-
|
|
56
|
-
const createScope = /* @__PURE__ */ __name(() => {
|
|
51
|
+
const createScope = () => {
|
|
57
52
|
const scopeContexts = defaultContexts.map((defaultContext) => {
|
|
58
53
|
return React.createContext(defaultContext);
|
|
59
54
|
});
|
|
60
|
-
return
|
|
55
|
+
return function useScope(scope) {
|
|
61
56
|
const contexts = (scope == null ? void 0 : scope[scopeName]) || scopeContexts;
|
|
62
|
-
return React.useMemo(
|
|
63
|
-
|
|
64
|
-
|
|
57
|
+
return React.useMemo(
|
|
58
|
+
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
59
|
+
[scope, contexts]
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
};
|
|
65
63
|
createScope.scopeName = scopeName;
|
|
66
64
|
return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
|
|
67
65
|
}
|
|
68
|
-
__name(createContextScope, "createContextScope");
|
|
69
66
|
function composeContextScopes(...scopes) {
|
|
70
67
|
const baseScope = scopes[0];
|
|
71
68
|
if (scopes.length === 1)
|
|
72
69
|
return baseScope;
|
|
73
|
-
const createScope =
|
|
70
|
+
const createScope = () => {
|
|
74
71
|
const scopeHooks = scopes.map((createScope2) => ({
|
|
75
72
|
useScope: createScope2(),
|
|
76
73
|
scopeName: createScope2.scopeName
|
|
77
74
|
}));
|
|
78
|
-
return
|
|
75
|
+
return function useComposedScopes(overrideScopes) {
|
|
79
76
|
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
80
77
|
const scopeProps = useScope(overrideScopes);
|
|
81
78
|
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
82
79
|
return { ...nextScopes2, ...currentScope };
|
|
83
80
|
}, {});
|
|
84
81
|
return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
82
|
+
};
|
|
83
|
+
};
|
|
87
84
|
createScope.scopeName = baseScope.scopeName;
|
|
88
85
|
return createScope;
|
|
89
86
|
}
|
|
90
|
-
__name(composeContextScopes, "composeContextScopes");
|
|
91
87
|
export {
|
|
92
88
|
createContext,
|
|
93
89
|
createContextScope
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/create-context.tsx"],
|
|
4
4
|
"sourcesContent": ["// 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) {\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 // eslint-disable-next-line react-hooks/exhaustive-deps\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) {\n const context = React.useContext(Context)\n if (context) return context\n if (defaultContext !== undefined) return defaultContext\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 Provider.displayName = rootComponentName + 'Provider'\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(scopeName: string, createContextScopeDeps: CreateScope[] = []) {\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 & { scope: Scope<ContextValueType>; children: React.ReactNode }\n ) {\n const { scope, children, ...context } = props\n const Context = scope?.[scopeName][index] || BaseContext\n // Only re-memoize when prop values change\n // eslint-disable-next-line react-hooks/exhaustive-deps\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, scope: Scope<ContextValueType | undefined>) {\n const Context = scope?.[scopeName][index] || BaseContext\n const context = React.useContext(Context)\n if (context) return context\n if (defaultContext !== undefined) return defaultContext\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 Provider.displayName = rootComponentName + 'Provider'\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 return [createContext, composeContextScopes(createScope, ...createContextScopeDeps)] 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 // eslint-disable-next-line react-hooks/rules-of-hooks\n const scopeProps = useScope(overrideScopes)\n const currentScope = scopeProps[`__scope${scopeName}`]\n return { ...nextScopes, ...currentScope }\n }, {})\n\n return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes])\n }\n }\n\n createScope.scopeName = baseScope.scopeName\n return createScope\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": "AAkBW;AAfX,YAAY,WAAW;AAIhB,SAAS,cACd,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UAAM,EAAE,aAAa,QAAQ,IAAI;AAGjC,UAAM,QAAQ,MAAM,QAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,WAAO,oBAAC,QAAQ,UAAR;AAAA,MAAiB;AAAA,MAAe;AAAA,KAAS;AAAA,EACnD;AAEA,WAAS,WAAW,cAAsB;AACxC,UAAM,UAAU,MAAM,WAAW,OAAO;AACxC,QAAI;AAAS,aAAO;AACpB,QAAI,mBAAmB;AAAW,aAAO;AAEzC,UAAM,IAAI,MAAM,KAAK,wCAAwC,qBAAqB;AAAA,EACpF;AAEA,WAAS,cAAc,oBAAoB;AAC3C,SAAO,CAAC,UAAU,UAAU;AAC9B;AAeO,SAAS,mBAAmB,WAAmB,yBAAwC,CAAC,GAAG;AAChG,M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EhC,QAAM,cAA2B,MAAM;AACrC,UAAM,aAAa,OAAO,IAAI,CAACC,kBAAiB;AAAA,MAC9C,UAAUA,aAAY;AAAA,MACtB,WAAWA,aAAY;AAAA,IACzB,EAAE;AAEF,WAAO,SAAS,kBAAkB,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAACC,aAAY,EAAE,UAAU,UAAU,MAAM;AAI5E,cAAM,aAAa,SAAS,cAAc;AAC1C,cAAM,eAAe,WAAW,UAAU;AAC1C,eAAO,EAAE,GAAGA,aAAY,GAAG,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM,QAAQ,OAAO,EAAE,CAAC,UAAU,UAAU,cAAc,WAAW,IAAI,CAAC,UAAU,CAAC;AAAA,IAC9F;AAAA,EACF;AAEA,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;",
|
|
6
|
+
"names": ["createContext", "createScope", "nextScopes"]
|
|
7
7
|
}
|
package/dist/esm/index.js
CHANGED
|
File without changes
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
1
|
import * as React from "react";
|
|
4
2
|
function createContext(rootComponentName, defaultContext) {
|
|
5
3
|
const Context = React.createContext(defaultContext);
|
|
@@ -8,7 +6,6 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
8
6
|
const value = React.useMemo(() => context, Object.values(context));
|
|
9
7
|
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
10
8
|
}
|
|
11
|
-
__name(Provider, "Provider");
|
|
12
9
|
function useContext(consumerName) {
|
|
13
10
|
const context = React.useContext(Context);
|
|
14
11
|
if (context)
|
|
@@ -17,11 +14,9 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
17
14
|
return defaultContext;
|
|
18
15
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
19
16
|
}
|
|
20
|
-
__name(useContext, "useContext");
|
|
21
17
|
Provider.displayName = rootComponentName + "Provider";
|
|
22
18
|
return [Provider, useContext];
|
|
23
19
|
}
|
|
24
|
-
__name(createContext, "createContext");
|
|
25
20
|
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
26
21
|
let defaultContexts = [];
|
|
27
22
|
function createContext2(rootComponentName, defaultContext) {
|
|
@@ -30,13 +25,12 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
30
25
|
defaultContexts = [...defaultContexts, defaultContext];
|
|
31
26
|
function Provider(props) {
|
|
32
27
|
const { scope, children, ...context } = props;
|
|
33
|
-
const Context =
|
|
28
|
+
const Context = scope?.[scopeName][index] || BaseContext;
|
|
34
29
|
const value = React.useMemo(() => context, Object.values(context));
|
|
35
30
|
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
36
31
|
}
|
|
37
|
-
__name(Provider, "Provider");
|
|
38
32
|
function useContext(consumerName, scope) {
|
|
39
|
-
const Context =
|
|
33
|
+
const Context = scope?.[scopeName][index] || BaseContext;
|
|
40
34
|
const context = React.useContext(Context);
|
|
41
35
|
if (context)
|
|
42
36
|
return context;
|
|
@@ -44,47 +38,47 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
44
38
|
return defaultContext;
|
|
45
39
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
46
40
|
}
|
|
47
|
-
__name(useContext, "useContext");
|
|
48
41
|
Provider.displayName = rootComponentName + "Provider";
|
|
49
42
|
return [Provider, useContext];
|
|
50
43
|
}
|
|
51
|
-
|
|
52
|
-
const createScope = /* @__PURE__ */ __name(() => {
|
|
44
|
+
const createScope = () => {
|
|
53
45
|
const scopeContexts = defaultContexts.map((defaultContext) => {
|
|
54
46
|
return React.createContext(defaultContext);
|
|
55
47
|
});
|
|
56
|
-
return
|
|
57
|
-
const contexts =
|
|
58
|
-
return React.useMemo(
|
|
59
|
-
|
|
60
|
-
|
|
48
|
+
return function useScope(scope) {
|
|
49
|
+
const contexts = scope?.[scopeName] || scopeContexts;
|
|
50
|
+
return React.useMemo(
|
|
51
|
+
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
52
|
+
[scope, contexts]
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
};
|
|
61
56
|
createScope.scopeName = scopeName;
|
|
62
57
|
return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
|
|
63
58
|
}
|
|
64
|
-
__name(createContextScope, "createContextScope");
|
|
65
59
|
function composeContextScopes(...scopes) {
|
|
66
60
|
const baseScope = scopes[0];
|
|
67
61
|
if (scopes.length === 1)
|
|
68
62
|
return baseScope;
|
|
69
|
-
const createScope =
|
|
63
|
+
const createScope = () => {
|
|
70
64
|
const scopeHooks = scopes.map((createScope2) => ({
|
|
71
65
|
useScope: createScope2(),
|
|
72
66
|
scopeName: createScope2.scopeName
|
|
73
67
|
}));
|
|
74
|
-
return
|
|
68
|
+
return function useComposedScopes(overrideScopes) {
|
|
75
69
|
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
76
70
|
const scopeProps = useScope(overrideScopes);
|
|
77
71
|
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
78
72
|
return { ...nextScopes2, ...currentScope };
|
|
79
73
|
}, {});
|
|
80
74
|
return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
75
|
+
};
|
|
76
|
+
};
|
|
83
77
|
createScope.scopeName = baseScope.scopeName;
|
|
84
78
|
return createScope;
|
|
85
79
|
}
|
|
86
|
-
__name(composeContextScopes, "composeContextScopes");
|
|
87
80
|
export {
|
|
88
81
|
createContext,
|
|
89
82
|
createContextScope
|
|
90
83
|
};
|
|
84
|
+
//# sourceMappingURL=create-context.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/create-context.tsx"],
|
|
4
|
+
"sourcesContent": ["// 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) {\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 // eslint-disable-next-line react-hooks/exhaustive-deps\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) {\n const context = React.useContext(Context)\n if (context) return context\n if (defaultContext !== undefined) return defaultContext\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 Provider.displayName = rootComponentName + 'Provider'\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(scopeName: string, createContextScopeDeps: CreateScope[] = []) {\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 & { scope: Scope<ContextValueType>; children: React.ReactNode }\n ) {\n const { scope, children, ...context } = props\n const Context = scope?.[scopeName][index] || BaseContext\n // Only re-memoize when prop values change\n // eslint-disable-next-line react-hooks/exhaustive-deps\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, scope: Scope<ContextValueType | undefined>) {\n const Context = scope?.[scopeName][index] || BaseContext\n const context = React.useContext(Context)\n if (context) return context\n if (defaultContext !== undefined) return defaultContext\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 Provider.displayName = rootComponentName + 'Provider'\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 return [createContext, composeContextScopes(createScope, ...createContextScopeDeps)] 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 // eslint-disable-next-line react-hooks/rules-of-hooks\n const scopeProps = useScope(overrideScopes)\n const currentScope = scopeProps[`__scope${scopeName}`]\n return { ...nextScopes, ...currentScope }\n }, {})\n\n return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes])\n }\n }\n\n createScope.scopeName = baseScope.scopeName\n return createScope\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n"],
|
|
5
|
+
"mappings": "AAGA,YAAY,WAAW;AAIhB,SAAS,cACd,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UAAM,EAAE,aAAa,QAAQ,IAAI;AAGjC,UAAM,QAAQ,MAAM,QAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,WAAO,CAAC,QAAQ,SAAS,OAAO,QAAQ,SAAS,EAAzC,QAAQ;AAAA,EAClB;AAEA,WAAS,WAAW,cAAsB;AACxC,UAAM,UAAU,MAAM,WAAW,OAAO;AACxC,QAAI;AAAS,aAAO;AACpB,QAAI,mBAAmB;AAAW,aAAO;AAEzC,UAAM,IAAI,MAAM,KAAK,wCAAwC,qBAAqB;AAAA,EACpF;AAEA,WAAS,cAAc,oBAAoB;AAC3C,SAAO,CAAC,UAAU,UAAU;AAC9B;AAeO,SAAS,mBAAmB,WAAmB,yBAAwC,CAAC,GAAG;AAChG,MAAI,kBAAyB,CAAC;AAM9B,WAASA,eACP,mBACA,gBACA;AACA,UAAM,cAAc,MAAM,cAA4C,cAAc;AACpF,UAAM,QAAQ,gBAAgB;AAC9B,sBAAkB,CAAC,GAAG,iBAAiB,cAAc;AAErD,aAAS,SACP,OACA;AACA,YAAM,EAAE,OAAO,aAAa,QAAQ,IAAI;AACxC,YAAM,UAAU,QAAQ,WAAW,UAAU;AAG7C,YAAM,QAAQ,MAAM,QAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,aAAO,CAAC,QAAQ,SAAS,OAAO,QAAQ,SAAS,EAAzC,QAAQ;AAAA,IAClB;AAEA,aAAS,WAAW,cAAsB,OAA4C;AACpF,YAAM,UAAU,QAAQ,WAAW,UAAU;AAC7C,YAAM,UAAU,MAAM,WAAW,OAA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;AAClC,SAAO;AACT;",
|
|
6
|
+
"names": ["createContext", "createScope", "nextScopes"]
|
|
7
|
+
}
|
package/dist/jsx/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,25 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/create-context",
|
|
3
|
-
"version": "1.0.1-beta.
|
|
3
|
+
"version": "1.0.1-beta.210",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"source": "src/index.ts",
|
|
6
|
-
"
|
|
6
|
+
"types": "./types/index.d.ts",
|
|
7
7
|
"main": "dist/cjs",
|
|
8
8
|
"module": "dist/esm",
|
|
9
9
|
"module:jsx": "dist/jsx",
|
|
10
10
|
"files": [
|
|
11
|
+
"src",
|
|
11
12
|
"types",
|
|
12
13
|
"dist"
|
|
13
14
|
],
|
|
14
15
|
"scripts": {
|
|
15
16
|
"build": "tamagui-build",
|
|
16
|
-
"watch": "tamagui-build --watch"
|
|
17
|
+
"watch": "tamagui-build --watch",
|
|
18
|
+
"lint": "eslint",
|
|
19
|
+
"lint:fix": "yarn lint --fix"
|
|
17
20
|
},
|
|
18
21
|
"peerDependencies": {
|
|
19
22
|
"react": "*"
|
|
20
23
|
},
|
|
21
24
|
"devDependencies": {
|
|
22
|
-
"@tamagui/build": "^1.0.1-beta.
|
|
25
|
+
"@tamagui/build": "^1.0.1-beta.210",
|
|
23
26
|
"react": "*"
|
|
24
27
|
},
|
|
25
28
|
"publishConfig": {
|
|
@@ -0,0 +1,139 @@
|
|
|
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
|
+
) {
|
|
12
|
+
const Context = React.createContext<ContextValueType | undefined>(defaultContext)
|
|
13
|
+
|
|
14
|
+
function Provider(props: ContextValueType & { children: React.ReactNode }) {
|
|
15
|
+
const { children, ...context } = props
|
|
16
|
+
// Only re-memoize when prop values change
|
|
17
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
18
|
+
const value = React.useMemo(() => context, Object.values(context)) as ContextValueType
|
|
19
|
+
return <Context.Provider value={value}>{children}</Context.Provider>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function useContext(consumerName: string) {
|
|
23
|
+
const context = React.useContext(Context)
|
|
24
|
+
if (context) return context
|
|
25
|
+
if (defaultContext !== undefined) return defaultContext
|
|
26
|
+
// if a defaultContext wasn't specified, it's a required context.
|
|
27
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
Provider.displayName = rootComponentName + 'Provider'
|
|
31
|
+
return [Provider, useContext] as const
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/* -------------------------------------------------------------------------------------------------
|
|
35
|
+
* createContextScope
|
|
36
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
37
|
+
|
|
38
|
+
type ScopeHook = (scope: Scope) => { [__scopeProp: string]: Scope }
|
|
39
|
+
|
|
40
|
+
export type Scope<C = any> = { [scopeName: string]: React.Context<C>[] } | undefined
|
|
41
|
+
|
|
42
|
+
export interface CreateScope {
|
|
43
|
+
scopeName: string
|
|
44
|
+
(): ScopeHook
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function createContextScope(scopeName: string, createContextScopeDeps: CreateScope[] = []) {
|
|
48
|
+
let defaultContexts: any[] = []
|
|
49
|
+
|
|
50
|
+
/* -----------------------------------------------------------------------------------------------
|
|
51
|
+
* createContext
|
|
52
|
+
* ---------------------------------------------------------------------------------------------*/
|
|
53
|
+
|
|
54
|
+
function createContext<ContextValueType extends object | null>(
|
|
55
|
+
rootComponentName: string,
|
|
56
|
+
defaultContext?: ContextValueType
|
|
57
|
+
) {
|
|
58
|
+
const BaseContext = React.createContext<ContextValueType | undefined>(defaultContext)
|
|
59
|
+
const index = defaultContexts.length
|
|
60
|
+
defaultContexts = [...defaultContexts, defaultContext]
|
|
61
|
+
|
|
62
|
+
function Provider(
|
|
63
|
+
props: ContextValueType & { scope: Scope<ContextValueType>; children: React.ReactNode }
|
|
64
|
+
) {
|
|
65
|
+
const { scope, children, ...context } = props
|
|
66
|
+
const Context = scope?.[scopeName][index] || BaseContext
|
|
67
|
+
// Only re-memoize when prop values change
|
|
68
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
69
|
+
const value = React.useMemo(() => context, Object.values(context)) as ContextValueType
|
|
70
|
+
return <Context.Provider value={value}>{children}</Context.Provider>
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function useContext(consumerName: string, scope: Scope<ContextValueType | undefined>) {
|
|
74
|
+
const Context = scope?.[scopeName][index] || BaseContext
|
|
75
|
+
const context = React.useContext(Context)
|
|
76
|
+
if (context) return context
|
|
77
|
+
if (defaultContext !== undefined) return defaultContext
|
|
78
|
+
// if a defaultContext wasn't specified, it's a required context.
|
|
79
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
Provider.displayName = rootComponentName + 'Provider'
|
|
83
|
+
return [Provider, useContext] as const
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/* -----------------------------------------------------------------------------------------------
|
|
87
|
+
* createScope
|
|
88
|
+
* ---------------------------------------------------------------------------------------------*/
|
|
89
|
+
|
|
90
|
+
const createScope: CreateScope = () => {
|
|
91
|
+
const scopeContexts = defaultContexts.map((defaultContext) => {
|
|
92
|
+
return React.createContext(defaultContext)
|
|
93
|
+
})
|
|
94
|
+
return function useScope(scope: Scope) {
|
|
95
|
+
const contexts = scope?.[scopeName] || scopeContexts
|
|
96
|
+
return React.useMemo(
|
|
97
|
+
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
98
|
+
[scope, contexts]
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
createScope.scopeName = scopeName
|
|
104
|
+
return [createContext, composeContextScopes(createScope, ...createContextScopeDeps)] as const
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/* -------------------------------------------------------------------------------------------------
|
|
108
|
+
* composeContextScopes
|
|
109
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
110
|
+
|
|
111
|
+
function composeContextScopes(...scopes: CreateScope[]) {
|
|
112
|
+
const baseScope = scopes[0]
|
|
113
|
+
if (scopes.length === 1) return baseScope
|
|
114
|
+
|
|
115
|
+
const createScope: CreateScope = () => {
|
|
116
|
+
const scopeHooks = scopes.map((createScope) => ({
|
|
117
|
+
useScope: createScope(),
|
|
118
|
+
scopeName: createScope.scopeName,
|
|
119
|
+
}))
|
|
120
|
+
|
|
121
|
+
return function useComposedScopes(overrideScopes) {
|
|
122
|
+
const nextScopes = scopeHooks.reduce((nextScopes, { useScope, scopeName }) => {
|
|
123
|
+
// We are calling a hook inside a callback which React warns against to avoid inconsistent
|
|
124
|
+
// renders, however, scoping doesn't have render side effects so we ignore the rule.
|
|
125
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
126
|
+
const scopeProps = useScope(overrideScopes)
|
|
127
|
+
const currentScope = scopeProps[`__scope${scopeName}`]
|
|
128
|
+
return { ...nextScopes, ...currentScope }
|
|
129
|
+
}, {})
|
|
130
|
+
|
|
131
|
+
return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes])
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
createScope.scopeName = baseScope.scopeName
|
|
136
|
+
return createScope
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/* -----------------------------------------------------------------------------------------------*/
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './create-context'
|
|
File without changes
|
package/types/index.d.ts
CHANGED
|
File without changes
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"create-context.d.ts","sourceRoot":"","sources":["../src/create-context.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,oBAAY,WAAW,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,IAAI,CAAC,GAAG;KAAG,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC,EAAE,KAAK;CAAE,CAAA;AAErF,wBAAgB,aAAa,CAAC,gBAAgB,SAAS,MAAM,GAAG,IAAI,EAClE,iBAAiB,EAAE,MAAM,EACzB,cAAc,CAAC,EAAE,gBAAgB;YAIR,gBAAgB,GAAG;QAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;KAAE;;kBAQvC,MAAM,uBAUzC;AAMD,aAAK,SAAS,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK;IAAE,CAAC,WAAW,EAAE,MAAM,GAAG,KAAK,CAAA;CAAE,CAAA;AAEnE,oBAAY,KAAK,CAAC,CAAC,GAAG,GAAG,IAAI;IAAE,CAAC,SAAS,EAAE,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAA;CAAE,GAAG,SAAS,CAAA;AAEpF,MAAM,WAAW,WAAW;IAC1B,SAAS,EAAE,MAAM,CAAA;IACjB,IAAI,SAAS,CAAA;CACd;AAED,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,MAAM,EAAE,sBAAsB,GAAE,WAAW,EAAO,yEAQzE,MAAM;;;kBAQ+C,MAAM,SAAS;;;kBAUrD,MAAM,iFAgC3C"}
|
package/types/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA"}
|