-
-
Notifications
You must be signed in to change notification settings - Fork 400
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
Changes from 17 commits
fa86f78
d83f47f
75d8be3
83ca4ab
ef2044f
6c4c60b
5d85697
8d9135f
d27982c
ee5b797
d954612
aefff36
440a575
689904d
8640e41
dbcad68
a222ca0
7c44dde
3069d30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
@@ -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< | ||
|
@@ -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 = {} | ||
|
@@ -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})` | ||
|
@@ -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 { | ||
|
@@ -245,31 +228,31 @@ export default function createHOC< | |
} | ||
|
||
render() { | ||
const {dynamicSheet, classes, staticSheet} = this.state | ||
const {classes} = this.state | ||
const { | ||
innerRef, | ||
theme, | ||
jssContext, | ||
// $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 | ||
if (injectTheme && props.theme) props.theme = theme | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure we even need injectTheme prop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always inject theme? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually always injecting theme shouldn't be a big problem, the theme shouldn't change often that it would cause a lot of rerenders There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say yes, btw that line doesn't make sense, bc you have no theme in props, props is the rest ... so we seem to lack a test for this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a test for that, the tests are failing anyway right now |
||
|
||
// We have merged classes already. | ||
if (injectMap.classes) props.classes = classes | ||
props.classes = classes | ||
|
||
if (innerRef) props.ref = innerRef | ||
|
||
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} />} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so we can remove injectTheme completely? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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