@tamagui/create-context 1.27.2 → 1.27.3
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/package.json +2 -2
- package/dist/esm/create-context.mjs +0 -102
- package/dist/esm/index.mjs +0 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/create-context",
|
|
3
|
-
"version": "1.27.
|
|
3
|
+
"version": "1.27.3",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"types": "./types/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"react": "*"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@tamagui/build": "1.27.
|
|
33
|
+
"@tamagui/build": "1.27.3",
|
|
34
34
|
"react": "^18.2.0"
|
|
35
35
|
},
|
|
36
36
|
"publishConfig": {
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import * as React from "react";
|
|
3
|
-
function createContext(rootComponentName, defaultContext) {
|
|
4
|
-
const Context = React.createContext(defaultContext);
|
|
5
|
-
function Provider(props) {
|
|
6
|
-
const { children, ...context } = props;
|
|
7
|
-
const value = React.useMemo(() => context, Object.values(context));
|
|
8
|
-
return /* @__PURE__ */ jsx(Context.Provider, { value, children });
|
|
9
|
-
}
|
|
10
|
-
function useContext(consumerName) {
|
|
11
|
-
const context = React.useContext(Context);
|
|
12
|
-
if (context)
|
|
13
|
-
return context;
|
|
14
|
-
if (defaultContext !== void 0)
|
|
15
|
-
return defaultContext;
|
|
16
|
-
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
17
|
-
}
|
|
18
|
-
Provider.displayName = `${rootComponentName}Provider`;
|
|
19
|
-
return [Provider, useContext];
|
|
20
|
-
}
|
|
21
|
-
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
22
|
-
let defaultContexts = [];
|
|
23
|
-
function createContext2(rootComponentName, defaultContext) {
|
|
24
|
-
const BaseContext = React.createContext(defaultContext);
|
|
25
|
-
const index = defaultContexts.length;
|
|
26
|
-
defaultContexts = [...defaultContexts, defaultContext];
|
|
27
|
-
function Provider(props) {
|
|
28
|
-
const { scope, children, ...context } = props;
|
|
29
|
-
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
30
|
-
const value = React.useMemo(
|
|
31
|
-
() => context,
|
|
32
|
-
Object.values(context)
|
|
33
|
-
);
|
|
34
|
-
return /* @__PURE__ */ jsx(Context.Provider, { value, children });
|
|
35
|
-
}
|
|
36
|
-
function useContext(consumerName, scope, options) {
|
|
37
|
-
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
38
|
-
const context = React.useContext(Context);
|
|
39
|
-
if (context)
|
|
40
|
-
return context;
|
|
41
|
-
if (defaultContext !== void 0)
|
|
42
|
-
return defaultContext;
|
|
43
|
-
const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
|
|
44
|
-
if (options == null ? void 0 : options.fallback) {
|
|
45
|
-
if ((options == null ? void 0 : options.warn) !== false) {
|
|
46
|
-
console.warn(missingContextMessage);
|
|
47
|
-
}
|
|
48
|
-
return options.fallback;
|
|
49
|
-
} else {
|
|
50
|
-
throw new Error(missingContextMessage);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
Provider.displayName = `${rootComponentName}Provider`;
|
|
54
|
-
return [Provider, useContext];
|
|
55
|
-
}
|
|
56
|
-
const createScope = () => {
|
|
57
|
-
const scopeContexts = defaultContexts.map((defaultContext) => {
|
|
58
|
-
return React.createContext(defaultContext);
|
|
59
|
-
});
|
|
60
|
-
return function useScope(scope) {
|
|
61
|
-
const contexts = (scope == null ? void 0 : scope[scopeName]) || scopeContexts;
|
|
62
|
-
return React.useMemo(
|
|
63
|
-
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
64
|
-
[scope, contexts]
|
|
65
|
-
);
|
|
66
|
-
};
|
|
67
|
-
};
|
|
68
|
-
createScope.scopeName = scopeName;
|
|
69
|
-
return [
|
|
70
|
-
createContext2,
|
|
71
|
-
composeContextScopes(createScope, ...createContextScopeDeps)
|
|
72
|
-
];
|
|
73
|
-
}
|
|
74
|
-
function composeContextScopes(...scopes) {
|
|
75
|
-
const baseScope = scopes[0];
|
|
76
|
-
if (scopes.length === 1)
|
|
77
|
-
return baseScope;
|
|
78
|
-
const createScope = () => {
|
|
79
|
-
const scopeHooks = scopes.map((createScope2) => ({
|
|
80
|
-
useScope: createScope2(),
|
|
81
|
-
scopeName: createScope2.scopeName
|
|
82
|
-
}));
|
|
83
|
-
return function useComposedScopes(overrideScopes) {
|
|
84
|
-
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
85
|
-
const scopeProps = useScope(overrideScopes);
|
|
86
|
-
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
87
|
-
return { ...nextScopes2, ...currentScope };
|
|
88
|
-
}, {});
|
|
89
|
-
return React.useMemo(
|
|
90
|
-
() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),
|
|
91
|
-
[nextScopes]
|
|
92
|
-
);
|
|
93
|
-
};
|
|
94
|
-
};
|
|
95
|
-
createScope.scopeName = baseScope.scopeName;
|
|
96
|
-
return createScope;
|
|
97
|
-
}
|
|
98
|
-
export {
|
|
99
|
-
createContext,
|
|
100
|
-
createContextScope
|
|
101
|
-
};
|
|
102
|
-
//# sourceMappingURL=create-context.mjs.map
|
package/dist/esm/index.mjs
DELETED