-
Notifications
You must be signed in to change notification settings - Fork 15
Implement injectStyled #6
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
43853de
Add es0215 babel preset
lttb 0ad79df
Update yarn.lock
lttb 3a90aa3
Implement injectStyled, resolve #4
lttb 253a136
Fix styledType
lttb b555ba6
Update README.md for injectStyled
lttb e994113
Merge branch 'master' into feature/injectStyled
lttb 3ff81d1
Avoid set and remove sheets prop in injectStyled
lttb fbbb43b
Update types, remove link for static sheet
lttb caec53a
Fix typos and naming
lttb 35eac8b
Rename TagOrStyledElement -> TagNameOrStyledElement
lttb 3860127
Move generateTagName to utils, use camelCase for filename
lttb 0d79dbb
Refactor createStyled, move styled function to the separate file
lttb b375d41
Add tests to pre-commit hook
lttb 41a24aa
Fix elementStyle type
lttb 8a0a61d
Move createStyled to the separate file
lttb 8cf691f
Add babel-plugin-transform-flow-strip-types
lttb 775dcbd
Cache generated dynamicTagName
lttb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,6 @@ parser: babel-eslint | |
|
|
||
| env: | ||
| jest: true | ||
|
|
||
| globals: | ||
| ReactClass: true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,16 +31,21 @@ const PrimaryButton = styled(Button, { | |
| Using base Style Sheet we can share classes between styled primitives. | ||
|
|
||
| ```js | ||
| import { Styled } from 'styled-jss' | ||
| import injectSheet from 'react-jss' | ||
| import { Styled, injectStyled } from 'styled-jss' | ||
|
|
||
| // Base styles, like a regular jss object. | ||
| const styled = Styled({ | ||
| root: { | ||
| margin: 10 | ||
| margin: 10, | ||
| '& $baseButton': { | ||
| fontSize: 16 | ||
| } | ||
| }, | ||
| baseButton: { | ||
| padding: 10 | ||
| padding: 10, | ||
| '& + &': { | ||
| marginLeft; 10 | ||
|
Member
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. typo |
||
| } | ||
| } | ||
| }) | ||
|
|
||
|
|
@@ -58,20 +63,24 @@ const PrimaryButton = styled(NormalButton, { | |
| // One can use classes AND styled primitives. | ||
| const MyComponent = ({classes}) => ( | ||
| <div className={classes.root}> | ||
| <NormalButton>normal button</NormalButton> | ||
| <PrimaryButton>primary button</PrimaryButton> | ||
| </div> | ||
| ) | ||
|
|
||
| const MyStyledComponent = injectSheet(styled.styles)(MyComponent) | ||
| const MyStyledComponent = injectStyled(styled)(MyComponent) | ||
| ``` | ||
|
|
||
| ### With custom JSS setup: | ||
|
|
||
| `styled-jss` use [jss-preset-default](https://github.com/cssinjs/jss-preset-default) by default. | ||
| But you can require `createStyled` and provide your custom jss instance. | ||
|
|
||
| ```js | ||
| import { create as createJss } from 'jss' | ||
| import vendorPrefixer from 'jss-vendor-prefixer' | ||
|
|
||
| import { createStyled } from 'styled-jss' | ||
| import createStyled from 'styled-jss/createStyled' | ||
|
|
||
| const jss = createJss() | ||
| jss.use(vendorPrefixer()) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import styled from './styled' | ||
|
|
||
| import type { | ||
| BaseStylesType, | ||
| ComponentStyleType, | ||
| StyledType, | ||
| StyledElementAttrsType, | ||
| StyledElementType, | ||
| TagNameOrStyledElementType | ||
| } from './types' | ||
|
|
||
| const createStyled = (jss: Function) => ( | ||
| baseStyles: BaseStylesType = {} | ||
| ): StyledType => { | ||
| let staticSheet | ||
| let dynamicSheet | ||
|
|
||
| const mountSheets = () => { | ||
| if (!staticSheet) { | ||
| staticSheet = jss.createStyleSheet(baseStyles, { | ||
| meta: 'StaticBaseSheet', | ||
| }).attach() | ||
|
|
||
| dynamicSheet = jss.createStyleSheet({}, { | ||
| link: true, | ||
| meta: 'DynamicComponentSheet', | ||
| }).attach() | ||
| } | ||
|
|
||
| return {staticSheet, dynamicSheet} | ||
| } | ||
|
|
||
| return Object.assign(( | ||
| tagNameOrStyledElement: TagNameOrStyledElementType, | ||
| ownStyle: ComponentStyleType | ||
| ): StyledElementType => { | ||
| const {tagName, style}: StyledElementAttrsType = typeof tagNameOrStyledElement === 'string' | ||
| ? {tagName: tagNameOrStyledElement, style: {}} | ||
| : tagNameOrStyledElement | ||
|
|
||
| const elementStyle = {...style, ...ownStyle} | ||
|
|
||
| return styled({tagName, baseStyles, elementStyle, mountSheets}) | ||
| }, {mountSheets, styles: baseStyles}) | ||
| } | ||
|
|
||
| export default createStyled |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,116 +1,11 @@ | ||
| import {PureComponent, createElement} from 'react' | ||
| import {create as createJss, getDynamicStyles} from 'jss' | ||
| import {create as createJss} from 'jss' | ||
| import preset from 'jss-preset-default' | ||
| import filterProps from './utils/filter-props' | ||
|
|
||
| const jssDefault = createJss(preset()) | ||
| import createStyled from './createStyled' | ||
|
|
||
| type StyledElementAttrsType = { tag: string, styles: Object } | ||
| type StyledElementType = Function & StyledElementAttrsType | ||
| type tagOrStyledElementTypeype = string | StyledElementType | ||
| type StyledElementPropsType = { | ||
| classes: Object, | ||
| children: ?any, | ||
| className: ?string, | ||
| } | ||
| const jss: Function = createJss(preset()) | ||
|
|
||
| const createStyled = (jss?: Function = jssDefault) => (baseStyles: Object = {}) => { | ||
| let sheet | ||
| let dynamicSheet | ||
| let counter = 0 | ||
| export const Styled = createStyled(jss) | ||
| export default Styled() | ||
|
|
||
| const styled = ( | ||
| tagOrStyledElement: tagOrStyledElementTypeype, | ||
| ownStyles: Object | ||
| ): StyledElementType => { | ||
| const {tag, styles}: StyledElementAttrsType = typeof tagOrStyledElement === 'string' | ||
| ? {tag: tagOrStyledElement, styles: {}} | ||
| : tagOrStyledElement | ||
|
|
||
| const elementStyles = {...styles, ...ownStyles} | ||
| const dynamicStyles = getDynamicStyles(elementStyles) | ||
| const staticTag = `${tag}-${++counter}` | ||
|
|
||
| return class StyledElement extends PureComponent { | ||
| static tag = tag | ||
|
|
||
| static styles = elementStyles | ||
|
|
||
| props: StyledElementPropsType | ||
|
|
||
| tagScoped = '' | ||
|
|
||
| constructor(props) { | ||
| super(props) | ||
| this.tagScoped = `${tag}-${++counter}` | ||
| } | ||
|
|
||
| componentWillMount() { | ||
| if (!sheet) { | ||
| sheet = jss.createStyleSheet(baseStyles, { | ||
| link: true, | ||
| meta: 'StaticBaseSheet', | ||
| }).attach() | ||
|
|
||
| dynamicSheet = jss.createStyleSheet({}, { | ||
| link: true, | ||
| meta: 'DynamicComponentSheet', | ||
| }).attach() | ||
| } | ||
|
|
||
| if (!sheet.getRule(staticTag)) { | ||
| sheet.addRule(staticTag, elementStyles) | ||
| } | ||
|
|
||
| if (dynamicStyles && !dynamicSheet.getRule(this.tagScoped)) { | ||
| dynamicSheet | ||
| .detach() | ||
| .addRule(this.tagScoped, dynamicStyles) | ||
| dynamicSheet | ||
| .update(this.tagScoped, this.props) | ||
| .attach() | ||
| .link() | ||
| } | ||
| } | ||
|
|
||
| componentWillReceiveProps(nextProps: StyledElementPropsType) { | ||
| if (dynamicStyles) { | ||
| dynamicSheet.update(this.tagScoped, nextProps) | ||
| } | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| dynamicSheet.deleteRule(this.tagScoped) | ||
| } | ||
|
|
||
| render() { | ||
| if (!sheet) return null | ||
|
|
||
| const {children, className, ...attrs} = this.props | ||
|
|
||
| const props = filterProps(attrs) | ||
| const tagClass = [ | ||
| sheet.classes[staticTag], | ||
| dynamicSheet.classes[this.tagScoped], | ||
| className, | ||
| ] | ||
| .filter(Boolean) | ||
| .join(' ') | ||
|
|
||
| return createElement(tag, {...props, className: tagClass}, children) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return Object.assign(styled, {styles: baseStyles}) | ||
| } | ||
|
|
||
| const defaultStyledCreator = createStyled() | ||
| const defaultStyled = defaultStyledCreator() | ||
|
|
||
| export { | ||
| createStyled, | ||
| defaultStyledCreator as Styled, | ||
| } | ||
|
|
||
| export default defaultStyled | ||
| export {default as injectStyled} from './injectStyled' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import {createElement} from 'react' | ||
|
|
||
| import composeClasses from './utils/composeClasses' | ||
| import type {StyledType} from './types' | ||
|
|
||
| const injectStyled = (styled: StyledType) => (InnerComponent: ReactClass<any>) => { | ||
| const {staticSheet, dynamicSheet} = styled.mountSheets() | ||
|
|
||
| const classNames = Object.keys({...staticSheet.classes, ...dynamicSheet.classes}) | ||
|
|
||
| const classes = [...classNames] | ||
| .reduce((acc, name) => ({ | ||
| ...acc, | ||
| [name]: composeClasses(staticSheet.classes[name], dynamicSheet.classes[name]), | ||
| }), {}) | ||
|
|
||
| return (props: Object) => createElement(InnerComponent, {classes, ...props}) | ||
| } | ||
|
|
||
| export default injectStyled |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import {PureComponent, createElement} from 'react' | ||
| import {getDynamicStyles} from 'jss' | ||
|
|
||
| import filterProps from './utils/filterProps' | ||
| import composeClasses from './utils/composeClasses' | ||
| import generateTagName from './utils/generateTagName' | ||
|
|
||
| import type { | ||
| JssStaticSheet, | ||
| JssDynamicSheet, | ||
| ComponentStyleType, | ||
| StyledElementPropsType | ||
| } from './types' | ||
|
|
||
| type StyledArgs = { | ||
| tagName: string, | ||
| elementStyle: ComponentStyleType, | ||
| mountSheets: Function | ||
| } | ||
|
|
||
| const styled = ({tagName, elementStyle, mountSheets}: StyledArgs) => { | ||
| const dynamicStyle = getDynamicStyles(elementStyle) | ||
| const staticTagName = generateTagName(tagName) | ||
|
|
||
| return class StyledElement extends PureComponent { | ||
| static tagName: string = tagName | ||
| static style: ComponentStyleType = elementStyle | ||
|
|
||
| props: StyledElementPropsType | ||
|
|
||
| dynamicTagName = '' | ||
| staticSheet: JssStaticSheet | ||
| dynamicSheet: JssDynamicSheet | ||
|
|
||
| constructor(props: StyledElementPropsType) { | ||
| super(props) | ||
| if (!this.dynamicTagName) { | ||
| this.dynamicTagName = generateTagName(tagName) | ||
| } | ||
| } | ||
|
|
||
| componentWillMount() { | ||
| Object.assign(this, mountSheets()) | ||
|
|
||
| if (!this.staticSheet.getRule(staticTagName)) { | ||
| this.staticSheet.addRule(staticTagName, elementStyle) | ||
| } | ||
|
|
||
| if (dynamicStyle && !this.dynamicSheet.getRule(this.dynamicTagName)) { | ||
| this.dynamicSheet | ||
| .detach() | ||
| .addRule(this.dynamicTagName, dynamicStyle) | ||
| this.dynamicSheet | ||
| .update(this.dynamicTagName, this.props) | ||
| .attach() | ||
| .link() | ||
| } | ||
| } | ||
|
|
||
| componentWillReceiveProps(nextProps: StyledElementPropsType) { | ||
| if (dynamicStyle) { | ||
| this.dynamicSheet.update(this.dynamicTagName, nextProps) | ||
| } | ||
| } | ||
|
|
||
| render() { | ||
| if (!this.staticSheet) return null | ||
|
|
||
| const {children, className, ...attrs} = this.props | ||
|
|
||
| const props = filterProps(attrs) | ||
| const tagClass = composeClasses( | ||
| this.staticSheet.classes[staticTagName], | ||
| this.dynamicSheet.classes[this.dynamicTagName], | ||
| className | ||
| ) | ||
|
|
||
| return createElement(tagName, {...props, className: tagClass}, children) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default styled |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why is this needed, where do we use such a global?
Uh oh!
There was an error while loading. Please reload this page.
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.
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