summaryrefslogtreecommitdiffstats
path: root/static/theme-manager.js
blob: 2ad080b5dd8ca71ab8f5adc341e8a86b2a33626e (plain) (blame)
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
/* jshint esversion: 2024, module: true */

/**
 * Theme Manager for handling light/dark themes
 * @class ThemeManager
 */
class ThemeManager {
    /**
     * @param {Object} elements - DOM elements
     */
    constructor(elements) {
        this.elements = elements;
        this.theme = localStorage.getItem('network-ui-theme') || 'dark';
    }

    /**
     * Initialize theme manager
     * @method init
     */
    init() {
        this.applyTheme(this.theme);
        this.setupEventListeners();
    }

    /**
     * Set up theme event listeners
     * @method setupEventListeners
     */
    setupEventListeners() {
        this.elements.themeToggle?.addEventListener('click', () => this.toggleTheme());
    }

    /**
     * Toggle between light and dark themes
     * @method toggleTheme
     */
    toggleTheme() {
        const newTheme = this.theme === 'dark' ? 'light' : 'dark';
        this.applyTheme(newTheme);
    }

    /**
     * Apply theme to document
     * @method applyTheme
     * @param {string} theme - Theme name ('light' or 'dark')
     */
    applyTheme(theme) {
        document.documentElement.setAttribute('data-theme', theme);
        this.theme = theme;
        localStorage.setItem('network-ui-theme', theme);

        // Update theme icon
        if (this.elements.themeIcon) {
            this.elements.themeIcon.textContent = theme === 'dark' ? '☀️' : '🌙';
        }
    }
}

export { ThemeManager };