@tamagui/create-context 1.13.3 → 1.14.0

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.
@@ -1,137 +1,2 @@
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
- var create_context_exports = {};
30
- __export(create_context_exports, {
31
- createContext: () => createContext,
32
- createContextScope: () => createContextScope
33
- });
34
- module.exports = __toCommonJS(create_context_exports);
35
- var import_jsx_runtime = require("react/jsx-runtime");
36
- var React = __toESM(require("react"));
37
- function createContext(rootComponentName, defaultContext) {
38
- const Context = React.createContext(defaultContext);
39
- function Provider(props) {
40
- const { children, ...context } = props;
41
- const value = React.useMemo(() => context, Object.values(context));
42
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
43
- }
44
- function useContext(consumerName) {
45
- const context = React.useContext(Context);
46
- if (context)
47
- return context;
48
- if (defaultContext !== void 0)
49
- return defaultContext;
50
- throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
51
- }
52
- Provider.displayName = `${rootComponentName}Provider`;
53
- return [Provider, useContext];
54
- }
55
- function createContextScope(scopeName, createContextScopeDeps = []) {
56
- let defaultContexts = [];
57
- function createContext2(rootComponentName, defaultContext) {
58
- const BaseContext = React.createContext(defaultContext);
59
- const index = defaultContexts.length;
60
- defaultContexts = [...defaultContexts, defaultContext];
61
- function Provider(props) {
62
- const { scope, children, ...context } = props;
63
- const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
64
- const value = React.useMemo(
65
- () => context,
66
- Object.values(context)
67
- );
68
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
69
- }
70
- function useContext(consumerName, scope, options) {
71
- const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
72
- const context = React.useContext(Context);
73
- if (context)
74
- return context;
75
- if (defaultContext !== void 0)
76
- return defaultContext;
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
- }
86
- }
87
- Provider.displayName = `${rootComponentName}Provider`;
88
- return [Provider, useContext];
89
- }
90
- const createScope = () => {
91
- const scopeContexts = defaultContexts.map((defaultContext) => {
92
- return React.createContext(defaultContext);
93
- });
94
- return function useScope(scope) {
95
- const contexts = (scope == null ? void 0 : scope[scopeName]) || scopeContexts;
96
- return React.useMemo(
97
- () => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
98
- [scope, contexts]
99
- );
100
- };
101
- };
102
- createScope.scopeName = scopeName;
103
- return [
104
- createContext2,
105
- composeContextScopes(createScope, ...createContextScopeDeps)
106
- ];
107
- }
108
- function composeContextScopes(...scopes) {
109
- const baseScope = scopes[0];
110
- if (scopes.length === 1)
111
- return baseScope;
112
- const createScope = () => {
113
- const scopeHooks = scopes.map((createScope2) => ({
114
- useScope: createScope2(),
115
- scopeName: createScope2.scopeName
116
- }));
117
- return function useComposedScopes(overrideScopes) {
118
- const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
119
- const scopeProps = useScope(overrideScopes);
120
- const currentScope = scopeProps[`__scope${scopeName}`];
121
- return { ...nextScopes2, ...currentScope };
122
- }, {});
123
- return React.useMemo(
124
- () => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),
125
- [nextScopes]
126
- );
127
- };
128
- };
129
- createScope.scopeName = baseScope.scopeName;
130
- return createScope;
131
- }
132
- // Annotate the CommonJS export names for ESM import in node:
133
- 0 && (module.exports = {
134
- createContext,
135
- createContextScope
136
- });
1
+ "use strict";var V=Object.create;var S=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var b=Object.getPrototypeOf,h=Object.prototype.hasOwnProperty;var _=(e,t)=>{for(var n in t)S(e,n,{get:t[n],enumerable:!0})},T=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of m(t))!h.call(e,r)&&r!==n&&S(e,r,{get:()=>t[r],enumerable:!(a=g(t,r))||a.enumerable});return e};var $=(e,t,n)=>(n=e!=null?V(b(e)):{},T(t||!e||!e.__esModule?S(n,"default",{value:e,enumerable:!0}):n,e)),w=e=>T(S({},"__esModule",{value:!0}),e);var N={};_(N,{createContext:()=>k,createContextScope:()=>R});module.exports=w(N);var P=require("react/jsx-runtime"),p=$(require("react"));function k(e,t){const n=p.createContext(t);function a(s){const{children:o,...c}=s,u=p.useMemo(()=>c,Object.values(c));return(0,P.jsx)(n.Provider,{value:u,children:o})}function r(s){const o=p.useContext(n);if(o)return o;if(t!==void 0)return t;throw new Error(`\`${s}\` must be used within \`${e}\``)}return a.displayName=`${e}Provider`,[a,r]}function R(e,t=[]){let n=[];function a(s,o){const c=p.createContext(o),u=n.length;n=[...n,o];function d(l){const{scope:x,children:i,...C}=l,f=(x==null?void 0:x[e][u])||c,y=p.useMemo(()=>C,Object.values(C));return(0,P.jsx)(f.Provider,{value:y,children:i})}function v(l,x,i){const C=(x==null?void 0:x[e][u])||c,f=p.useContext(C);if(f)return f;if(o!==void 0)return o;const y=`\`${l}\` must be used within \`${s}\``;if(i!=null&&i.fallback)return(i==null?void 0:i.warn)!==!1&&console.warn(y),i.fallback;throw new Error(y)}return d.displayName=`${s}Provider`,[d,v]}const r=()=>{const s=n.map(o=>p.createContext(o));return function(c){const u=(c==null?void 0:c[e])||s;return p.useMemo(()=>({[`__scope${e}`]:{...c,[e]:u}}),[c,u])}};return r.scopeName=e,[a,M(r,...t)]}function M(...e){const t=e[0];if(e.length===1)return t;const n=()=>{const a=e.map(r=>({useScope:r(),scopeName:r.scopeName}));return function(s){const o=a.reduce((c,{useScope:u,scopeName:d})=>{const l=u(s)[`__scope${d}`];return{...c,...l}},{});return p.useMemo(()=>({[`__scope${t.scopeName}`]:o}),[o])}};return n.scopeName=t.scopeName,n}0&&(module.exports={createContext,createContextScope});
137
2
  //# sourceMappingURL=create-context.js.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/create-context.tsx"],
4
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;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CAACE,EAAY,CAAE,SAAAC,EAAU,UAAAjB,CAAU,IAAM,CAK5E,MAAMkB,EADaD,EAASF,CAAc,EACV,UAAUf,GAAW,EACrD,MAAO,CAAE,GAAGgB,EAAY,GAAGE,CAAa,CAC1C,EAAG,CAAC,CAAC,EAEL,OAAO7B,EAAM,QACX,KAAO,CAAE,CAAC,UAAUwB,EAAU,WAAW,EAAGG,CAAW,GACvD,CAACA,CAAU,CACb,CACF,CACF,EAEA,OAAAR,EAAY,UAAYK,EAAU,UAC3BL,CACT",
6
+ "names": ["create_context_exports", "__export", "createContext", "createContextScope", "__toCommonJS", "import_jsx_runtime", "React", "rootComponentName", "defaultContext", "Context", "Provider", "props", "children", "context", "value", "useContext", "consumerName", "scopeName", "createContextScopeDeps", "defaultContexts", "BaseContext", "index", "scope", "options", "missingContextMessage", "createScope", "scopeContexts", "contexts", "composeContextScopes", "scopes", "baseScope", "scopeHooks", "overrideScopes", "nextScopes", "useScope", "currentScope"]
7
7
  }
package/dist/cjs/index.js CHANGED
@@ -1,19 +1,2 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
- var src_exports = {};
17
- module.exports = __toCommonJS(src_exports);
18
- __reExport(src_exports, require("./create-context"), module.exports);
1
+ "use strict";var a=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var p=(r,o,f,x)=>{if(o&&typeof o=="object"||typeof o=="function")for(let e of c(o))!d.call(r,e)&&e!==f&&a(r,e,{get:()=>o[e],enumerable:!(x=b(o,e))||x.enumerable});return r},t=(r,o,f)=>(p(r,o,"default"),f&&p(f,o,"default"));var g=r=>p(a({},"__esModule",{value:!0}),r);var m={};module.exports=g(m);t(m,require("./create-context"),module.exports);
19
2
  //# sourceMappingURL=index.js.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
4
  "sourcesContent": ["export * from './create-context'\n"],
5
- "mappings": ";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,wBAAc,6BAAd;",
6
- "names": []
5
+ "mappings": "iaAAA,IAAAA,EAAA,kBAAAC,EAAAD,GAAAE,EAAAF,EAAc,4BAAd",
6
+ "names": ["src_exports", "__toCommonJS", "__reExport"]
7
7
  }
@@ -1,102 +1,2 @@
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
- };
1
+ import{jsx as P}from"react/jsx-runtime";import*as u from"react";function T(t,c){const o=u.createContext(c);function x(r){const{children:e,...n}=r,s=u.useMemo(()=>n,Object.values(n));return P(o.Provider,{value:s,children:e})}function i(r){const e=u.useContext(o);if(e)return e;if(c!==void 0)return c;throw new Error(`\`${r}\` must be used within \`${t}\``)}return x.displayName=`${t}Provider`,[x,i]}function V(t,c=[]){let o=[];function x(r,e){const n=u.createContext(e),s=o.length;o=[...o,e];function d(l){const{scope:p,children:a,...C}=l,f=(p==null?void 0:p[t][s])||n,y=u.useMemo(()=>C,Object.values(C));return P(f.Provider,{value:y,children:a})}function S(l,p,a){const C=(p==null?void 0:p[t][s])||n,f=u.useContext(C);if(f)return f;if(e!==void 0)return e;const y=`\`${l}\` must be used within \`${r}\``;if(a!=null&&a.fallback)return(a==null?void 0:a.warn)!==!1&&console.warn(y),a.fallback;throw new Error(y)}return d.displayName=`${r}Provider`,[d,S]}const i=()=>{const r=o.map(e=>u.createContext(e));return function(n){const s=(n==null?void 0:n[t])||r;return u.useMemo(()=>({[`__scope${t}`]:{...n,[t]:s}}),[n,s])}};return i.scopeName=t,[x,v(i,...c)]}function v(...t){const c=t[0];if(t.length===1)return c;const o=()=>{const x=t.map(i=>({useScope:i(),scopeName:i.scopeName}));return function(r){const e=x.reduce((n,{useScope:s,scopeName:d})=>{const l=s(r)[`__scope${d}`];return{...n,...l}},{});return u.useMemo(()=>({[`__scope${c.scopeName}`]:e}),[e])}};return o.scopeName=c.scopeName,o}export{T as createContext,V as createContextScope};
102
2
  //# sourceMappingURL=create-context.js.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/create-context.tsx"],
4
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,cAA4C,cAAc;AACpF,UAAM,QAAQ,gBAAgB;AAC9B,sBAAkB,CAAC,GAAG,iBAAiB,cAAc;AAErD,aAAS,SACP,OAIA;AACA,YAAM,EAAE,OAAO,UAAU,GAAG,QAAQ,IAAI;AACxC,YAAM,WAAU,+BAAQ,WAAW,WAAU;AAG7C,YAAM,QAAQ,MAAM;AAAA,QAClB,MAAM;AAAA,QACN,OAAO,OAAO,OAAO;AAAA,MACvB;AACA,aAAO,oBAAC,QAAQ,UAAR,EAAiB,OAAe,UAAS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,EAAaF,EAAO,IAAKJ,IAAiB,CAC9C,SAAUA,EAAY,EACtB,UAAWA,EAAY,SACzB,EAAE,EAEF,OAAO,SAA2BO,EAAgB,CAChD,MAAMC,EAAaF,EAAW,OAAO,CAACE,EAAY,CAAE,SAAAC,EAAU,UAAAjB,CAAU,IAAM,CAK5E,MAAMkB,EADaD,EAASF,CAAc,EACV,UAAUf,GAAW,EACrD,MAAO,CAAE,GAAGgB,EAAY,GAAGE,CAAa,CAC1C,EAAG,CAAC,CAAC,EAEL,OAAO/B,EAAM,QACX,KAAO,CAAE,CAAC,UAAU0B,EAAU,WAAW,EAAGG,CAAW,GACvD,CAACA,CAAU,CACb,CACF,CACF,EAEA,OAAAR,EAAY,UAAYK,EAAU,UAC3BL,CACT",
6
+ "names": ["jsx", "React", "createContext", "rootComponentName", "defaultContext", "Context", "Provider", "props", "children", "context", "value", "useContext", "consumerName", "createContextScope", "scopeName", "createContextScopeDeps", "defaultContexts", "BaseContext", "index", "scope", "options", "missingContextMessage", "createScope", "scopeContexts", "contexts", "composeContextScopes", "scopes", "baseScope", "scopeHooks", "overrideScopes", "nextScopes", "useScope", "currentScope"]
7
7
  }
@@ -1,102 +1,2 @@
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
- };
1
+ import{jsx as P}from"react/jsx-runtime";import*as u from"react";function T(t,c){const o=u.createContext(c);function x(r){const{children:e,...n}=r,s=u.useMemo(()=>n,Object.values(n));return P(o.Provider,{value:s,children:e})}function i(r){const e=u.useContext(o);if(e)return e;if(c!==void 0)return c;throw new Error(`\`${r}\` must be used within \`${t}\``)}return x.displayName=`${t}Provider`,[x,i]}function V(t,c=[]){let o=[];function x(r,e){const n=u.createContext(e),s=o.length;o=[...o,e];function d(l){const{scope:p,children:a,...C}=l,f=(p==null?void 0:p[t][s])||n,y=u.useMemo(()=>C,Object.values(C));return P(f.Provider,{value:y,children:a})}function S(l,p,a){const C=(p==null?void 0:p[t][s])||n,f=u.useContext(C);if(f)return f;if(e!==void 0)return e;const y=`\`${l}\` must be used within \`${r}\``;if(a!=null&&a.fallback)return(a==null?void 0:a.warn)!==!1&&console.warn(y),a.fallback;throw new Error(y)}return d.displayName=`${r}Provider`,[d,S]}const i=()=>{const r=o.map(e=>u.createContext(e));return function(n){const s=(n==null?void 0:n[t])||r;return u.useMemo(()=>({[`__scope${t}`]:{...n,[t]:s}}),[n,s])}};return i.scopeName=t,[x,v(i,...c)]}function v(...t){const c=t[0];if(t.length===1)return c;const o=()=>{const x=t.map(i=>({useScope:i(),scopeName:i.scopeName}));return function(r){const e=x.reduce((n,{useScope:s,scopeName:d})=>{const l=s(r)[`__scope${d}`];return{...n,...l}},{});return u.useMemo(()=>({[`__scope${c.scopeName}`]:e}),[e])}};return o.scopeName=c.scopeName,o}export{T as createContext,V as createContextScope};
102
2
  //# sourceMappingURL=create-context.mjs.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/create-context.tsx"],
4
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,cAA4C,cAAc;AACpF,UAAM,QAAQ,gBAAgB;AAC9B,sBAAkB,CAAC,GAAG,iBAAiB,cAAc;AAErD,aAAS,SACP,OAIA;AACA,YAAM,EAAE,OAAO,UAAU,GAAG,QAAQ,IAAI;AACxC,YAAM,WAAU,+BAAQ,WAAW,WAAU;AAG7C,YAAM,QAAQ,MAAM;AAAA,QA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C,EAAYD,EAAO,CAAC,EAC1B,GAAIA,EAAO,SAAW,EAAG,OAAOC,EAEhC,MAAML,EAA2B,IAAM,CACrC,MAAMM,EAAaF,EAAO,IAAKJ,IAAiB,CAC9C,SAAUA,EAAY,EACtB,UAAWA,EAAY,SACzB,EAAE,EAEF,OAAO,SAA2BO,EAAgB,CAChD,MAAMC,EAAaF,EAAW,OAAO,CAACE,EAAY,CAAE,SAAAC,EAAU,UAAAjB,CAAU,IAAM,CAK5E,MAAMkB,EADaD,EAASF,CAAc,EACV,UAAUf,GAAW,EACrD,MAAO,CAAE,GAAGgB,EAAY,GAAGE,CAAa,CAC1C,EAAG,CAAC,CAAC,EAEL,OAAO/B,EAAM,QACX,KAAO,CAAE,CAAC,UAAU0B,EAAU,WAAW,EAAGG,CAAW,GACvD,CAACA,CAAU,CACb,CACF,CACF,EAEA,OAAAR,EAAY,UAAYK,EAAU,UAC3BL,CACT",
6
+ "names": ["jsx", "React", "createContext", "rootComponentName", "defaultContext", "Context", "Provider", "props", "children", "context", "value", "useContext", "consumerName", "createContextScope", "scopeName", "createContextScopeDeps", "defaultContexts", "BaseContext", "index", "scope", "options", "missingContextMessage", "createScope", "scopeContexts", "contexts", "composeContextScopes", "scopes", "baseScope", "scopeHooks", "overrideScopes", "nextScopes", "useScope", "currentScope"]
7
7
  }
package/dist/esm/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./create-context";
1
+ export*from"./create-context";
2
2
  //# sourceMappingURL=index.js.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
4
  "sourcesContent": ["export * from './create-context'\n"],
5
- "mappings": "AAAA,cAAc;",
5
+ "mappings": "AAAA,WAAc",
6
6
  "names": []
7
7
  }
@@ -1,2 +1,2 @@
1
- export * from "./create-context";
1
+ export*from"./create-context";
2
2
  //# sourceMappingURL=index.mjs.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
4
  "sourcesContent": ["export * from './create-context'\n"],
5
- "mappings": "AAAA,cAAc;",
5
+ "mappings": "AAAA,WAAc",
6
6
  "names": []
7
7
  }
@@ -1,101 +1,2 @@
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
- };
1
+ import*as a from"react";function m(t,r){const o=a.createContext(r);function p(c){const{children:e,...n}=c,s=a.useMemo(()=>n,Object.values(n));return<o.Provider value={s}>{e}</o.Provider>}function u(c){const e=a.useContext(o);if(e)return e;if(r!==void 0)return r;throw new Error(`\`${c}\` must be used within \`${t}\``)}return p.displayName=`${t}Provider`,[p,u]}function v(t,r=[]){let o=[];function p(c,e){const n=a.createContext(e),s=o.length;o=[...o,e];function l(i){const{scope:y,children:x,...d}=i,C=y?.[t][s]||n,f=a.useMemo(()=>d,Object.values(d));return<C.Provider value={f}>{x}</C.Provider>}function S(i,y,x){const d=y?.[t][s]||n,C=a.useContext(d);if(C)return C;if(e!==void 0)return e;const f=`\`${i}\` must be used within \`${c}\``;if(x?.fallback)return x?.warn!==!1&&console.warn(f),x.fallback;throw new Error(f)}return l.displayName=`${c}Provider`,[l,S]}const u=()=>{const c=o.map(e=>a.createContext(e));return function(n){const s=n?.[t]||c;return a.useMemo(()=>({[`__scope${t}`]:{...n,[t]:s}}),[n,s])}};return u.scopeName=t,[p,P(u,...r)]}function P(...t){const r=t[0];if(t.length===1)return r;const o=()=>{const p=t.map(u=>({useScope:u(),scopeName:u.scopeName}));return function(c){const e=p.reduce((n,{useScope:s,scopeName:l})=>{const i=s(c)[`__scope${l}`];return{...n,...i}},{});return a.useMemo(()=>({[`__scope${r.scopeName}`]:e}),[e])}};return o.scopeName=r.scopeName,o}export{m as createContext,v as createContextScope};
101
2
  //# sourceMappingURL=create-context.js.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/create-context.tsx"],
4
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,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,MAAMW,EAA2B,IAAM,CACrC,MAAMC,EAAgBP,EAAgB,IAAKZ,GAClCH,EAAM,cAAcG,CAAc,CAC1C,EACD,OAAO,SAAkBe,EAAc,CACrC,MAAMK,EAAWL,IAAQL,CAAS,GAAKS,EACvC,OAAOtB,EAAM,QACX,KAAO,CAAE,CAAC,UAAUa,GAAW,EAAG,CAAE,GAAGK,EAAO,CAACL,CAAS,EAAGU,CAAS,CAAE,GACtE,CAACL,EAAOK,CAAQ,CAClB,CACF,CACF,EAEA,OAAAF,EAAY,UAAYR,EACjB,CACLZ,EACAuB,EAAqBH,EAAa,GAAGP,CAAsB,CAC7D,CACF,CAMA,SAASU,KAAwBC,EAAuB,CACtD,MAAMC,EAAYD,EAAO,CAAC,EAC1B,GAAIA,EAAO,SAAW,EAAG,OAAOC,EAEhC,MAAML,EAA2B,IAAM,CACrC,MAAMM,EAAaF,EAAO,IAAKJ,IAAiB,CAC9C,SAAUA,EAAY,EACtB,UAAWA,EAAY,SACzB,EAAE,EAEF,OAAO,SAA2BO,EAAgB,CAChD,MAAMC,EAAaF,EAAW,OAAO,CAACE,EAAY,CAAE,SAAAC,EAAU,UAAAjB,CAAU,IAAM,CAK5E,MAAMkB,EADaD,EAASF,CAAc,EACV,UAAUf,GAAW,EACrD,MAAO,CAAE,GAAGgB,EAAY,GAAGE,CAAa,CAC1C,EAAG,CAAC,CAAC,EAEL,OAAO/B,EAAM,QACX,KAAO,CAAE,CAAC,UAAU0B,EAAU,WAAW,EAAGG,CAAW,GACvD,CAACA,CAAU,CACb,CACF,CACF,EAEA,OAAAR,EAAY,UAAYK,EAAU,UAC3BL,CACT",
6
+ "names": ["React", "createContext", "rootComponentName", "defaultContext", "Context", "Provider", "props", "children", "context", "value", "useContext", "consumerName", "createContextScope", "scopeName", "createContextScopeDeps", "defaultContexts", "BaseContext", "index", "scope", "options", "missingContextMessage", "createScope", "scopeContexts", "contexts", "composeContextScopes", "scopes", "baseScope", "scopeHooks", "overrideScopes", "nextScopes", "useScope", "currentScope"]
7
7
  }
@@ -1,101 +1,2 @@
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
- };
1
+ import*as a from"react";function m(t,r){const o=a.createContext(r);function p(c){const{children:e,...n}=c,s=a.useMemo(()=>n,Object.values(n));return<o.Provider value={s}>{e}</o.Provider>}function u(c){const e=a.useContext(o);if(e)return e;if(r!==void 0)return r;throw new Error(`\`${c}\` must be used within \`${t}\``)}return p.displayName=`${t}Provider`,[p,u]}function v(t,r=[]){let o=[];function p(c,e){const n=a.createContext(e),s=o.length;o=[...o,e];function l(i){const{scope:y,children:x,...d}=i,C=y?.[t][s]||n,f=a.useMemo(()=>d,Object.values(d));return<C.Provider value={f}>{x}</C.Provider>}function S(i,y,x){const d=y?.[t][s]||n,C=a.useContext(d);if(C)return C;if(e!==void 0)return e;const f=`\`${i}\` must be used within \`${c}\``;if(x?.fallback)return x?.warn!==!1&&console.warn(f),x.fallback;throw new Error(f)}return l.displayName=`${c}Provider`,[l,S]}const u=()=>{const c=o.map(e=>a.createContext(e));return function(n){const s=n?.[t]||c;return a.useMemo(()=>({[`__scope${t}`]:{...n,[t]:s}}),[n,s])}};return u.scopeName=t,[p,P(u,...r)]}function P(...t){const r=t[0];if(t.length===1)return r;const o=()=>{const p=t.map(u=>({useScope:u(),scopeName:u.scopeName}));return function(c){const e=p.reduce((n,{useScope:s,scopeName:l})=>{const i=s(c)[`__scope${l}`];return{...n,...i}},{});return a.useMemo(()=>({[`__scope${r.scopeName}`]:e}),[e])}};return o.scopeName=r.scopeName,o}export{m as createContext,v as createContextScope};
101
2
  //# sourceMappingURL=create-context.mjs.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/create-context.tsx"],
4
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;A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cAAcG,CAAc,CAC1C,EACD,OAAO,SAAkBe,EAAc,CACrC,MAAMK,EAAWL,IAAQL,CAAS,GAAKS,EACvC,OAAOtB,EAAM,QACX,KAAO,CAAE,CAAC,UAAUa,GAAW,EAAG,CAAE,GAAGK,EAAO,CAACL,CAAS,EAAGU,CAAS,CAAE,GACtE,CAACL,EAAOK,CAAQ,CAClB,CACF,CACF,EAEA,OAAAF,EAAY,UAAYR,EACjB,CACLZ,EACAuB,EAAqBH,EAAa,GAAGP,CAAsB,CAC7D,CACF,CAMA,SAASU,KAAwBC,EAAuB,CACtD,MAAMC,EAAYD,EAAO,CAAC,EAC1B,GAAIA,EAAO,SAAW,EAAG,OAAOC,EAEhC,MAAML,EAA2B,IAAM,CACrC,MAAMM,EAAaF,EAAO,IAAKJ,IAAiB,CAC9C,SAAUA,EAAY,EACtB,UAAWA,EAAY,SACzB,EAAE,EAEF,OAAO,SAA2BO,EAAgB,CAChD,MAAMC,EAAaF,EAAW,OAAO,CAACE,EAAY,CAAE,SAAAC,EAAU,UAAAjB,CAAU,IAAM,CAK5E,MAAMkB,EADaD,EAASF,CAAc,EACV,UAAUf,GAAW,EACrD,MAAO,CAAE,GAAGgB,EAAY,GAAGE,CAAa,CAC1C,EAAG,CAAC,CAAC,EAEL,OAAO/B,EAAM,QACX,KAAO,CAAE,CAAC,UAAU0B,EAAU,WAAW,EAAGG,CAAW,GACvD,CAACA,CAAU,CACb,CACF,CACF,EAEA,OAAAR,EAAY,UAAYK,EAAU,UAC3BL,CACT",
6
+ "names": ["React", "createContext", "rootComponentName", "defaultContext", "Context", "Provider", "props", "children", "context", "value", "useContext", "consumerName", "createContextScope", "scopeName", "createContextScopeDeps", "defaultContexts", "BaseContext", "index", "scope", "options", "missingContextMessage", "createScope", "scopeContexts", "contexts", "composeContextScopes", "scopes", "baseScope", "scopeHooks", "overrideScopes", "nextScopes", "useScope", "currentScope"]
7
7
  }
package/dist/jsx/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./create-context";
1
+ export*from"./create-context";
2
2
  //# sourceMappingURL=index.js.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
4
  "sourcesContent": ["export * from './create-context'\n"],
5
- "mappings": "AAAA,cAAc;",
5
+ "mappings": "AAAA,WAAc",
6
6
  "names": []
7
7
  }
@@ -1,2 +1,2 @@
1
- export * from "./create-context";
1
+ export*from"./create-context";
2
2
  //# sourceMappingURL=index.mjs.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
4
  "sourcesContent": ["export * from './create-context'\n"],
5
- "mappings": "AAAA,cAAc;",
5
+ "mappings": "AAAA,WAAc",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/create-context",
3
- "version": "1.13.3",
3
+ "version": "1.14.0",
4
4
  "sideEffects": false,
5
5
  "source": "src/index.ts",
6
6
  "types": "./types/index.d.ts",
@@ -30,7 +30,7 @@
30
30
  "react": "*"
31
31
  },
32
32
  "devDependencies": {
33
- "@tamagui/build": "1.13.3",
33
+ "@tamagui/build": "1.14.0",
34
34
  "react": "^18.2.0"
35
35
  },
36
36
  "publishConfig": {