-
Notifications
You must be signed in to change notification settings - Fork 0
/
dark-mode.min.js
22 lines (22 loc) · 4.9 KB
/
dark-mode.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
# Dark Mode Toggle Script (Minified)
#
# Author:
# Name: Marco Steinbrecher
# Email: dtm@steinbrecher.co
# GitHub: https://github.com/MacSteini/dark-mode-toggle
#
# Provides full functionality for toggling dark mode. Includes:
# - Localised ARIA labels and tooltips
# - Adaptations for RTL languages and touch devices
# - User preference handling (localStorage)
#
# Features:
# - Supports localisation for tooltips and accessibility
# - Detects and adapts to RTL languages
# - Automatically respects system colour-scheme preferences
# - Accessible and mobile-friendly design
#
# MIT Licence applies. See GitHub for details.
*/
(()=>{const t=document.getElementById("darkModeToggleBox");t&&t.classList.remove("hidden");const e={ar:{switchToLight:"التبديل إلى الوضع الفاتح",switchToDark:"التبديل إلى الوضع الداكن"},bg:{switchToLight:"Превключване към светъл режим",switchToDark:"Превключване към тъмен режим"},de:{switchToLight:"Zum hellen Modus wechseln",switchToDark:"Zum dunklen Modus wechseln"},en:{switchToLight:"Switch to light mode",switchToDark:"Switch to dark mode"},es:{switchToLight:"Cambiar al modo claro",switchToDark:"Cambiar al modo oscuro"},fr:{switchToLight:"Passer au mode clair",switchToDark:"Passer au mode sombre"},he:{switchToLight:"עבור למצב בהיר",switchToDark:"עבור למצב כהה"},fa:{switchToLight:"تغییر به حالت روشن",switchToDark:"تغییر به حالت تاریک"},hi:{switchToLight:"हल्के मोड में स्विच करें",switchToDark:"डार्क मोड में स्विच करें"},hu:{switchToLight:"Váltás világos módra",switchToDark:"Váltás sötét módra"},it:{switchToLight:"Passa alla modalità chiara",switchToDark:"Passa alla modalità scura"},ja:{switchToLight:"ライトモードに切り替え",switchToDark:"ダークモードに切り替え"},ko:{switchToLight:"라이트 모드로 전환",switchToDark:"다크 모드로 전환"},pt:{switchToLight:"Mudar para o modo claro",switchToDark:"Mudar para o modo escuro"},ro:{switchToLight:"Schimbați la modul luminos",switchToDark:"Schimbați la modul întunecat"},ru:{switchToLight:"Переключиться на светлый режим",switchToDark:"Переключиться на тёмный режим"},ur:{switchToLight:"ہلکے موڈ پر جائیں",switchToDark:"ڈارک موڈ پر جائیں"},"zh-CN":{switchToLight:"切换到浅色模式",switchToDark:"切换到深色模式"},"zh-TW":{switchToLight:"切換到淺色模式",switchToDark:"切換到深色模式"},zh:{switchToLight:"切换到浅色模式",switchToDark:"切换到深色模式"}},o=(()=>{const t=navigator.language||navigator.userLanguage,o=t.slice(0,2).toLowerCase(),a=t.slice(0,5).toLowerCase();return e[a]?e[a]:e[o]||e.en})(),a=(t,e=!1)=>{document.documentElement.getAttribute("data-theme")!==t&&(document.documentElement.setAttribute("data-theme",t),e?localStorage.setItem("theme",t):localStorage.getItem("theme")||localStorage.removeItem("theme"),i(t))},i=t=>{const e=document.getElementById("darkModeToggle");if(!e)return;const a=e.querySelector("i");if(!a)return;const i="dark"===t?o.switchToLight:o.switchToDark;e.setAttribute("aria-label",i),e.setAttribute("aria-checked","dark"===t),a.className="dark"===t?"fas fa-moon":"fas fa-sun",s(e)},s=t=>{const e=t.getBoundingClientRect();t.removeAttribute("data-tooltip-position"),e.left<50?t.setAttribute("data-tooltip-position","left"):e.right>window.innerWidth-50?t.setAttribute("data-tooltip-position","right"):t.setAttribute("data-tooltip-position","top")};document.addEventListener("DOMContentLoaded",(()=>{(()=>{const t=navigator.language||navigator.userLanguage,e=(t=>{const e=t.slice(0,2).toLowerCase();return["ar","he","fa","ur"].includes(e)})(t)?"rtl":"ltr";document.documentElement.setAttribute("lang",t.toLowerCase()),document.documentElement.setAttribute("dir",e);const o=document.getElementById("darkModeToggle");o&&("rtl"===e?o.setAttribute("data-tooltip-align","left"):o.setAttribute("data-tooltip-align","right"))})(),(()=>{const t=document.getElementById("darkModeToggle");t&&t.addEventListener("click",(()=>{const e="dark"===document.documentElement.getAttribute("data-theme")?"light":"dark";t.style.background="transparent",a(e,!0)}))})(),(()=>{const t=document.getElementById("darkModeToggle");if(!t)return;("ontouchstart"in window||navigator.maxTouchPoints>0)&&(t.classList.add("no-tooltip"),t.classList.add("no-focus"))})()})),(()=>{const t=localStorage.getItem("theme"),e=window.matchMedia("(prefers-color-scheme: dark)").matches;a(t||(e?"dark":"light"),!1),(()=>{const t=window.matchMedia("(prefers-color-scheme: dark)");"function"==typeof t.addEventListener?t.addEventListener("change",(t=>{localStorage.getItem("theme")||a(t.matches?"dark":"light")})):"function"==typeof t.addListener&&t.addListener((t=>{localStorage.getItem("theme")||a(t.matches?"dark":"light")}))})()})()})();