@tamagui/create-context 1.2.8 → 1.2.10
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 +13 -14
- package/dist/cjs/create-context.js.map +2 -2
- package/dist/esm/create-context.js +13 -14
- package/dist/esm/create-context.js.map +2 -2
- package/dist/esm/create-context.mjs +102 -0
- package/dist/esm/create-context.mjs.map +7 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/index.mjs.map +7 -0
- package/dist/jsx/create-context.js +13 -14
- package/dist/jsx/create-context.js.map +2 -2
- package/dist/jsx/create-context.mjs +101 -0
- package/dist/jsx/create-context.mjs.map +7 -0
- package/dist/jsx/index.mjs +2 -0
- package/dist/jsx/index.mjs.map +7 -0
- package/package.json +2 -2
- package/src/create-context.tsx +28 -24
- package/types/create-context.d.ts +5 -2
- package/types/create-context.d.ts.map +1 -1
|
@@ -38,10 +38,7 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
38
38
|
const Context = React.createContext(defaultContext);
|
|
39
39
|
function Provider(props) {
|
|
40
40
|
const { children, ...context } = props;
|
|
41
|
-
const value = React.useMemo(
|
|
42
|
-
() => context,
|
|
43
|
-
Object.values(context)
|
|
44
|
-
);
|
|
41
|
+
const value = React.useMemo(() => context, Object.values(context));
|
|
45
42
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
|
|
46
43
|
}
|
|
47
44
|
function useContext(consumerName) {
|
|
@@ -50,9 +47,7 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
50
47
|
return context;
|
|
51
48
|
if (defaultContext !== void 0)
|
|
52
49
|
return defaultContext;
|
|
53
|
-
throw new Error(
|
|
54
|
-
`\`${consumerName}\` must be used within \`${rootComponentName}\``
|
|
55
|
-
);
|
|
50
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
56
51
|
}
|
|
57
52
|
Provider.displayName = `${rootComponentName}Provider`;
|
|
58
53
|
return [Provider, useContext];
|
|
@@ -60,9 +55,7 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
60
55
|
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
61
56
|
let defaultContexts = [];
|
|
62
57
|
function createContext2(rootComponentName, defaultContext) {
|
|
63
|
-
const BaseContext = React.createContext(
|
|
64
|
-
defaultContext
|
|
65
|
-
);
|
|
58
|
+
const BaseContext = React.createContext(defaultContext);
|
|
66
59
|
const index = defaultContexts.length;
|
|
67
60
|
defaultContexts = [...defaultContexts, defaultContext];
|
|
68
61
|
function Provider(props) {
|
|
@@ -74,16 +67,22 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
74
67
|
);
|
|
75
68
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
|
|
76
69
|
}
|
|
77
|
-
function useContext(consumerName, scope) {
|
|
70
|
+
function useContext(consumerName, scope, options) {
|
|
78
71
|
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
79
72
|
const context = React.useContext(Context);
|
|
80
73
|
if (context)
|
|
81
74
|
return context;
|
|
82
75
|
if (defaultContext !== void 0)
|
|
83
76
|
return defaultContext;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
77
|
+
const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
|
|
78
|
+
if (options == null ? void 0 : options.fallback) {
|
|
79
|
+
if ((options == null ? void 0 : options.warn) !== false) {
|
|
80
|
+
console.warn(missingContextMessage);
|
|
81
|
+
}
|
|
82
|
+
return options.fallback;
|
|
83
|
+
} else {
|
|
84
|
+
throw new Error(missingContextMessage);
|
|
85
|
+
}
|
|
87
86
|
}
|
|
88
87
|
Provider.displayName = `${rootComponentName}Provider`;
|
|
89
88
|
return [Provider, useContext];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
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): Exclude<typeof context, undefined> {\n const context = React.useContext(Context)\n if (context) return context as any\n if (defaultContext !== undefined) return defaultContext as any\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(\n scopeName: string,\n createContextScopeDeps: CreateScope[] = []\n) {\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 & {\n scope: Scope<ContextValueType>\n children: React.ReactNode\n }\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(\n () => context,\n Object.values(context)\n ) as ContextValueType\n return <Context.Provider value={value}>{children}</Context.Provider>\n }\n\n function useContext(\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n options?: {\n warn?: boolean\n fallback?: Partial<ContextValueType>\n }\n ) {\n const Context = scope?.[scopeName][index] || BaseContext\n const context = React.useContext(Context)\n if (context) return context\n // if a defaultContext wasn't specified, it's a required context.\n if (defaultContext !== undefined) return defaultContext\n const missingContextMessage = `\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``\n // fallback can be given per-hook as well\n if (options?.fallback) {\n if (options?.warn !== false) {\n console.warn(missingContextMessage)\n }\n return options.fallback as ContextValueType\n } else {\n throw new Error(missingContextMessage)\n }\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 [\n createContext,\n composeContextScopes(createScope, ...createContextScopeDeps),\n ] 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(\n () => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),\n [nextScopes]\n )\n }\n }\n\n createScope.scopeName = baseScope.scopeName\n return createScope\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n"],
|
|
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,UAAU,GAAG,QAAQ,IAAI;AAGjC,UAAM,QAAQ,MAAM,QAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,WAAO,4CAAC,QAAQ,UAAR,EAAiB,OAAe,UAAS;AAAA,EACnD;AAEA,WAAS,WAAW,cAA0D;AAC5E,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,GAAG;AAC1B,SAAO,CAAC,UAAU,UAAU;AAC9B;AAeO,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OAAO,QAAQ;AAAA,MAClB;AAAA,IACF;AAAA,EACF;AAEA,cAAY,YAAY;AACxB,SAAO;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,SAAS,kBAAkB,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAACC,aAAY,EAAE,UAAU,UAAU,MAAM;AAI5E,cAAM,aAAa,SAAS,cAAc;AAC1C,cAAM,eAAe,WAAW,UAAU,WAAW;AACrD,eAAO,EAAE,GAAGA,aAAY,GAAG,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,UAAU,WAAW,GAAG,WAAW;AAAA,QACvD,CAAC,UAAU;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAEA,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;",
|
|
6
6
|
"names": ["createContext", "createScope", "nextScopes"]
|
|
7
7
|
}
|
|
@@ -4,10 +4,7 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
4
4
|
const Context = React.createContext(defaultContext);
|
|
5
5
|
function Provider(props) {
|
|
6
6
|
const { children, ...context } = props;
|
|
7
|
-
const value = React.useMemo(
|
|
8
|
-
() => context,
|
|
9
|
-
Object.values(context)
|
|
10
|
-
);
|
|
7
|
+
const value = React.useMemo(() => context, Object.values(context));
|
|
11
8
|
return /* @__PURE__ */ jsx(Context.Provider, { value, children });
|
|
12
9
|
}
|
|
13
10
|
function useContext(consumerName) {
|
|
@@ -16,9 +13,7 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
16
13
|
return context;
|
|
17
14
|
if (defaultContext !== void 0)
|
|
18
15
|
return defaultContext;
|
|
19
|
-
throw new Error(
|
|
20
|
-
`\`${consumerName}\` must be used within \`${rootComponentName}\``
|
|
21
|
-
);
|
|
16
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
22
17
|
}
|
|
23
18
|
Provider.displayName = `${rootComponentName}Provider`;
|
|
24
19
|
return [Provider, useContext];
|
|
@@ -26,9 +21,7 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
26
21
|
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
27
22
|
let defaultContexts = [];
|
|
28
23
|
function createContext2(rootComponentName, defaultContext) {
|
|
29
|
-
const BaseContext = React.createContext(
|
|
30
|
-
defaultContext
|
|
31
|
-
);
|
|
24
|
+
const BaseContext = React.createContext(defaultContext);
|
|
32
25
|
const index = defaultContexts.length;
|
|
33
26
|
defaultContexts = [...defaultContexts, defaultContext];
|
|
34
27
|
function Provider(props) {
|
|
@@ -40,16 +33,22 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
40
33
|
);
|
|
41
34
|
return /* @__PURE__ */ jsx(Context.Provider, { value, children });
|
|
42
35
|
}
|
|
43
|
-
function useContext(consumerName, scope) {
|
|
36
|
+
function useContext(consumerName, scope, options) {
|
|
44
37
|
const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
45
38
|
const context = React.useContext(Context);
|
|
46
39
|
if (context)
|
|
47
40
|
return context;
|
|
48
41
|
if (defaultContext !== void 0)
|
|
49
42
|
return defaultContext;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
+
}
|
|
53
52
|
}
|
|
54
53
|
Provider.displayName = `${rootComponentName}Provider`;
|
|
55
54
|
return [Provider, useContext];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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
|
|
5
|
-
"mappings": "
|
|
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): Exclude<typeof context, undefined> {\n const context = React.useContext(Context)\n if (context) return context as any\n if (defaultContext !== undefined) return defaultContext as any\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(\n scopeName: string,\n createContextScopeDeps: CreateScope[] = []\n) {\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 & {\n scope: Scope<ContextValueType>\n children: React.ReactNode\n }\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(\n () => context,\n Object.values(context)\n ) as ContextValueType\n return <Context.Provider value={value}>{children}</Context.Provider>\n }\n\n function useContext(\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n options?: {\n warn?: boolean\n fallback?: Partial<ContextValueType>\n }\n ) {\n const Context = scope?.[scopeName][index] || BaseContext\n const context = React.useContext(Context)\n if (context) return context\n // if a defaultContext wasn't specified, it's a required context.\n if (defaultContext !== undefined) return defaultContext\n const missingContextMessage = `\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``\n // fallback can be given per-hook as well\n if (options?.fallback) {\n if (options?.warn !== false) {\n console.warn(missingContextMessage)\n }\n return options.fallback as ContextValueType\n } else {\n throw new Error(missingContextMessage)\n }\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 [\n createContext,\n composeContextScopes(createScope, ...createContextScopeDeps),\n ] 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(\n () => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),\n [nextScopes]\n )\n }\n }\n\n createScope.scopeName = baseScope.scopeName\n return createScope\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n"],
|
|
5
|
+
"mappings": "AAkBW;AAfX,YAAY,WAAW;AAIhB,SAAS,cACd,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UAAM,EAAE,UAAU,GAAG,QAAQ,IAAI;AAGjC,UAAM,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,aAAO;AACpB,QAAI,mBAAmB;AAAW,aAAO;AAEzC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AO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,WAAW,GAAG,EAAE,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,EAAE;AAAA,QACtE,CAAC,OAAO,QAAQ;AAAA,MAClB;AAAA,IACF;AAAA,EACF;AAEA,cAAY,YAAY;AACxB,SAAO;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,SAAS,kBAAkB,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAACC,aAAY,EAAE,UAAU,UAAU,MAAM;AAI5E,cAAM,aAAa,SAAS,cAAc;AAC1C,cAAM,eAAe,WAAW,UAAU,WAAW;AACrD,eAAO,EAAE,GAAGA,aAAY,GAAG,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,UAAU,WAAW,GAAG,WAAW;AAAA,QACvD,CAAC,UAAU;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAEA,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;",
|
|
6
6
|
"names": ["createContext", "createScope", "nextScopes"]
|
|
7
7
|
}
|
|
@@ -0,0 +1,102 @@
|
|
|
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
|
|
@@ -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): Exclude<typeof context, undefined> {\n const context = React.useContext(Context)\n if (context) return context as any\n if (defaultContext !== undefined) return defaultContext as any\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(\n scopeName: string,\n createContextScopeDeps: CreateScope[] = []\n) {\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 & {\n scope: Scope<ContextValueType>\n children: React.ReactNode\n }\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(\n () => context,\n Object.values(context)\n ) as ContextValueType\n return <Context.Provider value={value}>{children}</Context.Provider>\n }\n\n function useContext(\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n options?: {\n warn?: boolean\n fallback?: Partial<ContextValueType>\n }\n ) {\n const Context = scope?.[scopeName][index] || BaseContext\n const context = React.useContext(Context)\n if (context) return context\n // if a defaultContext wasn't specified, it's a required context.\n if (defaultContext !== undefined) return defaultContext\n const missingContextMessage = `\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``\n // fallback can be given per-hook as well\n if (options?.fallback) {\n if (options?.warn !== false) {\n console.warn(missingContextMessage)\n }\n return options.fallback as ContextValueType\n } else {\n throw new Error(missingContextMessage)\n }\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 [\n createContext,\n composeContextScopes(createScope, ...createContextScopeDeps),\n ] 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(\n () => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),\n [nextScopes]\n )\n }\n }\n\n createScope.scopeName = baseScope.scopeName\n return createScope\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n"],
|
|
5
|
+
"mappings": "AAkBW;AAfX,YAAY,WAAW;AAIhB,SAAS,cACd,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UAAM,EAAE,UAAU,GAAG,QAAQ,IAAI;AAGjC,UAAM,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,aAAO;AACpB,QAAI,mBAAmB;AAAW,aAAO;AAEzC,UAAM,IAAI,MAAM,KAAK,wCAAwC,qBAAqB;AAAA,EACpF;AAEA,WAAS,cAAc,GAAG;AAC1B,SAAO,CAAC,UAAU,UAAU;AAC9B;AAeO,SAAS,mBACd,WACA,yBAAwC,CAAC,GACzC;AACA,MAAI,kBAAyB,CAAC;AAM9B,WAASA,eACP,mBACA,gBACA;AACA,UAAM,cAAc,MAAM,cAA4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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,SAAS,kBAAkB,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAACC,aAAY,EAAE,UAAU,UAAU,MAAM;AAI5E,cAAM,aAAa,SAAS,cAAc;AAC1C,cAAM,eAAe,WAAW,UAAU,WAAW;AACrD,eAAO,EAAE,GAAGA,aAAY,GAAG,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,UAAU,WAAW,GAAG,WAAW;AAAA,QACvD,CAAC,UAAU;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAEA,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;",
|
|
6
|
+
"names": ["createContext", "createScope", "nextScopes"]
|
|
7
|
+
}
|
|
@@ -3,10 +3,7 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
3
3
|
const Context = React.createContext(defaultContext);
|
|
4
4
|
function Provider(props) {
|
|
5
5
|
const { children, ...context } = props;
|
|
6
|
-
const value = React.useMemo(
|
|
7
|
-
() => context,
|
|
8
|
-
Object.values(context)
|
|
9
|
-
);
|
|
6
|
+
const value = React.useMemo(() => context, Object.values(context));
|
|
10
7
|
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
11
8
|
}
|
|
12
9
|
function useContext(consumerName) {
|
|
@@ -15,9 +12,7 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
15
12
|
return context;
|
|
16
13
|
if (defaultContext !== void 0)
|
|
17
14
|
return defaultContext;
|
|
18
|
-
throw new Error(
|
|
19
|
-
`\`${consumerName}\` must be used within \`${rootComponentName}\``
|
|
20
|
-
);
|
|
15
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
21
16
|
}
|
|
22
17
|
Provider.displayName = `${rootComponentName}Provider`;
|
|
23
18
|
return [Provider, useContext];
|
|
@@ -25,9 +20,7 @@ function createContext(rootComponentName, defaultContext) {
|
|
|
25
20
|
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
26
21
|
let defaultContexts = [];
|
|
27
22
|
function createContext2(rootComponentName, defaultContext) {
|
|
28
|
-
const BaseContext = React.createContext(
|
|
29
|
-
defaultContext
|
|
30
|
-
);
|
|
23
|
+
const BaseContext = React.createContext(defaultContext);
|
|
31
24
|
const index = defaultContexts.length;
|
|
32
25
|
defaultContexts = [...defaultContexts, defaultContext];
|
|
33
26
|
function Provider(props) {
|
|
@@ -39,16 +32,22 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
39
32
|
);
|
|
40
33
|
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
41
34
|
}
|
|
42
|
-
function useContext(consumerName, scope) {
|
|
35
|
+
function useContext(consumerName, scope, options) {
|
|
43
36
|
const Context = scope?.[scopeName][index] || BaseContext;
|
|
44
37
|
const context = React.useContext(Context);
|
|
45
38
|
if (context)
|
|
46
39
|
return context;
|
|
47
40
|
if (defaultContext !== void 0)
|
|
48
41
|
return defaultContext;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
42
|
+
const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
|
|
43
|
+
if (options?.fallback) {
|
|
44
|
+
if (options?.warn !== false) {
|
|
45
|
+
console.warn(missingContextMessage);
|
|
46
|
+
}
|
|
47
|
+
return options.fallback;
|
|
48
|
+
} else {
|
|
49
|
+
throw new Error(missingContextMessage);
|
|
50
|
+
}
|
|
52
51
|
}
|
|
53
52
|
Provider.displayName = `${rootComponentName}Provider`;
|
|
54
53
|
return [Provider, useContext];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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
|
|
5
|
-
"mappings": "AAGA,YAAY,WAAW;AAIhB,SAAS,cACd,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UAAM,EAAE,UAAU,GAAG,QAAQ,IAAI;AAGjC,UAAM,QAAQ,MAAM
|
|
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): Exclude<typeof context, undefined> {\n const context = React.useContext(Context)\n if (context) return context as any\n if (defaultContext !== undefined) return defaultContext as any\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(\n scopeName: string,\n createContextScopeDeps: CreateScope[] = []\n) {\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 & {\n scope: Scope<ContextValueType>\n children: React.ReactNode\n }\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(\n () => context,\n Object.values(context)\n ) as ContextValueType\n return <Context.Provider value={value}>{children}</Context.Provider>\n }\n\n function useContext(\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n options?: {\n warn?: boolean\n fallback?: Partial<ContextValueType>\n }\n ) {\n const Context = scope?.[scopeName][index] || BaseContext\n const context = React.useContext(Context)\n if (context) return context\n // if a defaultContext wasn't specified, it's a required context.\n if (defaultContext !== undefined) return defaultContext\n const missingContextMessage = `\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``\n // fallback can be given per-hook as well\n if (options?.fallback) {\n if (options?.warn !== false) {\n console.warn(missingContextMessage)\n }\n return options.fallback as ContextValueType\n } else {\n throw new Error(missingContextMessage)\n }\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 [\n createContext,\n composeContextScopes(createScope, ...createContextScopeDeps),\n ] 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(\n () => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),\n [nextScopes]\n )\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,UAAU,GAAG,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,cAA0D;AAC5E,UAAM,UAAU,MAAM,WAAW,OAAO;AACxC,QAAI;AAAS,aAAO;AACpB,QAAI,mBAAm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SAAS,OAAc;AACrC,YAAM,WAAW,QAAQ,SAAS,KAAK;AACvC,aAAO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,WAAW,GAAG,EAAE,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,EAAE;AAAA,QACtE,CAAC,OAAO,QAAQ;AAAA,MAClB;AAAA,IACF;AAAA,EACF;AAEA,cAAY,YAAY;AACxB,SAAO;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,SAAS,kBAAkB,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAACC,aAAY,EAAE,UAAU,UAAU,MAAM;AAI5E,cAAM,aAAa,SAAS,cAAc;AAC1C,cAAM,eAAe,WAAW,UAAU,WAAW;AACrD,eAAO,EAAE,GAAGA,aAAY,GAAG,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,UAAU,WAAW,GAAG,WAAW;AAAA,QACvD,CAAC,UAAU;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAEA,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;",
|
|
6
6
|
"names": ["createContext", "createScope", "nextScopes"]
|
|
7
7
|
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
function createContext(rootComponentName, defaultContext) {
|
|
3
|
+
const Context = React.createContext(defaultContext);
|
|
4
|
+
function Provider(props) {
|
|
5
|
+
const { children, ...context } = props;
|
|
6
|
+
const value = React.useMemo(() => context, Object.values(context));
|
|
7
|
+
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
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
|
+
Provider.displayName = `${rootComponentName}Provider`;
|
|
18
|
+
return [Provider, useContext];
|
|
19
|
+
}
|
|
20
|
+
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
21
|
+
let defaultContexts = [];
|
|
22
|
+
function createContext2(rootComponentName, defaultContext) {
|
|
23
|
+
const BaseContext = React.createContext(defaultContext);
|
|
24
|
+
const index = defaultContexts.length;
|
|
25
|
+
defaultContexts = [...defaultContexts, defaultContext];
|
|
26
|
+
function Provider(props) {
|
|
27
|
+
const { scope, children, ...context } = props;
|
|
28
|
+
const Context = scope?.[scopeName][index] || BaseContext;
|
|
29
|
+
const value = React.useMemo(
|
|
30
|
+
() => context,
|
|
31
|
+
Object.values(context)
|
|
32
|
+
);
|
|
33
|
+
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
34
|
+
}
|
|
35
|
+
function useContext(consumerName, scope, options) {
|
|
36
|
+
const Context = scope?.[scopeName][index] || BaseContext;
|
|
37
|
+
const context = React.useContext(Context);
|
|
38
|
+
if (context)
|
|
39
|
+
return context;
|
|
40
|
+
if (defaultContext !== void 0)
|
|
41
|
+
return defaultContext;
|
|
42
|
+
const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
|
|
43
|
+
if (options?.fallback) {
|
|
44
|
+
if (options?.warn !== false) {
|
|
45
|
+
console.warn(missingContextMessage);
|
|
46
|
+
}
|
|
47
|
+
return options.fallback;
|
|
48
|
+
} else {
|
|
49
|
+
throw new Error(missingContextMessage);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
Provider.displayName = `${rootComponentName}Provider`;
|
|
53
|
+
return [Provider, useContext];
|
|
54
|
+
}
|
|
55
|
+
const createScope = () => {
|
|
56
|
+
const scopeContexts = defaultContexts.map((defaultContext) => {
|
|
57
|
+
return React.createContext(defaultContext);
|
|
58
|
+
});
|
|
59
|
+
return function useScope(scope) {
|
|
60
|
+
const contexts = scope?.[scopeName] || scopeContexts;
|
|
61
|
+
return React.useMemo(
|
|
62
|
+
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
63
|
+
[scope, contexts]
|
|
64
|
+
);
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
createScope.scopeName = scopeName;
|
|
68
|
+
return [
|
|
69
|
+
createContext2,
|
|
70
|
+
composeContextScopes(createScope, ...createContextScopeDeps)
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
function composeContextScopes(...scopes) {
|
|
74
|
+
const baseScope = scopes[0];
|
|
75
|
+
if (scopes.length === 1)
|
|
76
|
+
return baseScope;
|
|
77
|
+
const createScope = () => {
|
|
78
|
+
const scopeHooks = scopes.map((createScope2) => ({
|
|
79
|
+
useScope: createScope2(),
|
|
80
|
+
scopeName: createScope2.scopeName
|
|
81
|
+
}));
|
|
82
|
+
return function useComposedScopes(overrideScopes) {
|
|
83
|
+
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
84
|
+
const scopeProps = useScope(overrideScopes);
|
|
85
|
+
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
86
|
+
return { ...nextScopes2, ...currentScope };
|
|
87
|
+
}, {});
|
|
88
|
+
return React.useMemo(
|
|
89
|
+
() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),
|
|
90
|
+
[nextScopes]
|
|
91
|
+
);
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
createScope.scopeName = baseScope.scopeName;
|
|
95
|
+
return createScope;
|
|
96
|
+
}
|
|
97
|
+
export {
|
|
98
|
+
createContext,
|
|
99
|
+
createContextScope
|
|
100
|
+
};
|
|
101
|
+
//# sourceMappingURL=create-context.mjs.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): Exclude<typeof context, undefined> {\n const context = React.useContext(Context)\n if (context) return context as any\n if (defaultContext !== undefined) return defaultContext as any\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(\n scopeName: string,\n createContextScopeDeps: CreateScope[] = []\n) {\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 & {\n scope: Scope<ContextValueType>\n children: React.ReactNode\n }\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(\n () => context,\n Object.values(context)\n ) as ContextValueType\n return <Context.Provider value={value}>{children}</Context.Provider>\n }\n\n function useContext(\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n options?: {\n warn?: boolean\n fallback?: Partial<ContextValueType>\n }\n ) {\n const Context = scope?.[scopeName][index] || BaseContext\n const context = React.useContext(Context)\n if (context) return context\n // if a defaultContext wasn't specified, it's a required context.\n if (defaultContext !== undefined) return defaultContext\n const missingContextMessage = `\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``\n // fallback can be given per-hook as well\n if (options?.fallback) {\n if (options?.warn !== false) {\n console.warn(missingContextMessage)\n }\n return options.fallback as ContextValueType\n } else {\n throw new Error(missingContextMessage)\n }\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 [\n createContext,\n composeContextScopes(createScope, ...createContextScopeDeps),\n ] 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(\n () => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),\n [nextScopes]\n )\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,UAAU,GAAG,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,cAA0D;AAC5E,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,GAAG;AAC1B,SAAO,CAAC,UAAU,UAAU;AAC9B;AAeO,SAAS,mBACd,WACA,yBAAwC,CAAC,GACzC;AACA,MAAI,kBAAyB,CAAC;AAM9B,WAASA,eACP,mBACA,gBACA;AACA,UAAM,cAAc,MAAM,cAA4C,cAAc;AACpF,UAAM,QAAQ,gBAAgB;AAC9B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sB;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,SAAS,kBAAkB,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAACC,aAAY,EAAE,UAAU,UAAU,MAAM;AAI5E,cAAM,aAAa,SAAS,cAAc;AAC1C,cAAM,eAAe,WAAW,UAAU,WAAW;AACrD,eAAO,EAAE,GAAGA,aAAY,GAAG,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM;AAAA,QACX,OAAO,EAAE,CAAC,UAAU,UAAU,WAAW,GAAG,WAAW;AAAA,QACvD,CAAC,UAAU;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAEA,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;",
|
|
6
|
+
"names": ["createContext", "createScope", "nextScopes"]
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/create-context",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.10",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"types": "./types/index.d.ts",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"react": "*"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@tamagui/build": "^1.2.
|
|
25
|
+
"@tamagui/build": "^1.2.10",
|
|
26
26
|
"react": "^18.2.0"
|
|
27
27
|
},
|
|
28
28
|
"publishConfig": {
|
package/src/create-context.tsx
CHANGED
|
@@ -7,7 +7,7 @@ export type ScopedProps<P, K extends string> = P & { [Key in `__scope${K}`]?: Sc
|
|
|
7
7
|
|
|
8
8
|
export function createContext<ContextValueType extends object | null>(
|
|
9
9
|
rootComponentName: string,
|
|
10
|
-
defaultContext?: ContextValueType
|
|
10
|
+
defaultContext?: ContextValueType
|
|
11
11
|
) {
|
|
12
12
|
const Context = React.createContext<ContextValueType | undefined>(defaultContext)
|
|
13
13
|
|
|
@@ -15,21 +15,16 @@ export function createContext<ContextValueType extends object | null>(
|
|
|
15
15
|
const { children, ...context } = props
|
|
16
16
|
// Only re-memoize when prop values change
|
|
17
17
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
18
|
-
const value = React.useMemo(
|
|
19
|
-
() => context,
|
|
20
|
-
Object.values(context),
|
|
21
|
-
) as ContextValueType
|
|
18
|
+
const value = React.useMemo(() => context, Object.values(context)) as ContextValueType
|
|
22
19
|
return <Context.Provider value={value}>{children}</Context.Provider>
|
|
23
20
|
}
|
|
24
21
|
|
|
25
|
-
function useContext(consumerName: string) {
|
|
22
|
+
function useContext(consumerName: string): Exclude<typeof context, undefined> {
|
|
26
23
|
const context = React.useContext(Context)
|
|
27
|
-
if (context) return context
|
|
28
|
-
if (defaultContext !== undefined) return defaultContext
|
|
24
|
+
if (context) return context as any
|
|
25
|
+
if (defaultContext !== undefined) return defaultContext as any
|
|
29
26
|
// if a defaultContext wasn't specified, it's a required context.
|
|
30
|
-
throw new Error(
|
|
31
|
-
`\`${consumerName}\` must be used within \`${rootComponentName}\``,
|
|
32
|
-
)
|
|
27
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``)
|
|
33
28
|
}
|
|
34
29
|
|
|
35
30
|
Provider.displayName = `${rootComponentName}Provider`
|
|
@@ -51,7 +46,7 @@ export interface CreateScope {
|
|
|
51
46
|
|
|
52
47
|
export function createContextScope(
|
|
53
48
|
scopeName: string,
|
|
54
|
-
createContextScopeDeps: CreateScope[] = []
|
|
49
|
+
createContextScopeDeps: CreateScope[] = []
|
|
55
50
|
) {
|
|
56
51
|
let defaultContexts: any[] = []
|
|
57
52
|
|
|
@@ -61,11 +56,9 @@ export function createContextScope(
|
|
|
61
56
|
|
|
62
57
|
function createContext<ContextValueType extends object | null>(
|
|
63
58
|
rootComponentName: string,
|
|
64
|
-
defaultContext?: ContextValueType
|
|
59
|
+
defaultContext?: ContextValueType
|
|
65
60
|
) {
|
|
66
|
-
const BaseContext = React.createContext<ContextValueType | undefined>(
|
|
67
|
-
defaultContext,
|
|
68
|
-
)
|
|
61
|
+
const BaseContext = React.createContext<ContextValueType | undefined>(defaultContext)
|
|
69
62
|
const index = defaultContexts.length
|
|
70
63
|
defaultContexts = [...defaultContexts, defaultContext]
|
|
71
64
|
|
|
@@ -73,7 +66,7 @@ export function createContextScope(
|
|
|
73
66
|
props: ContextValueType & {
|
|
74
67
|
scope: Scope<ContextValueType>
|
|
75
68
|
children: React.ReactNode
|
|
76
|
-
}
|
|
69
|
+
}
|
|
77
70
|
) {
|
|
78
71
|
const { scope, children, ...context } = props
|
|
79
72
|
const Context = scope?.[scopeName][index] || BaseContext
|
|
@@ -81,7 +74,7 @@ export function createContextScope(
|
|
|
81
74
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
82
75
|
const value = React.useMemo(
|
|
83
76
|
() => context,
|
|
84
|
-
Object.values(context)
|
|
77
|
+
Object.values(context)
|
|
85
78
|
) as ContextValueType
|
|
86
79
|
return <Context.Provider value={value}>{children}</Context.Provider>
|
|
87
80
|
}
|
|
@@ -89,15 +82,26 @@ export function createContextScope(
|
|
|
89
82
|
function useContext(
|
|
90
83
|
consumerName: string,
|
|
91
84
|
scope: Scope<ContextValueType | undefined>,
|
|
85
|
+
options?: {
|
|
86
|
+
warn?: boolean
|
|
87
|
+
fallback?: Partial<ContextValueType>
|
|
88
|
+
}
|
|
92
89
|
) {
|
|
93
90
|
const Context = scope?.[scopeName][index] || BaseContext
|
|
94
91
|
const context = React.useContext(Context)
|
|
95
92
|
if (context) return context
|
|
96
|
-
if (defaultContext !== undefined) return defaultContext
|
|
97
93
|
// if a defaultContext wasn't specified, it's a required context.
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
94
|
+
if (defaultContext !== undefined) return defaultContext
|
|
95
|
+
const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``
|
|
96
|
+
// fallback can be given per-hook as well
|
|
97
|
+
if (options?.fallback) {
|
|
98
|
+
if (options?.warn !== false) {
|
|
99
|
+
console.warn(missingContextMessage)
|
|
100
|
+
}
|
|
101
|
+
return options.fallback as ContextValueType
|
|
102
|
+
} else {
|
|
103
|
+
throw new Error(missingContextMessage)
|
|
104
|
+
}
|
|
101
105
|
}
|
|
102
106
|
|
|
103
107
|
Provider.displayName = `${rootComponentName}Provider`
|
|
@@ -116,7 +120,7 @@ export function createContextScope(
|
|
|
116
120
|
const contexts = scope?.[scopeName] || scopeContexts
|
|
117
121
|
return React.useMemo(
|
|
118
122
|
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
119
|
-
[scope, contexts]
|
|
123
|
+
[scope, contexts]
|
|
120
124
|
)
|
|
121
125
|
}
|
|
122
126
|
}
|
|
@@ -154,7 +158,7 @@ function composeContextScopes(...scopes: CreateScope[]) {
|
|
|
154
158
|
|
|
155
159
|
return React.useMemo(
|
|
156
160
|
() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),
|
|
157
|
-
[nextScopes]
|
|
161
|
+
[nextScopes]
|
|
158
162
|
)
|
|
159
163
|
}
|
|
160
164
|
}
|
|
@@ -7,7 +7,7 @@ export declare function createContext<ContextValueType extends object | null>(ro
|
|
|
7
7
|
children: React.ReactNode;
|
|
8
8
|
}): JSX.Element;
|
|
9
9
|
displayName: string;
|
|
10
|
-
}, (consumerName: string) => ContextValueType];
|
|
10
|
+
}, (consumerName: string) => Exclude<ContextValueType, undefined>];
|
|
11
11
|
type ScopeHook = (scope: Scope) => {
|
|
12
12
|
[__scopeProp: string]: Scope;
|
|
13
13
|
};
|
|
@@ -24,6 +24,9 @@ export declare function createContextScope(scopeName: string, createContextScope
|
|
|
24
24
|
children: React.ReactNode;
|
|
25
25
|
}): JSX.Element;
|
|
26
26
|
displayName: string;
|
|
27
|
-
}, (consumerName: string, scope: Scope<ContextValueType | undefined
|
|
27
|
+
}, (consumerName: string, scope: Scope<ContextValueType | undefined>, options?: {
|
|
28
|
+
warn?: boolean | undefined;
|
|
29
|
+
fallback?: Partial<ContextValueType> | undefined;
|
|
30
|
+
} | undefined) => ContextValueType], CreateScope];
|
|
28
31
|
export {};
|
|
29
32
|
//# sourceMappingURL=create-context.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-context.d.ts","sourceRoot":"","sources":["../src/create-context.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,MAAM,MAAM,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;;
|
|
1
|
+
{"version":3,"file":"create-context.d.ts","sourceRoot":"","sources":["../src/create-context.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,MAAM,MAAM,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;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