index.mjs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import {createContext as $3bkAK$createContext, useMemo as $3bkAK$useMemo, createElement as $3bkAK$createElement, useContext as $3bkAK$useContext} from "react";
  2. function $c512c27ab02ef895$export$fd42f52fd3ae1109(rootComponentName, defaultContext) {
  3. const Context = /*#__PURE__*/ $3bkAK$createContext(defaultContext);
  4. function Provider(props) {
  5. const { children: children , ...context } = props; // Only re-memoize when prop values change
  6. // eslint-disable-next-line react-hooks/exhaustive-deps
  7. const value = $3bkAK$useMemo(()=>context
  8. , Object.values(context));
  9. return /*#__PURE__*/ $3bkAK$createElement(Context.Provider, {
  10. value: value
  11. }, children);
  12. }
  13. function useContext(consumerName) {
  14. const context = $3bkAK$useContext(Context);
  15. if (context) return context;
  16. if (defaultContext !== undefined) return defaultContext; // if a defaultContext wasn't specified, it's a required context.
  17. throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
  18. }
  19. Provider.displayName = rootComponentName + 'Provider';
  20. return [
  21. Provider,
  22. useContext
  23. ];
  24. }
  25. /* -------------------------------------------------------------------------------------------------
  26. * createContextScope
  27. * -----------------------------------------------------------------------------------------------*/ function $c512c27ab02ef895$export$50c7b4e9d9f19c1(scopeName, createContextScopeDeps = []) {
  28. let defaultContexts = [];
  29. /* -----------------------------------------------------------------------------------------------
  30. * createContext
  31. * ---------------------------------------------------------------------------------------------*/ function $c512c27ab02ef895$export$fd42f52fd3ae1109(rootComponentName, defaultContext) {
  32. const BaseContext = /*#__PURE__*/ $3bkAK$createContext(defaultContext);
  33. const index = defaultContexts.length;
  34. defaultContexts = [
  35. ...defaultContexts,
  36. defaultContext
  37. ];
  38. function Provider(props) {
  39. const { scope: scope , children: children , ...context } = props;
  40. const Context = (scope === null || scope === void 0 ? void 0 : scope[scopeName][index]) || BaseContext; // Only re-memoize when prop values change
  41. // eslint-disable-next-line react-hooks/exhaustive-deps
  42. const value = $3bkAK$useMemo(()=>context
  43. , Object.values(context));
  44. return /*#__PURE__*/ $3bkAK$createElement(Context.Provider, {
  45. value: value
  46. }, children);
  47. }
  48. function useContext(consumerName, scope) {
  49. const Context = (scope === null || scope === void 0 ? void 0 : scope[scopeName][index]) || BaseContext;
  50. const context = $3bkAK$useContext(Context);
  51. if (context) return context;
  52. if (defaultContext !== undefined) return defaultContext; // if a defaultContext wasn't specified, it's a required context.
  53. throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``);
  54. }
  55. Provider.displayName = rootComponentName + 'Provider';
  56. return [
  57. Provider,
  58. useContext
  59. ];
  60. }
  61. /* -----------------------------------------------------------------------------------------------
  62. * createScope
  63. * ---------------------------------------------------------------------------------------------*/ const createScope = ()=>{
  64. const scopeContexts = defaultContexts.map((defaultContext)=>{
  65. return /*#__PURE__*/ $3bkAK$createContext(defaultContext);
  66. });
  67. return function useScope(scope) {
  68. const contexts = (scope === null || scope === void 0 ? void 0 : scope[scopeName]) || scopeContexts;
  69. return $3bkAK$useMemo(()=>({
  70. [`__scope${scopeName}`]: {
  71. ...scope,
  72. [scopeName]: contexts
  73. }
  74. })
  75. , [
  76. scope,
  77. contexts
  78. ]);
  79. };
  80. };
  81. createScope.scopeName = scopeName;
  82. return [
  83. $c512c27ab02ef895$export$fd42f52fd3ae1109,
  84. $c512c27ab02ef895$var$composeContextScopes(createScope, ...createContextScopeDeps)
  85. ];
  86. }
  87. /* -------------------------------------------------------------------------------------------------
  88. * composeContextScopes
  89. * -----------------------------------------------------------------------------------------------*/ function $c512c27ab02ef895$var$composeContextScopes(...scopes) {
  90. const baseScope = scopes[0];
  91. if (scopes.length === 1) return baseScope;
  92. const createScope1 = ()=>{
  93. const scopeHooks = scopes.map((createScope)=>({
  94. useScope: createScope(),
  95. scopeName: createScope.scopeName
  96. })
  97. );
  98. return function useComposedScopes(overrideScopes) {
  99. const nextScopes1 = scopeHooks.reduce((nextScopes, { useScope: useScope , scopeName: scopeName })=>{
  100. // We are calling a hook inside a callback which React warns against to avoid inconsistent
  101. // renders, however, scoping doesn't have render side effects so we ignore the rule.
  102. // eslint-disable-next-line react-hooks/rules-of-hooks
  103. const scopeProps = useScope(overrideScopes);
  104. const currentScope = scopeProps[`__scope${scopeName}`];
  105. return {
  106. ...nextScopes,
  107. ...currentScope
  108. };
  109. }, {});
  110. return $3bkAK$useMemo(()=>({
  111. [`__scope${baseScope.scopeName}`]: nextScopes1
  112. })
  113. , [
  114. nextScopes1
  115. ]);
  116. };
  117. };
  118. createScope1.scopeName = baseScope.scopeName;
  119. return createScope1;
  120. }
  121. export {$c512c27ab02ef895$export$fd42f52fd3ae1109 as createContext, $c512c27ab02ef895$export$50c7b4e9d9f19c1 as createContextScope};
  122. //# sourceMappingURL=index.mjs.map