PageSourceSearch

https://grantha.lk/static/version1790264966/frontend/Grantha/storefront/en_US/js/theme-switcher.js

js grantha.lk collected 2026-09-25 01:09:23 UTC 2,105 bytes, 67 lines download raw bytes

1/**
2 * Grantha Storefront — Theme Switcher (Dual Theme: Light / Dark Mode)
3 */
4(function () {
5    'use strict';
6
7    function getPreferredTheme() {
8        try {
9            var saved = localStorage.getItem('grantha-theme');
10            if (saved === 'dark' || saved === 'light') {
11                return saved;
12            }
13        } catch (e) {}
14
15        // Default storefront experience is always Light mode
16        return 'light';
17    }
18
19    function applyTheme(theme) {
20        document.documentElement.setAttribute('data-theme', theme);
21        try {
22            localStorage.setItem('grantha-theme', theme);
23        } catch (e) {}
24
25        updateToggleButtons(theme);
26    }
27
28    function updateToggleButtons(theme) {
29        var buttons = document.querySelectorAll('.theme-toggle-btn');
30        buttons.forEach(function (btn) {
31            var icon = btn.querySelector('i');
32            var label = btn.querySelector('.theme-label');
33            if (theme === 'dark') {
34                if (icon) icon.className = 'ph ph-sun';
35                if (label) label.textContent = 'Light';
36            } else {
37                if (icon) icon.className = 'ph ph-moon';
38                if (label) label.textContent = 'Dark';
39            }
40        });
41    }
42
43    // Initialize immediately
44    var initialTheme = getPreferredTheme();
45    document.documentElement.setAttribute('data-theme', initialTheme);
46
47    document.addEventListener('DOMContentLoaded', function () {
48        updateToggleButtons(initialTheme);
49
50        document.addEventListener('click', function (e) {
51            var btn = e.target.closest('.theme-toggle-btn');
52            if (btn) {
53                e.preventDefault();
54                var current = document.documentElement.getAttribute('data-theme') || 'light';
55                var next = current === 'dark' ? 'light' : 'dark';
56                applyTheme(next);
57            }
58        });
59    });
60
61    window.GranthaTheme = {
62        get: function () {
63            return document.documentElement.getAttribute('data-theme') || 'light';
64        },
65        set: applyTheme
66    };
67})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.