index.js 6.1 KB

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