22/* eslint-disable react/destructuring-assignment */
33import React , { Component , type ComponentType } from 'react'
44import PropTypes from 'prop-types'
5- import { withTheme } from 'theming'
5+ import { ThemeContext } from 'theming'
66import type { StyleSheet } from 'jss'
77import jss , { getDynamicStyles , SheetsManager } from './jss'
88import compose from './compose'
@@ -126,22 +126,15 @@ export default function createHOC<
126126 if ( isThemingEnabled && this . props . theme !== prevProps . theme ) {
127127 const newState = this . createState ( )
128128 this . manage ( newState )
129- this . manager . unmanage ( prevProps . theme )
129+ this . unmanage ( prevProps , prevState )
130+
130131 // eslint-disable-next-line react/no-did-update-set-state
131132 this . setState ( newState )
132133 }
133-
134- // We remove previous dynamicSheet only after new one was created to avoid FOUC.
135- if ( prevState . dynamicSheet !== this . state . dynamicSheet && prevState . dynamicSheet ) {
136- this . jss . removeStyleSheet ( prevState . dynamicSheet )
137- }
138134 }
139135
140136 componentWillUnmount ( ) {
141- this . manager . unmanage ( this . theme )
142- if ( this . state . dynamicSheet ) {
143- this . jss . removeStyleSheet ( this . state . dynamicSheet )
144- }
137+ this . unmanage ( this . props , this . state )
145138 }
146139
147140 get theme ( ) {
@@ -209,15 +202,29 @@ export default function createHOC<
209202 return null
210203 }
211204
212- manage ( { dynamicSheet, staticSheet} : State ) {
205+ manage ( state : State ) {
206+ const { dynamicSheet, staticSheet} = state
213207 const registry = this . context [ ns . sheetsRegistry ]
214208
215209 this . manager . manage ( this . theme )
216- if ( staticSheet && registry ) registry . add ( staticSheet )
210+ if ( staticSheet && registry ) {
211+ registry . add ( staticSheet )
212+ }
217213
218- if ( dynamicSheet !== null ) {
214+ if ( dynamicSheet ) {
219215 dynamicSheet . update ( this . props ) . attach ( )
220- if ( registry ) registry . add ( dynamicSheet )
216+
217+ if ( registry ) {
218+ registry . add ( dynamicSheet )
219+ }
220+ }
221+ }
222+
223+ unmanage ( prevProps , prevState ) {
224+ this . manager . unmanage ( prevProps . theme )
225+
226+ if ( prevState . dynamicSheet ) {
227+ this . jss . removeStyleSheet ( prevState . dynamicSheet )
221228 }
222229 }
223230
@@ -255,7 +262,8 @@ export default function createHOC<
255262
256263 render ( ) {
257264 const { dynamicSheet, classes, staticSheet} = this . state
258- const { innerRef, theme, ...props } : OuterPropsType = this . props
265+ // $FlowFixMe: Flow complains for no reason...
266+ const { innerRef, theme, ...props } = this . props
259267 const sheet = dynamicSheet || staticSheet
260268
261269 if ( injectMap . sheet && ! props . sheet && sheet ) props . sheet = sheet
@@ -269,9 +277,16 @@ export default function createHOC<
269277 }
270278
271279 if ( isThemingEnabled || injectMap . theme ) {
272- return theming
273- ? theming . withTheme ( Jss , { forwardInnerRef : true } )
274- : withTheme ( Jss , { forwardInnerRef : true } )
280+ const ThemeConsumer = ( theming && theming . context . Consumer ) || ThemeContext . Consumer
281+
282+ // eslint-disable-next-line no-inner-declarations
283+ function ContextSubscribers ( props ) {
284+ return < ThemeConsumer > { theme => < Jss theme = { theme } { ...props } /> } </ ThemeConsumer >
285+ }
286+
287+ ContextSubscribers . InnerComponent = InnerComponent
288+
289+ return ContextSubscribers
275290 }
276291
277292 return Jss
0 commit comments