Skip to content

Commit f0df08f

Browse files
committed
Merge branch 'master' of github.com:cssinjs/jss
2 parents 7ed519f + 56e451e commit f0df08f

38 files changed

Lines changed: 1370 additions & 1845 deletions

changelog.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
- [jss] Add scoped keyframes support ([#346](https://github.com/cssinjs/jss/pull/346))
1616
- [jss] Function values and function rules support now fallbacks, media queries, nesting, global styles ([#682](https://github.com/cssinjs/jss/pull/682))
1717
- [react-jss] Remove old lifecycle hooks ([#834](https://github.com/cssinjs/jss/pull/834))
18+
- [react-jss] Move JssContext to new React Context, deprecate the `sheetOptions` prop on the JssProvider and support a `media` prop ([#924](https://github.com/cssinjs/jss/pull/924))
19+
- [react-jss] Upgrade to theming version 3 ([#942](https://github.com/cssinjs/jss/pull/942))
20+
- [react-jss] Add forwardRef support ([#943](https://github.com/cssinjs/jss/pull/943))
21+
- [react-jss] Extend classes instead of overwriting theme ([#946](https://github.com/cssinjs/jss/pull/946))
1822
- [react-jss] Add flow types ([#818](https://github.com/cssinjs/jss/pull/818))
19-
- [jss] Migrate to a monorepo using yarn workspaces and lerna ([#729](https://github.com/cssinjs/jss/pull/729))
23+
- [all] Migrate to a monorepo using yarn workspaces and lerna ([#729](https://github.com/cssinjs/jss/pull/729))
2024
- [all] Add TypeScript definitions to all packages ([#889](https://github.com/cssinjs/jss/pull/889))
2125
- [all] Use smaller version of the warning package ([#953](https://github.com/cssinjs/jss/pull/953))
2226

@@ -27,6 +31,7 @@
2731
- [jss] Rule @keyframes has now scoped name by default, which means that you can access it using `$ref` from the same sheet and generate global one as before using `@global` rule ([#346](https://github.com/cssinjs/jss/pull/346)).
2832
- [jss][react-jss] Options `createGenerateClassName` and `generateClassName` are renamed to `createGenerateId` and `generateId` because the same function is now used to scope @keyframes rules.
2933
- [react-jss] Drop support for older React versions, require v16.3 or higher ([#868](https://github.com/cssinjs/jss/pull/868), [#851](https://github.com/cssinjs/jss/pull/851))
34+
- [react-jss] Remove inject option ([#934](https://github.com/cssinjs/jss/pull/934))
3035

3136
## Pre v10 changelogs
3237

docs/react-jss.md

Lines changed: 90 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Benefits compared to the lower level core:
66

77
- Dynamic Theming - allows context based theme propagation and runtime updates.
88
- Critical CSS extraction - only CSS from rendered components gets extracted.
9-
- Lazy evaluation - Style Sheets are only created when a component gets mounted.
10-
- Static part of a Style Sheet is shared between all elements.
9+
- Lazy evaluation - Style Sheets are created when a component mounts and removed when it's unmounted.
10+
- The static part of a Style Sheet will be shared between all elements.
1111
- Function values and rules are updated automatically with props as an argument.
1212

1313
### Table of Contents
@@ -36,7 +36,7 @@ yarn add react-jss
3636
### Usage
3737

3838
React-JSS wraps your component with a [higher-order component](https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750).
39-
It injects a `classes` prop, which is a simple map of rule names and generated class names. It can act both as a simple wrapping function and as an [ES7 decorator](https://github.com/wycats/javascript-decorators).
39+
It injects a `classes` prop, which is a simple map of rule names and generated class names.
4040

4141
Try it out in the [playground](https://codesandbox.io/s/j3l06yyqpw).
4242

@@ -45,8 +45,7 @@ Try it out in the [playground](https://codesandbox.io/s/j3l06yyqpw).
4545
```javascript
4646
import React from 'react'
4747
import {render} from 'react-dom'
48-
// Import React-JSS
49-
import injectSheet from 'react-jss'
48+
import withStyles from 'react-jss'
5049

5150
// Create your Styles. Remember, since React-JSS uses the default preset,
5251
// most plugins are available without further configuration needed.
@@ -79,9 +78,9 @@ const Button = ({classes, children}) => (
7978
)
8079

8180
// Finally, inject the stylesheet into the component.
82-
const StyledButton = injectSheet(styles)(Button)
81+
const StyledButton = withStyles(styles)(Button)
8382
// You can also export the component with
84-
// export default injectSheet(styles)(Button)
83+
// export default withStyles(styles)(Button)
8584

8685
const App = () => <StyledButton>Submit</StyledButton>
8786

@@ -117,15 +116,15 @@ and
117116

118117
#### Dynamic values
119118

120-
You can use [function values](jss-syntax.md#function-values), function rules and observables out of the box. Function values and function rules will receive a props object once the component receives new props or mounts for the first time.
119+
You can use [function values](jss-syntax.md#function-values), Function rules and observables out of the box. Function values and function rules will receive a props object once the component receives new props or mounts for the first time.
121120

122121
Caveats:
123122

124123
Static properties being rendered first so that function values will have higher source order specificity.
125124

126125
```javascript
127126
import React from 'react'
128-
import injectSheet from 'react-jss'
127+
import withStyles from 'react-jss'
129128

130129
const styles = {
131130
myButton: {
@@ -151,7 +150,7 @@ Button.defaultProps = {
151150
labelColor: 'red'
152151
}
153152

154-
const StyledButton = injectSheet(styles)(Button)
153+
const StyledButton = withStyles(styles)(Button)
155154

156155
const App = () => <StyledButton fontStyle="italic">Submit</StyledButton>
157156
```
@@ -195,11 +194,11 @@ Usage of `ThemeProvider`:
195194
- If it is `Object` and used in a nested `ThemeProvider`, then it gets merged with the theme from a parent `ThemeProvider` and passed down the react tree.
196195
- If it is `Function` and used in a nested `ThemeProvider`, then it's being applied to the theme from a parent `ThemeProvider`. If the result is an `Object` it will be passed down the react tree, throws otherwise.
197196
- `ThemeProvider` as every other component can render only a single child because it uses `React.Children.only` in render and throws otherwise.
198-
- [Read more about `ThemeProvider` in `theming`'s documentation.](https://github.com/iamstarkov/theming#themeprovider)
197+
- [Read more about `ThemeProvider` in `theming`'s documentation.](https://github.com/cssinjs/theming#themeprovider)
199198

200199
```javascript
201200
import React from 'react'
202-
import injectSheet, {ThemeProvider} from 'react-jss'
201+
import withStyles, {ThemeProvider} from 'react-jss'
203202

204203
const Button = ({classes, children}) => (
205204
<button className={classes.button}>
@@ -216,7 +215,7 @@ const styles = theme => ({
216215
}
217216
})
218217

219-
const StyledButton = injectSheet(styles)(Button)
218+
const StyledButton = withStyles(styles)(Button)
220219

221220
const theme = {
222221
colorPrimary: 'green'
@@ -229,25 +228,62 @@ const App = () => (
229228
)
230229
```
231230

232-
In case you need to access the theme but not render any CSS, you can also use `withTheme`. It is a Higher-order Component factory which takes a `React.Component` and maps the theme object from context to props. [Read more about `withTheme` in `theming`'s documentation.](https://github.com/iamstarkov/theming#withthemecomponent)
231+
#### Accessing the theme inside the styled component
232+
233+
The theme will not be injecting into the wrapped component.
234+
To inject the theme into the wrapped component, pass the `injectTheme` option to `withStyles`.
235+
236+
```javascript
237+
import React from 'react'
238+
import withStyles from 'react-jss'
239+
240+
const DeleteIcon = () => null
241+
242+
const Button = ({classes, children, theme}) => (
243+
<button className={classes.button}>
244+
<span className={classes.label}>{children}</span>
245+
246+
{theme.useIconButtons && <DeleteIcon />}
247+
</button>
248+
)
249+
250+
const styles = theme => ({
251+
button: {
252+
background: theme.colorPrimary
253+
},
254+
label: {
255+
fontWeight: 'bold'
256+
}
257+
})
258+
259+
const StyledButton = withStyles(styles, {injectTheme: true})(Button)
260+
```
261+
262+
#### Accessing the theme without a styled component
263+
264+
In case you need to access the theme but not render any CSS, you can also use `withTheme`. It is a Higher-order Component factory which takes a `React.Component` and maps the theme object from context to props. [Read more about `withTheme` in `theming`'s documentation.](https://github.com/cssinjs/theming#withthemecomponent)
233265

234266
```javascript
235267
import React from 'react'
236-
import injectSheet, {withTheme} from 'react-jss'
268+
import {withTheme} from 'react-jss'
237269

238270
const Button = withTheme(({theme}) => <button>I can access {theme.colorPrimary}</button>)
239271
```
240272

273+
#### Using custom Theming Context
274+
241275
Use _namespaced_ themes so that a set of UI components gets no conflicts with another set of UI components from a different library also using `react-jss`.
242276

243277
```javascript
244278
import React from 'react'
245-
import injectSheet, {createTheming} from 'react-jss'
279+
import withStyles, {createTheming} from 'react-jss'
280+
281+
const ThemeContext = React.createContext({})
246282

247283
// Creating a namespaced theming object.
248-
const theming = createTheming('__MY_NAMESPACED_THEME__')
284+
const theming = createTheming(ThemeContext)
249285

250-
const {ThemeProvider: MyThemeProvider} = theming
286+
const {ThemeProvider} = theming
251287

252288
const styles = theme => ({
253289
button: {
@@ -261,8 +297,8 @@ const theme = {
261297

262298
const Button = ({classes, children}) => <button className={classes.button}>{children}</button>
263299

264-
// Passing namespaced theming object inside injectSheet options.
265-
const StyledButton = injectSheet(styles, {theming})(Button)
300+
// Passing namespaced theming object inside withStyles options.
301+
const StyledButton = withStyles(styles, {theming})(Button)
266302
const OtherLibraryThemeProvider = () => null
267303
const OtherLibraryComponent = () => null
268304
const otherLibraryTheme = {}
@@ -271,9 +307,9 @@ const otherLibraryTheme = {}
271307
const App = () => (
272308
<OtherLibraryThemeProvider theme={otherLibraryTheme}>
273309
<OtherLibraryComponent />
274-
<MyThemeProvider theme={theme}>
310+
<ThemeProvider theme={theme}>
275311
<StyledButton>Green Button</StyledButton>
276-
</MyThemeProvider>
312+
</ThemeProvider>
277313
</OtherLibraryThemeProvider>
278314
)
279315
```
@@ -297,7 +333,7 @@ export default function render(req, res) {
297333
</JssProvider>
298334
)
299335

300-
// Any instances of `injectSheet` within `<MyApp />` will have gotten sheets
336+
// Any instances of `withStyles` within `<MyApp />` will have gotten sheets
301337
// from `context` and added their Style Sheets to it by now.
302338

303339
return res.send(
@@ -318,8 +354,8 @@ export default function render(req, res) {
318354
For traversing the React tree outside of the HTML rendering, you should add `disableStylesGeneration` property.
319355

320356
```javascript
321-
import * as React from 'react'
322-
import * as ReactDOM from 'react-dom'
357+
import React from 'react'
358+
import ReactDOM from 'react-dom'
323359
import bootstrapper from 'react-async-bootstrapper'
324360

325361
import {JssProvider} from 'react-jss'
@@ -345,14 +381,14 @@ To reuse the same styles **and** the same generated style sheet between 2 entire
345381

346382
```javascript
347383
import React from 'react'
348-
import injectSheet from 'react-jss'
384+
import withStyles from 'react-jss'
349385

350386
const styles = {
351387
button: {
352388
color: 'red'
353389
}
354390
}
355-
const RedButton = injectSheet(styles)(({classes, children}) => (
391+
const RedButton = withStyles(styles)(({classes, children}) => (
356392
<button className={classes.button}>{children}</button>
357393
))
358394

@@ -373,21 +409,33 @@ Alternatively, you can create own Style Sheet and use the `composes` feature. Al
373409

374410
#### The inner component
375411

376-
```es6
412+
```javascript
413+
import withStyles from 'react-jss'
414+
377415
const InnerComponent = () => null
378-
const StyledComponent = injectSheet(styles, InnerComponent)
416+
const StyledComponent = withStyles({})(InnerComponent)
379417
console.log(StyledComponent.InnerComponent) // Prints out the inner component.
380418
```
381419

382420
#### The inner ref
383421

384-
To get a `ref` to the inner element, use the `innerRef` prop.
422+
To get a `ref` to the inner element, use the `ref` prop.
423+
We will forward the ref to the inner component.
385424

386-
```es6
387-
const InnerComponent = () => null
388-
const StyledComponent = injectSheet({})(InnerComponent)
425+
```javascript
426+
import React from 'react'
427+
import withStyles from 'react-jss'
389428

390-
<StyledComponent innerRef={(ref) => {console.log(ref)}} />
429+
const InnerComponent = () => null
430+
const StyledComponent = withStyles({})(InnerComponent)
431+
432+
const App = (
433+
<StyledComponent
434+
ref={ref => {
435+
console.log(ref)
436+
}}
437+
/>
438+
)
391439
```
392440

393441
#### Custom setup
@@ -473,7 +521,7 @@ You will need [babel-plugin-transform-decorators-legacy](https://github.com/loga
473521

474522
```javascript
475523
import React, {Component} from 'react'
476-
import injectSheet from 'react-jss'
524+
import withStyles from 'react-jss'
477525

478526
const styles = {
479527
button: {
@@ -484,7 +532,7 @@ const styles = {
484532
}
485533
}
486534

487-
@injectSheet(styles)
535+
@withStyles(styles)
488536
class Button extends Component {
489537
render() {
490538
const {classes, children} = this.props
@@ -501,42 +549,23 @@ export default Button
501549

502550
### Injection order
503551

504-
Injection of style tags happens in the same order as the `injectSheet()` invocation.
505-
Source order specificity is higher the lower style tag is in the tree. Therefore you should call `injectSheet` of components you want to override first.
552+
Injection of style tags happens in the same order as the `withStyles()` invocation.
553+
Source order specificity is higher the lower style tag is in the tree. Therefore you should call `withStyles` of components you want to override first.
506554

507555
Example
508556

509-
```js
557+
```javascript
510558
import React from 'react'
511-
import injectSheet from 'react-jss'
559+
import withStyles from 'react-jss'
512560

513561
const labelStyles = {}
514562
const buttonStyles = {}
515563

516564
// Will render labelStyles first.
517-
const Label = injectSheet(labelStyles)(({children}) => <label>{children}</label>)
518-
const Button = injectSheet(buttonStyles)(() => (
565+
const Label = withStyles(labelStyles)(({children}) => <label>{children}</label>)
566+
const Button = withStyles(buttonStyles)(() => (
519567
<button>
520568
<Label>my button</Label>
521569
</button>
522570
))
523571
```
524-
525-
### Whitelist injected props
526-
527-
By default "classes" and "theme" are going to be injected into the child component over props. Property `theme` is only passed when you use a function instead of styles object.
528-
If you want to whitelist some of them, you can now use option `inject`. E.g., if you want to access the StyleSheet instance, you need to pass `{inject: ['sheet']}` and it will be available as `props.sheet`.
529-
530-
All user props passed to the HOC will still be forwarded as usual.
531-
532-
```js
533-
import React from 'react'
534-
import injectSheet from 'react-jss'
535-
536-
const styles = {}
537-
538-
// Only `classes` prop will be passed by the ReactJSS HOC now. No `sheet` or `theme`.
539-
const Button = injectSheet(styles, {inject: ['classes', 'sheet']})(({classes}) => (
540-
<button>My button</button>
541-
))
542-
```

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@babel/preset-flow": "^7.0.0",
4747
"@babel/preset-react": "^7.0.0",
4848
"babel-loader": "^8.0.4",
49+
"babel-plugin-dev-expression": "^0.2.1",
4950
"camelcase": "^5.0.0",
5051
"chalk": "^2.4.1",
5152
"codecov": "^1.0.1",
@@ -77,7 +78,7 @@
7778
"prettier": "^1.13.5",
7879
"raf": "^3.4.0",
7980
"react": "^16.4.1",
80-
"react-dom": "^16.4.1",
81+
"react-test-renderer": "^16.7.0",
8182
"rimraf": "^2.5.4",
8283
"rollup": "^0.65.0",
8384
"rollup-plugin-babel": "^4.0.3",

packages/jss-plugin-camel-case/.size-snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"dist/jss-plugin-camel-case.js": {
3-
"bundled": 2226,
3+
"bundled": 2277,
44
"minified": 747,
55
"gzipped": 433
66
},
77
"dist/jss-plugin-camel-case.min.js": {
8-
"bundled": 2226,
8+
"bundled": 2277,
99
"minified": 747,
1010
"gzipped": 433
1111
},

0 commit comments

Comments
 (0)