// @flow import { type EmotionCache } from '@emotion/utils' import * as React from 'react' import createCache from '@emotion/cache' import { isBrowser } from './utils' let EmotionCacheContext: React.Context = React.createContext( isBrowser ? createCache() : null ) export let ThemeContext = React.createContext({}) export let CacheProvider = EmotionCacheContext.Provider let withEmotionCache = function withEmotionCache( func: (props: Props, cache: EmotionCache, ref: React.Ref<*>) => React.Node ): React.StatelessFunctionalComponent { let render = (props: Props, ref: React.Ref<*>) => { return ( {( // $FlowFixMe we know it won't be null cache: EmotionCache ) => { return func(props, cache, ref) }} ) } // $FlowFixMe return React.forwardRef(render) } if (!isBrowser) { class BasicProvider extends React.Component< { children: EmotionCache => React.Node }, { value: EmotionCache } > { state = { value: createCache() } render() { return ( {this.props.children(this.state.value)} ) } } withEmotionCache = function withEmotionCache( func: (props: Props, cache: EmotionCache) => React.Node ): React.StatelessFunctionalComponent { return (props: Props) => ( {context => { if (context === null) { return ( {newContext => { return func(props, newContext) }} ) } else { return func(props, context) } }} ) } } export { withEmotionCache }