Skip to content

Commit f2f2b46

Browse files
author
Thomas Roger Lux
committed
Add material ui component library
1 parent 37157aa commit f2f2b46

File tree

8 files changed

+166
-16
lines changed

8 files changed

+166
-16
lines changed

Diff for: package.json

+3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
"start": "node build/server.js"
1111
},
1212
"dependencies": {
13+
"@material-ui/core": "^3.1.1",
1314
"express": "^4.16.3",
1415
"react": "^16.4.1",
1516
"react-dom": "^16.4.1",
17+
"react-jss": "^8.6.1",
1618
"react-redux": "^5.0.7",
1719
"react-router-dom": "^4.3.1",
1820
"redux": "^4.0.0"
@@ -21,6 +23,7 @@
2123
"@types/express": "^4.16.0",
2224
"@types/react": "^16.4.1",
2325
"@types/react-dom": "^16.0.6",
26+
"@types/react-jss": "^8.6.0",
2427
"@types/react-redux": "^6.0.2",
2528
"@types/react-router-dom": "^4.3.1",
2629
"nodemon": "^1.18.4",

Diff for: src/client/index.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ import * as ReactDOM from "react-dom";
33
import * as Redux from "redux";
44
import { Provider as ReduxProvider } from "react-redux";
55
import { BrowserRouter as Router } from "react-router-dom";
6+
import { JssProvider } from "react-jss";
7+
import {
8+
MuiThemeProvider,
9+
createGenerateClassName
10+
} from "@material-ui/core/styles";
611

712
import App from "common/App";
13+
import theme from "common/theme";
814
import { changeTitle } from "common/redux/reducer/title";
915

1016
const preloadedState = (window as any)["__PRELOADED_STATE__"];
@@ -15,7 +21,11 @@ const store = Redux.createStore(changeTitle, preloadedState);
1521
ReactDOM.hydrate(
1622
<ReduxProvider store={store}>
1723
<Router>
18-
<App />
24+
<JssProvider generateClassName={createGenerateClassName()}>
25+
<MuiThemeProvider theme={theme}>
26+
<App />
27+
</MuiThemeProvider>
28+
</JssProvider>
1929
</Router>
2030
</ReduxProvider>,
2131
document.getElementById("root")

Diff for: src/common/component/AppBar/AppBar.component.tsx

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import * as React from "react";
2+
import {
3+
AppBar as BaseAppBar,
4+
Typography,
5+
Button,
6+
Grid,
7+
Toolbar,
8+
withStyles
9+
} from "@material-ui/core";
10+
11+
import style from "./AppBar.style";
12+
13+
interface AppBarProps {
14+
classes: any;
15+
}
16+
17+
class AppBar extends React.Component<AppBarProps> {
18+
constructor(props: AppBarProps) {
19+
super(props);
20+
}
21+
22+
public render() {
23+
const { classes } = this.props;
24+
25+
return (
26+
<React.Fragment>
27+
<BaseAppBar>
28+
<Toolbar>
29+
<Grid container className={classes.gridContainer}>
30+
<Grid
31+
item
32+
xs
33+
className={classes.menuButtonContainer}
34+
>
35+
<Button
36+
color="secondary"
37+
variant="contained"
38+
className={classes.menuButton}
39+
>
40+
Menu
41+
</Button>
42+
</Grid>
43+
<Grid
44+
item
45+
container
46+
xs
47+
className={classes.titleContainer}
48+
>
49+
<Typography
50+
variant="title"
51+
className={classes.title}
52+
>
53+
Hello World!
54+
</Typography>
55+
</Grid>
56+
<Grid
57+
item
58+
container
59+
xs
60+
className={classes.loginButtonContainer}
61+
>
62+
<Button
63+
color="secondary"
64+
variant="contained"
65+
className={classes.loginButton}
66+
>
67+
Login
68+
</Button>
69+
</Grid>
70+
</Grid>
71+
</Toolbar>
72+
</BaseAppBar>
73+
</React.Fragment>
74+
);
75+
}
76+
}
77+
78+
export default withStyles(style)(AppBar);

Diff for: src/common/component/AppBar/AppBar.style.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createStyles } from "@material-ui/core";
2+
3+
const style = createStyles({
4+
gridContainer: {},
5+
menuButtonContainer: {
6+
justifyContent: "flex-start"
7+
},
8+
menuButton: {},
9+
titleContainer: {
10+
justifyContent: "center"
11+
},
12+
title: {
13+
margin: "auto"
14+
},
15+
loginButtonContainer: {
16+
justifyContent: "flex-end"
17+
},
18+
loginButton: {}
19+
});
20+
21+
export default style;

Diff for: src/common/container/Home.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { connect } from "react-redux";
33

44
import { Store } from "common/redux/store";
55
import { changeTitle } from "common/redux/action";
6+
import AppBar from "common/component/AppBar/AppBar.component";
67

78
interface HomeProps {
89
title: string;
@@ -36,9 +37,9 @@ class Home extends React.Component<HomeProps> {
3637

3738
public render() {
3839
return (
39-
<div className="Home">
40-
Home page
41-
</div>
40+
<React.Fragment>
41+
<AppBar />
42+
</React.Fragment>
4243
);
4344
}
4445
}

Diff for: src/common/theme.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createMuiTheme } from "@material-ui/core";
2+
3+
const theme = createMuiTheme({
4+
palette: {
5+
primary: {
6+
main: "#ffd54f",
7+
light: "#ffff81",
8+
dark: "#c8a415",
9+
contrastText: "#212121"
10+
},
11+
secondary: {
12+
main: "#7986cb",
13+
light: "#aab6fe",
14+
dark: "#49599a",
15+
contrastText: "#fafafa"
16+
},
17+
type: "light"
18+
}
19+
});
20+
21+
export default theme;

Diff for: src/server/index.tsx

+26-11
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import * as ReactDOM from "react-dom/server";
33
import * as Express from "express";
44
import * as Redux from "redux";
55
import { Provider as ReduxProvider } from "react-redux";
6-
import { ServerStyleSheet } from "styled-components";
76
import { StaticRouter as Router } from "react-router-dom";
7+
import { SheetsRegistry } from "react-jss";
8+
import { JssProvider } from "react-jss";
9+
import {
10+
MuiThemeProvider,
11+
createGenerateClassName
12+
} from "@material-ui/core/styles";
813

914
import App from "common/App";
15+
import theme from "common/theme";
1016
import { changeTitle } from "common/redux/reducer/title";
1117

1218
declare const module: any;
@@ -19,22 +25,31 @@ function main() {
1925

2026
express.get("/*", (req, res, next) => {
2127
const store = Redux.createStore(changeTitle);
22-
const sheet = new ServerStyleSheet();
28+
const sheetsRegistry = new SheetsRegistry();
29+
const sheetsManager = new Map();
2330

2431
const appHTML = ReactDOM.renderToString(
25-
sheet.collectStyles(
26-
<ReduxProvider store={store}>
27-
<Router location={req.path} context={{}}>
28-
<App />
29-
</Router>
30-
</ReduxProvider>
31-
)
32+
<ReduxProvider store={store}>
33+
<Router location={req.path} context={{}}>
34+
<JssProvider
35+
registry={sheetsRegistry}
36+
generateClassName={createGenerateClassName()}
37+
>
38+
<MuiThemeProvider
39+
theme={theme}
40+
sheetsManager={sheetsManager}
41+
>
42+
<App />
43+
</MuiThemeProvider>
44+
</JssProvider>
45+
</Router>
46+
</ReduxProvider>
3247
);
33-
const appCSS = sheet.getStyleTags();
3448
const appInitialState = JSON.stringify(store.getState()).replace(
3549
/</g,
3650
"\\u003c"
3751
);
52+
const appCSS = sheetsRegistry.toString();
3853

3954
res.send(`
4055
<!DOCTYPE html>
@@ -46,8 +61,8 @@ function main() {
4661
margin: 0px;
4762
padding: 0px;
4863
}
49-
${appCSS}
5064
</style>
65+
<style id="jss-server-side">${appCSS}</style>
5166
</head>
5267
<body>
5368
<main id="root">${appHTML}</main>

Diff for: tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"removeComments": true,
77
"moduleResolution": "node",
88
"jsx": "react",
9-
"baseUrl": "./src"
9+
"baseUrl": "./src",
10+
"lib": ["es5", "es6", "dom"]
1011
}
1112
}

0 commit comments

Comments
 (0)