provider.cjs.js 1.78 KB
'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var React = require('react');
var core = require('@emotion/core');
var weakMemoize = _interopDefault(require('@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 = core.withCSSContext(function (props, context) {
  if (props.theme !== context.theme) {
    context = createCreateCacheWithTheme(context)(props.theme);
  }

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

module.exports = index;