-
Notifications
You must be signed in to change notification settings - Fork 1
Implement Dark/Light Theme Toggle with Persistence and Accessibility #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rezwana-karim
merged 2 commits into
main
from
copilot/fix-b39274f8-a319-4fa5-9617-ebe50a9b841a
Sep 30, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,9 @@ | ||
| "use client" | ||
|
|
||
| import { useState, useEffect } from 'react' | ||
| import { useThemeContext } from '@/lib/theme-context' | ||
|
|
||
| export type Theme = 'light' | 'dark' | ||
| export type { Theme } from '@/lib/theme-context' | ||
|
|
||
| export function useTheme() { | ||
| const [theme, setTheme] = useState<Theme>('light') | ||
| const [mounted, setMounted] = useState(false) | ||
|
|
||
| useEffect(() => { | ||
| setMounted(true) | ||
|
|
||
| // Check for stored theme preference or default to system preference | ||
| const stored = localStorage.getItem('theme') as Theme | null | ||
| if (stored) { | ||
| setTheme(stored) | ||
| document.documentElement.classList.toggle('dark', stored === 'dark') | ||
| } else { | ||
| // Check system preference | ||
| const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches | ||
| const systemTheme: Theme = prefersDark ? 'dark' : 'light' | ||
| setTheme(systemTheme) | ||
| document.documentElement.classList.toggle('dark', systemTheme === 'dark') | ||
| } | ||
| }, []) | ||
|
|
||
| const toggleTheme = () => { | ||
| if (!mounted) return | ||
|
|
||
| const newTheme: Theme = theme === 'light' ? 'dark' : 'light' | ||
| setTheme(newTheme) | ||
| localStorage.setItem('theme', newTheme) | ||
| document.documentElement.classList.toggle('dark', newTheme === 'dark') | ||
| } | ||
|
|
||
| const setThemeValue = (newTheme: Theme) => { | ||
| if (!mounted) return | ||
|
|
||
| setTheme(newTheme) | ||
| localStorage.setItem('theme', newTheme) | ||
| document.documentElement.classList.toggle('dark', newTheme === 'dark') | ||
| } | ||
|
|
||
| return { | ||
| theme, | ||
| toggleTheme, | ||
| setTheme: setThemeValue, | ||
| mounted, | ||
| } | ||
| return useThemeContext() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| "use client" | ||
|
|
||
| import React, { createContext, useContext, useState, useEffect } from 'react' | ||
|
|
||
| export type Theme = 'light' | 'dark' | ||
|
|
||
| interface ThemeContextType { | ||
| theme: Theme | ||
| toggleTheme: () => void | ||
| setTheme: (theme: Theme) => void | ||
| mounted: boolean | ||
| } | ||
|
|
||
| const ThemeContext = createContext<ThemeContextType | undefined>(undefined) | ||
|
|
||
| interface ThemeProviderProps { | ||
| children: React.ReactNode | ||
| } | ||
|
|
||
| export function ThemeProvider({ children }: ThemeProviderProps) { | ||
| const [theme, setThemeState] = useState<Theme>('light') | ||
| const [mounted, setMounted] = useState(false) | ||
|
|
||
| // Initialize theme on mount | ||
| useEffect(() => { | ||
| setMounted(true) | ||
|
|
||
| // Check for stored theme preference or default to system preference | ||
| const stored = localStorage.getItem('theme') as Theme | null | ||
| if (stored && (stored === 'light' || stored === 'dark')) { | ||
| setThemeState(stored) | ||
| applyTheme(stored) | ||
| } else { | ||
| // Check system preference | ||
| const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches | ||
| const systemTheme: Theme = prefersDark ? 'dark' : 'light' | ||
| setThemeState(systemTheme) | ||
| applyTheme(systemTheme) | ||
| } | ||
| }, []) | ||
|
Comment on lines
+25
to
+40
|
||
|
|
||
| const applyTheme = (newTheme: Theme) => { | ||
| const root = document.documentElement | ||
| if (newTheme === 'dark') { | ||
| root.classList.add('dark') | ||
| } else { | ||
| root.classList.remove('dark') | ||
| } | ||
| } | ||
|
|
||
| const toggleTheme = () => { | ||
| if (!mounted) return | ||
|
|
||
| const newTheme: Theme = theme === 'light' ? 'dark' : 'light' | ||
| setThemeState(newTheme) | ||
| localStorage.setItem('theme', newTheme) | ||
| applyTheme(newTheme) | ||
| } | ||
|
|
||
| const setTheme = (newTheme: Theme) => { | ||
| if (!mounted) return | ||
|
|
||
| setThemeState(newTheme) | ||
| localStorage.setItem('theme', newTheme) | ||
| applyTheme(newTheme) | ||
| } | ||
|
|
||
| const value: ThemeContextType = { | ||
| theme, | ||
| toggleTheme, | ||
| setTheme, | ||
| mounted, | ||
| } | ||
|
|
||
| return ( | ||
| <ThemeContext.Provider value={value}> | ||
| {children} | ||
| </ThemeContext.Provider> | ||
| ) | ||
| } | ||
|
|
||
| export function useThemeContext(): ThemeContextType { | ||
| const context = useContext(ThemeContext) | ||
| if (context === undefined) { | ||
| throw new Error('useThemeContext must be used within a ThemeProvider') | ||
| } | ||
| return context | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type assertion
as Theme | nullis unsafe. If localStorage contains an invalid theme value, the type assertion will succeed but the runtime check will fail. Consider using type guards or removing the assertion and relying only on the runtime validation.