Skip to content

Commit

Permalink
[react-jss] Remove inject option (#934)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Henri authored Dec 20, 2018
1 parent e2f15ae commit 6d3c5ba
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 35 deletions.
42 changes: 12 additions & 30 deletions packages/react-jss/src/createHoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, {Component, type ComponentType} from 'react'
import PropTypes from 'prop-types'
import {ThemeContext} from 'theming'
import {getDynamicStyles, SheetsManager, type StyleSheet, type Classes} from 'jss'
import jss from './jss'
import defaultJss from './jss'
import compose from './compose'
import getDisplayName from './getDisplayName'
import JssContext from './JssContext'
Expand Down Expand Up @@ -48,22 +48,6 @@ const getStyles = (stylesOrCreator: StylesOrCreator, theme: Theme) => {
return stylesOrCreator(theme)
}

// Returns an object with array property as a key and true as a value.
const toMap = arr =>
arr.reduce(
(map, prop) => ({
...map,
[prop]: true
}),
{}
)

const defaultInjectProps = {
sheet: false,
classes: true,
theme: true
}

let managersCounter = 0

export default function createHOC<
Expand All @@ -76,8 +60,7 @@ export default function createHOC<
options: Options
): ComponentType<OuterPropsType> {
const isThemingEnabled = typeof stylesOrCreator === 'function'
const {theming, inject, jss: optionsJss, ...sheetOptions} = options
const injectMap = inject ? toMap(inject) : defaultInjectProps
const {theming, injectTheme, jss: optionsJss, ...sheetOptions} = options
const displayName = getDisplayName(InnerComponent)
const defaultClassNamePrefix = env === 'production' ? '' : `${displayName}-`
const noTheme = {}
Expand All @@ -88,7 +71,7 @@ export default function createHOC<
// $FlowFixMe: DefaultProps is missing in type definitions
const {classes: defaultClasses = {}, ...defaultProps} = {...InnerComponent.defaultProps}

const getTheme = props => (isThemingEnabled ? props.theme : noTheme)
const getTheme = props => (isThemingEnabled && props.theme ? props.theme : noTheme)

class Jss extends Component<OuterPropsType, State> {
static displayName = `Jss(${displayName})`
Expand Down Expand Up @@ -129,7 +112,7 @@ export default function createHOC<
}

get jss() {
return this.props.jssContext.jss || optionsJss || jss
return this.props.jssContext.jss || optionsJss || defaultJss
}

get manager(): SheetsManager {
Expand Down Expand Up @@ -245,31 +228,30 @@ export default function createHOC<
}

render() {
const {dynamicSheet, classes, staticSheet} = this.state
const {classes} = this.state
const {
innerRef,
theme,
jssContext,
theme,
// $FlowFixMe: Flow complains for no reason...
...props
} = this.props
const sheet = dynamicSheet || staticSheet

if (injectMap.sheet && !props.sheet && sheet) props.sheet = sheet
if (injectMap.theme) props.theme = theme

// We have merged classes already.
if (injectMap.classes) props.classes = classes
props.classes = classes

if (innerRef) props.ref = innerRef
if (injectTheme) props.theme = theme

return <InnerComponent ref={innerRef} {...props} />
return <InnerComponent {...props} />
}
}

return function JssContextSubscriber(props) {
return (
<JssContext.Consumer>
{context => {
if (isThemingEnabled || injectMap.theme) {
if (isThemingEnabled || injectTheme) {
return (
<ThemeConsumer>
{theme => <Jss theme={theme} {...props} jssContext={context} />}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-jss/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const JssProvider: React.ComponentType<{
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>
type Options = {
index?: number
inject?: Array<'classes' | 'theme' | 'sheet'>
injectTheme?: boolean
jss?: Jss
} & StyleSheetFactoryOptions

Expand Down
7 changes: 3 additions & 4 deletions packages/react-jss/src/types.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import type {StyleSheet, StyleSheetFactoryOptions, Jss, SheetsRegistry, SheetsManager} from 'jss'
import type {StyleSheetFactoryOptions, Jss, SheetsRegistry, SheetsManager} from 'jss'
import type {Node, ElementRef} from 'react'
import {type Theming} from 'theming'

Expand All @@ -9,14 +9,13 @@ export type Managers = {[key: number]: SheetsManager}

export type Options = {
theming?: Theming<Theme>,
inject?: Array<'classes' | 'themes' | 'sheet'>,
injectTheme?: boolean,
jss?: Jss
} & StyleSheetFactoryOptions
export type InnerProps = {
children?: Node,
classes?: {},
theme: Theme,
sheet?: StyleSheet
theme?: Theme
}
// Needs to be hard coded for stricter types
export type Context = {
Expand Down

0 comments on commit 6d3c5ba

Please sign in to comment.