Skip to content

Commit

Permalink
feat(JssProvider): provide custom isSSR flag (#1610)
Browse files Browse the repository at this point in the history
* jss provider accepts isSSR

* size snapshot
  • Loading branch information
enisdenjo authored Apr 21, 2022
1 parent 36b6a2c commit c40e67c
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 27 deletions.
30 changes: 15 additions & 15 deletions packages/react-jss/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"react-jss.js": {
"bundled": 136053,
"minified": 50140,
"gzipped": 16739
"bundled": 136245,
"minified": 50224,
"gzipped": 16773
},
"react-jss.min.js": {
"bundled": 103003,
"minified": 39999,
"gzipped": 13855
"bundled": 103195,
"minified": 40083,
"gzipped": 13886
},
"react-jss.cjs.js": {
"bundled": 21581,
"minified": 9508,
"gzipped": 3183
"bundled": 21759,
"minified": 9601,
"gzipped": 3220
},
"react-jss.esm.js": {
"bundled": 19703,
"minified": 8047,
"gzipped": 2975,
"bundled": 19855,
"minified": 8114,
"gzipped": 3000,
"treeshaked": {
"rollup": {
"code": 426,
"import_statements": 368
"code": 442,
"import_statements": 375
},
"webpack": {
"code": 1967
"code": 1922
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/react-jss/src/JssContext.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as React from 'react'
import isInBrowser from 'is-in-browser'

export default React.createContext({
classNamePrefix: '',
disableStylesGeneration: false
disableStylesGeneration: false,
isSSR: !isInBrowser
})
7 changes: 6 additions & 1 deletion packages/react-jss/src/JssProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default function JssProvider(props) {
const registryRef = React.useRef(null)

const createContext = (parentContext, prevContext = initialContext) => {
const {registry, classNamePrefix, jss, generateId, disableStylesGeneration, media, id} = props
const {registry, classNamePrefix, jss, generateId, disableStylesGeneration, media, id, isSSR} =
props

const context = {...parentContext}

Expand Down Expand Up @@ -57,6 +58,10 @@ export default function JssProvider(props) {
context.disableStylesGeneration = disableStylesGeneration
}

if (isSSR !== undefined) {
context.isSSR = isSSR
}

if (prevContext && shallowEqualObjects(prevContext, context)) {
return prevContext
}
Expand Down
15 changes: 8 additions & 7 deletions packages/react-jss/src/createUseStyles.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react'
import isInBrowser from 'is-in-browser'
import {ThemeContext as DefaultThemeContext} from 'theming'

import JssContext from './JssContext'
Expand All @@ -13,10 +12,12 @@ import getSheetIndex from './utils/getSheetIndex'
import {manageSheet, unmanageSheet} from './utils/managers'
import getSheetClasses from './utils/getSheetClasses'

const useInsertionEffect = isInBrowser
? React.useInsertionEffect || // React 18+ (https://github.com/reactwg/react-18/discussions/110)
React.useLayoutEffect
: React.useEffect
function getUseInsertionEffect(isSSR) {
return isSSR
? React.useEffect
: React.useInsertionEffect || // React 18+ (https://github.com/reactwg/react-18/discussions/110)
React.useLayoutEffect
}

const noTheme = {}

Expand Down Expand Up @@ -61,14 +62,14 @@ const createUseStyles = (styles, options = {}) => {
return [newSheet, newSheet ? addDynamicRules(newSheet, data) : null]
}, [context, theme])

useInsertionEffect(() => {
getUseInsertionEffect(context.isSSR)(() => {
// 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)
}
}, [data])

useInsertionEffect(
getUseInsertionEffect(context.isSSR)(
() => () => {
if (sheet) {
unmanageSheet({
Expand Down
2 changes: 1 addition & 1 deletion packages/react-jss/src/createUseStyles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('React-JSS: createUseStyles', () => {
const MyComponent = createStyledComponent(styles)

renderToString(
<JssProvider registry={registry} generateId={() => 'button'}>
<JssProvider registry={registry} generateId={() => 'button'} isSSR>
<MyComponent theme={{exampleColor: 'blue'}} />
</JssProvider>
)
Expand Down
1 change: 1 addition & 0 deletions packages/react-jss/src/flow-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type Context = {|
disableStylesGeneration?: boolean,
media?: string,
generateId?: GenerateId
isSSR?: boolean
|}

export type HOCProps<Theme, Props> = Props & {
Expand Down
2 changes: 2 additions & 0 deletions packages/react-jss/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ declare const JssProvider: ComponentType<{
disableStylesGeneration?: boolean
children: ReactNode
id?: CreateGenerateIdOptions
isSSR?: boolean
}>

interface Managers {
Expand All @@ -35,6 +36,7 @@ declare const JssContext: Context<{
managers?: Managers
sheetOptions: StyleSheetFactoryOptions
disableStylesGeneration: boolean
isSSR: boolean
}>

type ClassesForStyles<
Expand Down
4 changes: 2 additions & 2 deletions packages/react-jss/test-utils/createCommonBaseTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default ({createStyledComponent}) => {
})
const generateId = () => 'id'
renderToString(
<JssProvider registry={registry} generateId={generateId}>
<JssProvider registry={registry} generateId={generateId} isSSR>
<MyComponent />
</JssProvider>
)
Expand All @@ -81,7 +81,7 @@ export default ({createStyledComponent}) => {
options
)
renderToString(
<JssProvider registry={registry}>
<JssProvider registry={registry} isSSR>
<MyComponent />
</JssProvider>
)
Expand Down

0 comments on commit c40e67c

Please sign in to comment.