forked from keepandroidopen/keepandroidopen.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemeToggle.astro
More file actions
83 lines (76 loc) · 3.05 KB
/
Copy pathThemeToggle.astro
File metadata and controls
83 lines (76 loc) · 3.05 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
<button id="theme-toggle" aria-label="Toggle theme" title="Toggle theme">
<svg id="moon-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-moon">
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path>
</svg>
<svg id="sun-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-sun" style="display: none;">
<circle cx="12" cy="12" r="5"></circle>
<line x1="12" y1="1" x2="12" y2="3"></line>
<line x1="12" y1="21" x2="12" y2="23"></line>
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
<line x1="1" y1="12" x2="3" y2="12"></line>
<line x1="21" y1="12" x2="23" y2="12"></line>
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
</svg>
</button>
<style>
/*!
* @license magnet:?xt=urn:btih:8e4f440f4c65981c5bf93c76d35135ba5064d8b7&dn=apache-2.0.txt Apache-2.0
* Keep Android Open – Theme Toggle Styles
* Copyright 2025-2026 Keep Android Open Contributors
* SPDX-License-Identifier: Apache-2.0
*/
#theme-toggle {
color: var(--pico-text);
background: none;
border: none;
cursor: pointer;
padding: 0.5rem;
transition: all 0.3s ease;
}
#theme-toggle:hover {
opacity: 0.8;
}
</style>
<script is:inline>
/**
* @license magnet:?xt=urn:btih:8e4f440f4c65981c5bf93c76d35135ba5064d8b7&dn=apache-2.0.txt Apache-2.0
* Keep Android Open – Theme Toggle
* Copyright 2025–2026 Keep Android Open Contributors
* SPDX-License-Identifier: Apache-2.0
*/
const themeToggle = document.getElementById('theme-toggle');
const htmlElement = document.documentElement;
const moonIcon = document.getElementById('moon-icon');
const sunIcon = document.getElementById('sun-icon');
const getSavedTheme = () => localStorage.getItem('theme');
const getPreferredTheme = () => window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
const setThemeIcons = (theme) => {
if (theme === 'dark') {
moonIcon.style.display = 'block';
sunIcon.style.display = 'none';
} else {
moonIcon.style.display = 'none';
sunIcon.style.display = 'block';
}
};
const applyTheme = (theme) => {
if (theme === 'dark') {
htmlElement.setAttribute('data-theme', 'dark');
} else {
htmlElement.setAttribute('data-theme', 'light');
}
localStorage.setItem('theme', theme);
setThemeIcons(theme);
};
// Set theme on initial load
const initialTheme = getSavedTheme() || getPreferredTheme();
applyTheme(initialTheme);
themeToggle.addEventListener('click', () => {
const currentTheme = htmlElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'light';
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
applyTheme(newTheme);
});
// @license-end
</script>