From 064a9355e624b87c356545363e8c937398b7c272 Mon Sep 17 00:00:00 2001 From: pegasust Date: Thu, 1 Dec 2022 07:53:17 +0000 Subject: [PATCH] WIP on theme selector of about-me, docs: updated README.mds --- README.md | 11 ++-- apps/about-me/src/pages/index.tsx | 104 ++++++++++++++++-------------- 2 files changed, 62 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index c5a3438..1f57d02 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ Scaffolded by `pnpm create turbo@latest` This project manages my homelab infrastructure under a monorepo to keep things centralized and easily sharable across different modules - # Turbo-repo details ## What's inside? @@ -14,11 +13,11 @@ This turborepo uses [pnpm](https://pnpm.io) as a package manager. It includes th ### Apps and Packages -- `docs`: a [Next.js](https://nextjs.org/) app -- `web`: another [Next.js](https://nextjs.org/) app -- `ui`: a stub React component library shared by both `web` and `docs` applications -- `eslint-config-custom`: `eslint` configurations (includes `eslint-config-next` and `eslint-config-prettier`) -- `tsconfig`: `tsconfig.json`s used throughout the monorepo +- `apps/docs`: a [Next.js](https://nextjs.org/) app +- `apps/web`: another [Next.js](https://nextjs.org/) app +- `packages/ui`: a stub React component library shared by both `web` and `docs` applications +- `packages/eslint-config-custom`: `eslint` configurations (includes `eslint-config-next` and `eslint-config-prettier`) +- `packages/tsconfig`: `tsconfig.json`s used throughout the monorepo Each package/app is 100% [TypeScript](https://www.typescriptlang.org/). diff --git a/apps/about-me/src/pages/index.tsx b/apps/about-me/src/pages/index.tsx index 11e1527..11375d1 100644 --- a/apps/about-me/src/pages/index.tsx +++ b/apps/about-me/src/pages/index.tsx @@ -20,6 +20,41 @@ import SantaMonicaHigh from "@public/assets/santa-monica-high.jpg"; import assert from "assert"; import { Section, SectionHeader, UL } from "@components/index"; +type Theme = "dark" | "light"; + +const ThemeContext = React.createContext<{ + theme: Theme, + systemTheme: Theme, + setThemePreference(theme: Theme): void, +} | undefined>(undefined); +const ThemeProvider: React.FC<{ children: React.ReactNode, prefer?: "light" | "dark" }> = ({ children, prefer }) => { + const pref = prefer ?? "light"; + const [systemTheme, setSystemTheme] = useState(pref); + const [themePref, setThemePref] = useState(undefined); + // add a listener to system preference on which theme + // TODO: is there another way that remove event listener faster? + useEffect(() => { + console.log("useEffect called"); + type EventType = { matches: boolean }; + const onThemeChange = (event: EventType) => { + const newColorScheme = event.matches ? "dark" : "light"; + setSystemTheme(newColorScheme); + }; + window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', onThemeChange); + return () => { + console.log("useEffect unmount"); + window.matchMedia('(prefers-color-scheme: dark)').removeEventListener("change", onThemeChange) + } + }, []); + return {children} +} +const useTheme = () => useContext(ThemeContext); + + const circle = cva("absolute rounded-full border -z-10", { variants: { size: { @@ -237,7 +272,9 @@ const ImageDisplay: React.FC<{ imageDisplayHook: ImageDisplayHook }> = ({ imageD // setImageRepo, imgIndex, image -} }) =>
+} }) => { + const {theme} = useTheme()!; + return
@@ -248,13 +285,15 @@ const ImageDisplay: React.FC<{ imageDisplayHook: ImageDisplayHook }> = ({ imageD
{Array.from(Array(imageRepo.length).keys()).map(i =>
)}
- {image && {image.alt}} + {image && {image.alt}}
+} const MyLink: React.FC<{ href: string, children?: ReactNode }> = ({ href, children }) => @@ -266,7 +305,7 @@ const About = () => { return
About -
+
@@ -328,7 +367,7 @@ const NavbarItem: React.FC<{ -const iconCva = cva("transition-colors", { +const iconCva = cva("transition-colors group-hover:animate-bounce", { variants: { theme: { light: "group-hover:fill-gray-700 fill-gray-400" @@ -339,51 +378,22 @@ const iconCva = cva("transition-colors", { } }); -const Navbar = () =>
-
-
- - +const Navbar = () => { + const _theme = useTheme(); + if (!_theme) { throw new Error("no theme provider"); } + const { theme, setThemePreference } = _theme; + return
+
+
+ + +
+ + Hung Tran +
- - Hung Tran -
-
- -type Theme = "dark" | "light"; - -const ThemeContext = React.createContext<{ - theme: Theme, - systemTheme: Theme, - setThemePreference(theme: Theme): void, -} | undefined>(undefined); -const ThemeProvider: React.FC<{ children: React.ReactNode, prefer?: "light" | "dark" }> = ({ children, prefer }) => { - const pref = prefer ?? "light"; - const [systemTheme, setSystemTheme] = useState(pref); - const [themePref, setThemePref] = useState(undefined); - // add a listener to system preference on which theme - // TODO: is there another way that remove event listener faster? - useEffect(() => { - console.log("useEffect called"); - type EventType = { matches: boolean }; - const onThemeChange = (event: EventType) => { - const newColorScheme = event.matches ? "dark" : "light"; - setSystemTheme(newColorScheme); - }; - window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', onThemeChange); - return () => { - console.log("useEffect unmount"); - window.matchMedia('(prefers-color-scheme: dark)').removeEventListener("change", onThemeChange) - } - }, []); - return {children} } -const useTheme = () => useContext(ThemeContext); const Home: NextPage = () => { return (