Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions packages/react-jss/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"react-jss.js": {
"bundled": 169770,
"minified": 59580,
"gzipped": 19568
"bundled": 169886,
"minified": 59592,
"gzipped": 19571
},
"react-jss.min.js": {
"bundled": 112410,
"minified": 42780,
"gzipped": 14522
"bundled": 112526,
"minified": 42792,
"gzipped": 14525
},
"react-jss.cjs.js": {
"bundled": 27701,
"minified": 12334,
"gzipped": 3862
"bundled": 27813,
"minified": 12346,
"gzipped": 3868
},
"react-jss.esm.js": {
"bundled": 25284,
"minified": 10437,
"gzipped": 3639,
"bundled": 25396,
"minified": 10449,
"gzipped": 3643,
"treeshaked": {
"rollup": {
"code": 1815,
Expand Down
9 changes: 6 additions & 3 deletions packages/react-jss/src/createUseStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ type CreateUseStyles = <Theme: {}>(Styles<Theme>, HookOptions<Theme> | void) =>
const createUseStyles: CreateUseStyles = <Theme: {}>(styles, options = {}) => {
const {index = getSheetIndex(), theming, name, ...sheetOptions} = options
const ThemeContext = (theming && theming.context) || DefaultThemeContext

/* eslint-disable no-unused-vars */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an unused variable somewhere? I'm confused as to why I see this comment. In fact, it appears twice.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 1b78ac4

const useTheme =
typeof styles === 'function'
? // $FlowFixMe[incompatible-return]
(): Theme => React.useContext(ThemeContext) || noTheme
(propsTheme?: Theme): Theme => propsTheme || React.useContext(ThemeContext) || noTheme
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A minor detail. But is there a chance propsTheme could be renamed to theme? Technically speaking, it's more an argument to a function than a property of a component in this case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 1b78ac4

: // $FlowFixMe[incompatible-return]
(): Theme => noTheme
(_?: Theme): Theme => noTheme
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming you've added the unused _ argument because useTheme gets passed data.theme on line 41 and you wanted to avoid any type errors? Would it be better to make the whole declaration a one liner in this case?

Something like

const useTheme = (theme?: Theme): Theme => typeof styles !== 'function' ? noTheme : theme || React.useContext(ThemeContext) || noTheme;

They're both the same type of function, so there should only be a need to switch between the return types (instead of switching between function declarations).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 1b78ac4

/* eslint-enable no-unused-vars */

return function useStyles(data: any) {
const isFirstMount = React.useRef(true)
const context = React.useContext(JssContext)
const theme = useTheme()
const theme = useTheme(data.theme)

const [sheet, dynamicRules] = React.useMemo(
() => {
Expand Down
29 changes: 28 additions & 1 deletion packages/react-jss/src/createUseStyles.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/* eslint-disable react/prop-types */

import {createUseStyles} from '.'
import * as React from 'react'
import {renderToString} from 'react-dom/server'
import expect from 'expect.js'
import {stripIndent} from 'common-tags'
import createCommonBaseTests from '../test-utils/createCommonBaseTests'
import {createUseStyles, JssProvider, SheetsRegistry} from '.'

const createStyledComponent = (styles, options) => {
const useStyles = createUseStyles(styles, options)
Expand All @@ -14,4 +18,27 @@ const createStyledComponent = (styles, options) => {

describe('React-JSS: createUseStyles', () => {
createCommonBaseTests({createStyledComponent})

describe('theme prop', () => {
it('should pass theme from props priority', () => {
const registry = new SheetsRegistry()

const styles = theme => ({
button: {color: theme.exampleColor || 'green'}
})

const MyComponent = createStyledComponent(styles)

renderToString(
<JssProvider registry={registry} generateId={() => 'button'}>
<MyComponent theme={{exampleColor: 'blue'}} />
</JssProvider>
)
expect(registry.toString()).to.be(stripIndent`
.button {
color: blue;
}
`)
})
})
})