-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (98 loc) · 3.07 KB
/
Copy pathscript.js
File metadata and controls
114 lines (98 loc) · 3.07 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Theme switching
const themeToggle = document.getElementById('themeToggle');
const themes = ['system', 'light', 'dark'];
let currentThemeIndex = 0;
// Get the stored theme or default to 'system'
const getStoredTheme = () => localStorage.getItem('theme') || 'system';
// Get the preferred color scheme based on system settings
const getSystemTheme = () => {
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
};
// Get the display name for current theme
const getThemeDisplayName = (theme) => {
const names = {
'system': 'System',
'light': 'Light',
'dark': 'Dark'
};
return names[theme] || theme;
};
// Update the button icon based on theme
const updateThemeIcon = (theme) => {
const icons = {
'system': 'fa-desktop',
'light': 'fa-sun',
'dark': 'fa-moon'
};
const icon = themeToggle.querySelector('i');
icon.className = `fas ${icons[theme]}`;
};
// Update the tooltip based on current and next theme
const updateThemeTooltip = () => {
const currentTheme = themes[currentThemeIndex];
const nextTheme = themes[(currentThemeIndex + 1) % themes.length];
const currentName = getThemeDisplayName(currentTheme);
const nextName = getThemeDisplayName(nextTheme);
themeToggle.setAttribute('title', `Current: ${currentName} | Next: ${nextName}`);
};
// Apply theme to the document
const applyTheme = (theme) => {
let finalTheme = theme;
if (theme === 'system') {
finalTheme = getSystemTheme();
}
if (finalTheme === 'dark') {
document.documentElement.setAttribute('data-theme', 'dark');
} else {
document.documentElement.removeAttribute('data-theme');
}
};
// Initialize theme on page load
const initTheme = () => {
const storedTheme = getStoredTheme();
currentThemeIndex = themes.indexOf(storedTheme);
applyTheme(storedTheme);
updateThemeIcon(storedTheme);
updateThemeTooltip();
};
// Toggle to next theme
themeToggle.addEventListener('click', () => {
currentThemeIndex = (currentThemeIndex + 1) % themes.length;
const nextTheme = themes[currentThemeIndex];
localStorage.setItem('theme', nextTheme);
applyTheme(nextTheme);
updateThemeIcon(nextTheme);
updateThemeTooltip();
// Visual feedback
themeToggle.style.transform = 'scale(1.1)';
setTimeout(() => {
themeToggle.style.transform = 'scale(1)';
}, 150);
});
// Listen for system theme changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
const storedTheme = getStoredTheme();
if (storedTheme === 'system') {
applyTheme('system');
}
});
// Scroll to top button
const scrollToTopBtn = document.getElementById('scrollToTop');
// Show/hide scroll to top button based on scroll position
window.addEventListener('scroll', () => {
if (window.scrollY > 300) {
scrollToTopBtn.style.display = 'flex';
} else {
scrollToTopBtn.style.display = 'none';
}
});
// Scroll to top smoothly when button is clicked
scrollToTopBtn.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
// Initialize
scrollToTopBtn.style.display = 'none';
initTheme();