Skip to content

Implement injectStyled#6

Merged
lttb merged 17 commits intomasterfrom
feature/injectStyled
Apr 24, 2017
Merged

Implement injectStyled#6
lttb merged 17 commits intomasterfrom
feature/injectStyled

Conversation

@lttb
Copy link
Copy Markdown
Member

@lttb lttb commented Apr 22, 2017

Implement injectStyled from #4

@kof

@lttb lttb force-pushed the feature/injectStyled branch from e29a692 to b555ba6 Compare April 22, 2017 22:30
Comment thread src/injectStyled.js Outdated
const {sheets} = styled
const {staticSheet, dynamicSheet} = sheets

const classNames = new Set([
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we avoid Set? It needs a polyfil.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yeah, we can

Comment thread src/injectStyled.js Outdated
[name]: composeClasses(staticSheet.classes[name], dynamicSheet.classes[name]),
}), {})

return (...props: any) => createElement(InnerComponent, {sheets, classes, ...props})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

props is always an object, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

right, will fix it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would probably not inject sheets at all. What use cases are there anyways? Also in react-jss I inject sheet which refs to dynamicSheet if there is one and to static if there is no. Actually I did it because it was already there for the case people still using props.sheet. From my experience there should be no need to use sheet object in react.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

agree, I made it in relation to injectSheet, but it really doesn't look necessary

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If current sheet prop passing in react-jss makes troubles, we might want to remove it from there also.

Comment thread src/injectStyled.js Outdated
import composeClasses from './utils/compose-classes'
import type {styledType} from './types'

const injectStyled = (styled: styledType) => (InnerComponent: ReactClass<any>) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Where does ReactClass come from?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is native flow type

Comment thread .eslintrc
jest: true

globals:
ReactClass: true
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this needed, where do we use such a global?

Copy link
Copy Markdown
Member Author

@lttb lttb Apr 23, 2017

Choose a reason for hiding this comment

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

eslint consider flow types and it works with rules like 'no-undef`, but it dosnt about this flow type. so we need to add it as global.

but maybe there is better solution with some plugin etc., I'll take a look

Comment thread src/index.js Outdated
const createStyled = (jss?: Function = jssDefault) => (baseStyles: Object = {}) => {
let sheet
let dynamicSheet
const createStyled = (jss?: Function = jssDefault) => (baseStyles: Object = {}): styledType => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

styledType should be capitelized, no?

Copy link
Copy Markdown
Member

@kof kof Apr 23, 2017

Choose a reason for hiding this comment

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

Also lets use a type JssStyles = Object and type JssStyle = Object and put it into types, once we have a fully typed styles object for jss we can easily replace Object with the new one in types.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

👍

Comment thread src/types/index.js Outdated
}
export type StyledElementAttrsType = {tag: string, styles: Object}
export type StyledElementType = Function & StyledElementAttrsType
export type tagOrStyledElementTypeype = string | StyledElementType
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should be capitalized

Comment thread src/index.js Outdated
const mountSheets = () => {
if (!sheets.staticSheet) {
sheets.staticSheet = jss.createStyleSheet(baseStyles, {
link: true,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why do you need link: true for static sheet?

@lttb lttb force-pushed the feature/injectStyled branch from 719f977 to fbbb43b Compare April 23, 2017 22:39
Comment thread README.md
padding: 10
padding: 10,
'& + &': {
marginLeft; 10
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

typo

Comment thread src/index.js Outdated
StyledType,
StyledElementAttrsType,
StyledElementType,
TagOrStyledElementTypeype,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

typo

Comment thread src/index.js Outdated

static styles = elementStyles
static tag: string = tag
static styles: ComponentStylesType = elementStyles
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

just realized: elementStyles should be elementStyle, ownStyles should be ownStyle

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

right, thanks

Comment thread src/index.js Outdated
tagOrStyledElement: TagOrStyledElementTypeype,
ownStyles: ComponentStylesType
): StyledElementType => {
const {tag, styles}: StyledElementAttrsType = typeof tagOrStyledElement === 'string'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lets use tagName instead of tag everywhere, its more correct and aligned with element.tagName

Comment thread src/index.js Outdated
@@ -32,9 +51,8 @@ const createStyled = (jss?: Function = jssDefault) => (baseStyles: Object = {})
const staticTag = `${tag}-${++counter}`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would call it considered previous comment staticTagName

Comment thread src/index.js
sheets.staticSheet.addRule(staticTag, elementStyles)
}

if (dynamicStyles && !dynamicSheet.getRule(this.tagScoped)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of tagScoped: tagId

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

maybe dynamicTagName?

Comment thread src/index.js Outdated
): StyledElementType => {
const {tag, styles}: StyledElementAttrsType = typeof tagOrStyledElement === 'string'
? {tag: tagOrStyledElement, styles: {}}
const {tagName, style}: StyledElementAttrsType = typeof tagOrStyledElement === 'string'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

tagOrStyledElement -> tagNameOrStyledElement

@lttb lttb force-pushed the feature/injectStyled branch from 3de5ee1 to 35eac8b Compare April 24, 2017 05:49
Comment thread src/index.js Outdated
const sheets = {}
let counter = 0

const getScopedTagName = (tagName: string) => `${tagName}-${++counter}`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we can actually move it to the utils and call it generateTagName, similar to jss https://github.com/cssinjs/jss/blob/master/src/utils/generateClassName.js

Comment thread src/index.js Outdated
if (dynamicStyles && !dynamicSheet.getRule(this.tagScoped)) {
dynamicSheet
if (dynamicStyle && !sheets.dynamicSheet.getRule(this.dynamicTagName)) {
sheets.dynamicSheet
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

just realized we don't need sheets object any more.

Copy link
Copy Markdown
Member Author

@lttb lttb Apr 24, 2017

Choose a reason for hiding this comment

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

we need, because we need mutated sheets here too https://github.com/cssinjs/styled-jss/pull/6/files#diff-f336995dd331140b2022485b207c9fc1R10

if we just export them, with reassign we will lost links to new sheets

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

do we reassign them somewhere else than in mountSheets?? Should evtl mountSheets return them?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

no, you are right, we can just return them.
will fix it

Comment thread src/index.js Outdated
props: StyledElementPropsType

tagScoped = ''
dynamicTagName = ''
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we could set dynamicTagName = generateTagName(tagName) here and remove constructor override, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

nope, cause we can call Components with different styles

<Button color="pink" />
<Button color="green" />

and we need to have a scoped tagName for each instance

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

right

Comment thread src/index.js Outdated

componentWillUnmount() {
dynamicSheet.deleteRule(this.tagScoped)
sheets.dynamicSheet.deleteRule(this.dynamicTagName)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why do we remove the rule, we could reuse that rule next time the component mounts, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

because we generate new scoped name in constructor, so we can't get and old dynamicTagName for the pervious instance

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why? the instance will be reused in react ....

Copy link
Copy Markdown
Member Author

@lttb lttb Apr 24, 2017

Choose a reason for hiding this comment

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

right, but react will call constructor and we will lose it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

well, don't redefine it in this case if it is already defined?

@@ -0,0 +1 @@
export default (...args: any) => args.filter(Boolean).join(' ')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

just realized, why do we use sometimes camel case and sometimes dash separated file names? In JSS I stick to camel case.

@lttb lttb force-pushed the feature/injectStyled branch from aafd1d4 to 0d79dbb Compare April 24, 2017 07:00
@lttb lttb force-pushed the feature/injectStyled branch from 8ed9688 to 41a24aa Compare April 24, 2017 07:06
@lttb lttb force-pushed the feature/injectStyled branch from 364d001 to 8a0a61d Compare April 24, 2017 07:46
@lttb lttb merged commit ca8f9b2 into master Apr 24, 2017
@lttb lttb deleted the feature/injectStyled branch April 24, 2017 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants