@tamagui/create-context 2.7.7 → 3.0.0-beta.643.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+
1
2
  var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -5,138 +6,117 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
5
6
  var __getProtoOf = Object.getPrototypeOf;
6
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
8
  var __export = (target, all) => {
8
- for (var name in all) __defProp(target, name, {
9
- get: all[name],
10
- enumerable: true
11
- });
9
+ for (var name in all) __defProp(target, name, {
10
+ get: all[name],
11
+ enumerable: true
12
+ });
12
13
  };
13
14
  var __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from === "object" || typeof from === "function") {
15
- for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
16
- get: () => from[key],
17
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
18
- });
19
- }
20
- return to;
15
+ if (from && typeof from === "object" || typeof from === "function") {
16
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
17
+ get: () => from[key],
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ }
21
+ return to;
21
22
  };
22
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
23
- // If the importer is in node compatibility mode or this is not an ESM
24
- // file that has been converted to a CommonJS file using a Babel-
25
- // compatible transform (i.e. "__esModule" has not been set), then set
26
- // "default" to the CommonJS "module.exports" for node compatibility.
27
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
28
- value: mod,
29
- enumerable: true
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
24
+ value: mod,
25
+ enumerable: true
30
26
  }) : target, mod));
31
- var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
32
- value: true
33
- }), mod);
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
34
28
  var create_context_exports = {};
35
29
  __export(create_context_exports, {
36
- createContext: () => createContext,
37
- createContextScope: () => createContextScope
30
+ createContext: () => createContext,
31
+ createContextScope: () => createContextScope
38
32
  });
39
33
  module.exports = __toCommonJS(create_context_exports);
40
34
  var React = __toESM(require("react"), 1);
41
35
  var import_jsx_runtime = require("react/jsx-runtime");
42
36
  function createContext(rootComponentName, defaultContext) {
43
- const Context = React.createContext(defaultContext);
44
- function Provider(props) {
45
- const {
46
- children,
47
- ...context
48
- } = props;
49
- const value = React.useMemo(() => context, Object.values(context));
50
- return /* @__PURE__ */(0, import_jsx_runtime.jsx)(Context.Provider, {
51
- value,
52
- children
53
- });
54
- }
55
- function useContext(consumerName) {
56
- const context = React.useContext(Context);
57
- if (context) return context;
58
- if (defaultContext !== void 0) return defaultContext;
59
- throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
60
- }
61
- return [Provider, useContext];
37
+ const Context = React.createContext(defaultContext);
38
+ function Provider(props) {
39
+ const { children, ...context } = props;
40
+ const value = React.useMemo(() => context, Object.values(context));
41
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, {
42
+ value,
43
+ children
44
+ });
45
+ }
46
+ function useContext(consumerName) {
47
+ const context = React.useContext(Context);
48
+ if (context) return context;
49
+ if (defaultContext !== void 0) return defaultContext;
50
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
51
+ }
52
+ return [Provider, useContext];
62
53
  }
63
54
  function createContextScope(scopeName, createContextScopeDeps = []) {
64
- let defaultContexts = [];
65
- function createContext2(rootComponentName, defaultContext) {
66
- const BaseContext = React.createContext(defaultContext);
67
- const index = defaultContexts.length;
68
- defaultContexts = [...defaultContexts, defaultContext];
69
- function Provider(props) {
70
- const {
71
- scope,
72
- children,
73
- ...context
74
- } = props;
75
- const Context = scope?.[scopeName]?.[index] || BaseContext;
76
- const value = React.useMemo(() => context, Object.values(context));
77
- return /* @__PURE__ */(0, import_jsx_runtime.jsx)(Context.Provider, {
78
- value,
79
- children
80
- });
81
- }
82
- function useContext(consumerName, scope, options) {
83
- const Context = scope?.[scopeName]?.[index] || BaseContext;
84
- const context = React.useContext(Context);
85
- if (context) return context;
86
- if (defaultContext !== void 0) return defaultContext;
87
- const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
88
- if (options?.fallback) {
89
- if (options?.warn !== false) {
90
- console.warn(missingContextMessage);
91
- }
92
- return options.fallback;
93
- }
94
- throw new Error(missingContextMessage);
95
- }
96
- return [Provider, useContext];
97
- }
98
- const createScope = () => {
99
- const scopeContexts = defaultContexts.map(defaultContext => {
100
- return React.createContext(defaultContext);
101
- });
102
- return function useScope(scope) {
103
- const contexts = scope?.[scopeName] || scopeContexts;
104
- return React.useMemo(() => ({
105
- [`__scope${scopeName}`]: {
106
- ...scope,
107
- [scopeName]: contexts
108
- }
109
- }), [scope, contexts]);
110
- };
111
- };
112
- createScope.scopeName = scopeName;
113
- return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
55
+ let defaultContexts = [];
56
+ function createContext2(rootComponentName, defaultContext) {
57
+ const BaseContext = React.createContext(defaultContext);
58
+ const index = defaultContexts.length;
59
+ defaultContexts = [...defaultContexts, defaultContext];
60
+ function Provider(props) {
61
+ const { scope, children, ...context } = props;
62
+ const Context = scope?.[scopeName]?.[index] || BaseContext;
63
+ const value = React.useMemo(() => context, Object.values(context));
64
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, {
65
+ value,
66
+ children
67
+ });
68
+ }
69
+ function useContext(consumerName, scope, options) {
70
+ const Context = scope?.[scopeName]?.[index] || BaseContext;
71
+ const context = React.useContext(Context);
72
+ if (context) return context;
73
+ if (defaultContext !== void 0) return defaultContext;
74
+ const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
75
+ if (options?.fallback) {
76
+ if (options?.warn !== false) {
77
+ console.warn(missingContextMessage);
78
+ }
79
+ return options.fallback;
80
+ }
81
+ throw new Error(missingContextMessage);
82
+ }
83
+ return [Provider, useContext];
84
+ }
85
+ const createScope = () => {
86
+ const scopeContexts = defaultContexts.map((defaultContext) => {
87
+ return React.createContext(defaultContext);
88
+ });
89
+ return function useScope(scope) {
90
+ const contexts = scope?.[scopeName] || scopeContexts;
91
+ return React.useMemo(() => ({ [`__scope${scopeName}`]: {
92
+ ...scope,
93
+ [scopeName]: contexts
94
+ } }), [scope, contexts]);
95
+ };
96
+ };
97
+ createScope.scopeName = scopeName;
98
+ return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
114
99
  }
115
100
  function composeContextScopes(...scopes) {
116
- const baseScope = scopes[0];
117
- if (scopes.length === 1) return baseScope;
118
- const createScope = () => {
119
- const scopeHooks = scopes.map(createScope2 => ({
120
- useScope: createScope2(),
121
- scopeName: createScope2.scopeName
122
- }));
123
- return function useComposedScopes(overrideScopes) {
124
- const nextScopes = scopeHooks.reduce((nextScopes2, {
125
- useScope,
126
- scopeName
127
- }) => {
128
- const scopeProps = useScope(overrideScopes);
129
- const currentScope = scopeProps[`__scope${scopeName}`];
130
- return {
131
- ...nextScopes2,
132
- ...currentScope
133
- };
134
- }, {});
135
- return React.useMemo(() => ({
136
- [`__scope${baseScope.scopeName}`]: nextScopes
137
- }), [nextScopes]);
138
- };
139
- };
140
- createScope.scopeName = baseScope.scopeName;
141
- return createScope;
142
- }
101
+ const baseScope = scopes[0];
102
+ if (scopes.length === 1) return baseScope;
103
+ const createScope = () => {
104
+ const scopeHooks = scopes.map((createScope2) => ({
105
+ useScope: createScope2(),
106
+ scopeName: createScope2.scopeName
107
+ }));
108
+ return function useComposedScopes(overrideScopes) {
109
+ const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {
110
+ const scopeProps = useScope(overrideScopes);
111
+ const currentScope = scopeProps[`__scope${scopeName}`];
112
+ return {
113
+ ...nextScopes2,
114
+ ...currentScope
115
+ };
116
+ }, {});
117
+ return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);
118
+ };
119
+ };
120
+ createScope.scopeName = baseScope.scopeName;
121
+ return createScope;
122
+ }
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+
3
+
4
+ var __create = Object.create;
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __export = (target, all) => {
11
+ for (var name in all) __defProp(target, name, {
12
+ get: all[name],
13
+ enumerable: true
14
+ });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
19
+ get: () => from[key],
20
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
21
+ });
22
+ }
23
+ return to;
24
+ };
25
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
+ value: mod,
27
+ enumerable: true
28
+ }) : target, mod));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+ var create_context_exports = {};
31
+ __export(create_context_exports, {
32
+ createContext: () => createContext,
33
+ createContextScope: () => createContextScope
34
+ });
35
+ module.exports = __toCommonJS(create_context_exports);
36
+ var import_jsx_runtime = require("react/jsx-runtime");
37
+ var React = __toESM(require("react"), 1);
38
+ function createContext(rootComponentName, defaultContext) {
39
+ var Context = /* @__PURE__ */ React.createContext(defaultContext);
40
+ function Provider(props) {
41
+ var { children, ...context } = props;
42
+ var value = React.useMemo(function() {
43
+ return context;
44
+ }, Object.values(context));
45
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, {
46
+ value,
47
+ children
48
+ });
49
+ }
50
+ function useContext(consumerName) {
51
+ var context = React.useContext(Context);
52
+ if (context) return context;
53
+ if (defaultContext !== void 0) return defaultContext;
54
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
55
+ }
56
+ return [Provider, useContext];
57
+ }
58
+ function createContextScope(scopeName) {
59
+ var createContextScopeDeps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
60
+ var defaultContexts = [];
61
+ function createContext2(rootComponentName, defaultContext) {
62
+ var BaseContext = /* @__PURE__ */ React.createContext(defaultContext);
63
+ var index = defaultContexts.length;
64
+ defaultContexts = [...defaultContexts, defaultContext];
65
+ function Provider(props) {
66
+ var _scope_scopeName;
67
+ var { scope, children, ...context } = props;
68
+ var Context = (scope === null || scope === void 0 ? void 0 : (_scope_scopeName = scope[scopeName]) === null || _scope_scopeName === void 0 ? void 0 : _scope_scopeName[index]) || BaseContext;
69
+ var value = React.useMemo(function() {
70
+ return context;
71
+ }, Object.values(context));
72
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, {
73
+ value,
74
+ children
75
+ });
76
+ }
77
+ function useContext(consumerName, scope, options) {
78
+ var _scope_scopeName;
79
+ var Context = (scope === null || scope === void 0 ? void 0 : (_scope_scopeName = scope[scopeName]) === null || _scope_scopeName === void 0 ? void 0 : _scope_scopeName[index]) || BaseContext;
80
+ var context = React.useContext(Context);
81
+ if (context) return context;
82
+ if (defaultContext !== void 0) return defaultContext;
83
+ var missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``;
84
+ if (options === null || options === void 0 ? void 0 : options.fallback) {
85
+ if ((options === null || options === void 0 ? void 0 : options.warn) !== false) {
86
+ console.warn(missingContextMessage);
87
+ }
88
+ return options.fallback;
89
+ }
90
+ throw new Error(missingContextMessage);
91
+ }
92
+ return [Provider, useContext];
93
+ }
94
+ var createScope = function() {
95
+ var scopeContexts = defaultContexts.map(function(defaultContext) {
96
+ return /* @__PURE__ */ React.createContext(defaultContext);
97
+ });
98
+ return function useScope(scope) {
99
+ var contexts = (scope === null || scope === void 0 ? void 0 : scope[scopeName]) || scopeContexts;
100
+ return React.useMemo(function() {
101
+ return { [`__scope${scopeName}`]: {
102
+ ...scope,
103
+ [scopeName]: contexts
104
+ } };
105
+ }, [scope, contexts]);
106
+ };
107
+ };
108
+ createScope.scopeName = scopeName;
109
+ return [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)];
110
+ }
111
+ function composeContextScopes() {
112
+ for (var _len = arguments.length, scopes = new Array(_len), _key = 0; _key < _len; _key++) {
113
+ scopes[_key] = arguments[_key];
114
+ }
115
+ var baseScope = scopes[0];
116
+ if (scopes.length === 1) return baseScope;
117
+ var createScope = function() {
118
+ var scopeHooks = scopes.map(function(createScope2) {
119
+ return {
120
+ useScope: createScope2(),
121
+ scopeName: createScope2.scopeName
122
+ };
123
+ });
124
+ return function useComposedScopes(overrideScopes) {
125
+ var nextScopes = scopeHooks.reduce(function(nextScopes2, param) {
126
+ var { useScope, scopeName } = param;
127
+ var scopeProps = useScope(overrideScopes);
128
+ var currentScope = scopeProps[`__scope${scopeName}`];
129
+ return {
130
+ ...nextScopes2,
131
+ ...currentScope
132
+ };
133
+ }, {});
134
+ return React.useMemo(function() {
135
+ return { [`__scope${baseScope.scopeName}`]: nextScopes };
136
+ }, [nextScopes]);
137
+ };
138
+ };
139
+ createScope.scopeName = baseScope.scopeName;
140
+ return createScope;
141
+ }