Unverified Commit 73c8b8ca authored by Giuseppe's avatar Giuseppe Committed by GitHub

Update with-style-sheet example (#13323)

parent f3ea363d
{
"presets": [
"next/babel"
],
"plugins": [
"style-sheet/babel"
]
}
const path = require('path')
module.exports = {
presets: [
[
'next/babel',
{
'preset-react': {
pragma: 'createElement',
},
},
],
],
plugins: [
[
'style-sheet/babel',
{
stylePropName: 'css',
stylePropPackageName: path.resolve('./lib/styleSheet.js'),
},
],
],
}
const StyleSheet = require('style-sheet')
const setup = require('style-sheet/lib/cjs/createElement')
const stylePropName = 'css'
module.exports.createElement = setup(StyleSheet, stylePropName)
...@@ -3,9 +3,8 @@ const { RawSource } = require('webpack-sources') ...@@ -3,9 +3,8 @@ const { RawSource } = require('webpack-sources')
class StyleSheetPlugin { class StyleSheetPlugin {
apply(compiler) { apply(compiler) {
compiler.plugin('emit', (compilation, cb) => { compiler.hooks.emit.tap('StyleSheetPlugin', (compilation) => {
compilation.assets['static/bundle.css'] = new RawSource(getCss()) compilation.assets['static/bundle.css'] = new RawSource(getCss())
cb()
}) })
} }
} }
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
"next": "latest", "next": "latest",
"react": "^16.7.0", "react": "^16.7.0",
"react-dom": "^16.7.0", "react-dom": "^16.7.0",
"style-sheet": "^3.0.1" "style-sheet": "^4.0.4"
}, },
"author": "Giuseppe Gurgone", "author": "Giuseppe Gurgone",
"license": "ISC" "license": "ISC"
......
import React from 'react'
import { StyleSheet, StyleResolver } from 'style-sheet' import { StyleSheet, StyleResolver } from 'style-sheet'
const cls = StyleResolver.resolve const cls = StyleResolver.resolve
export default function Home() { export default function Home() {
const [color, setColor] = React.useState('#111')
React.useEffect(() => {
setTimeout(() => {
setColor('#00f')
}, 2000)
})
return ( return (
<div className={cls([styles.root, styles.color])}> <div
className={cls([styles.root, styles.another])}
css={{
color,
}}
>
<div> <div>
Hello from <span className={cls(styles.brand)}>Next.js</span> Hello from <span className={cls(styles.brand)}>Next.js</span>
</div> </div>
...@@ -13,7 +25,7 @@ export default function Home() { ...@@ -13,7 +25,7 @@ export default function Home() {
const styles = StyleSheet.create({ const styles = StyleSheet.create({
root: { root: {
fontSize: 30, fontSize: 16,
fontFamily: 'sans-serif', fontFamily: 'sans-serif',
display: 'flex', display: 'flex',
alignItems: 'center', alignItems: 'center',
...@@ -23,9 +35,8 @@ const styles = StyleSheet.create({ ...@@ -23,9 +35,8 @@ const styles = StyleSheet.create({
'radial-gradient(circle, #D7D7D7, #D7D7D7 1px, #FFF 1px, #FFF)', 'radial-gradient(circle, #D7D7D7, #D7D7D7 1px, #FFF 1px, #FFF)',
backgroundSize: '1em 1em', backgroundSize: '1em 1em',
}, },
color: { another: {
// showcasing dynamic styles fontSize: 30,
color: Math.random() > 0.5 ? '#111' : '#222',
}, },
brand: { brand: {
fontWeight: 'bold', fontWeight: 'bold',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment