@tamagui/create-context 1.74.3 → 1.74.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,85 @@
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 { children, ...context } = props, value = React.useMemo(() => context, Object.values(context));
7
+ return /* @__PURE__ */ jsx(Context.Provider, { value, children });
8
+ }
9
+ function useContext(consumerName) {
10
+ const context = React.useContext(Context);
11
+ if (context)
12
+ return context;
13
+ if (defaultContext !== void 0)
14
+ return defaultContext;
15
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
16
+ }
17
+ return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
18
+ }
19
+ function createContextScope(scopeName, createContextScopeDeps = []) {
20
+ let defaultContexts = [];
21
+ function createContext2(rootComponentName, defaultContext) {
22
+ const BaseContext = React.createContext(defaultContext), index = defaultContexts.length;
23
+ defaultContexts = [...defaultContexts, defaultContext];
24
+ function Provider(props) {
25
+ const { scope, children, ...context } = props, Context = scope?.[scopeName]?.[index] || BaseContext, value = React.useMemo(
26
+ () => context,
27
+ Object.values(context)
28
+ );
29
+ return /* @__PURE__ */ jsx(Context.Provider, { value, children });
30
+ }
31
+ function useContext(consumerName, scope, options) {
32
+ const Context = scope?.[scopeName]?.[index] || BaseContext, context = React.useContext(Context);
33
+ if (context)
34
+ return context;
35
+ if (defaultContext !== void 0)
36
+ return defaultContext;
37
+ const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
38
+ if (options?.fallback)
39
+ return options?.warn !== !1 && console.warn(missingContextMessage), options.fallback;
40
+ throw new Error(missingContextMessage);
41
+ }
42
+ return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext];
43
+ }
44
+ const createScope = () => {
45
+ const scopeContexts = defaultContexts.map((defaultContext) => React.createContext(defaultContext));
46
+ return function(scope) {
47
+ const contexts = scope?.[scopeName] || scopeContexts;
48
+ return React.useMemo(
49
+ () => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
50
+ [scope, contexts]
51
+ );
52
+ };
53
+ };
54
+ return createScope.scopeName = scopeName, [
55
+ createContext2,
56
+ composeContextScopes(createScope, ...createContextScopeDeps)
57
+ ];
58
+ }
59
+ function composeContextScopes(...scopes) {
60
+ const baseScope = scopes[0];
61
+ if (scopes.length === 1)
62
+ return baseScope;
63
+ const createScope = () => {
64
+ const scopeHooks = scopes.map((createScope2) => ({
65
+ useScope: createScope2(),
66
+ scopeName: createScope2.scopeName
67
+ }));
68
+ return function(overrideScopes) {
69
+ const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
70
+ const currentScope = useScope(overrideScopes)[`__scope${scopeName}`];
71
+ return { ...nextScopes2, ...currentScope };
72
+ }, {});
73
+ return React.useMemo(
74
+ () => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),
75
+ [nextScopes]
76
+ );
77
+ };
78
+ };
79
+ return createScope.scopeName = baseScope.scopeName, createScope;
80
+ }
81
+ export {
82
+ createContext,
83
+ createContextScope
84
+ };
85
+ //# sourceMappingURL=create-context.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/create-context.tsx"],
4
+ "mappings": "AAGA,YAAY,WAAW;AAeZ;AAXJ,SAAS,cACd,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UAAM,EAAE,UAAU,GAAG,QAAQ,IAAI,OAG3B,QAAQ,MAAM,QAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,WAAO,oBAAC,QAAQ,UAAR,EAAiB,OAAe,UAAS;AAAA,EACnD;AAEA,WAAS,WAAW,cAA0D;AAC5E,UAAM,UAAU,MAAM,WAAW,OAAO;AACxC,QAAI;AAAS,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A,QACX,OAAO,EAAE,CAAC,UAAU,SAAS,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,EAAE;AAAA,QACtE,CAAC,OAAO,QAAQ;AAAA,MAClB;AAAA,IACF;AAAA,EACF;AAEA,qBAAY,YAAY,WACjB;AAAA,IACLA;AAAA,IACA,qBAAqB,aAAa,GAAG,sBAAsB;AAAA,EAC7D;AACF;AAMA,SAAS,wBAAwB,QAAuB;AACtD,QAAM,YAAY,OAAO,CAAC;AAC1B,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,SAA2B,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAACC,aAAY,EAAE,UAAU,UAAU,MAAM;AAK5E,cAAM,eADa,SAAS,cAAc,EACV,UAAU,SAAS,EAAE;AACrD,eAAO,EAAE,GAAGA,aAAY,GAAG,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,UAAU,SAAS,EAAE,GAAG,WAAW;AAAA,QACvD,CAAC,UAAU;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAEA,qBAAY,YAAY,UAAU,WAC3B;AACT;",
5
+ "names": ["createContext", "createScope", "nextScopes"]
6
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./create-context";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.ts"],
4
+ "mappings": "AAAA,cAAc;",
5
+ "names": []
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/create-context",
3
- "version": "1.74.3",
3
+ "version": "1.74.5",
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.74.3",
33
+ "@tamagui/build": "1.74.5",
34
34
  "react": "^18.2.0"
35
35
  },
36
36
  "publishConfig": {