-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
98 lines (81 loc) · 2.91 KB
/
theme.js
File metadata and controls
98 lines (81 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Theme Controller for Drum Game
const ThemeController = {
STORAGE_KEY: 'drum-game-theme',
DEFAULT_THEME: 'analog-warmth',
themes: [
{ id: 'analog-warmth', name: 'Analog Warmth', color: '#1c1410' },
{ id: 'dark-synth', name: 'Dark Synth', color: '#1a1a2e' },
{ id: 'retrowave', name: 'Retrowave', color: '#1a0a2e' },
{ id: 'neon-club', name: 'Neon Club', color: '#0a0a0f' },
{ id: 'midnight-studio', name: 'Midnight Studio', color: '#0d1117' },
],
init() {
const savedTheme = localStorage.getItem(this.STORAGE_KEY) || this.DEFAULT_THEME;
this.setTheme(savedTheme);
this.createSelector();
this.bindEvents();
},
setTheme(themeName) {
// Validate theme exists
const theme = this.themes.find(t => t.id === themeName);
if (!theme) {
themeName = this.DEFAULT_THEME;
}
document.documentElement.setAttribute('data-theme', themeName);
localStorage.setItem(this.STORAGE_KEY, themeName);
// Update meta theme-color for mobile browsers
const metaThemeColor = document.querySelector('meta[name="theme-color"]');
if (metaThemeColor && theme) {
metaThemeColor.setAttribute('content', theme.color);
}
// Update selector if it exists
const selector = document.getElementById('theme-select');
if (selector) {
selector.value = themeName;
}
},
createSelector() {
// Find the instructions paragraph to insert after
const gameSection = document.querySelector('.game');
const instructions = gameSection?.querySelector('p');
if (!instructions) return;
// Create theme switcher paragraph
const switcher = document.createElement('p');
switcher.className = 'theme-switcher';
const label = document.createElement('span');
label.className = 'theme-label';
label.textContent = 'Theme:';
const select = document.createElement('select');
select.id = 'theme-select';
select.className = 'theme-selector';
select.setAttribute('aria-label', 'Select theme');
// Add theme options
this.themes.forEach(theme => {
const option = document.createElement('option');
option.value = theme.id;
option.textContent = theme.name;
select.appendChild(option);
});
// Set current value
const currentTheme = localStorage.getItem(this.STORAGE_KEY) || this.DEFAULT_THEME;
select.value = currentTheme;
switcher.appendChild(label);
switcher.appendChild(select);
// Insert after the instructions paragraph
instructions.insertAdjacentElement('afterend', switcher);
},
bindEvents() {
// Use event delegation for the selector
document.addEventListener('change', (e) => {
if (e.target.id === 'theme-select') {
this.setTheme(e.target.value);
}
});
}
};
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => ThemeController.init());
} else {
ThemeController.init();
}