UNPKG

@emotion/react

Version:
157 lines (124 loc) 5.32 kB
import * as React from 'react'; import { useContext, forwardRef } from 'react'; import createCache from '@emotion/cache'; import _extends from '@babel/runtime/helpers/esm/extends'; import weakMemoize from '@emotion/weak-memoize'; import hoistNonReactStatics from '../_isolated-hnrs/dist/emotion-react-_isolated-hnrs.browser.esm.js'; import { getRegisteredStyles, registerStyles, insertStyles } from '@emotion/utils'; import { serializeStyles } from '@emotion/serialize'; import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'; var isDevelopment = false; var EmotionCacheContext = /* #__PURE__ */React.createContext( // we're doing this to avoid preconstruct's dead code elimination in this one case // because this module is primarily intended for the browser and node // but it's also required in react native and similar environments sometimes // and we could have a special build just for that // but this is much easier and the native packages // might use a different theme context in the future anyway typeof HTMLElement !== 'undefined' ? /* #__PURE__ */createCache({ key: 'css' }) : null); var CacheProvider = EmotionCacheContext.Provider; var __unsafe_useEmotionCache = function useEmotionCache() { return useContext(EmotionCacheContext); }; var withEmotionCache = function withEmotionCache(func) { return /*#__PURE__*/forwardRef(function (props, ref) { // the cache will never be null in the browser var cache = useContext(EmotionCacheContext); return func(props, cache, ref); }); }; var ThemeContext = /* #__PURE__ */React.createContext({}); var useTheme = function useTheme() { return React.useContext(ThemeContext); }; var getTheme = function getTheme(outerTheme, theme) { if (typeof theme === 'function') { var mergedTheme = theme(outerTheme); return mergedTheme; } return _extends({}, outerTheme, theme); }; var createCacheWithTheme = /* #__PURE__ */weakMemoize(function (outerTheme) { return weakMemoize(function (theme) { return getTheme(outerTheme, theme); }); }); var ThemeProvider = function ThemeProvider(props) { var theme = React.useContext(ThemeContext); if (props.theme !== theme) { theme = createCacheWithTheme(theme)(props.theme); } return /*#__PURE__*/React.createElement(ThemeContext.Provider, { value: theme }, props.children); }; function withTheme(Component) { var componentName = Component.displayName || Component.name || 'Component'; var WithTheme = /*#__PURE__*/React.forwardRef(function render(props, ref) { var theme = React.useContext(ThemeContext); return /*#__PURE__*/React.createElement(Component, _extends({ theme: theme, ref: ref }, props)); }); WithTheme.displayName = "WithTheme(" + componentName + ")"; return hoistNonReactStatics(WithTheme, Component); } var hasOwn = {}.hasOwnProperty; var typePropName = '__EMOTION_TYPE_PLEASE_DO_NOT_USE__'; var createEmotionProps = function createEmotionProps(type, props) { var newProps = {}; for (var _key in props) { if (hasOwn.call(props, _key)) { newProps[_key] = props[_key]; } } newProps[typePropName] = type; // Runtime labeling is an opt-in feature because: return newProps; }; var Insertion = function Insertion(_ref) { var cache = _ref.cache, serialized = _ref.serialized, isStringTag = _ref.isStringTag; registerStyles(cache, serialized, isStringTag); useInsertionEffectAlwaysWithSyncFallback(function () { return insertStyles(cache, serialized, isStringTag); }); return null; }; var Emotion = /* #__PURE__ */withEmotionCache(function (props, cache, ref) { var cssProp = props.css; // so that using `css` from `emotion` and passing the result to the css prop works // not passing the registered cache to serializeStyles because it would // make certain babel optimisations not possible if (typeof cssProp === 'string' && cache.registered[cssProp] !== undefined) { cssProp = cache.registered[cssProp]; } var WrappedComponent = props[typePropName]; var registeredStyles = [cssProp]; var className = ''; if (typeof props.className === 'string') { className = getRegisteredStyles(cache.registered, registeredStyles, props.className); } else if (props.className != null) { className = props.className + " "; } var serialized = serializeStyles(registeredStyles, undefined, React.useContext(ThemeContext)); className += cache.key + "-" + serialized.name; var newProps = {}; for (var _key2 in props) { if (hasOwn.call(props, _key2) && _key2 !== 'css' && _key2 !== typePropName && (!isDevelopment )) { newProps[_key2] = props[_key2]; } } newProps.className = className; if (ref) { newProps.ref = ref; } return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Insertion, { cache: cache, serialized: serialized, isStringTag: typeof WrappedComponent === 'string' }), /*#__PURE__*/React.createElement(WrappedComponent, newProps)); }); var Emotion$1 = Emotion; export { CacheProvider as C, Emotion$1 as E, ThemeContext as T, __unsafe_useEmotionCache as _, ThemeProvider as a, withTheme as b, createEmotionProps as c, hasOwn as h, isDevelopment as i, useTheme as u, withEmotionCache as w };