Skip to content

Commit 5b34213

Browse files
author
Sergey Volodin
committed
add passing theme from props in useStyles
1 parent 6f151d4 commit 5b34213

3 files changed

Lines changed: 46 additions & 16 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
"react-jss.js": {
3-
"bundled": 169770,
4-
"minified": 59580,
5-
"gzipped": 19568
3+
"bundled": 169886,
4+
"minified": 59592,
5+
"gzipped": 19571
66
},
77
"react-jss.min.js": {
8-
"bundled": 112410,
9-
"minified": 42780,
10-
"gzipped": 14522
8+
"bundled": 112526,
9+
"minified": 42792,
10+
"gzipped": 14525
1111
},
1212
"react-jss.cjs.js": {
13-
"bundled": 27701,
14-
"minified": 12334,
15-
"gzipped": 3862
13+
"bundled": 27813,
14+
"minified": 12346,
15+
"gzipped": 3868
1616
},
1717
"react-jss.esm.js": {
18-
"bundled": 25284,
19-
"minified": 10437,
20-
"gzipped": 3639,
18+
"bundled": 25396,
19+
"minified": 10449,
20+
"gzipped": 3643,
2121
"treeshaked": {
2222
"rollup": {
2323
"code": 1815,

packages/react-jss/src/createUseStyles.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ type CreateUseStyles = <Theme: {}>(Styles<Theme>, HookOptions<Theme> | void) =>
2525
const createUseStyles: CreateUseStyles = <Theme: {}>(styles, options = {}) => {
2626
const {index = getSheetIndex(), theming, name, ...sheetOptions} = options
2727
const ThemeContext = (theming && theming.context) || DefaultThemeContext
28+
29+
/* eslint-disable no-unused-vars */
2830
const useTheme =
2931
typeof styles === 'function'
3032
? // $FlowFixMe[incompatible-return]
31-
(): Theme => React.useContext(ThemeContext) || noTheme
33+
(propsTheme?: Theme): Theme => propsTheme || React.useContext(ThemeContext) || noTheme
3234
: // $FlowFixMe[incompatible-return]
33-
(): Theme => noTheme
35+
(_?: Theme): Theme => noTheme
36+
/* eslint-enable no-unused-vars */
3437

3538
return function useStyles(data: any) {
3639
const isFirstMount = React.useRef(true)
3740
const context = React.useContext(JssContext)
38-
const theme = useTheme()
41+
const theme = useTheme(data.theme)
3942

4043
const [sheet, dynamicRules] = React.useMemo(
4144
() => {

packages/react-jss/src/createUseStyles.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
/* eslint-disable react/prop-types */
22

3-
import {createUseStyles} from '.'
3+
import * as React from 'react'
4+
import {renderToString} from 'react-dom/server'
5+
import expect from 'expect.js'
6+
import {stripIndent} from 'common-tags'
47
import createCommonBaseTests from '../test-utils/createCommonBaseTests'
8+
import {createUseStyles, JssProvider, SheetsRegistry} from '.'
59

610
const createStyledComponent = (styles, options) => {
711
const useStyles = createUseStyles(styles, options)
@@ -14,4 +18,27 @@ const createStyledComponent = (styles, options) => {
1418

1519
describe('React-JSS: createUseStyles', () => {
1620
createCommonBaseTests({createStyledComponent})
21+
22+
describe('theme prop', () => {
23+
it('should pass theme from props priority', () => {
24+
const registry = new SheetsRegistry()
25+
26+
const styles = theme => ({
27+
button: {color: theme.exampleColor || 'green'}
28+
})
29+
30+
const MyComponent = createStyledComponent(styles)
31+
32+
renderToString(
33+
<JssProvider registry={registry} generateId={() => 'button'}>
34+
<MyComponent theme={{exampleColor: 'blue'}} />
35+
</JssProvider>
36+
)
37+
expect(registry.toString()).to.be(stripIndent`
38+
.button {
39+
color: blue;
40+
}
41+
`)
42+
})
43+
})
1744
})

0 commit comments

Comments
 (0)