Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/react/src/html/ui/hooks/useTheme.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ const applySystemTheme = () => applyTheme('system');
* @returns {['system'|'light'|'dark', (next: 'system'|'light'|'dark') => void]}
*/
export const useTheme = () => {
// Read stored preference once on mount; default to 'system'.
const [pref, setPref] = useState(() =>
server ? 'system' : (localStorage.getItem('theme') ?? 'system')
);
const [pref, setPref] = useState('system');

useEffect(() => {
const stored = localStorage.getItem('theme');
if (stored === 'light' || stored === 'dark') {
setPref(stored);
}
}, []);
Comment on lines +34 to +39

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
useEffect(() => {
const stored = localStorage.getItem('theme');
if (stored) {
setPref(stored);
}
}, []);
useEffect(() => {
const stored = localStorage.getItem('theme');
if (stored === 'light' || stored === 'dark') {
setPref(stored);
}
}, []);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont need to reapply system

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn’t stored !== pref be a little more robust?

@TusharThakur04 TusharThakur04 Sep 15, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if somehow stored is returned as null, its better to check explicitly. WDYT?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to explicitly check for 'light' or 'dark' to safely handle null and avoid re-applying system.
Thanks for suggestion !!


// Apply theme on every preference change, and if 'system',
// also listen for OS-level color scheme changes.
Expand Down