@tamagui/create-context 1.88.12 → 1.88.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/esm/create-context.mjs +94 -0
- package/dist/esm/index.mjs +1 -0
- package/dist/jsx/create-context.mjs +94 -0
- package/dist/jsx/index.mjs +1 -0
- package/package.json +2 -2
- package/src/create-context.tsx +3 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Nate Wienert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
function createContext(rootComponentName, defaultContext) {
|
|
4
|
+
const Context = React.createContext(defaultContext);
|
|
5
|
+
function Provider(props) {
|
|
6
|
+
const {
|
|
7
|
+
children,
|
|
8
|
+
...context
|
|
9
|
+
} = props,
|
|
10
|
+
value = React.useMemo(() => context, Object.values(context));
|
|
11
|
+
return /* @__PURE__ */jsx(Context.Provider, {
|
|
12
|
+
value,
|
|
13
|
+
children
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
function useContext(consumerName) {
|
|
17
|
+
const context = React.useContext(Context);
|
|
18
|
+
if (context) return context;
|
|
19
|
+
if (defaultContext !== void 0) return defaultContext;
|
|
20
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
21
|
+
}
|
|
22
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
23
|
+
}
|
|
24
|
+
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
25
|
+
let defaultContexts = [];
|
|
26
|
+
function createContext2(rootComponentName, defaultContext) {
|
|
27
|
+
const BaseContext = React.createContext(defaultContext),
|
|
28
|
+
index = defaultContexts.length;
|
|
29
|
+
defaultContexts = [...defaultContexts, defaultContext];
|
|
30
|
+
function Provider(props) {
|
|
31
|
+
const {
|
|
32
|
+
scope,
|
|
33
|
+
children,
|
|
34
|
+
...context
|
|
35
|
+
} = props,
|
|
36
|
+
Context = scope?.[scopeName]?.[index] || BaseContext,
|
|
37
|
+
value = React.useMemo(() => context, Object.values(context));
|
|
38
|
+
return /* @__PURE__ */jsx(Context.Provider, {
|
|
39
|
+
value,
|
|
40
|
+
children
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function useContext(consumerName, scope, options) {
|
|
44
|
+
const Context = scope?.[scopeName]?.[index] || BaseContext,
|
|
45
|
+
context = React.useContext(Context);
|
|
46
|
+
if (context) return context;
|
|
47
|
+
if (defaultContext !== void 0) return defaultContext;
|
|
48
|
+
const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
|
|
49
|
+
if (options?.fallback) return options?.warn !== !1 && console.warn(missingContextMessage), options.fallback;
|
|
50
|
+
throw new Error(missingContextMessage);
|
|
51
|
+
}
|
|
52
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
53
|
+
}
|
|
54
|
+
const createScope = () => {
|
|
55
|
+
const scopeContexts = defaultContexts.map(defaultContext => React.createContext(defaultContext));
|
|
56
|
+
return function (scope) {
|
|
57
|
+
const contexts = scope?.[scopeName] || scopeContexts;
|
|
58
|
+
return React.useMemo(() => ({
|
|
59
|
+
[`__scope${scopeName}`]: {
|
|
60
|
+
...scope,
|
|
61
|
+
[scopeName]: contexts
|
|
62
|
+
}
|
|
63
|
+
}), [scope, contexts]);
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
return createScope.scopeName = scopeName, [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
|
|
67
|
+
}
|
|
68
|
+
function composeContextScopes(...scopes) {
|
|
69
|
+
const baseScope = scopes[0];
|
|
70
|
+
if (scopes.length === 1) return baseScope;
|
|
71
|
+
const createScope = () => {
|
|
72
|
+
const scopeHooks = scopes.map(createScope2 => ({
|
|
73
|
+
useScope: createScope2(),
|
|
74
|
+
scopeName: createScope2.scopeName
|
|
75
|
+
}));
|
|
76
|
+
return function (overrideScopes) {
|
|
77
|
+
const nextScopes = scopeHooks.reduce((nextScopes2, {
|
|
78
|
+
useScope,
|
|
79
|
+
scopeName
|
|
80
|
+
}) => {
|
|
81
|
+
const currentScope = useScope(overrideScopes)[`__scope${scopeName}`];
|
|
82
|
+
return {
|
|
83
|
+
...nextScopes2,
|
|
84
|
+
...currentScope
|
|
85
|
+
};
|
|
86
|
+
}, {});
|
|
87
|
+
return React.useMemo(() => ({
|
|
88
|
+
[`__scope${baseScope.scopeName}`]: nextScopes
|
|
89
|
+
}), [nextScopes]);
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
return createScope.scopeName = baseScope.scopeName, createScope;
|
|
93
|
+
}
|
|
94
|
+
export { createContext, createContextScope };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./create-context.mjs";
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
function createContext(rootComponentName, defaultContext) {
|
|
4
|
+
const Context = React.createContext(defaultContext);
|
|
5
|
+
function Provider(props) {
|
|
6
|
+
const {
|
|
7
|
+
children,
|
|
8
|
+
...context
|
|
9
|
+
} = props,
|
|
10
|
+
value = React.useMemo(() => context, Object.values(context));
|
|
11
|
+
return /* @__PURE__ */jsx(Context.Provider, {
|
|
12
|
+
value,
|
|
13
|
+
children
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
function useContext(consumerName) {
|
|
17
|
+
const context = React.useContext(Context);
|
|
18
|
+
if (context) return context;
|
|
19
|
+
if (defaultContext !== void 0) return defaultContext;
|
|
20
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
21
|
+
}
|
|
22
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
23
|
+
}
|
|
24
|
+
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
25
|
+
let defaultContexts = [];
|
|
26
|
+
function createContext2(rootComponentName, defaultContext) {
|
|
27
|
+
const BaseContext = React.createContext(defaultContext),
|
|
28
|
+
index = defaultContexts.length;
|
|
29
|
+
defaultContexts = [...defaultContexts, defaultContext];
|
|
30
|
+
function Provider(props) {
|
|
31
|
+
const {
|
|
32
|
+
scope,
|
|
33
|
+
children,
|
|
34
|
+
...context
|
|
35
|
+
} = props,
|
|
36
|
+
Context = scope?.[scopeName]?.[index] || BaseContext,
|
|
37
|
+
value = React.useMemo(() => context, Object.values(context));
|
|
38
|
+
return /* @__PURE__ */jsx(Context.Provider, {
|
|
39
|
+
value,
|
|
40
|
+
children
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function useContext(consumerName, scope, options) {
|
|
44
|
+
const Context = scope?.[scopeName]?.[index] || BaseContext,
|
|
45
|
+
context = React.useContext(Context);
|
|
46
|
+
if (context) return context;
|
|
47
|
+
if (defaultContext !== void 0) return defaultContext;
|
|
48
|
+
const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
|
|
49
|
+
if (options?.fallback) return options?.warn !== !1 && console.warn(missingContextMessage), options.fallback;
|
|
50
|
+
throw new Error(missingContextMessage);
|
|
51
|
+
}
|
|
52
|
+
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
|
|
53
|
+
}
|
|
54
|
+
const createScope = () => {
|
|
55
|
+
const scopeContexts = defaultContexts.map(defaultContext => React.createContext(defaultContext));
|
|
56
|
+
return function (scope) {
|
|
57
|
+
const contexts = scope?.[scopeName] || scopeContexts;
|
|
58
|
+
return React.useMemo(() => ({
|
|
59
|
+
[`__scope${scopeName}`]: {
|
|
60
|
+
...scope,
|
|
61
|
+
[scopeName]: contexts
|
|
62
|
+
}
|
|
63
|
+
}), [scope, contexts]);
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
return createScope.scopeName = scopeName, [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
|
|
67
|
+
}
|
|
68
|
+
function composeContextScopes(...scopes) {
|
|
69
|
+
const baseScope = scopes[0];
|
|
70
|
+
if (scopes.length === 1) return baseScope;
|
|
71
|
+
const createScope = () => {
|
|
72
|
+
const scopeHooks = scopes.map(createScope2 => ({
|
|
73
|
+
useScope: createScope2(),
|
|
74
|
+
scopeName: createScope2.scopeName
|
|
75
|
+
}));
|
|
76
|
+
return function (overrideScopes) {
|
|
77
|
+
const nextScopes = scopeHooks.reduce((nextScopes2, {
|
|
78
|
+
useScope,
|
|
79
|
+
scopeName
|
|
80
|
+
}) => {
|
|
81
|
+
const currentScope = useScope(overrideScopes)[`__scope${scopeName}`];
|
|
82
|
+
return {
|
|
83
|
+
...nextScopes2,
|
|
84
|
+
...currentScope
|
|
85
|
+
};
|
|
86
|
+
}, {});
|
|
87
|
+
return React.useMemo(() => ({
|
|
88
|
+
[`__scope${baseScope.scopeName}`]: nextScopes
|
|
89
|
+
}), [nextScopes]);
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
return createScope.scopeName = baseScope.scopeName, createScope;
|
|
93
|
+
}
|
|
94
|
+
export { createContext, createContextScope };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./create-context.mjs";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/create-context",
|
|
3
|
-
"version": "1.88.
|
|
3
|
+
"version": "1.88.14",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"types": "./types/index.d.ts",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"react": "*"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@tamagui/build": "1.88.
|
|
36
|
+
"@tamagui/build": "1.88.14",
|
|
37
37
|
"react": "^18.2.0"
|
|
38
38
|
},
|
|
39
39
|
"publishConfig": {
|
package/src/create-context.tsx
CHANGED
|
@@ -14,7 +14,7 @@ export function createContext<ContextValueType extends object | null>(
|
|
|
14
14
|
function Provider(props: ContextValueType & { children: React.ReactNode }) {
|
|
15
15
|
const { children, ...context } = props
|
|
16
16
|
// Only re-memoize when prop values change
|
|
17
|
-
|
|
17
|
+
|
|
18
18
|
const value = React.useMemo(() => context, Object.values(context)) as ContextValueType
|
|
19
19
|
return <Context.Provider value={value}>{children}</Context.Provider>
|
|
20
20
|
}
|
|
@@ -71,7 +71,7 @@ export function createContextScope(
|
|
|
71
71
|
const { scope, children, ...context } = props
|
|
72
72
|
const Context = scope?.[scopeName]?.[index] || BaseContext
|
|
73
73
|
// Only re-memoize when prop values change
|
|
74
|
-
|
|
74
|
+
|
|
75
75
|
const value = React.useMemo(
|
|
76
76
|
() => context,
|
|
77
77
|
Object.values(context)
|
|
@@ -149,7 +149,7 @@ function composeContextScopes(...scopes: CreateScope[]) {
|
|
|
149
149
|
const nextScopes = scopeHooks.reduce((nextScopes, { useScope, scopeName }) => {
|
|
150
150
|
// We are calling a hook inside a callback which React warns against to avoid inconsistent
|
|
151
151
|
// renders, however, scoping doesn't have render side effects so we ignore the rule.
|
|
152
|
-
|
|
152
|
+
|
|
153
153
|
const scopeProps = useScope(overrideScopes)
|
|
154
154
|
const currentScope = scopeProps[`__scope${scopeName}`]
|
|
155
155
|
return { ...nextScopes, ...currentScope }
|