Skip to content

Commit ece841c

Browse files
committed
use styled-components to replace scss
1 parent 9e199b0 commit ece841c

File tree

9 files changed

+272
-486
lines changed

9 files changed

+272
-486
lines changed

.babelrc

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
],
55
"plugins": [
66
"inline-dotenv",
7+
"styled-components",
78
["module-resolver", {
89
"root": ["./src"]
910
}]

next.config.js

-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const assetPrefix = ASSET_HOST || ''
55

66
module.exports = {
77
assetPrefix,
8-
target: 'serverless',
98
webpack: (config, { dev }) => {
109
config.output.publicPath = `${assetPrefix}${config.output.publicPath}`
1110

@@ -18,21 +17,6 @@ module.exports = {
1817
}))
1918
}
2019

21-
config.module.rules.push({
22-
test: /\.scss/,
23-
use: [{
24-
loader: 'emit-file-loader',
25-
options: {
26-
name: 'dist/[path][name].[ext]'
27-
}
28-
},
29-
'babel-loader',
30-
'styled-jsx-css-loader', {
31-
loader: 'sass-loader',
32-
options: { sourceMap: dev }
33-
}]
34-
})
35-
3620
return config
3721
}
3822
}

package.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
"next": "^8.1.0",
1010
"next-redux-wrapper": "^2.1.0",
1111
"next-routes": "^1.4.2",
12-
"node-sass": "^4.9.3",
13-
"normalize-scss": "^7.0.1",
1412
"prop-types": "^15.6.1",
1513
"react": "^16.8.1",
1614
"react-dom": "^16.8.1",
1715
"react-helmet": "^5.2.0",
1816
"react-redux": "^6.0.0",
1917
"redux": "^4.0.1",
2018
"redux-logger": "^3.0.6",
21-
"redux-thunk": "^2.3.0"
19+
"redux-thunk": "^2.3.0",
20+
"styled-components": "^4.2.0",
21+
"styled-reset": "^2.0.13"
2222
},
2323
"name": "nextjs-redux-starter",
2424
"version": "1.0.0",
@@ -28,7 +28,6 @@
2828
"babel-core": "7.0.0-bridge.0",
2929
"babel-eslint": "^10.0.1",
3030
"babel-jest": "^24.0.0",
31-
"babel-loader": "^8.0.5",
3231
"babel-plugin-inline-dotenv": "^1.1.2",
3332
"babel-plugin-module-resolver": "^3.2.0",
3433
"enzyme": "^3.9.0",
@@ -43,7 +42,6 @@
4342
"sass-loader": "^7.1.0",
4443
"snazzy": "^8.0.0",
4544
"standard": "^12.0.1",
46-
"styled-jsx-css-loader": "^0.3.0",
4745
"webpack-bundle-analyzer": "^3.3.2"
4846
},
4947
"scripts": {

pages/_app.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import withRedux from 'next-redux-wrapper'
2+
import { ThemeProvider, createGlobalStyle } from 'styled-components'
3+
import reset from 'styled-reset'
24
import { withRouter } from 'next/router'
35
import { Provider } from 'react-redux'
46
import App, { Container } from 'next/app'
57
import Layout from 'components/Layout'
68
import createStore from 'store/createStore'
79

10+
const GlobalStyle = createGlobalStyle`
11+
${reset}
12+
`
13+
const theme = {
14+
colors: {
15+
primary: '#003648'
16+
}
17+
}
18+
819
class MyApp extends App {
920
static async getInitialProps ({ Component, ctx }) {
1021
return {
@@ -17,11 +28,14 @@ class MyApp extends App {
1728
const { Component, pageProps, store, router } = this.props
1829
return (
1930
<Container>
20-
<Provider store={store}>
21-
<Layout>
22-
<Component router={router} {...pageProps} />
23-
</Layout>
24-
</Provider>
31+
<ThemeProvider theme={theme}>
32+
<Provider store={store}>
33+
<GlobalStyle />
34+
<Layout>
35+
<Component router={router} {...pageProps} />
36+
</Layout>
37+
</Provider>
38+
</ThemeProvider>
2539
</Container>
2640
)
2741
}

pages/_document.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
import Document, { Head, Main, NextScript } from 'next/document'
2+
import { ServerStyleSheet } from 'styled-components'
23
import Helmet from 'react-helmet'
3-
import styles from 'styles/base.scss'
44

55
// from https://github.com/zeit/next.js/edit/canary/examples/with-react-helmet/pages/_document.js
66
export default class extends Document {
7-
static async getInitialProps (...args) {
8-
const documentProps = await super.getInitialProps(...args)
9-
// see https://github.com/nfl/react-helmet#server-usage for more information
10-
// 'head' was occupied by 'renderPage().head', we cannot use it
11-
return { ...documentProps, helmet: Helmet.renderStatic() }
7+
static async getInitialProps (ctx) {
8+
const sheet = new ServerStyleSheet()
9+
const originalRenderPage = ctx.renderPage
10+
try {
11+
ctx.renderPage = () =>
12+
originalRenderPage({
13+
enhanceApp: App => props => sheet.collectStyles(<App {...props} />)
14+
})
15+
16+
const documentProps = await Document.getInitialProps(ctx)
17+
return {
18+
...documentProps,
19+
helmet: Helmet.renderStatic(),
20+
styles: (
21+
<>
22+
{documentProps.styles}
23+
{sheet.getStyleElement()}
24+
</>
25+
)
26+
}
27+
} finally {
28+
sheet.seal()
29+
}
1230
}
1331

1432
get helmetHtmlAttrComponents () {
@@ -44,7 +62,6 @@ export default class extends Document {
4462
{ this.helmetHeadComponents }
4563
</Head>
4664
<body {...this.helmetBodyAttrComponents}>
47-
<style>{styles}</style>
4865
<Main />
4966
<NextScript />
5067
</body>

src/components/SearchResults.js

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
11
import React from 'react'
2+
import styled from 'styled-components'
23
import { Map } from 'immutable'
34
import PropTypes from 'prop-types'
4-
import styles from 'styles/SearchResults.scss'
5+
6+
const Container = styled.div`
7+
width: 300px;
8+
padding: 1em;
9+
margin: 1em auto;
10+
`
11+
const Item = styled.li`
12+
background: #eee;
13+
padding: 4px 12px;
14+
:hover {
15+
a {
16+
color: #eee;
17+
}
18+
background: ${props => props.theme.colors.primary};
19+
}
20+
`
521

622
const REPO_COUNT = 10
723
const SearchResults = ({ repos }) => {
824
return (
9-
<div className='container'>
25+
<Container>
1026
<h2>Top { REPO_COUNT } { repos.get('lang') } repos</h2>
1127
<small>{ repos.get('totalCount').toLocaleString() } repos found</small>
1228
<ul>
1329
{
1430
repos.get('items').take(REPO_COUNT).map(item => (
15-
<li key={item.get('id')}>
31+
<Item key={item.get('id')}>
1632
<a href={item.get('htmlUrl')} target='_blank'>
1733
{ item.get('name') }
1834
</a>
19-
</li>
35+
</Item>
2036
))
2137
}
2238
</ul>
23-
<style jsx>{styles}</style>
24-
</div>
39+
</Container>
2540
)
2641
}
2742

src/styles/SearchResults.scss

-14
This file was deleted.

src/styles/base.scss

-1
This file was deleted.

0 commit comments

Comments
 (0)