-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:TYPO3incubator/surfcamp-team6
- Loading branch information
Showing
4 changed files
with
98 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,79 @@ | ||
const Theme = { | ||
DARK: 'theme-dark', | ||
LIGHT: 'theme-light' | ||
}; | ||
|
||
const systemSettingDark = window.matchMedia("(prefers-color-scheme: dark)"); | ||
const themeToggle = document.querySelector("[data-theme-toggle]"); | ||
const onButton = themeToggle.querySelector('.on') | ||
const offButton = themeToggle.querySelector('.off') | ||
|
||
function getCurrentTheme() { | ||
const storedTheme = localStorage.getItem("theme"); | ||
if (storedTheme !== null) { | ||
return storedTheme; | ||
} | ||
return systemSettingDark.matches ? Theme.DARK : Theme.LIGHT; | ||
} | ||
|
||
let currentThemeSetting = getCurrentTheme(); | ||
|
||
function applyTheme(theme) { | ||
const el = document.querySelector('.theme-js'); | ||
el.classList.remove(Theme.DARK, Theme.LIGHT); // Remove both to ensure clean state | ||
el.classList.add(theme); | ||
updateToggleButtonState(theme) | ||
} | ||
|
||
function changeTheme(theme, saveToLocalStorage = true) { | ||
if (theme === undefined) { | ||
theme = currentThemeSetting === Theme.DARK ? Theme.LIGHT : Theme.DARK; | ||
} | ||
|
||
if (saveToLocalStorage) { | ||
localStorage.setItem("theme", theme); | ||
} | ||
|
||
currentThemeSetting = theme; | ||
applyTheme(theme); | ||
} | ||
|
||
themeToggle.addEventListener("click", function(e) { | ||
e.preventDefault(); | ||
changeTheme(); | ||
}); | ||
|
||
function initTheme() { | ||
applyTheme(currentThemeSetting); | ||
colorThemeWatcher(); | ||
let el = document.querySelector('.theme-js'); | ||
if (el.classList.contains('theme-system')) { | ||
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { | ||
toggleDarkTheme(true); | ||
} | ||
} | ||
updateToggleButtonState(currentThemeSetting); | ||
} | ||
|
||
function colorThemeWatcher() { | ||
try { | ||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => { | ||
toggleDarkTheme(e.matches ? true : false); | ||
}); | ||
} catch { | ||
console.warn('color theme watcher not supported'); | ||
} | ||
systemSettingDark.addEventListener('change', e => { | ||
if (localStorage.getItem("theme") == null) { | ||
const newTheme = e.matches ? Theme.DARK : Theme.LIGHT; | ||
changeTheme(newTheme, false); | ||
} | ||
}); | ||
} | ||
|
||
function toggleDarkTheme(setting) { | ||
let el = document.querySelector('.theme-js'); | ||
if (setting) { | ||
el.classList.add('theme-dark'); | ||
function updateToggleButtonState(theme) { | ||
const onButton = document.querySelector('.on'); | ||
const offButton = document.querySelector('.off'); | ||
const themeToggleDot = document.querySelector('.themeToggler .dot') | ||
|
||
if (theme === Theme.DARK) { | ||
onButton.classList.remove('hidden'); | ||
offButton.classList.add('hidden'); | ||
themeToggle.checked = true; | ||
themeToggleDot.style.transform = 'translateX(1.25rem)'; | ||
} else { | ||
el.classList.remove('theme-dark'); | ||
onButton.classList.add('hidden'); | ||
offButton.classList.remove('hidden'); | ||
themeToggle.checked = false; | ||
themeToggleDot.style.transform = 'translateX(0)' | ||
} | ||
} | ||
|
||
initTheme(); | ||
initTheme(); |
20 changes: 7 additions & 13 deletions
20
local_packages/success/Configuration/Sets/Career/settings.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters