Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-jss): Media query styles applied only to the first element in function #1343

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@
{
"dist/react-jss.js": {
"bundled": 169946,
"minified": 58911,
"gzipped": 19311
"bundled": 171071,
"minified": 59339,
"gzipped": 19438
},
"dist/react-jss.min.js": {
"bundled": 112923,
"minified": 42170,
"gzipped": 14335
"bundled": 114048,
"minified": 42598,
"gzipped": 14460
},
"dist/react-jss.cjs.js": {
"bundled": 26398,
"minified": 11532,
"gzipped": 3759
"bundled": 27447,
"minified": 12023,
"gzipped": 3885
},
"dist/react-jss.esm.js": {
"bundled": 24901,
"minified": 10301,
"gzipped": 3602,
"bundled": 25941,
"minified": 10783,
"gzipped": 3728,
"treeshaked": {
"rollup": {
"code": 1838,
Expand Down
125 changes: 83 additions & 42 deletions packages/react-jss/src/createUseStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ const useEffectOrLayoutEffect = isInBrowser ? React.useLayoutEffect : React.useE

const noTheme = {}

const reducer = (prevState, action) => {
if (action.type === 'updateSheet') {
return action.payload
}
return prevState
}

const createUseStyles = <Theme: {}>(styles: Styles<Theme>, options?: HookOptions<Theme> = {}) => {
const {index = getSheetIndex(), theming, name, ...sheetOptions} = options
const ThemeContext = (theming && theming.context) || DefaultThemeContext
Expand All @@ -35,75 +42,109 @@ const createUseStyles = <Theme: {}>(styles: Styles<Theme>, options?: HookOptions
const context = React.useContext(JssContext)
const theme = useTheme()

const [sheet, dynamicRules] = React.useMemo(
const [state, dispatch] = React.useReducer(reducer, null, () => {
const sheet = createStyleSheet({
context,
styles,
name,
theme,
index,
sheetOptions
})

let dynamicRules
let classes
if (sheet) {
if (context.registry) {
context.registry.add(sheet)
}
dynamicRules = addDynamicRules(sheet, data)
classes = getSheetClasses(sheet, dynamicRules)
}

return {
sheet,
dynamicRules,
classes: classes || {}
}
})
React.useMemo(
() => {
if (!isFirstMount.current) {
const newSheet = createStyleSheet({
context,
styles,
name,
theme,
index,
sheetOptions
})
const newDynamicRules = newSheet && addDynamicRules(newSheet, data)
const newClasses = newSheet ? getSheetClasses(newSheet, newDynamicRules) : {}

dispatch({
type: 'updateSheet',
payload: {
sheet: newSheet,
dynamicRules: newDynamicRules,
classes: newClasses
}
})
}
},
[theme, context]
)
useEffectOrLayoutEffect(
() => {
const newSheet = createStyleSheet({
context,
styles,
name,
theme,
index,
sheetOptions
})

const newDynamicRules = newSheet ? addDynamicRules(newSheet, data) : null

if (newSheet) {
if (state.sheet) {
manageSheet({
index,
context,
sheet: newSheet,
sheet: state.sheet,
theme
})
}

return [newSheet, newDynamicRules]
return () => {
const {sheet, dynamicRules} = state

if (!sheet) return

unmanageSheet({
index,
context,
sheet,
theme
})

if (dynamicRules) {
removeDynamicRules(sheet, dynamicRules)
}
}
},
[context, theme]
[state.sheet]
)

useEffectOrLayoutEffect(
() => {
// We only need to update the rules on a subsequent update and not in the first mount
if (sheet && dynamicRules && !isFirstMount.current) {
updateDynamicRules(data, sheet, dynamicRules)
if (state.sheet && state.dynamicRules && !isFirstMount.current) {
updateDynamicRules(data, state.sheet, state.dynamicRules)
}
},
[data]
)

useEffectOrLayoutEffect(
() =>
// cleanup only
() => {
if (sheet) {
unmanageSheet({
index,
context,
sheet,
theme
})
}

if (sheet && dynamicRules) {
removeDynamicRules(sheet, dynamicRules)
}
},
[sheet]
)

const classes = sheet && dynamicRules ? getSheetClasses(sheet, dynamicRules) : {}

// $FlowFixMe
React.useDebugValue(classes)
React.useDebugValue(state.classes)
// $FlowFixMe
React.useDebugValue(theme === noTheme ? 'No theme' : theme)

React.useEffect(() => {
isFirstMount.current = false
})

return classes
return state.classes
}
}

Expand Down
76 changes: 66 additions & 10 deletions packages/react-jss/src/createUseStyles.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,72 @@
/* eslint-disable react/prop-types */
import createUseStyles from './createUseStyles'
import createBasicTests from '../test-utils/createBasicTests'

const createStyledComponent = (styles, options) => {
const useStyles = createUseStyles(styles, options)
const Comp = props => {
useStyles(props)
return null
}
return Comp
import expect from 'expect.js'
import React from 'react'
import TestRenderer from 'react-test-renderer'
import {stripIndent} from 'common-tags'

import {SheetsRegistry, JssProvider, ThemeProvider, createUseStyles} from '.'

const createGenerateId = () => {
let counter = 0
return rule => `${rule.key}-${counter++}`
}

const theme: Object = {
background: 'yellow',
background2: 'red'
}

describe('React-JSS: createUseStyles', () => {
createBasicTests({createStyledComponent})
it('should render multiple elements with applied media query', () => {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this test should be applied to both hook and hoc based implementation, just to be sure both work same way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you pls elaborate, like exactly which components are needed to be tested.

Copy link
Member

Choose a reason for hiding this comment

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

createBasicTests() is the function that runs all the tests which use the passed function which creates a styled component. You picked the test file that only serves as an entry point to generate tests using hooks interface,

If we want to run the test with both interfaces, you need to add the test inside createBasicTests

Also it seems like you removed createBasicTests() call entirely from this file, which means you don't run all the tests using hooks interface at all now

const registry = new SheetsRegistry()
const useStyles = createUseStyles(themeObj => ({
wrapper: () => ({
padding: 40,
background: themeObj.background,
textAlign: 'left',
'@media (min-width: 1024px)': {
backgroundColor: themeObj.background2
}
})
}))

const Comp = () => {
const classes = useStyles(theme)
return <div className={classes.wrapper} />
}

const a = [1, 2]
TestRenderer.create(
<JssProvider registry={registry} generateId={createGenerateId()}>
<ThemeProvider theme={theme}>
{a.map(item => (
<Comp key={item} />
))}
</ThemeProvider>
</JssProvider>
)
expect(registry.toString()).to.be(stripIndent`
.wrapper-0 {}
.wrapper-d0-1 {
padding: 40px;
background: yellow;
text-align: left;
}
@media (min-width: 1024px) {
.wrapper-d0-1 {
background-color: red;
}
}
.wrapper-d1-2 {
padding: 40px;
background: yellow;
text-align: left;
}
@media (min-width: 1024px) {
.wrapper-d1-2 {
background-color: red;
}
}`)
})
})