Skip to content

Commit dcad3d5

Browse files
committed
Add custom button states with emotion + add polished pkg
1 parent 2ff3c76 commit dcad3d5

File tree

13 files changed

+114
-23
lines changed

13 files changed

+114
-23
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ repo is built upon [example-nextjs-emotion11](https://github.com/Jareechang/exam
2121

2222
### Technologies
2323

24-
- emotion @ 11.0
25-
- material-ui @ 4.11
26-
- next @ 10.x
24+
- [emotion](https://emotion.sh/docs/@emotion/css) @ 11.0
25+
- [material-ui](https://material-ui.com/) @ 4.11
26+
- [next](https://nextjs.org/docs/getting-started) @ 10.x
27+
- [polished](https://polished.js.org/docs/) @ 4.x

components/button/Button.css.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { css, cx } from '@emotion/css'
2+
import { lighten } from 'polished';
3+
4+
export const common = css`
5+
padding: 1em 2em;
6+
`
7+
export const error = cx(
8+
common,
9+
css`
10+
background: #f44336;
11+
&:hover {
12+
background: ${lighten(0.2, '#f44336')};
13+
}
14+
`
15+
)
16+
17+
export const success = cx(
18+
common,
19+
css`
20+
background: lightgreen;
21+
&:hover {
22+
background: ${lighten(0.2, 'lightgreen')};
23+
}
24+
`
25+
)

components/button/Button.tsx

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import MuiButton from '@material-ui/core/Button'
2+
import Props from './types'
3+
import * as styles from './Button.css'
4+
import * as utils from './utils'
5+
6+
const defaultProps : Props = {
7+
type: ''
8+
}
9+
10+
const Button : React.FC<Props> = (props = defaultProps) => {
11+
return (
12+
<MuiButton
13+
className={utils.mapButtonTypeToStyle(props.type)}
14+
{...props}
15+
/>
16+
)
17+
}
18+
19+
export default Button

components/button/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default as Button} from './Button';

components/button/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Props {
2+
3+
}

components/button/utils.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as styles from './Button.css'
2+
3+
export const mapButtonTypeToStyle = (type) => {
4+
switch (type) {
5+
case 'error':
6+
return styles.error;
7+
case 'success':
8+
return styles.success;
9+
default:
10+
return styles.common;
11+
}
12+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"date-fns": "^2.11.1",
1818
"gray-matter": "^4.0.2",
1919
"next": "^10.0.0",
20+
"polished": "^4.0.3",
2021
"react": "16.13.1",
2122
"react-dom": "16.13.1",
2223
"remark": "^12.0.0",

pages/_app.tsx

+10-11
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ export default function App({ Component, pageProps }) {
1414
jssStyles.parentElement.removeChild(jssStyles);
1515
}
1616
}, []);
17-
18-
return (
19-
<StylesProvider injectFirst>
20-
<ThemeProvider theme={theme}>
21-
<CacheProvider value={cache}>
22-
{globalStyles}
23-
<Component {...pageProps} />
24-
</CacheProvider>
25-
</ThemeProvider >
26-
</StylesProvider>
27-
);
17+
return (
18+
<StylesProvider injectFirst>
19+
<ThemeProvider theme={theme}>
20+
<CacheProvider value={cache}>
21+
{globalStyles}
22+
<Component {...pageProps} />
23+
</CacheProvider>
24+
</ThemeProvider >
25+
</StylesProvider>
26+
);
2827
}

pages/_document.tsx

-3
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,15 @@ const { extractCritical } = createEmotionServer(cache)
99

1010
export default class MyDocument extends Document {
1111
static async getInitialProps(ctx) {
12-
1312
const sheets = new ServerStyleSheets()
1413
const originalRenderPage = ctx.renderPage
15-
1614
ctx.renderPage = () => {
1715
return (
1816
originalRenderPage({
1917
enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
2018
})
2119
)
2220
}
23-
2421
const initialProps = await Document.getInitialProps(ctx)
2522
const styles = extractCritical(initialProps.html)
2623
return {

pages/index.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
import Head from 'next/head'
22
import Link from 'next/link'
33
import { css, cx } from '@emotion/css'
4-
import Button from '@material-ui/core/Button';
4+
import Box from '@material-ui/core/Box';
5+
//import Button from '@material-ui/core/Button';
6+
import Typography from '@material-ui/core/Typography';
57
import Layout, { siteTitle } from '../components/layout'
68
import * as utilStyles from '../styles/utils.css'
79
import { getSortedPostsData } from '../lib/posts'
810
import Date from '../components/date'
11+
import { Button } from '../components/button';
912

1013
export default function Home({ allPostsData }) {
1114
return (
1215
<Layout home>
13-
<Button variant="contained" color="primary">Default</Button>
1416
<Head>
1517
<title>{siteTitle}</title>
1618
</Head>
1719
<section className={utilStyles.headingMd}>
18-
<p>[Your Self Introduction]</p>
19-
<p>
20+
<Typography variant="body1">[Your Self Introduction]</Typography>
21+
<Typography
22+
variant="body1">
2023
(This is a sample website - you’ll be building a site like this in{' '}
2124
<a href="https://nextjs.org/learn">our Next.js tutorial</a>.)
22-
</p>
25+
</Typography>
2326
</section>
27+
<Box
28+
py={3}
29+
display="flex"
30+
justifyContent="center">
31+
<Button
32+
type="error"
33+
variant="contained"
34+
color="primary">
35+
I’m a button
36+
</Button>
37+
</Box>
2438
<section className={cx(utilStyles.headingMd, utilStyles.padding1px)}>
2539
<h2 className={utilStyles.headingLg}>Blog</h2>
2640
<ul className={utilStyles.list}>

styles/material-ui/theme.ts

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ const theme = createMuiTheme({
1717
default: '#fff',
1818
},
1919
},
20+
typography: {
21+
body1: {
22+
fontFamily: `-apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
23+
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;`,
24+
padding: '1em 0',
25+
margin: 0,
26+
lineHeight: 1.6,
27+
fontSize: '18px'
28+
}
29+
30+
}
2031
});
2132

2233
export default theme;

styles/utils.css.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { css } from '@emotion/css'
22

3+
34
export const heading2Xl = css`
45
font-size: 2.5rem;
56
line-height: 1.2;

yarn.lock

+8-1
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@
10181018
dependencies:
10191019
regenerator-runtime "^0.13.4"
10201020

1021-
"@babel/runtime@^7.3.1", "@babel/runtime@^7.4.4", "@babel/runtime@^7.5.5", "@babel/runtime@^7.7.2", "@babel/runtime@^7.8.3", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7":
1021+
"@babel/runtime@^7.12.0", "@babel/runtime@^7.3.1", "@babel/runtime@^7.4.4", "@babel/runtime@^7.5.5", "@babel/runtime@^7.7.2", "@babel/runtime@^7.8.3", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7":
10221022
version "7.12.5"
10231023
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.12.5.tgz#410e7e487441e1b360c29be715d870d9b985882e"
10241024
integrity sha512-plcc+hbExy3McchJCEQG3knOsuh3HH+Prx1P6cLIkET/0dLuQDEnrT+s27Axgc9bqfsmNUNHfscgMUdBpC9xfg==
@@ -4680,6 +4680,13 @@ pnp-webpack-plugin@1.6.4:
46804680
dependencies:
46814681
ts-pnp "^1.1.6"
46824682

4683+
polished@^4.0.3:
4684+
version "4.0.3"
4685+
resolved "https://registry.yarnpkg.com/polished/-/polished-4.0.3.tgz#e2bde249f8884bdabc6997dd3eca225ee32f7a26"
4686+
integrity sha512-yOALHMrqcYHlHIxFkHvGX8NGsjsbSaYMZ8Z3uEElVpzF5ZgNmTgqMOh4bnSVXGC6sXjBTZCH6+GebTRWhJR4zQ==
4687+
dependencies:
4688+
"@babel/runtime" "^7.12.0"
4689+
46834690
popper.js@1.16.1-lts:
46844691
version "1.16.1-lts"
46854692
resolved "https://registry.yarnpkg.com/popper.js/-/popper.js-1.16.1-lts.tgz#cf6847b807da3799d80ee3d6d2f90df8a3f50b05"

0 commit comments

Comments
 (0)