Skip to content

Commit

Permalink
Update with-style-sheet example (vercel#13323)
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppeg authored and rokinsky committed Jul 11, 2020
1 parent 9d2cfb5 commit 156bf65
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 16 deletions.
8 changes: 0 additions & 8 deletions examples/with-style-sheet/.babelrc

This file was deleted.

23 changes: 23 additions & 0 deletions examples/with-style-sheet/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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'),
},
],
],
}
5 changes: 5 additions & 0 deletions examples/with-style-sheet/lib/styleSheet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const StyleSheet = require('style-sheet')
const setup = require('style-sheet/lib/cjs/createElement')

const stylePropName = 'css'
module.exports.createElement = setup(StyleSheet, stylePropName)
3 changes: 1 addition & 2 deletions examples/with-style-sheet/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ const { RawSource } = require('webpack-sources')

class StyleSheetPlugin {
apply(compiler) {
compiler.plugin('emit', (compilation, cb) => {
compiler.hooks.emit.tap('StyleSheetPlugin', (compilation) => {
compilation.assets['static/bundle.css'] = new RawSource(getCss())
cb()
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/with-style-sheet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"next": "latest",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"style-sheet": "^3.0.1"
"style-sheet": "^4.0.4"
},
"author": "Giuseppe Gurgone",
"license": "ISC"
Expand Down
21 changes: 16 additions & 5 deletions examples/with-style-sheet/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import React from 'react'
import { StyleSheet, StyleResolver } from 'style-sheet'
const cls = StyleResolver.resolve

export default function Home() {
const [color, setColor] = React.useState('#111')
React.useEffect(() => {
setTimeout(() => {
setColor('#00f')
}, 2000)
})
return (
<div className={cls([styles.root, styles.color])}>
<div
className={cls([styles.root, styles.another])}
css={{
color,
}}
>
<div>
Hello from <span className={cls(styles.brand)}>Next.js</span>
</div>
Expand All @@ -13,7 +25,7 @@ export default function Home() {

const styles = StyleSheet.create({
root: {
fontSize: 30,
fontSize: 16,
fontFamily: 'sans-serif',
display: 'flex',
alignItems: 'center',
Expand All @@ -23,9 +35,8 @@ const styles = StyleSheet.create({
'radial-gradient(circle, #D7D7D7, #D7D7D7 1px, #FFF 1px, #FFF)',
backgroundSize: '1em 1em',
},
color: {
// showcasing dynamic styles
color: Math.random() > 0.5 ? '#111' : '#222',
another: {
fontSize: 30,
},
brand: {
fontWeight: 'bold',
Expand Down

0 comments on commit 156bf65

Please sign in to comment.