Skip to content

Commit 2912127

Browse files
committed
react-jss docs improvements
1 parent 5925f4e commit 2912127

File tree

2 files changed

+28
-110
lines changed

2 files changed

+28
-110
lines changed

docs/intro.md

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ If you’re interested in playing around with JSS, you can use an online code pl
1010

1111
- [JSS](https://codesandbox.io/s/z21lpmvv33)
1212
- [React-JSS](https://codesandbox.io/s/j3l06yyqpw)
13-
- [Styled-JSS](https://codesandbox.io/s/xl89zx8zz4)
1413

1514
## Online Compiler
1615

@@ -82,11 +81,11 @@ document.body.innerHTML = `
8281
```javascript
8382
import React from 'react'
8483
import {render} from 'react-dom'
85-
import injectSheet from 'react-jss'
84+
import {createUseStyles} from 'react-jss'
8685

8786
// Create your Styles. Remember, since React-JSS uses the default preset,
8887
// most plugins are available without further configuration needed.
89-
const styles = {
88+
const useStyles = createUseStyles({
9089
myButton: {
9190
color: 'green',
9291
margin: {
@@ -104,55 +103,18 @@ const styles = {
104103
myLabel: {
105104
fontStyle: 'italic'
106105
}
107-
}
108-
109-
const Button = ({classes, children}) => (
110-
<button className={classes.myButton}>
111-
<span className={classes.myLabel}>{children}</span>
112-
</button>
113-
)
114-
115-
// Finally, inject the stylesheet into the component.
116-
const StyledButton = injectSheet(styles)(Button)
117-
118-
const App = () => <StyledButton>Submit</StyledButton>
119-
120-
render(<App />, document.getElementById('root'))
121-
```
122-
123-
## Styled-JSS Example
124-
125-
```javascript
126-
import React from 'react'
127-
import styled from 'styled-jss'
128-
129-
const Button = styled('button')({
130-
fontSize: 12,
131-
color: props => props.theme.textColor
132-
})
133-
134-
// You can also use curried interface this way.
135-
const div = styled('div')
136-
137-
const Container = div({
138-
padding: 20
139106
})
140107

141-
// Composition.
142-
const PrimaryButton = styled(Button)({
143-
color: 'red'
144-
})
108+
const Button = ({children}) => {
109+
const classes = useStyles()
110+
return (
111+
<button className={classes.myButton}>
112+
<span className={classes.myLabel}>{children}</span>
113+
</button>
114+
)
115+
}
145116

146-
// Composition with unstyled React Components too.
147-
const UnstyledButton = () => <button>Unstyled</button>
148-
const Button2 = styled(UnstyledButton)({
149-
color: 'blue'
150-
})
117+
const App = () => <Button>Submit</Button>
151118

152-
// Component Selectors.
153-
const ButtonContainer = styled(Container)({
154-
[`& ${PrimaryButton}`]: {
155-
color: 'green'
156-
}
157-
})
119+
render(<App />, document.getElementById('root'))
158120
```

docs/react-jss.md

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# JSS integration with React
22

3-
React-JSS integrates [JSS](https://github.com/cssinjs/jss) with React using the new Hooks API as well as a Styled Component API. JSS and the [default preset](https://github.com/cssinjs/jss/tree/master/packages/jss-preset-default) are already built in.
3+
React-JSS integrates [JSS](https://github.com/cssinjs/jss) with React using the new Hooks API. JSS and the [default preset](https://github.com/cssinjs/jss/tree/master/packages/jss-preset-default) are already built in.
44

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

7-
**HOC based API is deprecated as of v10 and will be removed in v11. HOC specific docs are available [here](./react-jss-hoc.md).**
7+
**HOC based API is deprecated as of v10 and will be removed in v11. You can still make a lazy migration like described [here](https://reacttraining.com/blog/using-hooks-in-classes/). HOC specific docs are available [here](./react-jss-hoc.md).**
88

9-
Benefits compared to using the core JSS package directly:
9+
### Benefits compared to using the core JSS package directly:
1010

1111
- Dynamic Theming - allows context based theme propagation and runtime updates.
1212
- Critical CSS extraction - only CSS from rendered components gets extracted.
1313
- Lazy evaluation - Style Sheets are created when a component mounts and removed when it's unmounted.
1414
- The static part of a Style Sheet will be shared between all elements.
15-
- Function values and rules are updated automatically with props as an argument.
15+
- Function values and rules are updated automatically with any data you pass to `useStyles(data)`. You can pass props, state or anything from context for example.
1616

1717
## Table of Contents
1818

@@ -196,17 +196,18 @@ Usage of `ThemeProvider`:
196196
import React from 'react'
197197
import {createUseStyles, useTheme, ThemeProvider} from 'react-jss'
198198

199-
const useStyles = createUseStyles(theme => ({
199+
const useStyles = createUseStyles({
200200
button: {
201-
background: theme.colorPrimary
201+
background: ({theme}) => theme.colorPrimary
202202
},
203203
label: {
204204
fontWeight: 'bold'
205205
}
206-
}))
206+
})
207207

208208
const Button = ({children, ...props}) => {
209-
const classes = useStyles(props)
209+
const theme = useTheme()
210+
const classes = useStyles({...props, theme})
210211
return (
211212
<button className={classes.button}>
212213
<span className={classes.label}>{children}</span>
@@ -225,51 +226,6 @@ const App = () => (
225226
)
226227
```
227228

228-
## Accessing the theme inside the styled component
229-
230-
Use `useTheme()` hook to access the theme inside of the function component.
231-
232-
```javascript
233-
import React from 'react'
234-
import {createUseStyles, useTheme, ThemeProvider} from 'react-jss'
235-
236-
const useStyles = createUseStyles(theme => ({
237-
button: {
238-
background: theme.colorPrimary
239-
},
240-
label: {
241-
fontWeight: 'bold'
242-
}
243-
}))
244-
245-
const DeleteIcon = () => null
246-
247-
const Button = ({children, ...props}) => {
248-
const classes = useStyles(props)
249-
const theme = useTheme()
250-
return (
251-
<button className={classes.button}>
252-
<span className={classes.label}>{children}</span>
253-
{theme.useIconButtons && <DeleteIcon />}
254-
</button>
255-
)
256-
}
257-
```
258-
259-
## Accessing the theme without styles
260-
261-
In case you need to access the theme without rendering any CSS, you can also use `useTheme()` standalone.
262-
263-
```javascript
264-
import React from 'react'
265-
import {useTheme} from 'react-jss'
266-
267-
const Button = () => {
268-
const theme = useTheme()
269-
return <button>I can access {theme.colorPrimary}</button>
270-
}
271-
```
272-
273229
## Using custom Theming Context
274230

275231
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` or in case you want to use the same theme from another context that is already used in your app.
@@ -287,22 +243,22 @@ const theming = createTheming(ThemeContext)
287243
const {ThemeProvider, useTheme} = theming
288244

289245
const useStyles = createUseStyles(
290-
theme => ({
246+
{
291247
button: {
292-
background: theme.colorPrimary
248+
background: ({theme}) => theme.colorPrimary
293249
}
294250
// Passing theming object to `createUseStyles()`
295-
}),
251+
},
296252
{theming}
297253
)
298254

299-
const theme = {
255+
const myTheme = {
300256
colorPrimary: 'green'
301257
}
302258

303259
const Button = ({children, ...props}) => {
304-
const classes = useStyles(props)
305-
const themeOverContext = useTheme() // In case you need to access the theme here.
260+
const theme = useTheme()
261+
const classes = useStyles({...props, theme})
306262
return <button className={classes.button}>{children}</button>
307263
}
308264

@@ -314,7 +270,7 @@ const otherLibraryTheme = {}
314270
const App = () => (
315271
<OtherLibraryThemeProvider theme={otherLibraryTheme}>
316272
<OtherLibraryComponent />
317-
<ThemeProvider theme={theme}>
273+
<ThemeProvider theme={myTheme}>
318274
<Button>Green Button</Button>
319275
</ThemeProvider>
320276
</OtherLibraryThemeProvider>

0 commit comments

Comments
 (0)