Skip to content
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

Added User theme(Light/Dark) preference detection #1072

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Changes from 3 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
2 changes: 1 addition & 1 deletion Client/src/Features/UI/uiSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const initialState = {
sidebar: {
collapsed: false,
},
mode: "light",
mode: window?.matchMedia?.('(prefers-color-scheme: dark)')?.matches? "dark": "light",
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

One more thing about that initialization! 💡

Consider extracting the theme detection logic into a separate utility function. This would make it easier to test and maintain.

Create a new file utils/themeDetection.js:

export const getPreferredTheme = () => {
  try {
    const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
    return mediaQuery.matches ? 'dark' : 'light';
  } catch {
    return 'light';
  }
};

export const initializeThemeListener = (callback) => {
  try {
    const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
    mediaQuery.addEventListener('change', e => callback(e.matches ? 'dark' : 'light'));
  } catch (err) {
    console.warn('Theme detection not supported');
  }
};

Then in uiSlice.js:

+import { getPreferredTheme } from '../utils/themeDetection';
...
-mode: window?.matchMedia?.('(prefers-color-scheme: dark)')?.matches? "dark": "light",
+mode: getPreferredTheme(),

⚠️ Potential issue

Yo! The theme detection needs some love! 🍝

While the implementation works, there are several improvements we could make to ensure robustness:

  1. We should add a listener for preference changes
  2. We need a proper fallback for browsers that don't support matchMedia
  3. The optional chaining is good but we should be more explicit about the fallback

Here's a more robust implementation:

-mode: window?.matchMedia?.('(prefers-color-scheme: dark)')?.matches? "dark": "light",
+mode: (() => {
+  try {
+    const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
+    // Add listener for theme changes
+    mediaQuery.addEventListener('change', e => {
+      store.dispatch(setMode(e.matches ? 'dark' : 'light'));
+    });
+    return mediaQuery.matches ? 'dark' : 'light';
+  } catch (err) {
+    console.warn('Color scheme detection not supported');
+    return 'light';
+  }
+})(),

Committable suggestion was skipped due to low confidence.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure what happened, but the formatting is not that clear without the space between the first part of the ternary and the question mark. Would you mind extracting the ternary from the object to increase legibility? I suggest saving it to a variable with a name like initialMode or something like that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@marcelluscaio I have made that change

greeting: { index: 0, lastUpdate: null },
timezone: "America/Toronto",
};
Expand Down