Skip to content

Commit 929e42a

Browse files
author
Henri Beck
committed
Update to theming v2.2.0
1 parent 43ec343 commit 929e42a

6 files changed

Lines changed: 56 additions & 41 deletions

File tree

packages/react-jss/.size-snapshot.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
22
"dist/react-jss.js": {
3-
"bundled": 147358,
4-
"minified": 48843,
5-
"gzipped": 14467
3+
"bundled": 147575,
4+
"minified": 48891,
5+
"gzipped": 14498
66
},
77
"dist/react-jss.min.js": {
8-
"bundled": 112534,
9-
"minified": 38141,
10-
"gzipped": 11760
8+
"bundled": 112751,
9+
"minified": 38190,
10+
"gzipped": 11783
1111
},
1212
"dist/react-jss.cjs.js": {
13-
"bundled": 16580,
14-
"minified": 6957,
15-
"gzipped": 2412
13+
"bundled": 16900,
14+
"minified": 7025,
15+
"gzipped": 2444
1616
},
1717
"dist/react-jss.esm.js": {
18-
"bundled": 16112,
19-
"minified": 6567,
20-
"gzipped": 2306,
18+
"bundled": 16417,
19+
"minified": 6620,
20+
"gzipped": 2335,
2121
"treeshaked": {
2222
"rollup": {
2323
"code": 1896,

packages/react-jss/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@
4343
"jss": "^9.7.0",
4444
"jss-preset-default": "^4.3.0",
4545
"prop-types": "^15.6.0",
46-
"theming": "^2.1.2"
46+
"theming": "^2.2.0"
4747
}
4848
}

packages/react-jss/src/createHoc.js

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable react/destructuring-assignment */
33
import React, {Component, type ComponentType} from 'react'
44
import PropTypes from 'prop-types'
5-
import {withTheme} from 'theming'
5+
import {ThemeContext} from 'theming'
66
import type {StyleSheet} from 'jss'
77
import jss, {getDynamicStyles, SheetsManager} from './jss'
88
import 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

packages/react-jss/src/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {type Theming} from 'theming'
66
export type Theme = {}
77

88
export type Options = {
9-
theming?: Theming,
9+
theming?: Theming<Theme>,
1010
inject?: Array<'classes' | 'themes' | 'sheet'>,
1111
jss?: Jss
1212
} & StyleSheetFactoryOptions

packages/react-jss/tests/theming.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ describe('React-JSS: theming', () => {
245245
node
246246
)
247247

248-
expect(document.querySelectorAll('style').length).to.equal(2)
248+
expect(document.querySelectorAll('style').length).to.equal(1)
249249

250250
render(
251251
<div>
@@ -275,7 +275,7 @@ describe('React-JSS: theming', () => {
275275
node
276276
)
277277

278-
expect(document.querySelectorAll('style').length).to.equal(4)
278+
expect(document.querySelectorAll('style').length).to.equal(3)
279279

280280
render(
281281
<div>
@@ -319,7 +319,7 @@ describe('React-JSS: theming', () => {
319319
node
320320
)
321321

322-
expect(document.querySelectorAll('style').length).to.equal(2)
322+
expect(document.querySelectorAll('style').length).to.equal(1)
323323
})
324324

325325
it('two themed instances w/ dynamic props w/ different themes = 4 styles, same theme update = 3 styles', () => {
@@ -349,7 +349,7 @@ describe('React-JSS: theming', () => {
349349
node
350350
)
351351

352-
expect(document.querySelectorAll('style').length).to.equal(4)
352+
expect(document.querySelectorAll('style').length).to.equal(3)
353353
})
354354

355355
it('with JssProvider should render two different sheets', () => {

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8760,10 +8760,10 @@ text-table@^0.2.0:
87608760
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
87618761
integrity sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=
87628762

8763-
theming@^2.1.2:
8764-
version "2.1.2"
8765-
resolved "https://registry.yarnpkg.com/theming/-/theming-2.1.2.tgz#f5a3353168b713f1becc0351bd35a6d4599d6b25"
8766-
integrity sha512-AUS4S1WwukBHTh3R6k4gQJoF07BhyRcT5c1nIsuJXRBkZiFnPc3ev5VE5/EoInWYseR772kGthgIOW61GgyDUw==
8763+
theming@^2.2.0:
8764+
version "2.2.0"
8765+
resolved "https://registry.yarnpkg.com/theming/-/theming-2.2.0.tgz#64fe667afc5fb742060dd084fe4af6fac1227a97"
8766+
integrity sha512-S+augXK6352pVHzuo3usMWpPMDZGYywhQkqwhhgHBaXt5nAohzbItm4ptBsXXDZZ5fN+BIlGGmf1ADI7Q/DduQ==
87678767
dependencies:
87688768
"@types/react" "^16.4.18"
87698769
create-react-context "^0.2.3"

0 commit comments

Comments
 (0)