@tamagui/create-context 1.0.0-beta.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.
@@ -0,0 +1,150 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
+ var __spreadValues = (a, b) => {
13
+ for (var prop in b || (b = {}))
14
+ if (__hasOwnProp.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ if (__getOwnPropSymbols)
17
+ for (var prop of __getOwnPropSymbols(b)) {
18
+ if (__propIsEnum.call(b, prop))
19
+ __defNormalProp(a, prop, b[prop]);
20
+ }
21
+ return a;
22
+ };
23
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
25
+ var __objRest = (source, exclude) => {
26
+ var target = {};
27
+ for (var prop in source)
28
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
29
+ target[prop] = source[prop];
30
+ if (source != null && __getOwnPropSymbols)
31
+ for (var prop of __getOwnPropSymbols(source)) {
32
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
33
+ target[prop] = source[prop];
34
+ }
35
+ return target;
36
+ };
37
+ var __export = (target, all) => {
38
+ for (var name in all)
39
+ __defProp(target, name, { get: all[name], enumerable: true });
40
+ };
41
+ var __copyProps = (to, from, except, desc) => {
42
+ if (from && typeof from === "object" || typeof from === "function") {
43
+ for (let key of __getOwnPropNames(from))
44
+ if (!__hasOwnProp.call(to, key) && key !== except)
45
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
46
+ }
47
+ return to;
48
+ };
49
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
50
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
51
+ var create_context_exports = {};
52
+ __export(create_context_exports, {
53
+ createContext: () => createContext,
54
+ createContextScope: () => createContextScope
55
+ });
56
+ module.exports = __toCommonJS(create_context_exports);
57
+ var React = __toESM(require("react"));
58
+ function createContext(rootComponentName, defaultContext) {
59
+ const Context = React.createContext(defaultContext);
60
+ function Provider(props) {
61
+ const _a = props, { children } = _a, context = __objRest(_a, ["children"]);
62
+ const value = React.useMemo(() => context, Object.values(context));
63
+ return /* @__PURE__ */ React.createElement(Context.Provider, {
64
+ value
65
+ }, children);
66
+ }
67
+ __name(Provider, "Provider");
68
+ function useContext(consumerName) {
69
+ const context = React.useContext(Context);
70
+ if (context)
71
+ return context;
72
+ if (defaultContext !== void 0)
73
+ return defaultContext;
74
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
75
+ }
76
+ __name(useContext, "useContext");
77
+ Provider.displayName = rootComponentName + "Provider";
78
+ return [Provider, useContext];
79
+ }
80
+ __name(createContext, "createContext");
81
+ function createContextScope(scopeName, createContextScopeDeps = []) {
82
+ let defaultContexts = [];
83
+ function createContext2(rootComponentName, defaultContext) {
84
+ const BaseContext = React.createContext(defaultContext);
85
+ const index = defaultContexts.length;
86
+ defaultContexts = [...defaultContexts, defaultContext];
87
+ function Provider(props) {
88
+ const _a = props, { scope, children } = _a, context = __objRest(_a, ["scope", "children"]);
89
+ const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
90
+ const value = React.useMemo(() => context, Object.values(context));
91
+ return /* @__PURE__ */ React.createElement(Context.Provider, {
92
+ value
93
+ }, children);
94
+ }
95
+ __name(Provider, "Provider");
96
+ function useContext(consumerName, scope) {
97
+ const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
98
+ const context = React.useContext(Context);
99
+ if (context)
100
+ return context;
101
+ if (defaultContext !== void 0)
102
+ return defaultContext;
103
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
104
+ }
105
+ __name(useContext, "useContext");
106
+ Provider.displayName = rootComponentName + "Provider";
107
+ return [Provider, useContext];
108
+ }
109
+ __name(createContext2, "createContext");
110
+ const createScope = /* @__PURE__ */ __name(() => {
111
+ const scopeContexts = defaultContexts.map((defaultContext) => {
112
+ return React.createContext(defaultContext);
113
+ });
114
+ return /* @__PURE__ */ __name(function useScope(scope) {
115
+ const contexts = (scope == null ? void 0 : scope[scopeName]) || scopeContexts;
116
+ return React.useMemo(() => ({ [`__scope${scopeName}`]: __spreadProps(__spreadValues({}, scope), { [scopeName]: contexts }) }), [scope, contexts]);
117
+ }, "useScope");
118
+ }, "createScope");
119
+ createScope.scopeName = scopeName;
120
+ return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
121
+ }
122
+ __name(createContextScope, "createContextScope");
123
+ function composeContextScopes(...scopes) {
124
+ const baseScope = scopes[0];
125
+ if (scopes.length === 1)
126
+ return baseScope;
127
+ const createScope = /* @__PURE__ */ __name(() => {
128
+ const scopeHooks = scopes.map((createScope2) => ({
129
+ useScope: createScope2(),
130
+ scopeName: createScope2.scopeName
131
+ }));
132
+ return /* @__PURE__ */ __name(function useComposedScopes(overrideScopes) {
133
+ const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
134
+ const scopeProps = useScope(overrideScopes);
135
+ const currentScope = scopeProps[`__scope${scopeName}`];
136
+ return __spreadValues(__spreadValues({}, nextScopes2), currentScope);
137
+ }, {});
138
+ return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
139
+ }, "useComposedScopes");
140
+ }, "createScope");
141
+ createScope.scopeName = baseScope.scopeName;
142
+ return createScope;
143
+ }
144
+ __name(composeContextScopes, "composeContextScopes");
145
+ // Annotate the CommonJS export names for ESM import in node:
146
+ 0 && (module.exports = {
147
+ createContext,
148
+ createContextScope
149
+ });
150
+ //# sourceMappingURL=create-context.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/create-context.tsx"],
4
+ "sourcesContent": ["// from radix\n// https://github.com/radix-ui/primitives/blob/main/packages/react/context/src/createContext.tsx\n\nimport * as React from 'react'\n\nexport type ScopedProps<P, K extends string> = P & { [Key in `__scope${K}`]?: Scope }\n\nexport function createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext?: ContextValueType\n) {\n const Context = React.createContext<ContextValueType | undefined>(defaultContext)\n\n function Provider(props: ContextValueType & { children: React.ReactNode }) {\n const { children, ...context } = props\n // Only re-memoize when prop values change\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const value = React.useMemo(() => context, Object.values(context)) as ContextValueType\n return <Context.Provider value={value}>{children}</Context.Provider>\n }\n\n function useContext(consumerName: string) {\n const context = React.useContext(Context)\n if (context) return context\n if (defaultContext !== undefined) return defaultContext\n // if a defaultContext wasn't specified, it's a required context.\n throw new Error(`\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``)\n }\n\n Provider.displayName = rootComponentName + 'Provider'\n return [Provider, useContext] as const\n}\n\n/* -------------------------------------------------------------------------------------------------\n * createContextScope\n * -----------------------------------------------------------------------------------------------*/\n\ntype 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(scopeName: string, createContextScopeDeps: CreateScope[] = []) {\n let defaultContexts: any[] = []\n\n /* -----------------------------------------------------------------------------------------------\n * createContext\n * ---------------------------------------------------------------------------------------------*/\n\n function createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext?: ContextValueType\n ) {\n const BaseContext = React.createContext<ContextValueType | undefined>(defaultContext)\n const index = defaultContexts.length\n defaultContexts = [...defaultContexts, defaultContext]\n\n function Provider(\n props: ContextValueType & { scope: Scope<ContextValueType>; children: React.ReactNode }\n ) {\n const { scope, children, ...context } = props\n const Context = scope?.[scopeName][index] || BaseContext\n // Only re-memoize when prop values change\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const value = React.useMemo(() => context, Object.values(context)) as ContextValueType\n return <Context.Provider value={value}>{children}</Context.Provider>\n }\n\n function useContext(consumerName: string, scope: Scope<ContextValueType | undefined>) {\n const Context = scope?.[scopeName][index] || BaseContext\n const context = React.useContext(Context)\n if (context) return context\n if (defaultContext !== undefined) return defaultContext\n // if a defaultContext wasn't specified, it's a required context.\n throw new Error(`\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``)\n }\n\n Provider.displayName = rootComponentName + 'Provider'\n return [Provider, useContext] as const\n }\n\n /* -----------------------------------------------------------------------------------------------\n * createScope\n * ---------------------------------------------------------------------------------------------*/\n\n const createScope: CreateScope = () => {\n const scopeContexts = defaultContexts.map((defaultContext) => {\n return React.createContext(defaultContext)\n })\n return function useScope(scope: Scope) {\n const contexts = scope?.[scopeName] || scopeContexts\n return React.useMemo(\n () => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),\n [scope, contexts]\n )\n }\n }\n\n createScope.scopeName = scopeName\n return [createContext, composeContextScopes(createScope, ...createContextScopeDeps)] as const\n}\n\n/* -------------------------------------------------------------------------------------------------\n * composeContextScopes\n * -----------------------------------------------------------------------------------------------*/\n\nfunction composeContextScopes(...scopes: CreateScope[]) {\n const baseScope = scopes[0]\n if (scopes.length === 1) return baseScope\n\n const createScope: CreateScope = () => {\n const scopeHooks = scopes.map((createScope) => ({\n useScope: createScope(),\n scopeName: createScope.scopeName,\n }))\n\n return function useComposedScopes(overrideScopes) {\n const nextScopes = scopeHooks.reduce((nextScopes, { useScope, scopeName }) => {\n // We are calling a hook inside a callback which React warns against to avoid inconsistent\n // renders, however, scoping doesn't have render side effects so we ignore the rule.\n // eslint-disable-next-line react-hooks/rules-of-hooks\n const scopeProps = useScope(overrideScopes)\n const currentScope = scopeProps[`__scope${scopeName}`]\n return { ...nextScopes, ...currentScope }\n }, {})\n\n return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes])\n }\n }\n\n createScope.scopeName = baseScope.scopeName\n return createScope\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,YAAuB;AAIhB,uBACL,mBACA,gBACA;AACA,QAAM,UAAU,MAAM,cAA4C,cAAc;AAEhF,oBAAkB,OAAyD;AACzE,UAAiC,YAAzB,eAAyB,IAAZ,oBAAY,IAAZ,CAAb;AAGR,UAAM,QAAQ,MAAM,QAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,WAAO,oCAAC,QAAQ,UAAR;AAAA,MAAiB;AAAA,OAAe,QAAS;AAAA,EACnD;AANS;AAQT,sBAAoB,cAAsB;AACxC,UAAM,UAAU,MAAM,WAAW,OAAO;AACxC,QAAI;AAAS,aAAO;AACpB,QAAI,mBAAmB;AAAW,aAAO;AAEzC,UAAM,IAAI,MAAM,KAAK,wCAAwC,qBAAqB;AAAA,EACpF;AANS;AAQT,WAAS,cAAc,oBAAoB;AAC3C,SAAO,CAAC,UAAU,UAAU;AAC9B;AAxBgB;AAuCT,4BAA4B,WAAmB,yBAAwC,CAAC,GAAG;AAChG,MAAI,kBAAyB,CAAC;AAM9B,0BACE,mBACA,gBACA;AACA,UAAM,cAAc,MAAM,cAA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BAAiB;AAAA,MAC9C,UAAU,aAAY;AAAA,MACtB,WAAW,aAAY;AAAA,IACzB,EAAE;AAEF,WAAO,kDAA2B,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAAC,aAAY,EAAE,UAAU,gBAAgB;AAI5E,cAAM,aAAa,SAAS,cAAc;AAC1C,cAAM,eAAe,WAAW,UAAU;AAC1C,eAAO,kCAAK,cAAe;AAAA,MAC7B,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM,QAAQ,MAAO,IAAG,UAAU,UAAU,cAAc,WAAW,IAAI,CAAC,UAAU,CAAC;AAAA,IAC9F,GAXO;AAAA,EAYT,GAlBiC;AAoBjC,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;AA1BS;",
6
+ "names": []
7
+ }
@@ -0,0 +1,18 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+ var src_exports = {};
16
+ module.exports = __toCommonJS(src_exports);
17
+ __reExport(src_exports, require("./create-context"), module.exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.ts"],
4
+ "sourcesContent": ["export * from './create-context'\n"],
5
+ "mappings": ";;;;;;;;;;;;;;AAAA;AAAA;AAAA,wBAAc,6BAAd;",
6
+ "names": []
7
+ }
@@ -0,0 +1,95 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ import * as React from "react";
4
+ function createContext(rootComponentName, defaultContext) {
5
+ const Context = React.createContext(defaultContext);
6
+ function Provider(props) {
7
+ const { children, ...context } = props;
8
+ const value = React.useMemo(() => context, Object.values(context));
9
+ return /* @__PURE__ */ React.createElement(Context.Provider, {
10
+ value
11
+ }, children);
12
+ }
13
+ __name(Provider, "Provider");
14
+ function useContext(consumerName) {
15
+ const context = React.useContext(Context);
16
+ if (context)
17
+ return context;
18
+ if (defaultContext !== void 0)
19
+ return defaultContext;
20
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
21
+ }
22
+ __name(useContext, "useContext");
23
+ Provider.displayName = rootComponentName + "Provider";
24
+ return [Provider, useContext];
25
+ }
26
+ __name(createContext, "createContext");
27
+ function createContextScope(scopeName, createContextScopeDeps = []) {
28
+ let defaultContexts = [];
29
+ function createContext2(rootComponentName, defaultContext) {
30
+ const BaseContext = React.createContext(defaultContext);
31
+ const index = defaultContexts.length;
32
+ defaultContexts = [...defaultContexts, defaultContext];
33
+ function Provider(props) {
34
+ const { scope, children, ...context } = props;
35
+ const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
36
+ const value = React.useMemo(() => context, Object.values(context));
37
+ return /* @__PURE__ */ React.createElement(Context.Provider, {
38
+ value
39
+ }, children);
40
+ }
41
+ __name(Provider, "Provider");
42
+ function useContext(consumerName, scope) {
43
+ const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
44
+ const context = React.useContext(Context);
45
+ if (context)
46
+ return context;
47
+ if (defaultContext !== void 0)
48
+ return defaultContext;
49
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
50
+ }
51
+ __name(useContext, "useContext");
52
+ Provider.displayName = rootComponentName + "Provider";
53
+ return [Provider, useContext];
54
+ }
55
+ __name(createContext2, "createContext");
56
+ const createScope = /* @__PURE__ */ __name(() => {
57
+ const scopeContexts = defaultContexts.map((defaultContext) => {
58
+ return React.createContext(defaultContext);
59
+ });
60
+ return /* @__PURE__ */ __name(function useScope(scope) {
61
+ const contexts = (scope == null ? void 0 : scope[scopeName]) || scopeContexts;
62
+ return React.useMemo(() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }), [scope, contexts]);
63
+ }, "useScope");
64
+ }, "createScope");
65
+ createScope.scopeName = scopeName;
66
+ return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
67
+ }
68
+ __name(createContextScope, "createContextScope");
69
+ function composeContextScopes(...scopes) {
70
+ const baseScope = scopes[0];
71
+ if (scopes.length === 1)
72
+ return baseScope;
73
+ const createScope = /* @__PURE__ */ __name(() => {
74
+ const scopeHooks = scopes.map((createScope2) => ({
75
+ useScope: createScope2(),
76
+ scopeName: createScope2.scopeName
77
+ }));
78
+ return /* @__PURE__ */ __name(function useComposedScopes(overrideScopes) {
79
+ const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
80
+ const scopeProps = useScope(overrideScopes);
81
+ const currentScope = scopeProps[`__scope${scopeName}`];
82
+ return { ...nextScopes2, ...currentScope };
83
+ }, {});
84
+ return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
85
+ }, "useComposedScopes");
86
+ }, "createScope");
87
+ createScope.scopeName = baseScope.scopeName;
88
+ return createScope;
89
+ }
90
+ __name(composeContextScopes, "composeContextScopes");
91
+ export {
92
+ createContext,
93
+ createContextScope
94
+ };
95
+ //# sourceMappingURL=create-context.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/create-context.tsx"],
4
+ "sourcesContent": ["// from radix\n// https://github.com/radix-ui/primitives/blob/main/packages/react/context/src/createContext.tsx\n\nimport * as React from 'react'\n\nexport type ScopedProps<P, K extends string> = P & { [Key in `__scope${K}`]?: Scope }\n\nexport function createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext?: ContextValueType\n) {\n const Context = React.createContext<ContextValueType | undefined>(defaultContext)\n\n function Provider(props: ContextValueType & { children: React.ReactNode }) {\n const { children, ...context } = props\n // Only re-memoize when prop values change\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const value = React.useMemo(() => context, Object.values(context)) as ContextValueType\n return <Context.Provider value={value}>{children}</Context.Provider>\n }\n\n function useContext(consumerName: string) {\n const context = React.useContext(Context)\n if (context) return context\n if (defaultContext !== undefined) return defaultContext\n // if a defaultContext wasn't specified, it's a required context.\n throw new Error(`\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``)\n }\n\n Provider.displayName = rootComponentName + 'Provider'\n return [Provider, useContext] as const\n}\n\n/* -------------------------------------------------------------------------------------------------\n * createContextScope\n * -----------------------------------------------------------------------------------------------*/\n\ntype 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(scopeName: string, createContextScopeDeps: CreateScope[] = []) {\n let defaultContexts: any[] = []\n\n /* -----------------------------------------------------------------------------------------------\n * createContext\n * ---------------------------------------------------------------------------------------------*/\n\n function createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext?: ContextValueType\n ) {\n const BaseContext = React.createContext<ContextValueType | undefined>(defaultContext)\n const index = defaultContexts.length\n defaultContexts = [...defaultContexts, defaultContext]\n\n function Provider(\n props: ContextValueType & { scope: Scope<ContextValueType>; children: React.ReactNode }\n ) {\n const { scope, children, ...context } = props\n const Context = scope?.[scopeName][index] || BaseContext\n // Only re-memoize when prop values change\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const value = React.useMemo(() => context, Object.values(context)) as ContextValueType\n return <Context.Provider value={value}>{children}</Context.Provider>\n }\n\n function useContext(consumerName: string, scope: Scope<ContextValueType | undefined>) {\n const Context = scope?.[scopeName][index] || BaseContext\n const context = React.useContext(Context)\n if (context) return context\n if (defaultContext !== undefined) return defaultContext\n // if a defaultContext wasn't specified, it's a required context.\n throw new Error(`\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``)\n }\n\n Provider.displayName = rootComponentName + 'Provider'\n return [Provider, useContext] as const\n }\n\n /* -----------------------------------------------------------------------------------------------\n * createScope\n * ---------------------------------------------------------------------------------------------*/\n\n const createScope: CreateScope = () => {\n const scopeContexts = defaultContexts.map((defaultContext) => {\n return React.createContext(defaultContext)\n })\n return function useScope(scope: Scope) {\n const contexts = scope?.[scopeName] || scopeContexts\n return React.useMemo(\n () => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),\n [scope, contexts]\n )\n }\n }\n\n createScope.scopeName = scopeName\n return [createContext, composeContextScopes(createScope, ...createContextScopeDeps)] as const\n}\n\n/* -------------------------------------------------------------------------------------------------\n * composeContextScopes\n * -----------------------------------------------------------------------------------------------*/\n\nfunction composeContextScopes(...scopes: CreateScope[]) {\n const baseScope = scopes[0]\n if (scopes.length === 1) return baseScope\n\n const createScope: CreateScope = () => {\n const scopeHooks = scopes.map((createScope) => ({\n useScope: createScope(),\n scopeName: createScope.scopeName,\n }))\n\n return function useComposedScopes(overrideScopes) {\n const nextScopes = scopeHooks.reduce((nextScopes, { useScope, scopeName }) => {\n // We are calling a hook inside a callback which React warns against to avoid inconsistent\n // renders, however, scoping doesn't have render side effects so we ignore the rule.\n // eslint-disable-next-line react-hooks/rules-of-hooks\n const scopeProps = useScope(overrideScopes)\n const currentScope = scopeProps[`__scope${scopeName}`]\n return { ...nextScopes, ...currentScope }\n }, {})\n\n return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes])\n }\n }\n\n createScope.scopeName = baseScope.scopeName\n return createScope\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n"],
5
+ "mappings": ";;AAGA;AAIO,uBACL,mBACA,gBACA;AACA,QAAM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CT,QAAM,cAA2B,6BAAM;AACrC,UAAM,gBAAgB,gBAAgB,IAAI,CAAC,mBAAmB;AAC5D,aAAO,MAAM,cAAc,cAAc;AAAA,IAC3C,CAAC;AACD,WAAO,yCAAkB,OAAc;AACrC,YAAM,WAAW,gCAAQ,eAAc;AACvC,aAAO,MAAM,QACX,MAAO,IAAG,UAAU,cAAc,KAAK,QAAQ,YAAY,SAAS,EAAE,IACtE,CAAC,OAAO,QAAQ,CAClB;AAAA,IACF,GANO;AAAA,EAOT,GAXiC;AAajC,cAAY,YAAY;AACxB,SAAO,CAAC,gBAAe,qBAAqB,aAAa,GAAG,sBAAsB,CAAC;AACrF;AA1DgB;AAgEhB,iCAAiC,QAAuB;AACtD,QAAM,YAAY,OAAO;AACzB,MAAI,OAAO,WAAW;AAAG,WAAO;AAEhC,QAAM,cAA2B,6BAAM;AACrC,UAAM,aAAa,OAAO,IAAI,CAAC,iBAAiB;AAAA,MAC9C,UAAU,aAAY;AAAA,MACtB,WAAW,aAAY;AAAA,IACzB,EAAE;AAEF,WAAO,kDAA2B,gBAAgB;AAChD,YAAM,aAAa,WAAW,OAAO,CAAC,aAAY,EAAE,UAAU,gBAAgB;AAI5E,cAAM,aAAa,SAAS,cAAc;AAC1C,cAAM,eAAe,WAAW,UAAU;AAC1C,eAAO,KAAK,gBAAe,aAAa;AAAA,MAC1C,GAAG,CAAC,CAAC;AAEL,aAAO,MAAM,QAAQ,MAAO,IAAG,UAAU,UAAU,cAAc,WAAW,IAAI,CAAC,UAAU,CAAC;AAAA,IAC9F,GAXO;AAAA,EAYT,GAlBiC;AAoBjC,cAAY,YAAY,UAAU;AAClC,SAAO;AACT;AA1BS;",
6
+ "names": []
7
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./create-context";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.ts"],
4
+ "sourcesContent": ["export * from './create-context'\n"],
5
+ "mappings": "AAAA;",
6
+ "names": []
7
+ }
@@ -0,0 +1,90 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ import * as React from "react";
4
+ function createContext(rootComponentName, defaultContext) {
5
+ const Context = React.createContext(defaultContext);
6
+ function Provider(props) {
7
+ const { children, ...context } = props;
8
+ const value = React.useMemo(() => context, Object.values(context));
9
+ return <Context.Provider value={value}>{children}</Context.Provider>;
10
+ }
11
+ __name(Provider, "Provider");
12
+ function useContext(consumerName) {
13
+ const context = React.useContext(Context);
14
+ if (context)
15
+ return context;
16
+ if (defaultContext !== void 0)
17
+ return defaultContext;
18
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
19
+ }
20
+ __name(useContext, "useContext");
21
+ Provider.displayName = rootComponentName + "Provider";
22
+ return [Provider, useContext];
23
+ }
24
+ __name(createContext, "createContext");
25
+ function createContextScope(scopeName, createContextScopeDeps = []) {
26
+ let defaultContexts = [];
27
+ function createContext2(rootComponentName, defaultContext) {
28
+ const BaseContext = React.createContext(defaultContext);
29
+ const index = defaultContexts.length;
30
+ defaultContexts = [...defaultContexts, defaultContext];
31
+ function Provider(props) {
32
+ const { scope, children, ...context } = props;
33
+ const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
34
+ const value = React.useMemo(() => context, Object.values(context));
35
+ return <Context.Provider value={value}>{children}</Context.Provider>;
36
+ }
37
+ __name(Provider, "Provider");
38
+ function useContext(consumerName, scope) {
39
+ const Context = (scope == null ? void 0 : scope[scopeName][index]) || BaseContext;
40
+ const context = React.useContext(Context);
41
+ if (context)
42
+ return context;
43
+ if (defaultContext !== void 0)
44
+ return defaultContext;
45
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
46
+ }
47
+ __name(useContext, "useContext");
48
+ Provider.displayName = rootComponentName + "Provider";
49
+ return [Provider, useContext];
50
+ }
51
+ __name(createContext2, "createContext");
52
+ const createScope = /* @__PURE__ */ __name(() => {
53
+ const scopeContexts = defaultContexts.map((defaultContext) => {
54
+ return React.createContext(defaultContext);
55
+ });
56
+ return /* @__PURE__ */ __name(function useScope(scope) {
57
+ const contexts = (scope == null ? void 0 : scope[scopeName]) || scopeContexts;
58
+ return React.useMemo(() => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }), [scope, contexts]);
59
+ }, "useScope");
60
+ }, "createScope");
61
+ createScope.scopeName = scopeName;
62
+ return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
63
+ }
64
+ __name(createContextScope, "createContextScope");
65
+ function composeContextScopes(...scopes) {
66
+ const baseScope = scopes[0];
67
+ if (scopes.length === 1)
68
+ return baseScope;
69
+ const createScope = /* @__PURE__ */ __name(() => {
70
+ const scopeHooks = scopes.map((createScope2) => ({
71
+ useScope: createScope2(),
72
+ scopeName: createScope2.scopeName
73
+ }));
74
+ return /* @__PURE__ */ __name(function useComposedScopes(overrideScopes) {
75
+ const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
76
+ const scopeProps = useScope(overrideScopes);
77
+ const currentScope = scopeProps[`__scope${scopeName}`];
78
+ return { ...nextScopes2, ...currentScope };
79
+ }, {});
80
+ return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
81
+ }, "useComposedScopes");
82
+ }, "createScope");
83
+ createScope.scopeName = baseScope.scopeName;
84
+ return createScope;
85
+ }
86
+ __name(composeContextScopes, "composeContextScopes");
87
+ export {
88
+ createContext,
89
+ createContextScope
90
+ };
@@ -0,0 +1 @@
1
+ export * from "./create-context";
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@tamagui/create-context",
3
+ "version": "1.0.0-beta.0",
4
+ "sideEffects": false,
5
+ "source": "src/index.ts",
6
+ "typings": "types",
7
+ "main": "dist/cjs",
8
+ "module": "dist/esm",
9
+ "module:jsx": "dist/jsx",
10
+ "files": [
11
+ "types",
12
+ "dist"
13
+ ],
14
+ "scripts": {
15
+ "build": "tamagui-build",
16
+ "watch": "tamagui-build --watch"
17
+ },
18
+ "peerDependencies": {
19
+ "react": "*"
20
+ },
21
+ "devDependencies": {
22
+ "@tamagui/build": "^1.0.0-beta.0",
23
+ "react": "*"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "gitHead": "a49cc7ea6b93ba384e77a4880ae48ac4a5635c14"
29
+ }
@@ -0,0 +1,29 @@
1
+ import * as React from 'react';
2
+ export declare type ScopedProps<P, K extends string> = P & {
3
+ [Key in `__scope${K}`]?: Scope;
4
+ };
5
+ export declare function createContext<ContextValueType extends object | null>(rootComponentName: string, defaultContext?: ContextValueType): readonly [{
6
+ (props: ContextValueType & {
7
+ children: React.ReactNode;
8
+ }): JSX.Element;
9
+ displayName: string;
10
+ }, (consumerName: string) => ContextValueType];
11
+ declare type ScopeHook = (scope: Scope) => {
12
+ [__scopeProp: string]: Scope;
13
+ };
14
+ export declare type Scope<C = any> = {
15
+ [scopeName: string]: React.Context<C>[];
16
+ } | undefined;
17
+ export interface CreateScope {
18
+ scopeName: string;
19
+ (): ScopeHook;
20
+ }
21
+ export declare function createContextScope(scopeName: string, createContextScopeDeps?: CreateScope[]): readonly [<ContextValueType extends object | null>(rootComponentName: string, defaultContext?: ContextValueType | undefined) => readonly [{
22
+ (props: ContextValueType & {
23
+ scope: Scope<ContextValueType>;
24
+ children: React.ReactNode;
25
+ }): JSX.Element;
26
+ displayName: string;
27
+ }, (consumerName: string, scope: Scope<ContextValueType | undefined>) => ContextValueType], CreateScope];
28
+ export {};
29
+ //# sourceMappingURL=create-context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-context.d.ts","sourceRoot":"","sources":["../src/create-context.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,oBAAY,WAAW,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,IAAI,CAAC,GAAG;KAAG,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC,EAAE,KAAK;CAAE,CAAA;AAErF,wBAAgB,aAAa,CAAC,gBAAgB,SAAS,MAAM,GAAG,IAAI,EAClE,iBAAiB,EAAE,MAAM,EACzB,cAAc,CAAC,EAAE,gBAAgB;YAIR,gBAAgB,GAAG;QAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;KAAE;;kBAQvC,MAAM,uBAUzC;AAMD,aAAK,SAAS,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK;IAAE,CAAC,WAAW,EAAE,MAAM,GAAG,KAAK,CAAA;CAAE,CAAA;AAEnE,oBAAY,KAAK,CAAC,CAAC,GAAG,GAAG,IAAI;IAAE,CAAC,SAAS,EAAE,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAA;CAAE,GAAG,SAAS,CAAA;AAEpF,MAAM,WAAW,WAAW;IAC1B,SAAS,EAAE,MAAM,CAAA;IACjB,IAAI,SAAS,CAAA;CACd;AAED,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,MAAM,EAAE,sBAAsB,GAAE,WAAW,EAAO,yEAQzE,MAAM;;;kBAQ+C,MAAM,SAAS;;;kBAUrD,MAAM,iFAgC3C"}
@@ -0,0 +1,2 @@
1
+ export * from './create-context';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA"}