Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9a8e85b647 | ||
|
|
30325f4071 | ||
|
|
99e01b3de3 |
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
use Cake\Core\Configure;
|
||||
|
||||
/**
|
||||
* @var \App\View\AppView $this
|
||||
*/
|
||||
$strategy = Configure::read('CheeseCake.darkMode.strategy', 'dataset');
|
||||
|
||||
?>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function (e) {
|
||||
const toggleDarkLink = document.getElementById('toggle-dark-mode');
|
||||
const toggleLightLink = document.getElementById('toggle-light-mode');
|
||||
const toggleAutoLink = document.getElementById('toggle-auto-mode');
|
||||
if (toggleDarkLink) {
|
||||
toggleDarkLink.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
localStorage.theme = 'dark';
|
||||
console.debug('dark');
|
||||
<?php if ($strategy === 'class') : ?>
|
||||
document.documentElement.classList.add('dark');
|
||||
document.documentElement.classList.remove('auto');
|
||||
<?php else : ?>
|
||||
document.documentElement.dataset.theme = 'dark';
|
||||
<?php endif; ?>
|
||||
|
||||
});
|
||||
}
|
||||
if (toggleLightLink) {
|
||||
toggleLightLink.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
localStorage.theme = 'light';
|
||||
console.debug('light');
|
||||
<?php if ($strategy === 'class') : ?>
|
||||
document.documentElement.classList.remove('auto');
|
||||
document.documentElement.classList.remove('dark');
|
||||
<?php else : ?>
|
||||
document.documentElement.dataset.theme = 'light';
|
||||
<?php endif; ?>
|
||||
});
|
||||
}
|
||||
if (toggleAutoLink) {
|
||||
toggleAutoLink.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
localStorage.theme = 'auto';
|
||||
<?php if ($strategy === 'class') : ?>
|
||||
document.documentElement.classList.add('auto');
|
||||
document.documentElement.classList.remove('dark');
|
||||
|
||||
<?php else : ?>
|
||||
delete document.documentElement.dataset.theme;
|
||||
<?php endif; ?>
|
||||
});
|
||||
}
|
||||
})
|
||||
</script>
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
use Cake\Core\Configure;
|
||||
|
||||
/**
|
||||
* @var \App\View\AppView $this
|
||||
*/
|
||||
$strategy = Configure::read('CheeseCake.darkMode.strategy', 'dataset');
|
||||
|
||||
?>
|
||||
<script>
|
||||
// On page load or when changing themes, best to add inline in `head` to avoid FOUC
|
||||
<?php if ($strategy === 'class') : ?>
|
||||
|
||||
document.documentElement.classList.toggle(
|
||||
"dark",
|
||||
localStorage.theme === "dark" ||
|
||||
(!("theme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches),
|
||||
);
|
||||
document.documentElement.classList.toggle(
|
||||
"auto",
|
||||
localStorage.theme === "auto"
|
||||
);
|
||||
<?php else : ?>
|
||||
localStorage.theme === "dark" ||
|
||||
(!("theme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches) ?
|
||||
document.documentElement.dataset.theme = "dark" :
|
||||
document.documentElement.dataset.theme = "light";
|
||||
<?php endif; ?>
|
||||
|
||||
// Whenever the user explicitly chooses light mode
|
||||
|
||||
// localStorage.theme = "light";
|
||||
// // Whenever the user explicitly chooses dark mode
|
||||
// localStorage.theme = "dark";
|
||||
// // Whenever the user explicitly chooses to respect the OS preference
|
||||
// localStorage.removeItem("theme");
|
||||
</script>
|
||||
Reference in New Issue
Block a user