provider.esm.js 1.65 KB
import { createElement } from 'react';
import { withCSSContext, Provider } from '@emotion/core';
import weakMemoize from '@emotion/weak-memoize';

function _extends() {
  _extends = Object.assign || function (target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];

      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }

    return target;
  };

  return _extends.apply(this, arguments);
}

var getTheme = function getTheme(outerTheme, theme) {
  if (typeof theme === 'function') {
    var mergedTheme = theme(outerTheme);

    if (process.env.NODE_ENV !== 'production' && Object.prototype.toString.call(mergedTheme) !== '[object Object]') {
      throw new Error('[@emotion/provider] Please return an object from your theme function, i.e. theme={() => ({})}!');
    }

    return mergedTheme;
  }

  if (process.env.NODE_ENV !== 'production' && Object.prototype.toString.call(theme) !== '[object Object]') {
    throw new Error('[@emotion/provider] Please make your theme prop a plain object');
  }

  return _extends({}, outerTheme, theme);
};

var createCreateCacheWithTheme = weakMemoize(function (cache) {
  return weakMemoize(function (theme) {
    var actualTheme = getTheme(cache.theme, theme);
    return _extends({}, cache, {
      theme: actualTheme
    });
  });
});
var index = withCSSContext(function (props, context) {
  if (props.theme !== context.theme) {
    context = createCreateCacheWithTheme(context)(props.theme);
  }

  return createElement(Provider, {
    value: context
  }, props.children);
});

export default index;