Skip to content

Commit 9f81f2d

Browse files
committed
Refactor common theme support into heatmap_shared.js
1 parent e501054 commit 9f81f2d

File tree

3 files changed

+38
-53
lines changed

3 files changed

+38
-53
lines changed

Lib/profiling/sampling/_heatmap_assets/heatmap.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,13 @@ let coldCodeHidden = false;
1515
// ============================================================================
1616

1717
function toggleTheme() {
18-
const html = document.documentElement;
19-
const current = html.getAttribute('data-theme') || 'light';
20-
const next = current === 'light' ? 'dark' : 'light';
21-
html.setAttribute('data-theme', next);
22-
localStorage.setItem('heatmap-theme', next);
23-
24-
// Update theme button icon
25-
const btn = document.getElementById('theme-btn');
26-
if (btn) {
27-
btn.querySelector('.icon-moon').style.display = next === 'dark' ? 'none' : '';
28-
btn.querySelector('.icon-sun').style.display = next === 'dark' ? '' : 'none';
29-
}
18+
toggleAndSaveTheme();
3019
applyLineColors();
3120

3221
// Rebuild scroll marker with new theme colors
3322
buildScrollMarker();
3423
}
3524

36-
function restoreUIState() {
37-
// Restore theme from localStorage, or use browser preference
38-
let theme = localStorage.getItem('heatmap-theme');
39-
if (!theme) {
40-
theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
41-
}
42-
document.documentElement.setAttribute('data-theme', theme);
43-
const btn = document.getElementById('theme-btn');
44-
if (btn) {
45-
btn.querySelector('.icon-moon').style.display = theme === 'dark' ? 'none' : '';
46-
btn.querySelector('.icon-sun').style.display = theme === 'dark' ? '' : 'none';
47-
}
48-
}
49-
5025
// ============================================================================
5126
// Utility Functions
5227
// ============================================================================

Lib/profiling/sampling/_heatmap_assets/heatmap_index.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,10 @@ function applyHeatmapBarColors() {
1919
// ============================================================================
2020

2121
function toggleTheme() {
22-
const html = document.documentElement;
23-
const current = html.getAttribute('data-theme') || 'light';
24-
const next = current === 'light' ? 'dark' : 'light';
25-
html.setAttribute('data-theme', next);
26-
localStorage.setItem('heatmap-theme', next);
27-
28-
// Update theme button icon
29-
const btn = document.getElementById('theme-btn');
30-
if (btn) {
31-
btn.querySelector('.icon-moon').style.display = next === 'dark' ? 'none' : '';
32-
btn.querySelector('.icon-sun').style.display = next === 'dark' ? '' : 'none';
33-
}
34-
22+
toggleAndSaveTheme();
3523
applyHeatmapBarColors();
3624
}
3725

38-
function restoreUIState() {
39-
// Restore theme from localStorage, or use browser preference
40-
let theme = localStorage.getItem('heatmap-theme');
41-
if (!theme) {
42-
theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
43-
}
44-
document.documentElement.setAttribute('data-theme', theme);
45-
const btn = document.getElementById('theme-btn');
46-
if (btn) {
47-
btn.querySelector('.icon-moon').style.display = theme === 'dark' ? 'none' : '';
48-
btn.querySelector('.icon-sun').style.display = theme === 'dark' ? '' : 'none';
49-
}
50-
}
51-
5226
// ============================================================================
5327
// Type Section Toggle (stdlib, project, etc)
5428
// ============================================================================

Lib/profiling/sampling/_heatmap_assets/heatmap_shared.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,42 @@ function intensityToColor(intensity) {
3939
return rootStyle.getPropertyValue(`--heat-${level}`).trim();
4040
}
4141

42+
// ============================================================================
43+
// Theme Support
44+
// ============================================================================
45+
46+
// Get the preferred theme from localStorage or browser preference
47+
function getPreferredTheme() {
48+
const saved = localStorage.getItem('heatmap-theme');
49+
if (saved) return saved;
50+
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
51+
}
52+
53+
// Apply theme and update UI. Returns the applied theme.
54+
function applyTheme(theme) {
55+
document.documentElement.setAttribute('data-theme', theme);
56+
const btn = document.getElementById('theme-btn');
57+
if (btn) {
58+
btn.querySelector('.icon-moon').style.display = theme === 'dark' ? 'none' : '';
59+
btn.querySelector('.icon-sun').style.display = theme === 'dark' ? '' : 'none';
60+
}
61+
return theme;
62+
}
63+
64+
// Toggle theme and save preference. Returns the new theme.
65+
function toggleAndSaveTheme() {
66+
const current = document.documentElement.getAttribute('data-theme') || 'light';
67+
const next = current === 'light' ? 'dark' : 'light';
68+
applyTheme(next);
69+
localStorage.setItem('heatmap-theme', next);
70+
return next;
71+
}
72+
73+
// Restore theme from localStorage, or use browser preference
74+
function restoreUIState() {
75+
applyTheme(getPreferredTheme());
76+
}
77+
4278
// ============================================================================
4379
// Favicon (Reuse logo image as favicon)
4480
// ============================================================================

0 commit comments

Comments
 (0)