diff --git a/index.html b/index.html index c8c477e..a21350a 100644 --- a/index.html +++ b/index.html @@ -25,7 +25,7 @@ Tor ( Mirror ) - + diff --git a/js/toggle.js b/js/toggle.js index b364ab0..fe70698 100644 --- a/js/toggle.js +++ b/js/toggle.js @@ -1,30 +1,60 @@ const moon = document.getElementById('moon'); const sun = document.getElementById('sun'); -moon.addEventListener('click', () => { +if (!document.cookie.includes('preference')) { + setPreference(true); +} else { + const preference = document.cookie.split(';').find(cookie => cookie.includes('preference')).split('=')[1]; + if (preference === 'true') { + turnLight(); + } else { + turnDark(); + } + +} + +function setPreference(value) { + if (typeof value === 'boolean') { + const expires = new Date(); + expires.setTime(expires.getTime() + 1000 * 60 * 60); // 1 hour + document.cookie = `preference=${value};expires=${expires.toUTCString()};path=/`; + } +} + +function turnLight() { document.documentElement.classList.add('light'); - // Fade out moon and fade in sun moon.style.opacity = '0'; sun.style.opacity = '1'; - // After the transition ends, hide the moon and show the sun (for accessibility) setTimeout(() => { moon.style.display = 'none'; sun.style.display = 'block'; - }, 100); // Match the duration of the transition (100ms) + } + , 100); +} + +function turnDark() { + document.documentElement.classList.remove('light'); + + sun.style.opacity = '0'; + moon.style.opacity = '1'; + + setTimeout(() => { + sun.style.display = 'none'; + moon.style.display = 'block'; + } + , 100); +} + +moon.addEventListener('click', () => { + document.documentElement.classList.add('light'); + setPreference(true); + turnLight(); }); sun.addEventListener('click', () => { document.documentElement.classList.remove('light'); - - // Fade out sun and fade in moon - sun.style.opacity = '0'; - moon.style.opacity = '1'; - - // After the transition ends, hide the sun and show the moon (for accessibility) - setTimeout(() => { - sun.style.display = 'none'; - moon.style.display = 'block'; - }, 100); // Match the duration of the transition (100ms) + setPreference(false); + turnDark(); });