@huin-core/react-context 1.0.1 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.mts +28 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +114 -0
- package/dist/index.js.map +7 -0
- package/dist/index.mjs +81 -0
- package/dist/index.mjs.map +7 -0
- package/package.json +1 -1
package/dist/index.d.mts
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
2
|
+
import * as React from 'react';
|
3
|
+
|
4
|
+
declare function createContext<ContextValueType extends object | null>(rootComponentName: string, defaultContext?: ContextValueType): readonly [{
|
5
|
+
(props: ContextValueType & {
|
6
|
+
children: React.ReactNode;
|
7
|
+
}): react_jsx_runtime.JSX.Element;
|
8
|
+
displayName: string;
|
9
|
+
}, (consumerName: string) => ContextValueType];
|
10
|
+
type Scope<C = any> = {
|
11
|
+
[scopeName: string]: React.Context<C>[];
|
12
|
+
} | undefined;
|
13
|
+
type ScopeHook = (scope: Scope) => {
|
14
|
+
[__scopeProp: string]: Scope;
|
15
|
+
};
|
16
|
+
interface CreateScope {
|
17
|
+
scopeName: string;
|
18
|
+
(): ScopeHook;
|
19
|
+
}
|
20
|
+
declare function createContextScope(scopeName: string, createContextScopeDeps?: CreateScope[]): readonly [<ContextValueType extends object | null>(rootComponentName: string, defaultContext?: ContextValueType | undefined) => readonly [{
|
21
|
+
(props: ContextValueType & {
|
22
|
+
scope: Scope<ContextValueType>;
|
23
|
+
children: React.ReactNode;
|
24
|
+
}): react_jsx_runtime.JSX.Element;
|
25
|
+
displayName: string;
|
26
|
+
}, (consumerName: string, scope: Scope<ContextValueType | undefined>) => ContextValueType], CreateScope];
|
27
|
+
|
28
|
+
export { type CreateScope, type Scope, createContext, createContextScope };
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
2
|
+
import * as React from 'react';
|
3
|
+
|
4
|
+
declare function createContext<ContextValueType extends object | null>(rootComponentName: string, defaultContext?: ContextValueType): readonly [{
|
5
|
+
(props: ContextValueType & {
|
6
|
+
children: React.ReactNode;
|
7
|
+
}): react_jsx_runtime.JSX.Element;
|
8
|
+
displayName: string;
|
9
|
+
}, (consumerName: string) => ContextValueType];
|
10
|
+
type Scope<C = any> = {
|
11
|
+
[scopeName: string]: React.Context<C>[];
|
12
|
+
} | undefined;
|
13
|
+
type ScopeHook = (scope: Scope) => {
|
14
|
+
[__scopeProp: string]: Scope;
|
15
|
+
};
|
16
|
+
interface CreateScope {
|
17
|
+
scopeName: string;
|
18
|
+
(): ScopeHook;
|
19
|
+
}
|
20
|
+
declare function createContextScope(scopeName: string, createContextScopeDeps?: CreateScope[]): readonly [<ContextValueType extends object | null>(rootComponentName: string, defaultContext?: ContextValueType | undefined) => readonly [{
|
21
|
+
(props: ContextValueType & {
|
22
|
+
scope: Scope<ContextValueType>;
|
23
|
+
children: React.ReactNode;
|
24
|
+
}): react_jsx_runtime.JSX.Element;
|
25
|
+
displayName: string;
|
26
|
+
}, (consumerName: string, scope: Scope<ContextValueType | undefined>) => ContextValueType], CreateScope];
|
27
|
+
|
28
|
+
export { type CreateScope, type Scope, createContext, createContextScope };
|
package/dist/index.js
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __create = Object.create;
|
3
|
+
var __defProp = Object.defineProperty;
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
+
var __export = (target, all) => {
|
9
|
+
for (var name in all)
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
+
};
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
+
for (let key of __getOwnPropNames(from))
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
+
}
|
18
|
+
return to;
|
19
|
+
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
+
|
30
|
+
// packages/react/context/src/index.ts
|
31
|
+
var src_exports = {};
|
32
|
+
__export(src_exports, {
|
33
|
+
createContext: () => createContext2,
|
34
|
+
createContextScope: () => createContextScope
|
35
|
+
});
|
36
|
+
module.exports = __toCommonJS(src_exports);
|
37
|
+
|
38
|
+
// packages/react/context/src/createContext.tsx
|
39
|
+
var React = __toESM(require("react"));
|
40
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
41
|
+
function createContext2(rootComponentName, defaultContext) {
|
42
|
+
const Context = React.createContext(defaultContext);
|
43
|
+
function Provider(props) {
|
44
|
+
const { children, ...context } = props;
|
45
|
+
const value = React.useMemo(() => context, Object.values(context));
|
46
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
|
47
|
+
}
|
48
|
+
function useContext2(consumerName) {
|
49
|
+
const context = React.useContext(Context);
|
50
|
+
if (context) return context;
|
51
|
+
if (defaultContext !== void 0) return defaultContext;
|
52
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
53
|
+
}
|
54
|
+
Provider.displayName = rootComponentName + "Provider";
|
55
|
+
return [Provider, useContext2];
|
56
|
+
}
|
57
|
+
function createContextScope(scopeName, createContextScopeDeps = []) {
|
58
|
+
let defaultContexts = [];
|
59
|
+
function createContext3(rootComponentName, defaultContext) {
|
60
|
+
const BaseContext = React.createContext(defaultContext);
|
61
|
+
const index = defaultContexts.length;
|
62
|
+
defaultContexts = [...defaultContexts, defaultContext];
|
63
|
+
function Provider(props) {
|
64
|
+
const { scope, children, ...context } = props;
|
65
|
+
const Context = scope?.[scopeName][index] || BaseContext;
|
66
|
+
const value = React.useMemo(() => context, Object.values(context));
|
67
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
|
68
|
+
}
|
69
|
+
function useContext2(consumerName, scope) {
|
70
|
+
const Context = scope?.[scopeName][index] || BaseContext;
|
71
|
+
const context = React.useContext(Context);
|
72
|
+
if (context) return context;
|
73
|
+
if (defaultContext !== void 0) return defaultContext;
|
74
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
75
|
+
}
|
76
|
+
Provider.displayName = rootComponentName + "Provider";
|
77
|
+
return [Provider, useContext2];
|
78
|
+
}
|
79
|
+
const createScope = () => {
|
80
|
+
const scopeContexts = defaultContexts.map((defaultContext) => {
|
81
|
+
return React.createContext(defaultContext);
|
82
|
+
});
|
83
|
+
return function useScope(scope) {
|
84
|
+
const contexts = scope?.[scopeName] || scopeContexts;
|
85
|
+
return React.useMemo(
|
86
|
+
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
87
|
+
[scope, contexts]
|
88
|
+
);
|
89
|
+
};
|
90
|
+
};
|
91
|
+
createScope.scopeName = scopeName;
|
92
|
+
return [createContext3, composeContextScopes(createScope, ...createContextScopeDeps)];
|
93
|
+
}
|
94
|
+
function composeContextScopes(...scopes) {
|
95
|
+
const baseScope = scopes[0];
|
96
|
+
if (scopes.length === 1) return baseScope;
|
97
|
+
const createScope = () => {
|
98
|
+
const scopeHooks = scopes.map((createScope2) => ({
|
99
|
+
useScope: createScope2(),
|
100
|
+
scopeName: createScope2.scopeName
|
101
|
+
}));
|
102
|
+
return function useComposedScopes(overrideScopes) {
|
103
|
+
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
104
|
+
const scopeProps = useScope(overrideScopes);
|
105
|
+
const currentScope = scopeProps[`__scope${scopeName}`];
|
106
|
+
return { ...nextScopes2, ...currentScope };
|
107
|
+
}, {});
|
108
|
+
return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
|
109
|
+
};
|
110
|
+
};
|
111
|
+
createScope.scopeName = baseScope.scopeName;
|
112
|
+
return createScope;
|
113
|
+
}
|
114
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1,7 @@
|
|
1
|
+
{
|
2
|
+
"version": 3,
|
3
|
+
"sources": ["../src/index.ts", "../src/createContext.tsx"],
|
4
|
+
"sourcesContent": ["export { createContext, createContextScope } from './createContext';\nexport type { CreateScope, Scope } from './createContext';\n", "import * as React from 'react';\n\nfunction 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 Scope<C = any> = { [scopeName: string]: React.Context<C>[] } | undefined;\ntype ScopeHook = (scope: Scope) => { [__scopeProp: string]: Scope };\ninterface CreateScope {\n scopeName: string;\n (): ScopeHook;\n}\n\nfunction 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\nexport { createContext, createContextScope };\nexport type { CreateScope, Scope };\n"],
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,uBAAAA;AAAA,EAAA;AAAA;AAAA;;;ACAA,YAAuB;AAaZ;AAXX,SAASC,eACP,mBACA,gBACA;AACA,QAAM,UAAgB,oBAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UAAM,EAAE,UAAU,GAAG,QAAQ,IAAI;AAGjC,UAAM,QAAc,cAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,WAAO,4CAAC,QAAQ,UAAR,EAAiB,OAAe,UAAS;AAAA,EACnD;AAEA,WAASC,YAAW,cAAsB;AACxC,UAAM,UAAgB,iBAAW,OAAO;AACxC,QAAI,QAAS,QAAO;AACpB,QAAI,mBAAmB,OAAW,QAAO;AAEzC,UAAM,IAAI,MAAM,KAAK,YAAY,4BAA4B,iBAAiB,IAAI;AAAA,EACpF;AAEA,WAAS,cAAc,oBAAoB;A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AG,sBAAsB,CAAC;AACrF;AAMA,SAAS,wBAAwB,QAAuB;AACtD,QAAM,YAAY,OAAO,CAAC;AAC1B,MAAI,OAAO,WAAW,EAAG,QAAO;AAEhC,QAAM,cAA2B,MAAM;AACrC,UAAM,aAAa,OAAO,IAAI,CAACE,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,SAAS,EAAE;AACrD,eAAO,EAAE,GAAGA,aAAY,GAAG,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAa,cAAQ,OAAO,EAAE,CAAC,UAAU,UAAU,SAAS,EAAE,GAAG,WAAW,IAAI,CAAC,UAAU,CAAC;AAAA,IAC9F;AAAA,EACF;AAEA,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;",
|
6
|
+
"names": ["createContext", "createContext", "useContext", "createScope", "nextScopes"]
|
7
|
+
}
|
package/dist/index.mjs
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
// packages/react/context/src/createContext.tsx
|
2
|
+
import * as React from "react";
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
4
|
+
function createContext2(rootComponentName, defaultContext) {
|
5
|
+
const Context = React.createContext(defaultContext);
|
6
|
+
function Provider(props) {
|
7
|
+
const { children, ...context } = props;
|
8
|
+
const value = React.useMemo(() => context, Object.values(context));
|
9
|
+
return /* @__PURE__ */ jsx(Context.Provider, { value, children });
|
10
|
+
}
|
11
|
+
function useContext2(consumerName) {
|
12
|
+
const context = React.useContext(Context);
|
13
|
+
if (context) return context;
|
14
|
+
if (defaultContext !== void 0) return defaultContext;
|
15
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
16
|
+
}
|
17
|
+
Provider.displayName = rootComponentName + "Provider";
|
18
|
+
return [Provider, useContext2];
|
19
|
+
}
|
20
|
+
function createContextScope(scopeName, createContextScopeDeps = []) {
|
21
|
+
let defaultContexts = [];
|
22
|
+
function createContext3(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(() => context, Object.values(context));
|
30
|
+
return /* @__PURE__ */ jsx(Context.Provider, { value, children });
|
31
|
+
}
|
32
|
+
function useContext2(consumerName, scope) {
|
33
|
+
const Context = scope?.[scopeName][index] || BaseContext;
|
34
|
+
const context = React.useContext(Context);
|
35
|
+
if (context) return context;
|
36
|
+
if (defaultContext !== void 0) return defaultContext;
|
37
|
+
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
38
|
+
}
|
39
|
+
Provider.displayName = rootComponentName + "Provider";
|
40
|
+
return [Provider, useContext2];
|
41
|
+
}
|
42
|
+
const createScope = () => {
|
43
|
+
const scopeContexts = defaultContexts.map((defaultContext) => {
|
44
|
+
return React.createContext(defaultContext);
|
45
|
+
});
|
46
|
+
return function useScope(scope) {
|
47
|
+
const contexts = scope?.[scopeName] || scopeContexts;
|
48
|
+
return React.useMemo(
|
49
|
+
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
50
|
+
[scope, contexts]
|
51
|
+
);
|
52
|
+
};
|
53
|
+
};
|
54
|
+
createScope.scopeName = scopeName;
|
55
|
+
return [createContext3, composeContextScopes(createScope, ...createContextScopeDeps)];
|
56
|
+
}
|
57
|
+
function composeContextScopes(...scopes) {
|
58
|
+
const baseScope = scopes[0];
|
59
|
+
if (scopes.length === 1) return baseScope;
|
60
|
+
const createScope = () => {
|
61
|
+
const scopeHooks = scopes.map((createScope2) => ({
|
62
|
+
useScope: createScope2(),
|
63
|
+
scopeName: createScope2.scopeName
|
64
|
+
}));
|
65
|
+
return function useComposedScopes(overrideScopes) {
|
66
|
+
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
67
|
+
const scopeProps = useScope(overrideScopes);
|
68
|
+
const currentScope = scopeProps[`__scope${scopeName}`];
|
69
|
+
return { ...nextScopes2, ...currentScope };
|
70
|
+
}, {});
|
71
|
+
return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
|
72
|
+
};
|
73
|
+
};
|
74
|
+
createScope.scopeName = baseScope.scopeName;
|
75
|
+
return createScope;
|
76
|
+
}
|
77
|
+
export {
|
78
|
+
createContext2 as createContext,
|
79
|
+
createContextScope
|
80
|
+
};
|
81
|
+
//# sourceMappingURL=index.mjs.map
|
@@ -0,0 +1,7 @@
|
|
1
|
+
{
|
2
|
+
"version": 3,
|
3
|
+
"sources": ["../src/createContext.tsx"],
|
4
|
+
"sourcesContent": ["import * as React from 'react';\n\nfunction 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 Scope<C = any> = { [scopeName: string]: React.Context<C>[] } | undefined;\ntype ScopeHook = (scope: Scope) => { [__scopeProp: string]: Scope };\ninterface CreateScope {\n scopeName: string;\n (): ScopeHook;\n}\n\nfunction 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\nexport { createContext, createContextScope };\nexport type { CreateScope, Scope };\n"],
|
5
|
+
"mappings": ";AAAA,YAAY,WAAW;AAaZ;AAXX,SAASA,eACP,mBACA,gBACA;AACA,QAAM,UAAgB,oBAA4C,cAAc;AAEhF,WAAS,SAAS,OAAyD;AACzE,UAAM,EAAE,UAAU,GAAG,QAAQ,IAAI;AAGjC,UAAM,QAAc,cAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,WAAO,oBAAC,QAAQ,UAAR,EAAiB,OAAe,UAAS;AAAA,EACnD;AAEA,WAASC,YAAW,cAAsB;AACxC,UAAM,UAAgB,iBAAW,OAAO;AACxC,QAAI,QAAS,QAAO;AACpB,QAAI,mBAAmB,OAAW,QAAO;AAEzC,UAAM,IAAI,MAAM,KAAK,YAAY,4BAA4B,iBAAiB,IAAI;AAAA,EACpF;AAEA,WAAS,cAAc,oBAAoB;AAC3C,SAAO,CAAC,UAAUA,WAAU;AAC9B;AAaA,SAA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