Skip to content

Commit 7a80d1a

Browse files
tajoDSchau
authored andcommitted
feat(gatsby-plugin-styletron): update to v5, debug mode, useStyletron (#13955)
* feat(gatsby-plugin-styletron): update to v5, debug mode, useStyletron hook BREAKING CHANGE: styletron.css() removed, useStyletron as a native replacement * feat(gatsby-plugin-styletron): debug mode can be disabled through the config * feat(gatsby-plugin-styletron): add note about styletron-react v5 support
1 parent b96c333 commit 7a80d1a

File tree

5 files changed

+44
-88
lines changed

5 files changed

+44
-88
lines changed
Lines changed: 24 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# gatsby-plugin-styletron
22

33
A [Gatsby](https://github.com/gatsbyjs/gatsby) plugin for
4-
[styletron](https://github.com/rtsao/styletron) with built-in server-side
4+
[styletron](https://github.com/styletron/styletron) with built-in server-side
55
rendering support.
66

7+
**This plugin supports `styletron-react` v5.** Check [the release notes](https://github.com/styletron/styletron/releases/tag/styletron-react%405.0.0) for more details about v5.
8+
79
## Install
810

9-
`npm install --dev gatsby-plugin-styletron`
11+
```sh
12+
npm install gatsby-plugin-styletron
13+
```
1014

1115
## How to use
1216

@@ -16,98 +20,41 @@ Edit `gatsby-config.js`
1620
module.exports = {
1721
plugins: [
1822
{
19-
resolve: "gatsby-plugin-styletron",
23+
resolve: `gatsby-plugin-styletron`,
2024
options: {
2125
// You can pass options to Styletron.
2226
prefix: "_",
27+
// Disable dev debug mode, enabled by default
28+
debug: false,
2329
},
2430
},
2531
],
2632
}
2733
```
2834

29-
This can be used as described by [styletron-react](https://github.com/rtsao/styletron/tree/master/packages/styletron-react) such as:
30-
31-
```javascript
32-
import React from "react"
33-
import { styled, withStyle } from "styletron-react"
34-
35-
const RedAnchor = styled("a", { color: "red" })
36-
const FancyAnchor = withStyle(RedAnchor, { fontFamily: "cursive" })
37-
38-
export default () => <FancyAnchor>Hi!</FancyAnchor>
39-
```
40-
41-
Or, using the `css` convenience function:
42-
43-
```javascript
44-
import React from "react"
45-
import styletron from "gatsby-plugin-styletron"
46-
47-
const styles = {
48-
fontFamily: "cursive",
49-
color: "blue",
50-
}
51-
52-
export default props => {
53-
const css = styletron().css
54-
return <div className={css({ backgroundColor: "#fcc", ...styles })}>Hi!</div>
55-
}
56-
```
57-
58-
Or, crazy flexible combinations:
35+
This can be used as described by [styletron-react](https://github.com/styletron/styletron/tree/master/packages/styletron-react) such as:
5936

60-
```javascript
37+
```jsx
6138
import React from "react"
62-
import { styled, withStyle } from "styletron-react"
63-
import styletron from "gatsby-plugin-styletron"
64-
65-
const fancyStyles = {
66-
":hover": {
67-
backgroundColor: "papayawhip",
68-
},
69-
"@media (max-width: 768px)": {
70-
":hover": {
71-
animationDuration: "3s",
72-
animationFillMode: "forwards",
73-
animationName: {
74-
"0%": {
75-
transform: "translate3d(0,0,0)",
76-
},
77-
to: {
78-
transform: "translate3d(100%,0,0)",
79-
},
80-
},
81-
willChange: "transform",
82-
},
83-
fontFamily: {
84-
fontStyle: "normal",
85-
fontWeight: "normal",
86-
src:
87-
"url(https://fonts.gstatic.com/s/inconsolata/v16/QldKNThLqRwH-OJ1UHjlKGlW5qhExfHwNJU.woff2) format(woff2)",
88-
},
89-
fontSize: "24px",
90-
},
91-
fontSize: "36px",
92-
}
93-
94-
const divStyles = {
95-
border: "1px dashed #333",
96-
}
39+
import { styled, useStyletron } from "styletron-react"
9740

41+
// statically styled component
9842
const RedAnchor = styled("a", { color: "red" })
99-
const FancyAnchor = withStyle(RedAnchor, { fontFamily: "cursive" })
10043

101-
export default () => {
102-
const css = styletron().css
44+
// dynamically styled component
45+
const BigAnchor = styled("a", ({ $size }) => ({ fontSize: `${$size}px` }))
10346

47+
const IndexPage = () => {
48+
// an alternative hook based API
49+
const [css] = useStyletron()
10450
return (
105-
<div
106-
className={css({ backgroundColor: "#cff", ...divStyles, ...fancyStyles })}
107-
>
108-
<FancyAnchor>Hi!</FancyAnchor>
109-
<div className={css(fancyStyles)}>Cool huh?</div>
51+
<div>
52+
<RedAnchor>Red Anchor</RedAnchor>
53+
<BigAnchor $size={64}>Big Anchor</BigAnchor>
54+
<p className={css({ color: "blue" })}>blue text</p>
11055
</div>
11156
)
11257
}
11358
```
59+
60+
Check more documentation and examples at [styletron.org](https://www.styletron.org/).

packages/gatsby-plugin-styletron/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
},
99
"dependencies": {
1010
"@babel/runtime": "^7.0.0",
11-
"styletron-engine-atomic": "^1.0.8",
12-
"styletron-react": "^4",
13-
"styletron-standard": "^1.0.6"
11+
"styletron-engine-atomic": "^1.1.0",
12+
"styletron-react": "^5.0.1"
1413
},
1514
"devDependencies": {
1615
"@babel/cli": "^7.0.0",
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
const React = require(`react`)
22
const styletron = require(`./index`)
3-
const { Provider } = require(`styletron-react`)
3+
const { Provider, DebugEngine } = require(`styletron-react`)
4+
5+
const debug = process.env.NODE_ENV === `production` ? void 0 : new DebugEngine()
46

57
// eslint-disable-next-line react/prop-types
6-
exports.wrapRootElement = ({ element }, options) => (
7-
<Provider value={styletron(options).instance}>{element}</Provider>
8-
)
8+
exports.wrapRootElement = ({ element }, options) => {
9+
const enableDebug =
10+
options.debug === true || typeof options.debug === `undefined`
11+
return (
12+
<Provider
13+
value={styletron(options).instance}
14+
debug={enableDebug ? debug : undefined}
15+
debugAfterHydration={enableDebug}
16+
>
17+
{element}
18+
</Provider>
19+
)
20+
}

packages/gatsby-plugin-styletron/src/gatsby-ssr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ exports.wrapRootElement = ({ element }, options) => (
77
<Provider value={styletron(options).instance}>{element}</Provider>
88
)
99

10-
exports.onRenderBody = ({ bodyComponent, setHeadComponents }, options) => {
10+
exports.onRenderBody = ({ setHeadComponents }, options) => {
1111
const instance = styletron(options).instance
12-
1312
const stylesheets = instance.getStylesheets()
1413
const headComponents = stylesheets[0].css
1514
? stylesheets.map((sheet, index) => (
@@ -20,6 +19,7 @@ exports.onRenderBody = ({ bodyComponent, setHeadComponents }, options) => {
2019
}}
2120
key={index}
2221
media={sheet.attrs.media}
22+
data-hydrate={sheet.attrs[`data-hydrate`]}
2323
/>
2424
))
2525
: null

packages/gatsby-plugin-styletron/src/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { Client, Server } = require(`styletron-engine-atomic`)
2-
const { driver } = require(`styletron-standard`)
32

43
let memoizedValue
54

@@ -15,7 +14,6 @@ module.exports = (() => options => {
1514
instance = new Server(options)
1615
}
1716
memoizedValue = {
18-
css: json => driver(json, instance),
1917
instance,
2018
}
2119
}

0 commit comments

Comments
 (0)