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

[react-jss] Remove inject option #934

Merged
merged 19 commits into from
Dec 20, 2018
Merged
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'
Copy link
Member

Choose a reason for hiding this comment

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

I think we can also get rid of isThemingEnabled, because if a theme is on the context, its weird it would be implicitely depending on styles being a function

Copy link
Member Author

Choose a reason for hiding this comment

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

No, we still need to do some checks based on isThemingEnabled

Copy link
Member

Choose a reason for hiding this comment

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

Lets revisit them, I think we can get rid of it.

Copy link
Member Author

Choose a reason for hiding this comment

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

We still need this to decide which theme we are using.

When someone passed a function as the styles, we use the theme prop.

Because the user still might pass injectTheme: true which would subscribe to the theme, even though the styles are not theme dependent, we would create additional stylesheets.

Copy link
Member

Choose a reason for hiding this comment

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

I think isThemingEnabled logic needs to be different, e.g. shouldSubscribeTheme = options.injectTheme || stylesIsfunction and rendering of styles just based on stylesIsFunction

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
Copy link
Member

Choose a reason for hiding this comment

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

so we can remove injectTheme completely?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, someone might still want to subscribe to the theme even though he has a styles object

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