Skip to content

Commit 63f9304

Browse files
gkorlandCopilot
andcommitted
Fix theme toggle review feedback
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d6dbc23 commit 63f9304

3 files changed

Lines changed: 27 additions & 14 deletions

File tree

app/src/App.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,12 @@ export default function App() {
408408
<Tooltip>
409409
<TooltipTrigger asChild>
410410
<Button variant="ghost" size="icon" asChild>
411-
<a title="Github" href="https://github.com/FalkorDB/code-graph" target='_blank' rel="noopener noreferrer">
411+
<a title="GitHub" href="https://github.com/FalkorDB/code-graph" target='_blank' rel="noopener noreferrer">
412412
<Github className="h-5 w-5" />
413413
</a>
414414
</Button>
415415
</TooltipTrigger>
416-
<TooltipContent>Github</TooltipContent>
416+
<TooltipContent>GitHub</TooltipContent>
417417
</Tooltip>
418418
</li>
419419
<li>
@@ -596,7 +596,7 @@ export default function App() {
596596
<ul className='h-full flex flex-col gap-16 p-8 items-center'>
597597
<li>
598598
<a href="https://github.com/FalkorDB/code-graph" target='_blank' rel="noopener noreferrer">
599-
<p>Github</p>
599+
<p>GitHub</p>
600600
</a>
601601
</li>
602602
<li>

app/src/components/theme-provider.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import { createContext, useContext, useEffect, useState } from "react"
22

33
type Theme = "dark" | "light" | "system"
4+
type ResolvedTheme = Exclude<Theme, "system">
45

56
type ThemeProviderState = {
67
theme: Theme
8+
resolvedTheme: ResolvedTheme
79
setTheme: (theme: Theme) => void
810
}
911

1012
const ThemeProviderContext = createContext<ThemeProviderState>({
1113
theme: "system",
14+
resolvedTheme: "light",
1215
setTheme: () => null,
1316
})
1417

18+
const MEDIA_QUERY = "(prefers-color-scheme: dark)"
19+
20+
const getSystemTheme = (): ResolvedTheme =>
21+
typeof window !== "undefined" && window.matchMedia(MEDIA_QUERY).matches ? "dark" : "light"
22+
1523
export function ThemeProvider({
1624
children,
1725
defaultTheme = "system",
@@ -24,23 +32,28 @@ export function ThemeProvider({
2432
const [theme, setTheme] = useState<Theme>(
2533
() => (localStorage.getItem(storageKey) as Theme) || defaultTheme
2634
)
35+
const [systemTheme, setSystemTheme] = useState<ResolvedTheme>(getSystemTheme)
36+
37+
useEffect(() => {
38+
const mediaQuery = window.matchMedia(MEDIA_QUERY)
39+
const updateSystemTheme = () => setSystemTheme(mediaQuery.matches ? "dark" : "light")
40+
41+
updateSystemTheme()
42+
mediaQuery.addEventListener("change", updateSystemTheme)
43+
44+
return () => mediaQuery.removeEventListener("change", updateSystemTheme)
45+
}, [])
2746

2847
useEffect(() => {
2948
const root = window.document.documentElement
3049
root.classList.remove("light", "dark")
3150

32-
if (theme === "system") {
33-
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)").matches
34-
? "dark"
35-
: "light"
36-
root.classList.add(systemTheme)
37-
} else {
38-
root.classList.add(theme)
39-
}
40-
}, [theme])
51+
root.classList.add(theme === "system" ? systemTheme : theme)
52+
}, [theme, systemTheme])
4153

4254
const value = {
4355
theme,
56+
resolvedTheme: theme === "system" ? systemTheme : theme,
4457
setTheme: (theme: Theme) => {
4558
localStorage.setItem(storageKey, theme)
4659
setTheme(theme)

app/src/components/theme-toggle.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { Button } from "@/components/ui/button"
33
import { useTheme } from "./theme-provider"
44

55
export function ThemeToggle() {
6-
const { theme, setTheme } = useTheme()
6+
const { resolvedTheme, setTheme } = useTheme()
77

8-
const isDark = theme === "dark" || (theme === "system" && window.matchMedia("(prefers-color-scheme: dark)").matches)
8+
const isDark = resolvedTheme === "dark"
99

1010
return (
1111
<Button

0 commit comments

Comments
 (0)