Skip to content

Commit 599350b

Browse files
committed
Performance updates
Testing moving all critical logic out to a direct inline JS in the <head> while the rest remains in this external file and will be deferred.
1 parent 4f0eb75 commit 599350b

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

global-brand-code/brand-color-modes.js

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// ---- Color Mode Management ----
1+
// ---- Color Mode Management (Deferred) ----
2+
// Note: Initial color mode should be set inline in <head> to prevent flicker
23

34
(function () {
45
// Function to swap icon URLs between Light and Dark modes
56
function swapIconUrls(isDark) {
67
const iconElements = document.querySelectorAll("[data-component='icon']");
78
const modeToUse = isDark ? "Dark" : "Light";
8-
const modeToReplace = isDark ? "Light" : "Dark";
99

1010
iconElements.forEach((img) => {
1111
if (img.tagName.toLowerCase() === "img" && img.src) {
@@ -79,30 +79,19 @@
7979
}
8080
}
8181

82-
// Check if user preference exists in localStorage
83-
const savedMode = localStorage.getItem("darkMode");
84-
let prefersDark;
85-
let usingOSPreference = false;
86-
87-
// If user has explicitly set a preference (via the buttons), use that
88-
if (savedMode !== null) {
89-
prefersDark = savedMode === "true";
90-
}
91-
// Otherwise, check the OS preference
92-
else {
93-
// Check if the browser supports prefers-color-scheme media query
94-
const prefersColorScheme = window.matchMedia(
95-
"(prefers-color-scheme: dark)"
96-
);
97-
prefersDark = prefersColorScheme.matches;
98-
usingOSPreference = true;
99-
}
82+
// Initialize when DOM is ready
83+
function initializeColorModeUI() {
84+
// Get current state from what was already set inline
85+
const isDarkMode =
86+
document.documentElement.classList.contains("u-mode-dark");
87+
const savedMode = localStorage.getItem("darkMode");
88+
const isAutoMode = savedMode === null;
89+
let usingOSPreference = isAutoMode;
10090

101-
// Apply mode immediately (before page render to prevent flash)
102-
setColorMode(prefersDark, !usingOSPreference, usingOSPreference);
91+
// Ensure icons are set correctly on initial load
92+
swapIconUrls(isDarkMode);
10393

104-
// Set up event listeners for buttons once DOM is loaded
105-
window.addEventListener("DOMContentLoaded", function () {
94+
// Get all button elements
10695
const lightButtons = document.querySelectorAll(
10796
'[data-mode-button="light"]'
10897
);
@@ -120,17 +109,7 @@
120109
button.classList.remove("cc-active-mode");
121110
});
122111

123-
// Set initial state based on current mode
124-
const isDarkMode =
125-
document.documentElement.classList.contains("u-mode-dark");
126-
127-
// Ensure icons are set correctly on initial load
128-
swapIconUrls(isDarkMode);
129-
130-
// Check if we're currently using OS preference (no saved mode)
131-
const savedMode = localStorage.getItem("darkMode");
132-
const isAutoMode = savedMode === null;
133-
112+
// Set initial button states based on current mode
134113
if (isAutoMode) {
135114
autoButtons.forEach((button) => {
136115
button.classList.add("cc-active-mode");
@@ -186,5 +165,13 @@
186165
setColorMode(e.matches, false);
187166
}
188167
});
189-
});
168+
}
169+
170+
// Initialize the UI when DOM is ready
171+
if (document.readyState === "loading") {
172+
document.addEventListener("DOMContentLoaded", initializeColorModeUI);
173+
} else {
174+
// DOM already loaded
175+
initializeColorModeUI();
176+
}
190177
})();

0 commit comments

Comments
 (0)