Skip to content

fix: dispatch themeChanged event for instant UI synchronization#160

Open
Lilaschromish wants to merge 2 commits into
prem-k-r:mainfrom
Lilaschromish:fix/dark-mode-shortcut
Open

fix: dispatch themeChanged event for instant UI synchronization#160
Lilaschromish wants to merge 2 commits into
prem-k-r:mainfrom
Lilaschromish:fix/dark-mode-shortcut

Conversation

@Lilaschromish

@Lilaschromish Lilaschromish commented Mar 19, 2026

Copy link
Copy Markdown

Description

This PR implements real-time theme synchronization across UI components. It adds a themeChanged event that broadcasts both the selected mode and the effective (resolved) color.

Changes

  • Added emitThemeChanged helper to resolve system themes.
  • Updated applyThemeMode to dispatch events.
  • Added listener for OS-level dark mode flips.
  • Cleaned up duplicate function declarations.

Checklist

  • Tested in Chrome/Edge/Firefox
  • Code follows project style guidelines
  • Verified system theme switching works automatically

@coderabbitai

coderabbitai Bot commented Mar 19, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

Added intra-app theme-change notification: scripts/theme.js now computes an effective theme, emits a CustomEvent("themeChanged") (detail: { theme, effectiveTheme }) after persisting and applying theme changes, and updates the system matchMedia listener to emit when the active mode is system.

Changes

Cohort / File(s) Summary
Theme control
scripts/theme.js
Added emitThemeChanged(theme) helper; extended applyThemeMode(theme) to emit themeChanged after persisting/applying theme and when selecting system; adjusted matchMedia('change') listener to emit themeChanged("system") when active mode is system; trimmed inline comments and preserved event wiring and listener removal.

Sequence Diagram(s)

sequenceDiagram
  participant Segment as "Theme Segment UI"
  participant Script as "scripts/theme.js"
  participant System as "OS Color Scheme (matchMedia)"
  participant Window as "Global Event Bus (window)"
  participant Listener as "Other Component(s)"

  Segment->>Script: user selects theme (light/dark/system)
  Script->>Script: persist choice, compute effectiveTheme
  Script->>Script: update DOM (dataset, indicator, classes)
  Script->>Window: dispatch CustomEvent "themeChanged" {theme,effectiveTheme}
  Window->>Listener: listener receives themeChanged -> apply updates
  System-->>Script: (if system mode) matchMedia change triggers -> Script recomputes and dispatches themeChanged
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

refactor, ui/ux

Suggested reviewers

  • itz-rj-here
🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning The PR description covers the main purpose and changes, but omits several required sections from the template including Visual Changes, Related Issues, and most critically, the full checklist with CHANGELOG.md verification and Contributing Guidelines confirmation. Add all missing sections: Visual Changes (if applicable), Related Issues section, and complete the checklist including CHANGELOG.md update verification and confirmation of following Contributing Guidelines.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: dispatching a themeChanged event for instant UI synchronization, which aligns with the actual modifications in the code.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can generate a title for your PR based on the changes with custom instructions.

Set the reviews.auto_title_instructions setting to generate a title for your PR based on the changes in the PR with custom instructions.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
scripts/theme.js (1)

47-57: Emit resolved mode for system and mirror OS-scheme changes.

Listeners currently only get { theme: "system" }, and they won’t receive follow-up notifications when the OS theme flips. Consider emitting an effectiveTheme and dispatching again from the media-query listener when system is active.

♻️ Suggested refinement
+    const emitThemeChanged = (theme, source = "user") => {
+        const effectiveTheme =
+            theme === "system" ? (systemTheme.matches ? "dark" : "light") : theme;
+        window.dispatchEvent(new CustomEvent("themeChanged", {
+            detail: { theme, effectiveTheme, source }
+        }));
+    };

 function applyThemeMode(theme) {
     localStorage.setItem(preferredThemeKey, theme);
     segment.dataset.active = theme;
     moveIndicator(theme);

     // Update sysTheme attribute when system mode is selected
     if (theme === "system") {
         syncThemeChange(systemTheme);
     }

-    window.dispatchEvent(new CustomEvent('themeChanged', { 
-        detail: { theme: theme } 
-    }));
+    emitThemeChanged(theme);
 }

-    systemTheme.addEventListener('change', syncThemeChange);
+    systemTheme.addEventListener("change", (event) => {
+        syncThemeChange(event);
+        if (segment.dataset.active === "system") {
+            emitThemeChanged("system", "system");
+        }
+    });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@scripts/theme.js` around lines 47 - 57, When dispatching the theme change
event from the existing window.dispatchEvent call, include both the declared
mode and the resolved mode (e.g., detail: { theme, effectiveTheme: systemTheme
|| theme }) so listeners receive the actual colors when theme === "system"; also
locate the media-query listener that updates systemTheme (and the function
syncThemeChange) and ensure it re-dispatches the same 'themeChanged' event with
the updated effectiveTheme whenever the OS color-scheme flips while the current
theme is "system".
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@scripts/theme.js`:
- Around line 47-57: When dispatching the theme change event from the existing
window.dispatchEvent call, include both the declared mode and the resolved mode
(e.g., detail: { theme, effectiveTheme: systemTheme || theme }) so listeners
receive the actual colors when theme === "system"; also locate the media-query
listener that updates systemTheme (and the function syncThemeChange) and ensure
it re-dispatches the same 'themeChanged' event with the updated effectiveTheme
whenever the OS color-scheme flips while the current theme is "system".

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f9a64d2c-9ccf-4f69-a3d0-9b894f70305e

📥 Commits

Reviewing files that changed from the base of the PR and between dc4519c and b54b34a.

📒 Files selected for processing (1)
  • scripts/theme.js

@prem-k-r prem-k-r added under-review Currently being reviewed. Please wait for feedback. bugfix Fixes existing bugs or regressions labels Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Fixes existing bugs or regressions under-review Currently being reviewed. Please wait for feedback.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants