@tamagui/create-context
Advanced tools
Comparing version
import { jsx as _jsx } from "react/jsx-runtime"; | ||
import * as React from "react"; | ||
function createContext(rootComponentName, defaultContext) { | ||
var Context = /* @__PURE__ */ React.createContext(defaultContext); | ||
var Context = /* @__PURE__ */React.createContext(defaultContext); | ||
function Provider(props) { | ||
var { children, ...context } = props, value = React.useMemo(function() { | ||
return context; | ||
}, Object.values(context)); | ||
return /* @__PURE__ */ _jsx(Context.Provider, { | ||
var { | ||
children, | ||
...context | ||
} = props, | ||
value = React.useMemo(function () { | ||
return context; | ||
}, Object.values(context)); | ||
return /* @__PURE__ */_jsx(Context.Provider, { | ||
value, | ||
@@ -20,20 +24,23 @@ children | ||
} | ||
return Provider.displayName = `${rootComponentName}Provider`, [ | ||
Provider, | ||
useContext | ||
]; | ||
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext]; | ||
} | ||
function createContextScope(scopeName) { | ||
var createContextScopeDeps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [], defaultContexts = []; | ||
var createContextScopeDeps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [], | ||
defaultContexts = []; | ||
function createContext2(rootComponentName, defaultContext) { | ||
var BaseContext = /* @__PURE__ */ React.createContext(defaultContext), index = defaultContexts.length; | ||
defaultContexts = [ | ||
...defaultContexts, | ||
defaultContext | ||
]; | ||
var BaseContext = /* @__PURE__ */React.createContext(defaultContext), | ||
index = defaultContexts.length; | ||
defaultContexts = [...defaultContexts, defaultContext]; | ||
function Provider(props) { | ||
var _scope_scopeName, { scope, children, ...context } = props, Context = (scope == null || (_scope_scopeName = scope[scopeName]) === null || _scope_scopeName === void 0 ? void 0 : _scope_scopeName[index]) || BaseContext, value = React.useMemo(function() { | ||
return context; | ||
}, Object.values(context)); | ||
return /* @__PURE__ */ _jsx(Context.Provider, { | ||
var _scope_scopeName, | ||
{ | ||
scope, | ||
children, | ||
...context | ||
} = props, | ||
Context = (scope == null || (_scope_scopeName = scope[scopeName]) === null || _scope_scopeName === void 0 ? void 0 : _scope_scopeName[index]) || BaseContext, | ||
value = React.useMemo(function () { | ||
return context; | ||
}, Object.values(context)); | ||
return /* @__PURE__ */_jsx(Context.Provider, { | ||
value, | ||
@@ -44,22 +51,20 @@ children | ||
function useContext(consumerName, scope, options) { | ||
var _scope_scopeName, Context = (scope == null || (_scope_scopeName = scope[scopeName]) === null || _scope_scopeName === void 0 ? void 0 : _scope_scopeName[index]) || BaseContext, context = React.useContext(Context); | ||
var _scope_scopeName, | ||
Context = (scope == null || (_scope_scopeName = scope[scopeName]) === null || _scope_scopeName === void 0 ? void 0 : _scope_scopeName[index]) || BaseContext, | ||
context = React.useContext(Context); | ||
if (context) return context; | ||
if (defaultContext !== void 0) return defaultContext; | ||
var missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\``; | ||
if (options?.fallback) | ||
return options?.warn !== !1 && console.warn(missingContextMessage), options.fallback; | ||
if (options?.fallback) return options?.warn !== !1 && console.warn(missingContextMessage), options.fallback; | ||
throw new Error(missingContextMessage); | ||
} | ||
return Provider.displayName = `${rootComponentName}Provider`, [ | ||
Provider, | ||
useContext | ||
]; | ||
return Provider.displayName = `${rootComponentName}Provider`, [Provider, useContext]; | ||
} | ||
var createScope = function() { | ||
var scopeContexts = defaultContexts.map(function(defaultContext) { | ||
return /* @__PURE__ */ React.createContext(defaultContext); | ||
var createScope = function () { | ||
var scopeContexts = defaultContexts.map(function (defaultContext) { | ||
return /* @__PURE__ */React.createContext(defaultContext); | ||
}); | ||
return function(scope) { | ||
return function (scope) { | ||
var contexts = scope?.[scopeName] || scopeContexts; | ||
return React.useMemo(function() { | ||
return React.useMemo(function () { | ||
return { | ||
@@ -71,20 +76,13 @@ [`__scope${scopeName}`]: { | ||
}; | ||
}, [ | ||
scope, | ||
contexts | ||
]); | ||
}, [scope, contexts]); | ||
}; | ||
}; | ||
return createScope.scopeName = scopeName, [ | ||
createContext2, | ||
composeContextScopes(createScope, ...createContextScopeDeps) | ||
]; | ||
return createScope.scopeName = scopeName, [createContext2, composeContextScopes(createScope, ...createContextScopeDeps)]; | ||
} | ||
function composeContextScopes() { | ||
for (var _len = arguments.length, scopes = new Array(_len), _key = 0; _key < _len; _key++) | ||
scopes[_key] = arguments[_key]; | ||
for (var _len = arguments.length, scopes = new Array(_len), _key = 0; _key < _len; _key++) scopes[_key] = arguments[_key]; | ||
var baseScope = scopes[0]; | ||
if (scopes.length === 1) return baseScope; | ||
var createScope = function() { | ||
var scopeHooks = scopes.map(function(createScope2) { | ||
var createScope = function () { | ||
var scopeHooks = scopes.map(function (createScope2) { | ||
return { | ||
@@ -95,5 +93,10 @@ useScope: createScope2(), | ||
}); | ||
return function(overrideScopes) { | ||
var nextScopes = scopeHooks.reduce(function(nextScopes2, param) { | ||
var { useScope, scopeName } = param, scopeProps = useScope(overrideScopes), currentScope = scopeProps[`__scope${scopeName}`]; | ||
return function (overrideScopes) { | ||
var nextScopes = scopeHooks.reduce(function (nextScopes2, param) { | ||
var { | ||
useScope, | ||
scopeName | ||
} = param, | ||
scopeProps = useScope(overrideScopes), | ||
currentScope = scopeProps[`__scope${scopeName}`]; | ||
return { | ||
@@ -104,9 +107,7 @@ ...nextScopes2, | ||
}, {}); | ||
return React.useMemo(function() { | ||
return React.useMemo(function () { | ||
return { | ||
[`__scope${baseScope.scopeName}`]: nextScopes | ||
}; | ||
}, [ | ||
nextScopes | ||
]); | ||
}, [nextScopes]); | ||
}; | ||
@@ -116,6 +117,3 @@ }; | ||
} | ||
export { | ||
createContext, | ||
createContextScope | ||
}; | ||
//# sourceMappingURL=create-context.js.map | ||
export { createContext, createContextScope }; | ||
//# sourceMappingURL=create-context.native.js.map |
@@ -1,2 +0,2 @@ | ||
export * from "./create-context"; | ||
//# sourceMappingURL=index.js.map | ||
export * from "./create-context.native.js"; | ||
//# sourceMappingURL=index.native.js.map |
{ | ||
"name": "@tamagui/create-context", | ||
"version": "1.111.8", | ||
"version": "1.111.9", | ||
"sideEffects": false, | ||
@@ -26,3 +26,3 @@ "source": "src/index.ts", | ||
".": { | ||
"react-native-import": "./dist/esm/index.native.mjs", | ||
"react-native-import": "./dist/esm/index.native.js", | ||
"react-native": "./dist/cjs/index.native.js", | ||
@@ -35,3 +35,3 @@ "types": "./types/index.d.ts", | ||
"devDependencies": { | ||
"@tamagui/build": "1.111.8", | ||
"@tamagui/build": "1.111.9", | ||
"react": "^18.2.0 || ^19.0.0" | ||
@@ -38,0 +38,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
71503
-9.71%40
-9.09%1045
-10.07%