@radix-ui/react-context 1.2.0 → 1.2.1
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/index.js +21 -12
- package/dist/index.js.map +2 -2
- package/dist/index.mjs +23 -12
- package/dist/index.mjs.map +2 -2
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
9
|
var __export = (target, all) => {
|
|
9
10
|
for (var name in all)
|
|
10
11
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -38,14 +39,15 @@ module.exports = __toCommonJS(index_exports);
|
|
|
38
39
|
// src/create-context.tsx
|
|
39
40
|
var React = __toESM(require("react"));
|
|
40
41
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
42
|
+
// @__NO_SIDE_EFFECTS__
|
|
41
43
|
function createContext2(rootComponentName, defaultContext) {
|
|
42
44
|
const Context = React.createContext(defaultContext);
|
|
43
45
|
Context.displayName = rootComponentName + "Context";
|
|
44
|
-
const Provider = (props) => {
|
|
46
|
+
const Provider = /* @__PURE__ */ __name((props) => {
|
|
45
47
|
const { children, ...context } = props;
|
|
46
48
|
const value = React.useMemo(() => context, Object.values(context));
|
|
47
49
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
|
|
48
|
-
};
|
|
50
|
+
}, "Provider");
|
|
49
51
|
Provider.displayName = rootComponentName + "Provider";
|
|
50
52
|
function useContext2(consumerName, options = {}) {
|
|
51
53
|
const { optional = false } = options;
|
|
@@ -55,8 +57,11 @@ function createContext2(rootComponentName, defaultContext) {
|
|
|
55
57
|
if (optional) return void 0;
|
|
56
58
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
57
59
|
}
|
|
60
|
+
__name(useContext2, "useContext");
|
|
58
61
|
return [Provider, useContext2];
|
|
59
62
|
}
|
|
63
|
+
__name(createContext2, "createContext");
|
|
64
|
+
// @__NO_SIDE_EFFECTS__
|
|
60
65
|
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
61
66
|
let defaultContexts = [];
|
|
62
67
|
function createContext3(rootComponentName, defaultContext) {
|
|
@@ -64,12 +69,12 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
64
69
|
BaseContext.displayName = rootComponentName + "Context";
|
|
65
70
|
const index = defaultContexts.length;
|
|
66
71
|
defaultContexts = [...defaultContexts, defaultContext];
|
|
67
|
-
const Provider = (props) => {
|
|
72
|
+
const Provider = /* @__PURE__ */ __name((props) => {
|
|
68
73
|
const { scope, children, ...context } = props;
|
|
69
74
|
const Context = scope?.[scopeName]?.[index] || BaseContext;
|
|
70
75
|
const value = React.useMemo(() => context, Object.values(context));
|
|
71
76
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
|
|
72
|
-
};
|
|
77
|
+
}, "Provider");
|
|
73
78
|
Provider.displayName = rootComponentName + "Provider";
|
|
74
79
|
function useContext2(consumerName, scope, options = {}) {
|
|
75
80
|
const { optional = false } = options;
|
|
@@ -80,41 +85,45 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
80
85
|
if (optional) return void 0;
|
|
81
86
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
82
87
|
}
|
|
88
|
+
__name(useContext2, "useContext");
|
|
83
89
|
return [Provider, useContext2];
|
|
84
90
|
}
|
|
85
|
-
|
|
91
|
+
__name(createContext3, "createContext");
|
|
92
|
+
const createScope = /* @__PURE__ */ __name(() => {
|
|
86
93
|
const scopeContexts = defaultContexts.map((defaultContext) => {
|
|
87
94
|
return React.createContext(defaultContext);
|
|
88
95
|
});
|
|
89
|
-
return function useScope(scope) {
|
|
96
|
+
return /* @__PURE__ */ __name(function useScope(scope) {
|
|
90
97
|
const contexts = scope?.[scopeName] || scopeContexts;
|
|
91
98
|
return React.useMemo(
|
|
92
99
|
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
93
100
|
[scope, contexts]
|
|
94
101
|
);
|
|
95
|
-
};
|
|
96
|
-
};
|
|
102
|
+
}, "useScope");
|
|
103
|
+
}, "createScope");
|
|
97
104
|
createScope.scopeName = scopeName;
|
|
98
105
|
return [createContext3, composeContextScopes(createScope, ...createContextScopeDeps)];
|
|
99
106
|
}
|
|
107
|
+
__name(createContextScope, "createContextScope");
|
|
100
108
|
function composeContextScopes(...scopes) {
|
|
101
109
|
const baseScope = scopes[0];
|
|
102
110
|
if (scopes.length === 1) return baseScope;
|
|
103
|
-
const createScope = () => {
|
|
111
|
+
const createScope = /* @__PURE__ */ __name(() => {
|
|
104
112
|
const scopeHooks = scopes.map((createScope2) => ({
|
|
105
113
|
useScope: createScope2(),
|
|
106
114
|
scopeName: createScope2.scopeName
|
|
107
115
|
}));
|
|
108
|
-
return function useComposedScopes(overrideScopes) {
|
|
116
|
+
return /* @__PURE__ */ __name(function useComposedScopes(overrideScopes) {
|
|
109
117
|
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
110
118
|
const scopeProps = useScope(overrideScopes);
|
|
111
119
|
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
112
120
|
return { ...nextScopes2, ...currentScope };
|
|
113
121
|
}, {});
|
|
114
122
|
return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
|
|
115
|
-
};
|
|
116
|
-
};
|
|
123
|
+
}, "useComposedScopes");
|
|
124
|
+
}, "createScope");
|
|
117
125
|
createScope.scopeName = baseScope.scopeName;
|
|
118
126
|
return createScope;
|
|
119
127
|
}
|
|
128
|
+
__name(composeContextScopes, "composeContextScopes");
|
|
120
129
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts", "../src/create-context.tsx"],
|
|
4
|
-
"sourcesContent": ["export { createContext, createContextScope } from './create-context';\nexport type { CreateScope, Scope } from './create-context';\n", "import * as React from 'react';\n\ntype ContextProvider<ContextValueType extends object | null> = React.FC<\n ContextValueType & {\n children: React.ReactNode;\n }\n>;\n\ninterface UseAssertedContext<ContextValueType extends object | null> {\n (consumerName: string): ContextValueType;\n}\n\ninterface UseContext<ContextValueType extends object | null> {\n (consumerName: string): ContextValueType;\n (consumerName: string, options: { optional: true }): ContextValueType | undefined;\n}\n\nfunction createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n): readonly [ContextProvider<ContextValueType>, UseContext<ContextValueType>];\nfunction createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext: ContextValueType,\n): readonly [ContextProvider<ContextValueType>, UseAssertedContext<ContextValueType>];\n\
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["export { createContext, createContextScope } from './create-context';\nexport type { CreateScope, Scope } from './create-context';\n", "import * as React from 'react';\n\ntype ContextProvider<ContextValueType extends object | null> = React.FC<\n ContextValueType & {\n children: React.ReactNode;\n }\n>;\n\ninterface UseAssertedContext<ContextValueType extends object | null> {\n (consumerName: string): ContextValueType;\n}\n\ninterface UseContext<ContextValueType extends object | null> {\n (consumerName: string): ContextValueType;\n (consumerName: string, options: { optional: true }): ContextValueType | undefined;\n}\n\nfunction createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n): readonly [ContextProvider<ContextValueType>, UseContext<ContextValueType>];\nfunction createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext: ContextValueType,\n): readonly [ContextProvider<ContextValueType>, UseAssertedContext<ContextValueType>];\n\n/* @__NO_SIDE_EFFECTS__ */ function createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext?: ContextValueType,\n): readonly [ContextProvider<ContextValueType>, UseContext<ContextValueType>] {\n const Context = React.createContext<ContextValueType | undefined>(defaultContext);\n Context.displayName = rootComponentName + 'Context';\n\n const Provider: ContextProvider<ContextValueType> = (props) => {\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 Provider.displayName = rootComponentName + 'Provider';\n\n function useContext(consumerName: string): ContextValueType;\n function useContext(\n consumerName: string,\n options: { optional: true },\n ): ContextValueType | undefined;\n\n function useContext(\n consumerName: string,\n options: { optional?: boolean } = {},\n ): ContextValueType | undefined {\n const { optional = false } = options;\n const context = React.useContext(Context);\n if (context) return context;\n if (defaultContext !== undefined) return defaultContext;\n if (optional) return undefined;\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 return [Provider, useContext];\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\ntype ScopedContextProvider<ContextValueType extends object | null> = React.FC<\n ContextValueType & {\n scope: Scope<ContextValueType>;\n children: React.ReactNode;\n }\n>;\n\ninterface UseScopedContext<ContextValueType extends object | null> {\n (consumerName: string, scope: Scope<ContextValueType | undefined>): ContextValueType;\n (\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n options: { optional?: true },\n ): ContextValueType | undefined;\n}\n\ninterface UseAssertedScopedContext<ContextValueType extends object | null> {\n (consumerName: string, scope: Scope<ContextValueType | undefined>): ContextValueType;\n}\n\ninterface CreateScopedContext {\n <ContextValueType extends object | null>(\n rootComponentName: string,\n ): readonly [ScopedContextProvider<ContextValueType>, UseScopedContext<ContextValueType>];\n <ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext: ContextValueType,\n ): readonly [ScopedContextProvider<ContextValueType>, UseAssertedScopedContext<ContextValueType>];\n}\n\n/* @__NO_SIDE_EFFECTS__ */ function createContextScope(\n scopeName: string,\n createContextScopeDeps: CreateScope[] = [],\n): readonly [CreateScopedContext, 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 BaseContext.displayName = rootComponentName + 'Context';\n const index = defaultContexts.length;\n defaultContexts = [...defaultContexts, defaultContext];\n\n const Provider: ScopedContextProvider<ContextValueType> = (props) => {\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 Provider.displayName = rootComponentName + 'Provider';\n\n function useContext(\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n ): ContextValueType;\n function useContext(\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n options: { optional: true },\n ): ContextValueType | undefined;\n\n function useContext(\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n options: { optional?: boolean } = {},\n ): ContextValueType | undefined {\n const { optional = false } = options;\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 (optional) return undefined;\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 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, ...CreateScope[]]): 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 {\n CreateScope,\n CreateScopedContext,\n Scope,\n UseAssertedContext,\n UseAssertedScopedContext,\n UseContext,\n UseScopedContext,\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,uBAAAA;AAAA,EAAA;AAAA;AAAA;;;ACAA,YAAuB;AAqCZ;AA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;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,GAXO;AAAA,EAYT,GAlBiC;AAoBjC,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;AA1BS;",
|
|
6
6
|
"names": ["createContext", "createContext", "useContext", "createScope", "nextScopes"]
|
|
7
7
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
1
4
|
// src/create-context.tsx
|
|
2
5
|
import * as React from "react";
|
|
3
6
|
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
// @__NO_SIDE_EFFECTS__
|
|
4
8
|
function createContext2(rootComponentName, defaultContext) {
|
|
5
9
|
const Context = React.createContext(defaultContext);
|
|
6
10
|
Context.displayName = rootComponentName + "Context";
|
|
7
|
-
const Provider = (props) => {
|
|
11
|
+
const Provider = /* @__PURE__ */ __name((props) => {
|
|
8
12
|
const { children, ...context } = props;
|
|
9
13
|
const value = React.useMemo(() => context, Object.values(context));
|
|
10
14
|
return /* @__PURE__ */ jsx(Context.Provider, { value, children });
|
|
11
|
-
};
|
|
15
|
+
}, "Provider");
|
|
12
16
|
Provider.displayName = rootComponentName + "Provider";
|
|
13
17
|
function useContext2(consumerName, options = {}) {
|
|
14
18
|
const { optional = false } = options;
|
|
@@ -18,8 +22,11 @@ function createContext2(rootComponentName, defaultContext) {
|
|
|
18
22
|
if (optional) return void 0;
|
|
19
23
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
20
24
|
}
|
|
25
|
+
__name(useContext2, "useContext");
|
|
21
26
|
return [Provider, useContext2];
|
|
22
27
|
}
|
|
28
|
+
__name(createContext2, "createContext");
|
|
29
|
+
// @__NO_SIDE_EFFECTS__
|
|
23
30
|
function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
24
31
|
let defaultContexts = [];
|
|
25
32
|
function createContext3(rootComponentName, defaultContext) {
|
|
@@ -27,12 +34,12 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
27
34
|
BaseContext.displayName = rootComponentName + "Context";
|
|
28
35
|
const index = defaultContexts.length;
|
|
29
36
|
defaultContexts = [...defaultContexts, defaultContext];
|
|
30
|
-
const Provider = (props) => {
|
|
37
|
+
const Provider = /* @__PURE__ */ __name((props) => {
|
|
31
38
|
const { scope, children, ...context } = props;
|
|
32
39
|
const Context = scope?.[scopeName]?.[index] || BaseContext;
|
|
33
40
|
const value = React.useMemo(() => context, Object.values(context));
|
|
34
41
|
return /* @__PURE__ */ jsx(Context.Provider, { value, children });
|
|
35
|
-
};
|
|
42
|
+
}, "Provider");
|
|
36
43
|
Provider.displayName = rootComponentName + "Provider";
|
|
37
44
|
function useContext2(consumerName, scope, options = {}) {
|
|
38
45
|
const { optional = false } = options;
|
|
@@ -43,43 +50,47 @@ function createContextScope(scopeName, createContextScopeDeps = []) {
|
|
|
43
50
|
if (optional) return void 0;
|
|
44
51
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
45
52
|
}
|
|
53
|
+
__name(useContext2, "useContext");
|
|
46
54
|
return [Provider, useContext2];
|
|
47
55
|
}
|
|
48
|
-
|
|
56
|
+
__name(createContext3, "createContext");
|
|
57
|
+
const createScope = /* @__PURE__ */ __name(() => {
|
|
49
58
|
const scopeContexts = defaultContexts.map((defaultContext) => {
|
|
50
59
|
return React.createContext(defaultContext);
|
|
51
60
|
});
|
|
52
|
-
return function useScope(scope) {
|
|
61
|
+
return /* @__PURE__ */ __name(function useScope(scope) {
|
|
53
62
|
const contexts = scope?.[scopeName] || scopeContexts;
|
|
54
63
|
return React.useMemo(
|
|
55
64
|
() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
|
|
56
65
|
[scope, contexts]
|
|
57
66
|
);
|
|
58
|
-
};
|
|
59
|
-
};
|
|
67
|
+
}, "useScope");
|
|
68
|
+
}, "createScope");
|
|
60
69
|
createScope.scopeName = scopeName;
|
|
61
70
|
return [createContext3, composeContextScopes(createScope, ...createContextScopeDeps)];
|
|
62
71
|
}
|
|
72
|
+
__name(createContextScope, "createContextScope");
|
|
63
73
|
function composeContextScopes(...scopes) {
|
|
64
74
|
const baseScope = scopes[0];
|
|
65
75
|
if (scopes.length === 1) return baseScope;
|
|
66
|
-
const createScope = () => {
|
|
76
|
+
const createScope = /* @__PURE__ */ __name(() => {
|
|
67
77
|
const scopeHooks = scopes.map((createScope2) => ({
|
|
68
78
|
useScope: createScope2(),
|
|
69
79
|
scopeName: createScope2.scopeName
|
|
70
80
|
}));
|
|
71
|
-
return function useComposedScopes(overrideScopes) {
|
|
81
|
+
return /* @__PURE__ */ __name(function useComposedScopes(overrideScopes) {
|
|
72
82
|
const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
|
|
73
83
|
const scopeProps = useScope(overrideScopes);
|
|
74
84
|
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
75
85
|
return { ...nextScopes2, ...currentScope };
|
|
76
86
|
}, {});
|
|
77
87
|
return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
|
|
78
|
-
};
|
|
79
|
-
};
|
|
88
|
+
}, "useComposedScopes");
|
|
89
|
+
}, "createScope");
|
|
80
90
|
createScope.scopeName = baseScope.scopeName;
|
|
81
91
|
return createScope;
|
|
82
92
|
}
|
|
93
|
+
__name(composeContextScopes, "composeContextScopes");
|
|
83
94
|
export {
|
|
84
95
|
createContext2 as createContext,
|
|
85
96
|
createContextScope
|
package/dist/index.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/create-context.tsx"],
|
|
4
|
-
"sourcesContent": ["import * as React from 'react';\n\ntype ContextProvider<ContextValueType extends object | null> = React.FC<\n ContextValueType & {\n children: React.ReactNode;\n }\n>;\n\ninterface UseAssertedContext<ContextValueType extends object | null> {\n (consumerName: string): ContextValueType;\n}\n\ninterface UseContext<ContextValueType extends object | null> {\n (consumerName: string): ContextValueType;\n (consumerName: string, options: { optional: true }): ContextValueType | undefined;\n}\n\nfunction createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n): readonly [ContextProvider<ContextValueType>, UseContext<ContextValueType>];\nfunction createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext: ContextValueType,\n): readonly [ContextProvider<ContextValueType>, UseAssertedContext<ContextValueType>];\n\
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\n\ntype ContextProvider<ContextValueType extends object | null> = React.FC<\n ContextValueType & {\n children: React.ReactNode;\n }\n>;\n\ninterface UseAssertedContext<ContextValueType extends object | null> {\n (consumerName: string): ContextValueType;\n}\n\ninterface UseContext<ContextValueType extends object | null> {\n (consumerName: string): ContextValueType;\n (consumerName: string, options: { optional: true }): ContextValueType | undefined;\n}\n\nfunction createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n): readonly [ContextProvider<ContextValueType>, UseContext<ContextValueType>];\nfunction createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext: ContextValueType,\n): readonly [ContextProvider<ContextValueType>, UseAssertedContext<ContextValueType>];\n\n/* @__NO_SIDE_EFFECTS__ */ function createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext?: ContextValueType,\n): readonly [ContextProvider<ContextValueType>, UseContext<ContextValueType>] {\n const Context = React.createContext<ContextValueType | undefined>(defaultContext);\n Context.displayName = rootComponentName + 'Context';\n\n const Provider: ContextProvider<ContextValueType> = (props) => {\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 Provider.displayName = rootComponentName + 'Provider';\n\n function useContext(consumerName: string): ContextValueType;\n function useContext(\n consumerName: string,\n options: { optional: true },\n ): ContextValueType | undefined;\n\n function useContext(\n consumerName: string,\n options: { optional?: boolean } = {},\n ): ContextValueType | undefined {\n const { optional = false } = options;\n const context = React.useContext(Context);\n if (context) return context;\n if (defaultContext !== undefined) return defaultContext;\n if (optional) return undefined;\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 return [Provider, useContext];\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\ntype ScopedContextProvider<ContextValueType extends object | null> = React.FC<\n ContextValueType & {\n scope: Scope<ContextValueType>;\n children: React.ReactNode;\n }\n>;\n\ninterface UseScopedContext<ContextValueType extends object | null> {\n (consumerName: string, scope: Scope<ContextValueType | undefined>): ContextValueType;\n (\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n options: { optional?: true },\n ): ContextValueType | undefined;\n}\n\ninterface UseAssertedScopedContext<ContextValueType extends object | null> {\n (consumerName: string, scope: Scope<ContextValueType | undefined>): ContextValueType;\n}\n\ninterface CreateScopedContext {\n <ContextValueType extends object | null>(\n rootComponentName: string,\n ): readonly [ScopedContextProvider<ContextValueType>, UseScopedContext<ContextValueType>];\n <ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext: ContextValueType,\n ): readonly [ScopedContextProvider<ContextValueType>, UseAssertedScopedContext<ContextValueType>];\n}\n\n/* @__NO_SIDE_EFFECTS__ */ function createContextScope(\n scopeName: string,\n createContextScopeDeps: CreateScope[] = [],\n): readonly [CreateScopedContext, 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 BaseContext.displayName = rootComponentName + 'Context';\n const index = defaultContexts.length;\n defaultContexts = [...defaultContexts, defaultContext];\n\n const Provider: ScopedContextProvider<ContextValueType> = (props) => {\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 Provider.displayName = rootComponentName + 'Provider';\n\n function useContext(\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n ): ContextValueType;\n function useContext(\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n options: { optional: true },\n ): ContextValueType | undefined;\n\n function useContext(\n consumerName: string,\n scope: Scope<ContextValueType | undefined>,\n options: { optional?: boolean } = {},\n ): ContextValueType | undefined {\n const { optional = false } = options;\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 (optional) return undefined;\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 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, ...CreateScope[]]): 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 {\n CreateScope,\n CreateScopedContext,\n Scope,\n UseAssertedContext,\n UseAssertedScopedContext,\n UseContext,\n UseScopedContext,\n};\n"],
|
|
5
|
+
"mappings": ";;;;AAAA,YAAY,WAAW;AAqCZ;AAAA;AAZgB,SAASA,eAClC,mBACA,gBAC4E;AAC5E,QAAM,UAAgB,oBAA4C,cAAc;AAChF,UAAQ,cAAc,oBAAoB;AAE1C,QAAM,WAA8C,wBAAC,UAAU;AAC7D,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,E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AA1BS;",
|
|
6
6
|
"names": ["createContext", "useContext", "createScope", "nextScopes"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@radix-ui/react-context",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
],
|
|
12
12
|
"sideEffects": false,
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"@types/react": "^19.2.
|
|
15
|
-
"@types/react-dom": "^19.2.
|
|
16
|
-
"react": "^19.2.
|
|
17
|
-
"react-dom": "^19.2.
|
|
14
|
+
"@types/react": "^19.2.17",
|
|
15
|
+
"@types/react-dom": "^19.2.3",
|
|
16
|
+
"react": "^19.2.7",
|
|
17
|
+
"react-dom": "^19.2.7",
|
|
18
18
|
"typescript": "^5.9.3",
|
|
19
|
-
"@repo/
|
|
20
|
-
"@repo/
|
|
19
|
+
"@repo/builder": "0.0.0",
|
|
20
|
+
"@repo/typescript-config": "0.0.0"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"@types/react": "*",
|