129 lines
6.0 KiB
JavaScript
129 lines
6.0 KiB
JavaScript
import {createContext as $3bkAK$createContext, useMemo as $3bkAK$useMemo, createElement as $3bkAK$createElement, useContext as $3bkAK$useContext} from "react";
|
|
|
|
|
|
function $c512c27ab02ef895$export$fd42f52fd3ae1109(rootComponentName, defaultContext) {
|
|
const Context = /*#__PURE__*/ $3bkAK$createContext(defaultContext);
|
|
function Provider(props) {
|
|
const { children: children , ...context } = props; // Only re-memoize when prop values change
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
const value = $3bkAK$useMemo(()=>context
|
|
, Object.values(context));
|
|
return /*#__PURE__*/ $3bkAK$createElement(Context.Provider, {
|
|
value: value
|
|
}, children);
|
|
}
|
|
function useContext(consumerName) {
|
|
const context = $3bkAK$useContext(Context);
|
|
if (context) return context;
|
|
if (defaultContext !== undefined) return defaultContext; // if a defaultContext wasn't specified, it's a required context.
|
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
}
|
|
Provider.displayName = rootComponentName + 'Provider';
|
|
return [
|
|
Provider,
|
|
useContext
|
|
];
|
|
}
|
|
/* -------------------------------------------------------------------------------------------------
|
|
* createContextScope
|
|
* -----------------------------------------------------------------------------------------------*/ function $c512c27ab02ef895$export$50c7b4e9d9f19c1(scopeName, createContextScopeDeps = []) {
|
|
let defaultContexts = [];
|
|
/* -----------------------------------------------------------------------------------------------
|
|
* createContext
|
|
* ---------------------------------------------------------------------------------------------*/ function $c512c27ab02ef895$export$fd42f52fd3ae1109(rootComponentName, defaultContext) {
|
|
const BaseContext = /*#__PURE__*/ $3bkAK$createContext(defaultContext);
|
|
const index = defaultContexts.length;
|
|
defaultContexts = [
|
|
...defaultContexts,
|
|
defaultContext
|
|
];
|
|
function Provider(props) {
|
|
const { scope: scope , children: children , ...context } = props;
|
|
const Context = (scope === null || scope === void 0 ? void 0 : scope[scopeName][index]) || BaseContext; // Only re-memoize when prop values change
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
const value = $3bkAK$useMemo(()=>context
|
|
, Object.values(context));
|
|
return /*#__PURE__*/ $3bkAK$createElement(Context.Provider, {
|
|
value: value
|
|
}, children);
|
|
}
|
|
function useContext(consumerName, scope) {
|
|
const Context = (scope === null || scope === void 0 ? void 0 : scope[scopeName][index]) || BaseContext;
|
|
const context = $3bkAK$useContext(Context);
|
|
if (context) return context;
|
|
if (defaultContext !== undefined) return defaultContext; // if a defaultContext wasn't specified, it's a required context.
|
|
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
|
|
}
|
|
Provider.displayName = rootComponentName + 'Provider';
|
|
return [
|
|
Provider,
|
|
useContext
|
|
];
|
|
}
|
|
/* -----------------------------------------------------------------------------------------------
|
|
* createScope
|
|
* ---------------------------------------------------------------------------------------------*/ const createScope = ()=>{
|
|
const scopeContexts = defaultContexts.map((defaultContext)=>{
|
|
return /*#__PURE__*/ $3bkAK$createContext(defaultContext);
|
|
});
|
|
return function useScope(scope) {
|
|
const contexts = (scope === null || scope === void 0 ? void 0 : scope[scopeName]) || scopeContexts;
|
|
return $3bkAK$useMemo(()=>({
|
|
[`__scope${scopeName}`]: {
|
|
...scope,
|
|
[scopeName]: contexts
|
|
}
|
|
})
|
|
, [
|
|
scope,
|
|
contexts
|
|
]);
|
|
};
|
|
};
|
|
createScope.scopeName = scopeName;
|
|
return [
|
|
$c512c27ab02ef895$export$fd42f52fd3ae1109,
|
|
$c512c27ab02ef895$var$composeContextScopes(createScope, ...createContextScopeDeps)
|
|
];
|
|
}
|
|
/* -------------------------------------------------------------------------------------------------
|
|
* composeContextScopes
|
|
* -----------------------------------------------------------------------------------------------*/ function $c512c27ab02ef895$var$composeContextScopes(...scopes) {
|
|
const baseScope = scopes[0];
|
|
if (scopes.length === 1) return baseScope;
|
|
const createScope1 = ()=>{
|
|
const scopeHooks = scopes.map((createScope)=>({
|
|
useScope: createScope(),
|
|
scopeName: createScope.scopeName
|
|
})
|
|
);
|
|
return function useComposedScopes(overrideScopes) {
|
|
const nextScopes1 = scopeHooks.reduce((nextScopes, { useScope: useScope , scopeName: scopeName })=>{
|
|
// We are calling a hook inside a callback which React warns against to avoid inconsistent
|
|
// renders, however, scoping doesn't have render side effects so we ignore the rule.
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
const scopeProps = useScope(overrideScopes);
|
|
const currentScope = scopeProps[`__scope${scopeName}`];
|
|
return {
|
|
...nextScopes,
|
|
...currentScope
|
|
};
|
|
}, {});
|
|
return $3bkAK$useMemo(()=>({
|
|
[`__scope${baseScope.scopeName}`]: nextScopes1
|
|
})
|
|
, [
|
|
nextScopes1
|
|
]);
|
|
};
|
|
};
|
|
createScope1.scopeName = baseScope.scopeName;
|
|
return createScope1;
|
|
}
|
|
|
|
|
|
|
|
|
|
export {$c512c27ab02ef895$export$fd42f52fd3ae1109 as createContext, $c512c27ab02ef895$export$50c7b4e9d9f19c1 as createContextScope};
|
|
//# sourceMappingURL=index.mjs.map
|