Fix theme toggle broken by shared DOMContentLoaded scope#2
Draft
Fix theme toggle broken by shared DOMContentLoaded scope#2
Conversation
Co-authored-by: bacanijesse <66404384+bacanijesse@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix dark/light theme toggle functionality
Fix theme toggle broken by shared DOMContentLoaded scope
Feb 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A
returninside the theme guard and shared function scope with the fetch chain meant any early exit in ride-loading could silently kill theme initialization.Changes
assets/js/main.js— ExtractloadRides()andinitTheme()as independent top-level functions;DOMContentLoadedcalls both unconditionally. Thereturnin the missing-element guard now only exitsinitTheme().assets/css/styles.css— Extendbodytransition to includecolorso text smoothly follows background on theme switch.Original prompt
Problem
The dark/light theme toggle is not working — clicking the 🌙 button does not switch between dark and light mode.
Root Cause
In
assets/js/main.js, the entire script is inside a singleDOMContentLoadedcallback. The theme system code (lines 43-75) runs after thefetch()Promise chain (lines 4-41). The problem is that iffetch("data/rides.json")fails (which it does when served as a staticfile://page or if the JSON path is wrong), the.catch()block on line 35-41 runs, but sincefetchis async, the theme code still runs synchronously.However, there's a subtle issue: the
returnstatement on line 49 (return;inside theif (!toggleBtn || !icon)guard) exits the entireDOMContentLoadedcallback function. If for any reason the elements aren't found at that point, the theme system never initializes.But the real fix needed is: The theme toggle code and the ride-loading code should be completely independent of each other. Currently they share the same function scope, and any uncaught error in the fetch chain or the rides rendering could prevent the theme system from initializing.
Required Fix
In
assets/js/main.js, restructure the code so that:loadRides()) called fromDOMContentLoadedinitTheme()) called fromDOMContentLoadedreturn;in the theme guard to just skip the theme init (not exit the whole callback)Here's the current file:
The fixed version should look like this: