@tamagui/create-context 1.0.0-canary.204 → 1.0.1-beta.102
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 +8 -17
- package/dist/cjs/create-context.js.map +1 -1
- package/dist/esm/create-context.js +42 -21
- package/dist/esm/create-context.js.map +1 -1
- package/dist/jsx/create-context.js +9 -18
- 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 +5 -4
- package/src/create-context.tsx +139 -0
- package/src/index.ts +1 -0
- package/types/create-context.d.ts +0 -0
- package/types/create-context.d.ts.map +0 -0
- package/types/index.d.ts +0 -0
- package/types/index.d.ts.map +0 -0
- package/LICENSE +0 -21
|
@@ -21,7 +21,6 @@ var __spreadValues = (a, b) => {
|
|
|
21
21
|
return a;
|
|
22
22
|
};
|
|
23
23
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
25
24
|
var __objRest = (source, exclude) => {
|
|
26
25
|
var target = {};
|
|
27
26
|
for (var prop in source)
|
|
@@ -64,7 +63,6 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
64
63
|
value
|
|
65
64
|
}, children);
|
|
66
65
|
}
|
|
67
|
-
__name(Provider, "Provider");
|
|
68
66
|
function useContext(consumerName) {
|
|
69
67
|
const context = React.useContext(Context);
|
|
70
68
|
if (context)
|
|
@@ -73,11 +71,9 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
73
71
|
return defaultContext;
|
|
74
72
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
75
73
|
}
|
|
76
|
-
__name(useContext, "useContext");
|
|
77
74
|
Provider.displayName = rootComponentName + "Provider";
|
|
78
75
|
return [Provider, useContext];
|
|
79
76
|
}
|
|
80
|
-
__name(createContext, "createContext");
|
|
81
77
|
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
82
78
|
let defaultContexts = [];
|
|
83
79
|
function createContext2(rootComponentName, defaultContext) {
|
|
@@ -92,7 +88,6 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
92
88
|
value
|
|
93
89
|
}, children);
|
|
94
90
|
}
|
|
95
|
-
__name(Provider, "Provider");
|
|
96
91
|
function useContext(consumerName, scope) {
|
|
97
92
|
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
98
93
|
const context = React.useContext(Context);
|
|
@@ -102,46 +97,42 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
102
97
|
return defaultContext;
|
|
103
98
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
104
99
|
}
|
|
105
|
-
__name(useContext, "useContext");
|
|
106
100
|
Provider.displayName = rootComponentName + "Provider";
|
|
107
101
|
return [Provider, useContext];
|
|
108
102
|
}
|
|
109
|
-
|
|
110
|
-
const createScope = /* @__PURE__ */ __name(() => {
|
|
103
|
+
const createScope = () => {
|
|
111
104
|
const scopeContexts = defaultContexts.map((defaultContext) => {
|
|
112
105
|
return React.createContext(defaultContext);
|
|
113
106
|
});
|
|
114
|
-
return
|
|
107
|
+
return function useScope(scope) {
|
|
115
108
|
const contexts = (scope == null ? void 0 : scope[scopeName]) || scopeContexts;
|
|
116
109
|
return React.useMemo(() => ({ [`__scope${scopeName}`]: __spreadProps(__spreadValues({}, scope), { [scopeName]: contexts }) }), [scope, contexts]);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
110
|
+
};
|
|
111
|
+
};
|
|
119
112
|
createScope.scopeName = scopeName;
|
|
120
113
|
return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
|
|
121
114
|
}
|
|
122
|
-
__name(createContextScope, "createContextScope");
|
|
123
115
|
function composeContextScopes(...scopes) {
|
|
124
116
|
const baseScope = scopes[0];
|
|
125
117
|
if (scopes.length === 1)
|
|
126
118
|
return baseScope;
|
|
127
|
-
const createScope =
|
|
119
|
+
const createScope = () => {
|
|
128
120
|
const scopeHooks = scopes.map((createScope2) => ({
|
|
129
121
|
useScope: createScope2(),
|
|
130
122
|
scopeName: createScope2.scopeName
|
|
131
123
|
}));
|
|
132
|
-
return
|
|
124
|
+
return function useComposedScopes(overrideScopes) {
|
|
133
125
|
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
134
126
|
const scopeProps = useScope(overrideScopes);
|
|
135
127
|
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
136
128
|
return __spreadValues(__spreadValues({}, nextScopes2), currentScope);
|
|
137
129
|
}, {});
|
|
138
130
|
return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
131
|
+
};
|
|
132
|
+
};
|
|
141
133
|
createScope.scopeName = baseScope.scopeName;
|
|
142
134
|
return createScope;
|
|
143
135
|
}
|
|
144
|
-
__name(composeContextScopes, "composeContextScopes");
|
|
145
136
|
// Annotate the CommonJS export names for ESM import in node:
|
|
146
137
|
0 && (module.exports = {
|
|
147
138
|
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": "
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,YAAuB;AAIhB,uBACL,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,oBAAkB,OAAyD;AACzE,UAAiC,YAAzB,eAAyB,IAAZ,oBAAY,IAAZ,CAAb;AAGR,UAAM,QAAQ,MAAM,QAAQ,MAAM,SAAS,OAAO,OAAO,OAAO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ACrC,YAAM,WAAW,gCAAQ,eAAc;AACvC,aAAO,MAAM,QACX,MAAO,GAAE,CAAC,UAAU,cAAc,iCAAK,QAAL,EAAY,CAAC,YAAY,SAAS,GAAE,IACtE,CAAC,OAAO,QAAQ,CAClB;AAAA,IACF;AAAA,EACF;AAEA,cAAY,YAAY;AACxB,SAAO,CAAC,gBAAe,qBAAqB,aAAa,GAAG,sBAAsB,CAAC;AACrF;AAMA,iCAAiC,QAAuB;AACtD,QAAM,YAAY,OAAO;AACzB,MAAI,OAAO,WAAW;AAAG,WAAO;AAEhC,QAAM,cAA2B,MAAM;AACrC,UAAM,aAAa,OAAO,IAAI,CAAC,iBAAiB;AAAA,MAC9C,UAAU,aAAY;AAAA,MACtB,WAAW,aAAY;AAAA,IACzB,EAAE;AAEF,WAAO,2BAA2B,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAAC,aAAY,EAAE,UAAU,gBAAgB;AAI5E,cAAM,aAAa,SAAS,cAAc;AAC1C,cAAM,eAAe,WAAW,UAAU;AAC1C,eAAO,kCAAK,cAAe;AAAA,MAC7B,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM,QAAQ,MAAO,GAAE,CAAC,UAAU,UAAU,cAAc,WAAW,IAAI,CAAC,UAAU,CAAC;AAAA,IAC9F;AAAA,EACF;AAEA,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,16 +1,44 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
3
32
|
import * as React from "react";
|
|
4
33
|
function createContext(rootComponentName, defaultContext) {
|
|
5
34
|
const Context = React.createContext(defaultContext);
|
|
6
35
|
function Provider(props) {
|
|
7
|
-
const { children,
|
|
36
|
+
const _a = props, { children } = _a, context = __objRest(_a, ["children"]);
|
|
8
37
|
const value = React.useMemo(() => context, Object.values(context));
|
|
9
38
|
return /* @__PURE__ */ React.createElement(Context.Provider, {
|
|
10
39
|
value
|
|
11
40
|
}, children);
|
|
12
41
|
}
|
|
13
|
-
__name(Provider, "Provider");
|
|
14
42
|
function useContext(consumerName) {
|
|
15
43
|
const context = React.useContext(Context);
|
|
16
44
|
if (context)
|
|
@@ -19,11 +47,9 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
19
47
|
return defaultContext;
|
|
20
48
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
21
49
|
}
|
|
22
|
-
__name(useContext, "useContext");
|
|
23
50
|
Provider.displayName = rootComponentName + "Provider";
|
|
24
51
|
return [Provider, useContext];
|
|
25
52
|
}
|
|
26
|
-
__name(createContext, "createContext");
|
|
27
53
|
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
28
54
|
let defaultContexts = [];
|
|
29
55
|
function createContext2(rootComponentName, defaultContext) {
|
|
@@ -31,14 +57,13 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
31
57
|
const index = defaultContexts.length;
|
|
32
58
|
defaultContexts = [...defaultContexts, defaultContext];
|
|
33
59
|
function Provider(props) {
|
|
34
|
-
const { scope, children,
|
|
60
|
+
const _a = props, { scope, children } = _a, context = __objRest(_a, ["scope", "children"]);
|
|
35
61
|
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
36
62
|
const value = React.useMemo(() => context, Object.values(context));
|
|
37
63
|
return /* @__PURE__ */ React.createElement(Context.Provider, {
|
|
38
64
|
value
|
|
39
65
|
}, children);
|
|
40
66
|
}
|
|
41
|
-
__name(Provider, "Provider");
|
|
42
67
|
function useContext(consumerName, scope) {
|
|
43
68
|
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
44
69
|
const context = React.useContext(Context);
|
|
@@ -48,46 +73,42 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
48
73
|
return defaultContext;
|
|
49
74
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
50
75
|
}
|
|
51
|
-
__name(useContext, "useContext");
|
|
52
76
|
Provider.displayName = rootComponentName + "Provider";
|
|
53
77
|
return [Provider, useContext];
|
|
54
78
|
}
|
|
55
|
-
|
|
56
|
-
const createScope = /* @__PURE__ */ __name(() => {
|
|
79
|
+
const createScope = () => {
|
|
57
80
|
const scopeContexts = defaultContexts.map((defaultContext) => {
|
|
58
81
|
return React.createContext(defaultContext);
|
|
59
82
|
});
|
|
60
|
-
return
|
|
83
|
+
return function useScope(scope) {
|
|
61
84
|
const contexts = (scope == null ? void 0 : scope[scopeName]) || scopeContexts;
|
|
62
|
-
return React.useMemo(() => ({ [`__scope${scopeName}`]: {
|
|
63
|
-
}
|
|
64
|
-
}
|
|
85
|
+
return React.useMemo(() => ({ [`__scope${scopeName}`]: __spreadProps(__spreadValues({}, scope), { [scopeName]: contexts }) }), [scope, contexts]);
|
|
86
|
+
};
|
|
87
|
+
};
|
|
65
88
|
createScope.scopeName = scopeName;
|
|
66
89
|
return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
|
|
67
90
|
}
|
|
68
|
-
__name(createContextScope, "createContextScope");
|
|
69
91
|
function composeContextScopes(...scopes) {
|
|
70
92
|
const baseScope = scopes[0];
|
|
71
93
|
if (scopes.length === 1)
|
|
72
94
|
return baseScope;
|
|
73
|
-
const createScope =
|
|
95
|
+
const createScope = () => {
|
|
74
96
|
const scopeHooks = scopes.map((createScope2) => ({
|
|
75
97
|
useScope: createScope2(),
|
|
76
98
|
scopeName: createScope2.scopeName
|
|
77
99
|
}));
|
|
78
|
-
return
|
|
100
|
+
return function useComposedScopes(overrideScopes) {
|
|
79
101
|
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
80
102
|
const scopeProps = useScope(overrideScopes);
|
|
81
103
|
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
82
|
-
return {
|
|
104
|
+
return __spreadValues(__spreadValues({}, nextScopes2), currentScope);
|
|
83
105
|
}, {});
|
|
84
106
|
return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
107
|
+
};
|
|
108
|
+
};
|
|
87
109
|
createScope.scopeName = baseScope.scopeName;
|
|
88
110
|
return createScope;
|
|
89
111
|
}
|
|
90
|
-
__name(composeContextScopes, "composeContextScopes");
|
|
91
112
|
export {
|
|
92
113
|
createContext,
|
|
93
114
|
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": "
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA;AAIO,uBACL,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,oBAAkB,OAAyD;AACzE,UAAiC,YAAzB,eAAyB,IAAZ,oBAAY,IAAZ,CAAb;AAGR,UAAM,QAAQ,MAAM,QAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,WAAO,oCAAC,QAAQ,UAAR;AAAA,MAAiB;AAAA,OAAe,QAAS;AAAA,EACnD;AAEA,sBAAoB,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,4BAA4B,WAAmB,yBAAwC,CAAC,GAAG;AAChG,MAAI,kBAAyB,CAAC;AAM9B,0BACE,mBACA,gBACA;AACA,UAAM,cAAc,MAAM,cAA4C,cAAc;AACpF,UAAM,QAAQ,gBAAgB;AAC9B,sBAAkB,CAAC,GAAG,iBAAiB,cAAc;AAErD,sBACE,OACA;AACA,YAAwC,YAAhC,SAAO,aAAyB,IAAZ,oBAAY,IAAZ,CAApB,SAAO;AACf,YAAM,UAAU,gCAAQ,WAAW,WAAU;AAG7C,YAAM,QAAQ,MAAM,QAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,aAAO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AAO,GAAE,CAAC,UAAU,UAAU,cAAc,WAAW,IAAI,CAAC,UAAU,CAAC;AAAA,IAC9F;AAAA,EACF;AAEA,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -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) {
|
|
@@ -34,7 +29,6 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
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
33
|
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
40
34
|
const context = React.useContext(Context);
|
|
@@ -44,47 +38,44 @@ 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
|
|
48
|
+
return function useScope(scope) {
|
|
57
49
|
const contexts = (scope == null ? void 0 : scope[scopeName]) || scopeContexts;
|
|
58
50
|
return React.useMemo(() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }), [scope, contexts]);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
51
|
+
};
|
|
52
|
+
};
|
|
61
53
|
createScope.scopeName = scopeName;
|
|
62
54
|
return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
|
|
63
55
|
}
|
|
64
|
-
__name(createContextScope, "createContextScope");
|
|
65
56
|
function composeContextScopes(...scopes) {
|
|
66
57
|
const baseScope = scopes[0];
|
|
67
58
|
if (scopes.length === 1)
|
|
68
59
|
return baseScope;
|
|
69
|
-
const createScope =
|
|
60
|
+
const createScope = () => {
|
|
70
61
|
const scopeHooks = scopes.map((createScope2) => ({
|
|
71
62
|
useScope: createScope2(),
|
|
72
63
|
scopeName: createScope2.scopeName
|
|
73
64
|
}));
|
|
74
|
-
return
|
|
65
|
+
return function useComposedScopes(overrideScopes) {
|
|
75
66
|
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
76
67
|
const scopeProps = useScope(overrideScopes);
|
|
77
68
|
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
78
69
|
return { ...nextScopes2, ...currentScope };
|
|
79
70
|
}, {});
|
|
80
71
|
return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
72
|
+
};
|
|
73
|
+
};
|
|
83
74
|
createScope.scopeName = baseScope.scopeName;
|
|
84
75
|
return createScope;
|
|
85
76
|
}
|
|
86
|
-
__name(composeContextScopes, "composeContextScopes");
|
|
87
77
|
export {
|
|
88
78
|
createContext,
|
|
89
79
|
createContextScope
|
|
90
80
|
};
|
|
81
|
+
//# 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;AAIO,uBACL,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,oBAAkB,OAAyD;AACzE,UAAM,EAAE,aAAa,YAAY;AAGjC,UAAM,QAAQ,MAAM,QAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,WAAO,CAAC,QAAQ,SAAS,OAAO,QAAQ,SAAS,EAAzC,QAAQ;AAAA,EAClB;AAEA,sBAAoB,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,4BAA4B,WAAmB,yBAAwC,CAAC,GAAG;AAChG,MAAI,kBAAyB,CAAC;AAM9B,0BACE,mBACA,gBACA;AACA,UA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,WAAW,aAAY;AAAA,IACzB,EAAE;AAEF,WAAO,2BAA2B,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAAC,aAAY,EAAE,UAAU,gBAAgB;AAI5E,cAAM,aAAa,SAAS,cAAc;AAC1C,cAAM,eAAe,WAAW,UAAU;AAC1C,eAAO,EAAE,GAAG,aAAY,GAAG,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM,QAAQ,MAAO,GAAE,CAAC,UAAU,UAAU,cAAc,WAAW,IAAI,CAAC,UAAU,CAAC;AAAA,IAC9F;AAAA,EACF;AAEA,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/jsx/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/create-context",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1-beta.102",
|
|
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
|
],
|
|
@@ -19,11 +20,11 @@
|
|
|
19
20
|
"react": "*"
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|
|
22
|
-
"@tamagui/build": "^1.0.
|
|
23
|
+
"@tamagui/build": "^1.0.1-beta.102",
|
|
23
24
|
"react": "*"
|
|
24
25
|
},
|
|
25
26
|
"publishConfig": {
|
|
26
27
|
"access": "public"
|
|
27
28
|
},
|
|
28
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "a49cc7ea6b93ba384e77a4880ae48ac4a5635c14"
|
|
29
30
|
}
|
|
@@ -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
|
|
File without changes
|
package/types/index.d.ts
CHANGED
|
File without changes
|
package/types/index.d.ts.map
CHANGED
|
File without changes
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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.
|