-
Notifications
You must be signed in to change notification settings - Fork 62
Fix: Persist theme selection using localStorage #161
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,14 +10,19 @@ interface ThemeContextType { | |||||||||||||||||||||||||||
| export const ThemeContext = createContext<ThemeContextType | null>(null); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const ThemeWrapper = ({ children }: { children: ReactNode }) => { | ||||||||||||||||||||||||||||
| const [mode, setMode] = useState<'light' | 'dark'>('light'); | ||||||||||||||||||||||||||||
| const [mode, setMode] = useState<'light' | 'dark'>(() => { | ||||||||||||||||||||||||||||
| const savedMode = localStorage.getItem('theme'); | ||||||||||||||||||||||||||||
| return savedMode === 'dark' ? 'dark' : 'light'; | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||||
| if (mode === 'dark') { | ||||||||||||||||||||||||||||
| document.documentElement.classList.add('dark'); | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| document.documentElement.classList.remove('dark'); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| localStorage.setItem('theme', mode); | ||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for localStorage persistence. The localStorage persistence works correctly but lacks error handling. If localStorage is unavailable or full, it could throw an exception and potentially break the theme functionality. useEffect(() => {
if (mode === 'dark') {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
- localStorage.setItem('theme', mode);
+ try {
+ localStorage.setItem('theme', mode);
+ } catch (error) {
+ console.warn('Failed to save theme preference:', error);
+ }
}, [mode]);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||
| }, [mode]); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const toggleTheme = () => { | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
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.
🛠️ Refactor suggestion
Add error handling for localStorage access.
The lazy initializer correctly reads from localStorage and defaults appropriately, but it lacks error handling for cases where localStorage might not be available (e.g., SSR, private browsing, or storage quota exceeded).
const [mode, setMode] = useState<'light' | 'dark'>(() => { - const savedMode = localStorage.getItem('theme'); - return savedMode === 'dark' ? 'dark' : 'light'; + try { + const savedMode = localStorage.getItem('theme'); + return savedMode === 'dark' ? 'dark' : 'light'; + } catch (error) { + return 'light'; + } });📝 Committable suggestion
🤖 Prompt for AI Agents