Skip to content

Commit

Permalink
feat(dark mode): Started to do the base code for a dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
skykosiner committed Sep 12, 2024
1 parent 89ca538 commit 30e489c
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ next-env.d.ts

# Local Netlify folder
.netlify

certificates
88 changes: 44 additions & 44 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"dev": "next dev --experimental-https",
"build": "next build",
"start": "next start",
"lint": "next lint"
Expand All @@ -16,7 +16,7 @@
"axios": "^1.7.7",
"dotenv": "^16.4.5",
"gray-matter": "^4.0.3",
"next": "14.2.6",
"next": "^14.2.10",
"next-compose-plugins": "^2.2.1",
"next-mdx-remote": "^5.0.0",
"react": "^18",
Expand Down
36 changes: 36 additions & 0 deletions src/app/components/dark-mode.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use client";

import { useEffect, useState } from "react";

export default function DarkMode(): JSX.Element {
const [isDarkMode, setIsDarkMode] = useState<boolean>(false);

useEffect(() => {
const darkModePreference = localStorage.getItem("dark-mode");
if (darkModePreference !== null) {
setIsDarkMode(darkModePreference === "true");
} else {
setIsDarkMode(window.matchMedia("(prefers-color-scheme: dark)").matches);
}
}, []);

useEffect(() => {
if (isDarkMode) {
document.body.classList.remove("light");
document.body.classList.add("dark");
} else {
document.body.classList.remove("dark");
document.body.classList.add("light");
}
}, [isDarkMode]);

function handleDarkMode() {
const newDarkMode = !isDarkMode;
setIsDarkMode(newDarkMode);
localStorage.setItem("dark-mode", newDarkMode.toString());
}

return (
<button className="darkModeToggle" onClick={handleDarkMode}>{!isDarkMode ? "☀️" : "🌙"}</button>
);
}
18 changes: 17 additions & 1 deletion src/app/globals.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
/* TODO: Set this with the correct colors to use */
:root {
/* Light theme variables */
--color-background-light: #F2F2F2;
/* Dark theme variables */
--color-background-dark: #1f2023;
}

.light {
--color-background: var(--color-background-light);
}

.dark {
--color-background: var(--color-background-dark);
}

* {
margin: 0;
padding: 0;
color: #333333;
background-color: #F2F2F2;
background-color: var(--color-background);
overflow-x: hidden;
}

Expand Down
3 changes: 3 additions & 0 deletions src/app/projects/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Link from "next/link";
import styles from "./projects.module.css";
import { useState } from "react";
import DarkMode from "../components/dark-mode";

type Project = {
name: string,
Expand Down Expand Up @@ -54,6 +55,8 @@ export default function Projects(): JSX.Element {
</button>
</div>
))}

<DarkMode />
</div>
);
}

0 comments on commit 30e489c

Please sign in to comment.