Skip to content

Commit

Permalink
(split)[react-jss] Big Refactor (cssinjs#949)
Browse files Browse the repository at this point in the history
* [WIP] Update to new react context for the jss context (cssinjs#924)

* Update to new react context for the jss context

* Fixed types

* Fix some tests

* Remove unnecessary cloning

* Fix JssProvider

* Update logic of merging context

* Fix a few issues and tests

* Make getTheme non public

* Update warning inside JssProvider

* Remove duplicate beforeEach

* Update size-snapshot

* Fix a few bugs

* Created a Managers type

* Create sheetOptions prop type definition

* Removed meta property from sheet options shape

* Fix creating a new generateId for every render

* Move media into it's own prop

* Added changelog

* Rename to JssContextSubscriber

* Moved context to jssContext prop instead of spreading it

* [react-jss] Remove inject option (cssinjs#934)

* Update to new react context for the jss context

* Fixed types

* Fix some tests

* Remove unnecessary cloning

* Fix JssProvider

* Update logic of merging context

* Fix a few issues and tests

* Make getTheme non public

* Update warning inside JssProvider

* Remove duplicate beforeEach

* Update size-snapshot

* Fix a few bugs

* Remove inject option

* Fix

* Always inject the theme

* Only inject theme when injectTheme is true

* Upgrade theming package (cssinjs#942)

* Add forwardRef support (cssinjs#943)

* [react-jss] Merge classes instead of overwriting (cssinjs#946)

* Add forwardRef support

* Add support for merging the classes

* Fix path of test file

* Remove jsdoc comment

* Implement custom memoize-one

* Convert function to arrow function

* Fix

* Update some of the tests

* Update dynamic styles tests

* Fix SSR tests

* Add theme inject tests

* Add merge classes tests

* Remove dependency of react-dom

* Fix an issue with memoizing and memoize the context

* Remove createHoC file and remove injectSheet to withStyles

* Update TS types

* Update TS types

* Fix flow types

* Fix react-jss types

* Rename injectSheet to withStyles in docs

* Fix react-jss types

* Update changelog.md

* Rename class and

* Fix tests

* Fix a few typos in docs

* Fix changelog

* Use javascript for codeblocks instead of js

* Upgrade theming

* Fix type import

* Update size-snapshot

* Fix docs

* Update size-snapshot

* Upgrade theming to v3.0.2

* Use travis_wait for test command

* Remove travis_wait command

* Update docs
  • Loading branch information
kof authored Jan 16, 2019
1 parent a7d41fb commit 2a6ba11
Show file tree
Hide file tree
Showing 23 changed files with 1,175 additions and 1,683 deletions.
30 changes: 15 additions & 15 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"dist/react-jss.js": {
"bundled": 145377,
"minified": 48635,
"gzipped": 14427
"bundled": 131866,
"minified": 44695,
"gzipped": 13215
},
"dist/react-jss.min.js": {
"bundled": 111229,
"minified": 37989,
"gzipped": 11616
"bundled": 100134,
"minified": 34854,
"gzipped": 10467
},
"dist/react-jss.cjs.js": {
"bundled": 16624,
"minified": 6969,
"gzipped": 2423
"bundled": 14377,
"minified": 6602,
"gzipped": 2191
},
"dist/react-jss.esm.js": {
"bundled": 16142,
"minified": 6566,
"gzipped": 2315,
"bundled": 13737,
"minified": 6056,
"gzipped": 2082,
"treeshaked": {
"rollup": {
"code": 1866,
"import_statements": 410
"code": 1925,
"import_statements": 436
},
"webpack": {
"code": 3178
"code": 3285
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@
"jss": "^10.0.0-alpha.7",
"jss-preset-default": "^10.0.0-alpha.7",
"prop-types": "^15.6.0",
"theming": "^2.2.0"
"theming": "^3.0.2"
}
}
10 changes: 10 additions & 0 deletions src/JssContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @flow
import React, {type Context} from 'react'
import type {Context as JssContextValue} from './types'

const JssContext: Context<JssContextValue> = React.createContext({
sheetOptions: {},
disableStylesGeneration: false
})

export default JssContext
136 changes: 66 additions & 70 deletions src/JssProvider.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,100 @@
// @flow
import {Component, type Node} from 'react'
import React, {Component, type Node} from 'react'
import PropTypes from 'prop-types'
import {
createGenerateId as createGenerateIdDefault,
type Jss,
type GenerateId,
type SheetsRegistry
} from 'jss'
import * as ns from './ns'
import contextTypes from './contextTypes'
import propTypes from './propTypes'
import type {Context} from './types'

/* eslint-disable react/require-default-props */
import defaultJss, {createGenerateId, type Jss, type GenerateId, SheetsRegistry} from 'jss'
import type {Context, Managers} from './types'
import JssContext from './JssContext'
import memoize from './memoize-one'

/* eslint-disable react/require-default-props, react/no-unused-prop-types */

type Props = {
jss?: Jss,
registry?: SheetsRegistry,
generateId?: GenerateId,
classNamePrefix?: string,
disableStylesGeneration?: boolean,
media?: string,
children: Node
}

export default class JssProvider extends Component<Props> {
static propTypes = {
...propTypes,
registry: PropTypes.instanceOf(SheetsRegistry),
jss: PropTypes.instanceOf(defaultJss.constructor),
generateId: PropTypes.func,
classNamePrefix: PropTypes.string,
disableStylesGeneration: PropTypes.bool,
children: PropTypes.node.isRequired
children: PropTypes.node.isRequired,
media: PropTypes.string
}

static childContextTypes = contextTypes

static contextTypes = contextTypes

// JssProvider can be nested. We allow to overwrite any context prop at any level.
// 1. Check if there is a value passed over props.
// 2. If value was passed, we set it on the child context.
// 3. If value was not passed, we proxy parent context (default context behaviour).
getChildContext(): Context {
const {
registry,
classNamePrefix,
jss: localJss,
generateId,
disableStylesGeneration
} = this.props
// eslint-disable-next-line react/react/destructuring-assignment
const sheetOptions = this.context[ns.sheetOptions] || {}
const context: Context = {[ns.sheetOptions]: sheetOptions}

if (registry) {
context[ns.sheetsRegistry] = registry
// This way we identify a new request on the server, because user will create
// a new Registry instance for each.
if (registry !== this.registry) {
// We reset managers because we have to regenerate all sheets for the new request.
this.managers = {}
this.registry = registry
managers: Managers = {}

createContext = memoize(
(outerContext: Context, props: Props): Context => {
const {registry, classNamePrefix, jss, generateId, disableStylesGeneration, media} = props
// Clone the outer context
const context = {...outerContext}

if (registry) {
context.registry = registry

// This way we identify a new request on the server, because user will create
// a new Registry instance for each.
if (registry !== this.registry) {
// We reset managers because we have to regenerate all sheets for the new request.
this.managers = {}
this.registry = registry
}
}
}

// Make sure we don't loose managers when JssProvider is used inside of a stateful
// component which decides to rerender.
context[ns.managers] = this.managers

if (generateId) {
sheetOptions.generateId = generateId
} else if (!sheetOptions.generateId) {
if (!this.generateId) {
let createGenerateId = createGenerateIdDefault
if (localJss && localJss.options.createGenerateId) {
createGenerateId = localJss.options.createGenerateId
context.managers = this.managers

if (generateId) {
context.sheetOptions.generateId = generateId
} else if (!context.sheetOptions.generateId) {
if (!this.generateId) {
this.generateId = createGenerateId()
}
// Make sure we don't loose the generator when JssProvider is used inside of a stateful
// component which decides to rerender.
this.generateId = createGenerateId()
context.sheetOptions.generateId = this.generateId
}

sheetOptions.generateId = this.generateId
}
// Merge the classname prefix
if (classNamePrefix) {
context.sheetOptions.classNamePrefix =
(context.sheetOptions.classNamePrefix || '') + classNamePrefix
}

if (classNamePrefix) sheetOptions.classNamePrefix = classNamePrefix
if (localJss) context[ns.jss] = localJss
if (disableStylesGeneration !== undefined) {
sheetOptions.disableStylesGeneration = disableStylesGeneration
if (media !== undefined) {
context.sheetOptions.media = media
}

if (jss) {
context.jss = jss
}

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

return context
}
)

return context
}
generateId: ?GenerateId

registry: SheetsRegistry
registry: ?SheetsRegistry

managers: {}
renderProvider = (outerContext: Context) => {
const {children} = this.props
// $FlowFixMe
const context: Context = this.createContext(outerContext, this.props)

generateId: GenerateId
return <JssContext.Provider value={context}>{children}</JssContext.Provider>
}

render() {
return this.props.children
return <JssContext.Consumer>{this.renderProvider}</JssContext.Consumer>
}
}
Loading

0 comments on commit 2a6ba11

Please sign in to comment.