Skip to content

Commit

Permalink
feat: add dark mode 🌔
Browse files Browse the repository at this point in the history
  • Loading branch information
BolajiAyodeji committed Feb 20, 2021
1 parent 06074f0 commit 585b929
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 40 deletions.
21 changes: 21 additions & 0 deletions components/DarkModeToggle.js
Original file line number Diff line number Diff line change
@@ -0,0 1,21 @@
import React from "react";
import Toggle from "./Toggle";
import useDarkMode from "use-dark-mode";

const DarkModeToggle = () => {
const darkMode = useDarkMode(false);

return (
<div className="dark-mode-toggle">
<button type="button" onClick={darkMode.disable}>
</button>
<Toggle checked={darkMode.value} onChange={darkMode.toggle} />
<button type="button" onClick={darkMode.enable}>
</button>
</div>
);
};

export default DarkModeToggle;
14 changes: 14 additions & 0 deletions components/Footer/Footer.js
Original file line number Diff line number Diff line change
@@ -0,0 1,14 @@
import React from "react";
import styles from "./Footer.module.css";

const footerYear = new Date().getFullYear();

const Footer = () => {
return (
<footer className={styles.footer}>
&copy; Bolaji Ayodeji | 2019 - {footerYear}.
</footer>
);
};

export default Footer;
8 changes: 8 additions & 0 deletions components/Footer/Footer.module.css
Original file line number Diff line number Diff line change
@@ -0,0 1,8 @@
.footer {
width: 100%;
height: 100px;
border-top: 2px solid var(--primaryMainColor);
display: flex;
justify-content: center;
align-items: center;
}
16 changes: 16 additions & 0 deletions components/Toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 1,16 @@
import React from "react";

const Toggle = ({ checked, onChange }) => (
<span className="toggle-control">
<input
className="dmcheck"
type="checkbox"
checked={checked}
onChange={onChange}
id="dmcheck"
/>
<label htmlFor="dmcheck" />
</span>
);

export default Toggle;
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 3,7 @@ module.exports = {
return [
{
source: "/newsletter",
destination: "https://tinyletter.com/bolajiayodeji",
destination: "https://bawd.bolajiayodeji.com",
},
{
source: "/blog",
Expand Down
82 changes: 75 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 11,7 @@
"dependencies": {
"next": "10.0.3",
"react": "17.0.1",
"react-dom": "17.0.1"
"react-dom": "17.0.1",
"use-dark-mode": "^2.3.1"
}
}
2 changes: 1 addition & 1 deletion pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
import '../styles/globals.css'
import '../styles/globals.css';

function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
Expand Down
1 change: 1 addition & 0 deletions pages/_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 11,7 @@ class MyDocument extends Document {
<script src="/__/firebase/8.2.1/firebase-app.js"></script>
<script src="/__/firebase/8.2.1/firebase-analytics.js"></script>
<script src="/__/firebase/init.js"></script>
<script src="noflash.js" />
</body>
</Html>
);
Expand Down
21 changes: 14 additions & 7 deletions pages/index.js
Original file line number Diff line number Diff line change
@@ -1,5 1,7 @@
import Head from "next/head";
import styles from "../styles/Home.module.css";
import DarkModeToggle from "../components/DarkModeToggle";
import Footer from "../components/Footer/Footer";

const metaData = {
pageTitle:
Expand Down Expand Up @@ -56,6 58,8 @@ export default function Home() {
<link rel="icon" href={favicon} />
</Head>

<DarkModeToggle />

<main className={styles.main}>
<h1 className={styles.title}>BOLAJI AYODEJI</h1>

Expand Down Expand Up @@ -106,12 110,17 @@ export default function Home() {
>
YouTube)
</a>
, build useful <a href="https://github.com/BolajiAyodeji" target="_blank"
rel="noopener noreferrer">open-source projects</a>, speak at some meetups/conferences, and build technical communities.
, build useful{" "}
<a
href="https://github.com/BolajiAyodeji"
target="_blank"
rel="noopener noreferrer"
>
open-source projects
</a>
, speak at some meetups/conferences, and build technical communities.
</p>



<div className={styles.grid}>
<a href="https://blog.bolajiayodeji.com" className={styles.card}>
<h2>✍🏾 Blog</h2>
Expand Down Expand Up @@ -148,9 157,7 @@ export default function Home() {
</div>
</main>

<footer className={styles.footer}>
&copy; Bolaji Ayodeji | 2019 - Today.
</footer>
<Footer />
</div>
);
}
40 changes: 40 additions & 0 deletions public/noflash.js
Original file line number Diff line number Diff line change
@@ -0,0 1,40 @@
// Insert this script in your index.html right after the <body> tag.
// This will help to prevent a flash if dark mode is the default.

(function() {
// Change these if you use something different in your hook.
var storageKey = 'darkMode';
var classNameDark = 'dark-mode';
var classNameLight = 'light-mode';

function setClassOnDocumentBody(darkMode) {
document.body.classList.add(darkMode ? classNameDark : classNameLight);
document.body.classList.remove(darkMode ? classNameLight : classNameDark);
}

var preferDarkQuery = '(prefers-color-scheme: dark)';
var mql = window.matchMedia(preferDarkQuery);
var supportsColorSchemeQuery = mql.media === preferDarkQuery;
var localStorageTheme = null;
try {
localStorageTheme = localStorage.getItem(storageKey);
} catch (err) {}
var localStorageExists = localStorageTheme !== null;
if (localStorageExists) {
localStorageTheme = JSON.parse(localStorageTheme);
}

// Determine the source of truth
if (localStorageExists) {
// source of truth from localStorage
setClassOnDocumentBody(localStorageTheme);
} else if (supportsColorSchemeQuery) {
// source of truth from system
setClassOnDocumentBody(mql.matches);
localStorage.setItem(storageKey, mql.matches);
} else {
// source of truth from document.body
var isDarkMode = document.body.classList.contains(classNameDark);
localStorage.setItem(storageKey, JSON.stringify(isDarkMode));
}
})();
23 changes: 7 additions & 16 deletions styles/Home.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 8,7 @@
}

.main {
padding: 5rem 0;
padding: 4rem 0;
flex: 1;
display: flex;
flex-direction: column;
Expand All @@ -20,7 20,7 @@
margin: 0;
line-height: 1.15;
font-size: 5.5rem;
color: #3a7ebf;
color: var(--primaryMainColor);
text-align: center;
}

Expand All @@ -39,13 39,13 @@
}

.description a {
border-bottom: 2px dotted #3a7ebf;
border-bottom: 2px dotted var(--primaryMainColor);
text-decoration: none;
}

.description a:hover {
color: #3a7ebf;
border-bottom: 2px dotted #000000;
color: var(--primaryMainColor);
border-bottom: 2px dotted var(--primaryTextColor);
}

.grid {
Expand All @@ -64,7 64,7 @@
text-align: left;
color: inherit;
text-decoration: none;
border: 5px solid #3a7ebf;
border: 5px solid var(--primaryMainColor);
}

.card:hover,
Expand All @@ -87,15 87,6 @@
line-height: 1.5;
}

.footer {
width: 100%;
height: 100px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
}

@media (max-width: 600px) {
.grid {
width: 100%;
Expand All @@ -108,6 99,6 @@
padding: 1.5rem;
}
.card {
border: 3px solid #3a7ebf;
border: 3px solid var(--primaryMainColor);
}
}
Loading

0 comments on commit 585b929

Please sign in to comment.