Skip to content

Commit 846cb41

Browse files
committed
Merge branch 'master' into feature/improve-mount-performance
2 parents 8e57f91 + 8fbe4f9 commit 846cb41

18 files changed

Lines changed: 479 additions & 171 deletions

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'react',
55
],
66
plugins: [
7-
'transform-es2015-modules-commonjs',
7+
'transform-flow-strip-types',
88
'transform-class-properties',
99
['transform-object-rest-spread', { useBuiltIns: true }],
1010
]

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ parser: babel-eslint
33

44
env:
55
jest: true
6+
7+
globals:
8+
ReactClass: true

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
3+
node_js:
4+
- 7
5+
6+
after_success: npm run test:coverage && npm run coveralls

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Change Log
2+
3+
## [v0.5.0](https://github.com/cssinjs/styled-jss/tree/v0.5.0) (2017-04-24)
4+
[Full Changelog](https://github.com/cssinjs/styled-jss/compare/v0.4.0...v0.5.0)
5+
6+
**Implemented enhancements:**
7+
8+
- Implement injectStyled [\#6](https://github.com/cssinjs/styled-jss/pull/6) ([lttb](https://github.com/lttb))
9+
10+
## [v0.4.0](https://github.com/cssinjs/styled-jss/tree/v0.4.0) (2017-04-23)
11+
[Full Changelog](https://github.com/cssinjs/styled-jss/compare/v0.3.1...v0.4.0)
12+
13+
**Implemented enhancements:**
14+
15+
- Improved performance for dynamic styles update
16+
17+
18+
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*

README.md

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1+
<a href="https://github.com/cssinjs/styled-jss">
2+
<img alt="styled-jss" src="https://github.com/cssinjs/logo/blob/master/styled-jss-logo.png" height="150px" />
3+
</a>
4+
15
# Styled Components on top of JSS
26

3-
## Install
7+
[![Travis branch](https://img.shields.io/travis/cssinjs/styled-jss/master.svg?style=flat)](https://travis-ci.org/cssinjs/styled-jss)
8+
[![Coverage Status branch](https://img.shields.io/coveralls/cssinjs/styled-jss/master.svg?style=flat)](https://img.shields.io/coveralls/cssinjs/styled-jss/master.svg?branch=master)
9+
[![npm version](https://img.shields.io/npm/v/styled-jss.svg?style=flat)](https://www.npmjs.com/package/styled-jss)
10+
[![npm license](https://img.shields.io/npm/l/styled-jss.svg?style=flat)](https://www.npmjs.com/package/styled-jss)
411

5-
This has peer dependencies of `react` and `react-dom`, which will have to be installed as well.
12+
## Install
613

714
```sh
815
npm install --save styled-jss
916
```
1017

18+
Install peer dependencies `react` and `react-dom` in your project.
19+
20+
1121
## Usage
1222

13-
### With default styled function
23+
### With a default styled function
1424

1525
```js
1626
import styled from 'styled-jss'
@@ -26,21 +36,26 @@ const PrimaryButton = styled(Button, {
2636
})
2737
```
2838

29-
### With base Style Sheet
39+
### With a base Style Sheet
3040

31-
Using base Style Sheet we can share classes between styled primitives.
41+
Using base Style Sheet we can share classes between styled primitives and render function.
3242

3343
```js
34-
import { Styled } from 'styled-jss'
35-
import injectSheet from 'react-jss'
44+
import { Styled, injectStyled } from 'styled-jss'
3645

3746
// Base styles, like a regular jss object.
3847
const styled = Styled({
3948
root: {
40-
margin: 10
49+
margin: 10,
50+
'& $baseButton': {
51+
fontSize: 16
52+
}
4153
},
4254
baseButton: {
43-
padding: 10
55+
padding: 10,
56+
'& + &': {
57+
marginLeft: 10
58+
}
4459
}
4560
})
4661

@@ -58,28 +73,30 @@ const PrimaryButton = styled(NormalButton, {
5873
// One can use classes AND styled primitives.
5974
const MyComponent = ({classes}) => (
6075
<div className={classes.root}>
76+
<NormalButton>normal button</NormalButton>
6177
<PrimaryButton>primary button</PrimaryButton>
6278
</div>
6379
)
6480

65-
const MyStyledComponent = injectSheet(styled.styles)(MyComponent)
81+
const MyStyledComponent = injectStyled(styled)(MyComponent)
6682
```
6783

68-
### With custom JSS setup:
84+
### With custom JSS setup
85+
86+
`styled-jss` uses [jss-preset-default](https://github.com/cssinjs/jss-preset-default) by default. You can require `createStyled` function and provide your custom JSS instance.
6987

7088
```js
7189
import { create as createJss } from 'jss'
7290
import vendorPrefixer from 'jss-vendor-prefixer'
73-
74-
import { createStyled } from 'styled-jss'
91+
import createStyled from 'styled-jss/createStyled'
7592

7693
const jss = createJss()
7794
jss.use(vendorPrefixer())
7895

79-
// Create custom Styled, that allows to set BaseStyles
96+
// Create a custom Styled function, that allows to set BaseStyles.
8097
const Styled = createStyled(jss)
8198

82-
// Create custom styled function without BaseStyles accordingly
99+
// Create a custom styled function that allows to create styled components.
83100
export const styled = createStyled()
84101

85102
export default Styled

package.json

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "styled-jss",
3-
"version": "0.4.0",
3+
"version": "0.5.1",
44
"description": "Styled Components on top of JSS.",
5-
"main": "lib/index.js",
5+
"main": "index.js",
66
"scripts": {
77
"clean": "rimraf lib/*",
88
"lint": "eslint src",
@@ -11,17 +11,15 @@
1111
"test": "jest",
1212
"test:watch": "npm test -- --watch",
1313
"test:coverage": "npm test -- --coverage",
14+
"coveralls": "cat ./coverage/lcov.info | coveralls",
1415
"prebuild": "npm run clean && npm run flow && npm run lint && npm run test:coverage",
1516
"build": "babel src --out-dir lib --ignore tests",
17+
"postbuild": "npm run flow:csrc && copyfiles -e '**/tests/*' ./*.* LICENSE -a -f lib",
18+
"flow:csrc": "flow-copy-source -i '**/tests/*' src lib",
1619
"flow:typed": "flow-typed install",
1720
"flow:stub": "flow-typed create-stub",
18-
"preversion": "npm run build",
19-
"postversion": "git push --follow-tags && npm publish"
21+
"release": "npm run build && git push --follow-tags && npm publish lib"
2022
},
21-
"files": [
22-
"src",
23-
"lib"
24-
],
2523
"repository": {
2624
"type": "git",
2725
"url": "git+https://github.com/cssinjs/styled-jss.git"
@@ -54,17 +52,20 @@
5452
"babel-core": "^6.23.1",
5553
"babel-eslint": "^7.2.2",
5654
"babel-plugin-transform-class-properties": "^6.23.0",
57-
"babel-plugin-transform-es2015-modules-commonjs": "^6.23.0",
55+
"babel-plugin-transform-flow-strip-types": "^6.22.0",
5856
"babel-plugin-transform-object-rest-spread": "^6.23.0",
5957
"babel-preset-es2015": "^6.24.1",
6058
"babel-preset-react": "^6.23.0",
59+
"copyfiles": "^1.2.0",
60+
"coveralls": "^2.13.0",
6161
"eslint": "^3.13.0",
6262
"eslint-config-airbnb": "^14.1.0",
6363
"eslint-config-jss": "^3.0.0",
6464
"eslint-plugin-import": "^2.2.0",
6565
"eslint-plugin-jsx-a11y": "^4.0.0",
6666
"eslint-plugin-react": "^6.10.3",
6767
"flow-bin": "^0.44.2",
68+
"flow-copy-source": "^1.1.0",
6869
"flow-typed": "^2.0.0",
6970
"jest": "^18.1.0",
7071
"lint-staged": "^3.4.0",
@@ -80,5 +81,11 @@
8081
"git add"
8182
]
8283
},
83-
"pre-commit": "lint:staged"
84+
"pre-commit": [
85+
"lint:staged",
86+
"test"
87+
],
88+
"jest": {
89+
"rootDir": "src"
90+
}
8491
}

src/createStyled.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import styled from './styled'
2+
3+
import type {
4+
BaseStylesType,
5+
ComponentStyleType,
6+
StyledType,
7+
StyledElementAttrsType,
8+
StyledElementType,
9+
TagNameOrStyledElementType
10+
} from './types'
11+
12+
const createStyled = (jss: Function) => (
13+
baseStyles: BaseStylesType = {}
14+
): StyledType => {
15+
let staticSheet
16+
let dynamicSheet
17+
18+
const mountSheets = () => {
19+
if (!staticSheet) {
20+
staticSheet = jss.createStyleSheet(baseStyles, {
21+
meta: 'StaticBaseSheet',
22+
}).attach()
23+
24+
dynamicSheet = jss.createStyleSheet({}, {
25+
link: true,
26+
meta: 'DynamicComponentSheet',
27+
}).attach()
28+
}
29+
30+
return {staticSheet, dynamicSheet}
31+
}
32+
33+
return Object.assign((
34+
tagNameOrStyledElement: TagNameOrStyledElementType,
35+
ownStyle: ComponentStyleType
36+
): StyledElementType => {
37+
const {tagName, style}: StyledElementAttrsType = typeof tagNameOrStyledElement === 'string'
38+
? {tagName: tagNameOrStyledElement, style: {}}
39+
: tagNameOrStyledElement
40+
41+
const elementStyle = {...style, ...ownStyle}
42+
43+
return styled({tagName, baseStyles, elementStyle, mountSheets})
44+
}, {mountSheets, styles: baseStyles})
45+
}
46+
47+
export default createStyled

src/index.js

Lines changed: 6 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,11 @@
1-
import {PureComponent, createElement} from 'react'
2-
import {create as createJss, getDynamicStyles} from 'jss'
1+
import {create as createJss} from 'jss'
32
import preset from 'jss-preset-default'
4-
import raf from 'raf'
5-
import filterProps from './utils/filter-props'
63

7-
const jssDefault = createJss(preset())
4+
import createStyled from './createStyled'
85

9-
type StyledElementAttrsType = { tag: string, styles: Object }
10-
type StyledElementType = Function & StyledElementAttrsType
11-
type tagOrStyledElementTypeype = string | StyledElementType
12-
type StyledElementPropsType = {
13-
classes: Object,
14-
children: ?any,
15-
className: ?string,
16-
}
6+
const jss: Function = createJss(preset())
177

18-
const createStyled = (jss?: Function = jssDefault) => (baseStyles: Object = {}) => {
19-
let sheet
20-
let dynamicSheet
21-
let counter = 0
8+
export const Styled = createStyled(jss)
9+
export default Styled()
2210

23-
let dynamicCounter = 0
24-
raf(function update() {
25-
if (dynamicCounter) {
26-
dynamicCounter = 0
27-
dynamicSheet.attach().link()
28-
}
29-
raf(update)
30-
})
31-
32-
const styled = (
33-
tagOrStyledElement: tagOrStyledElementTypeype,
34-
ownStyles: Object
35-
): StyledElementType => {
36-
const {tag, styles}: StyledElementAttrsType = typeof tagOrStyledElement === 'string'
37-
? {tag: tagOrStyledElement, styles: {}}
38-
: tagOrStyledElement
39-
40-
const elementStyles = {...styles, ...ownStyles}
41-
const dynamicStyles = getDynamicStyles(elementStyles)
42-
const staticTag = `${tag}-${++counter}`
43-
44-
return class StyledElement extends PureComponent {
45-
static tag = tag
46-
47-
static styles = elementStyles
48-
49-
props: StyledElementPropsType
50-
51-
tagScoped = ''
52-
53-
constructor(props) {
54-
super(props)
55-
this.tagScoped = `${tag}-${++counter}`
56-
}
57-
58-
componentWillMount() {
59-
if (!sheet) {
60-
sheet = jss.createStyleSheet(baseStyles, {
61-
link: true,
62-
meta: 'StaticBaseSheet',
63-
}).attach()
64-
65-
dynamicSheet = jss.createStyleSheet({}, {
66-
link: true,
67-
meta: 'DynamicComponentSheet',
68-
}).attach()
69-
}
70-
71-
if (!sheet.getRule(staticTag)) {
72-
sheet.addRule(staticTag, elementStyles)
73-
}
74-
75-
if (dynamicStyles && !dynamicSheet.getRule(this.tagScoped)) {
76-
dynamicSheet.detach().addRule(this.tagScoped, dynamicStyles)
77-
dynamicSheet.update(this.tagScoped, this.props)
78-
dynamicCounter++
79-
}
80-
}
81-
82-
componentWillReceiveProps(nextProps: StyledElementPropsType) {
83-
if (dynamicStyles) {
84-
dynamicSheet.update(this.tagScoped, nextProps)
85-
}
86-
}
87-
88-
componentWillUnmount() {
89-
dynamicSheet.deleteRule(this.tagScoped)
90-
}
91-
92-
render() {
93-
if (!sheet) return null
94-
95-
const {children, className, ...attrs} = this.props
96-
97-
const props = filterProps(attrs)
98-
const tagClass = [
99-
sheet.classes[staticTag],
100-
dynamicSheet.classes[this.tagScoped],
101-
className,
102-
]
103-
.filter(Boolean)
104-
.join(' ')
105-
106-
return createElement(tag, {...props, className: tagClass}, children)
107-
}
108-
}
109-
}
110-
111-
return Object.assign(styled, {styles: baseStyles})
112-
}
113-
114-
const defaultStyledCreator = createStyled()
115-
const defaultStyled = defaultStyledCreator()
116-
117-
export {
118-
createStyled,
119-
defaultStyledCreator as Styled,
120-
}
121-
122-
export default defaultStyled
11+
export {default as injectStyled} from './injectStyled'

0 commit comments

Comments
 (0)