Skip to content

Commit f9e331d

Browse files
Merge pull request #7 from shreystechtips/darkmode
Finished Adding Dark Mode Functionality with Caching (card color is a bit off but acceptable)
2 parents 65eb3e0 + f4943b5 commit f9e331d

File tree

7 files changed

+273
-213
lines changed

7 files changed

+273
-213
lines changed

gatsby-node.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ exports.createPages = ({ boundActionCreators, graphql }) => {
4545
if (result.errors) {
4646
return Promise.reject(result.errors);
4747
}
48-
console.log(result);
4948
var ret = { data: { allMarkdownRemark: { edges: [] } } };
5049
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
5150
if (

src/components/DarkContext.js

Whitespace-only changes.

src/components/header.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ import {
77
Toolbar,
88
IconButton,
99
Typography,
10-
Button,
1110
makeStyles,
1211
useScrollTrigger,
1312
Slide,
1413
List,
1514
ListItem,
1615
SwipeableDrawer,
17-
Divider,
1816
ListItemText,
1917
Collapse,
18+
Switch,
2019
} from "@material-ui/core";
2120
import { ExpandLess, ExpandMore } from "@material-ui/icons";
2221
import MenuIcon from "@material-ui/icons/Menu";
@@ -34,10 +33,14 @@ const useStyles = makeStyles((theme) => ({
3433
nested2: {
3534
paddingLeft: theme.spacing(6),
3635
},
36+
bg: {
37+
backgroundColor: theme.palette.background.paper,
38+
overflowX: "hidden",
39+
},
3740
}));
3841

3942
function SlideHeader(props) {
40-
const { window, children } = props;
43+
const { children } = props;
4144
const trigger = useScrollTrigger();
4245
return (
4346
<Slide appear={false} direction="down" in={!trigger}>
@@ -96,10 +99,11 @@ const NotesList = () => {
9699
width: 250,
97100
minHeight: "100vh",
98101
height: "100%",
99-
backgroundColor: "#F3F3F7",
102+
// backgroundColor: "#F3F3F7",0
100103
}}
104+
className={classes.bg}
101105
>
102-
<List style={{ backgroundColor: "#F3F3F7" }}>
106+
<List className={classes.bg}>
103107
{Object.keys(assortedPosts).map((key) => (
104108
<>
105109
<ListItem button key={key} onClick={(e) => handleClick(key)}>
@@ -199,6 +203,10 @@ function Header(props) {
199203
{props.siteTitle}
200204
</Link>
201205
</Typography>
206+
<Switch
207+
checked={props.themeDirection}
208+
onChange={() => props.themeChange()}
209+
/>
202210
</Toolbar>
203211
<SwipeableDrawer
204212
anchor="left"

src/components/layout.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ textarea {
176176
[type="search"]::-webkit-search-decoration {
177177
-webkit-appearance: none;
178178
}
179+
::-webkit-scrollbar {
180+
/* width: 5px; */
181+
/* background: grey; */
182+
/* scrollbar-color: red; */
183+
}
179184
::-webkit-input-placeholder {
180185
color: inherit;
181186
opacity: 0.54;
@@ -199,7 +204,7 @@ html {
199204
box-sizing: inherit;
200205
}
201206
body {
202-
color: hsla(0, 0%, 0%, 0.8);
207+
/* color: hsla(0, 0%, 0%, 0.8); */
203208
font-family: georgia, serif;
204209
font-weight: normal;
205210
word-wrap: break-word;

src/components/layout.js

Lines changed: 89 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,102 @@
55
* See: https://www.gatsbyjs.org/docs/use-static-query/
66
*/
77

8-
import React from "react"
9-
import PropTypes from "prop-types"
10-
import { useStaticQuery, graphql } from "gatsby"
8+
import React from "react";
9+
import PropTypes from "prop-types";
10+
import { useStaticQuery, graphql } from "gatsby";
11+
import CssBaseline from "@material-ui/core/CssBaseline";
12+
import Header from "./header";
13+
import "./layout.css";
14+
import { createMuiTheme, ThemeProvider } from "@material-ui/core/styles";
15+
import { useMediaQuery } from "@material-ui/core";
1116

12-
import Header from "./header"
13-
import "./layout.css"
17+
console.warn = console.error = () => {};
1418

1519
const Layout = ({ children }) => {
16-
const data = useStaticQuery(graphql`
17-
query SiteTitleQuery {
18-
site {
19-
siteMetadata {
20-
title
21-
}
22-
}
23-
}
24-
`)
25-
26-
return (
27-
<div style={{ overflow: "auto" }}>
28-
<Header siteTitle={`Shrey's ${data.site.siteMetadata.title}`} />
29-
<div
30-
style={{
31-
margin: `0 auto`,
32-
maxWidth: 960,
33-
padding: `0 1.0875rem 1.45rem`,
34-
paddingTop: 65,
35-
}}
36-
>
37-
<main>{children}</main>
38-
{/* <footer>
20+
const data = useStaticQuery(graphql`
21+
query SiteTitleQuery {
22+
site {
23+
siteMetadata {
24+
title
25+
}
26+
}
27+
}
28+
`);
29+
30+
const systemDark = useMediaQuery("(prefers-color-scheme: dark)");
31+
React.useEffect(() => {
32+
var theme = window.localStorage.getItem("theme");
33+
if (theme === null) {
34+
setDarkState(systemDark);
35+
} else {
36+
theme = theme === "true";
37+
setDarkState(theme);
38+
}
39+
}, []);
40+
41+
const [darkState, setDarkState] = React.useState(false);
42+
// const palletType = darkState ? "dark" : "light";
43+
// const mainPrimaryColor = darkState ? orange[500] : lightBlue[500]
44+
// const mainSecondaryColor = darkState ? deepOrange[900] : deepPurple[500]
45+
const getTheme = (state) => {
46+
return createMuiTheme({
47+
palette: {
48+
type: state ? "dark" : "light",
49+
// background: { paper: "#F3F3F7" },
50+
},
51+
});
52+
};
53+
const darkTheme = getTheme(darkState);
54+
// createMuiTheme({
55+
// palette: {
56+
57+
// type: darkState ? "dark" : "light",
58+
59+
// },
60+
// });
61+
62+
const handleThemeChange = async () => {
63+
window.localStorage.setItem("theme", !darkState);
64+
setDarkState(!darkState);
65+
};
66+
return (
67+
<ThemeProvider theme={darkTheme}>
68+
<CssBaseline>
69+
<div
70+
style={{
71+
overflow: "auto",
72+
backgroundColor: darkTheme.palette.background.default,
73+
}}
74+
>
75+
<Header
76+
siteTitle={`Shrey's ${data.site.siteMetadata.title}`}
77+
themeChange={handleThemeChange}
78+
themeDirection={darkState}
79+
/>
80+
81+
<div
82+
style={{
83+
margin: `0 auto`,
84+
maxWidth: 960,
85+
padding: `0 1.0875rem 1.45rem`,
86+
paddingTop: 65,
87+
}}
88+
>
89+
<main>{children}</main>
90+
{/* <footer>
3991
© {new Date().getFullYear()}, Built with
4092
{` `}
4193
<a href="https://www.gatsbyjs.org">Gatsby</a>
4294
</footer> */}
43-
</div>
44-
</div>
45-
)
46-
}
95+
</div>
96+
</div>
97+
</CssBaseline>
98+
</ThemeProvider>
99+
);
100+
};
47101

48102
Layout.propTypes = {
49-
children: PropTypes.node.isRequired,
50-
}
103+
children: PropTypes.node.isRequired,
104+
};
51105

52-
export default Layout
106+
export default Layout;

src/templates/blog-post.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react";
22
import Helmet from "react-helmet";
33
import Layout from "../components/layout";
4+
import { graphql } from "gatsby";
45
require(`katex/dist/katex.min.css`);
56

67
export default function Template({ data }) {

0 commit comments

Comments
 (0)