fix: dispatch themeChanged event for instant UI synchronization#160
fix: dispatch themeChanged event for instant UI synchronization#160Lilaschromish wants to merge 2 commits into
Conversation
📝 WalkthroughWalkthroughAdded intra-app theme-change notification: Changes
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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 📝 Coding Plan
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. Comment Tip CodeRabbit can generate a title for your PR based on the changes with custom instructions.Set the |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
scripts/theme.js (1)
47-57: Emit resolved mode forsystemand 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 aneffectiveThemeand dispatching again from the media-query listener whensystemis 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".
Description
This PR implements real-time theme synchronization across UI components. It adds a
themeChangedevent that broadcasts both the selected mode and the effective (resolved) color.Changes
emitThemeChangedhelper to resolve system themes.applyThemeModeto dispatch events.Checklist