Add possibility to reset theme to auto

This commit is contained in:
schlagmichdoch 2023-03-28 17:26:22 +02:00
parent 1bb8a63eed
commit ac1e88b6a0
6 changed files with 326 additions and 92 deletions

View file

@ -44,11 +44,25 @@
<use xlink:href="#info-outline" />
</svg>
</a>
<a id="theme" class="icon-button" title="Switch Darkmode/Lightmode" >
<svg class="icon">
<use xlink:href="#icon-theme" />
</svg>
</a>
<div id="theme-wrapper">
<a id="theme-auto" class="icon-button selected" title="Adapt to System" >
<svg class="icon">
<use xlink:href="#icon-theme-auto" />
</svg>
</a>
<div>
<a id="theme-light" class="icon-button" title="Always Light" >
<svg class="icon">
<use xlink:href="#icon-theme-light" />
</svg>
</a>
<a id="theme-dark" class="icon-button" title="Always Dark" >
<svg class="icon">
<use xlink:href="#icon-theme-dark" />
</svg>
</a>
</div>
</div>
<a id="notification" class="icon-button" title="Enable Notifications" hidden>
<svg class="icon">
<use xlink:href="#notifications" />
@ -328,7 +342,13 @@
<path d="M0 0h24v24H0z" fill="none" />
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1.41 16.09V20h-2.67v-1.93c-1.71-.36-3.16-1.46-3.27-3.4h1.96c.1 1.05.82 1.87 2.65 1.87 1.96 0 2.4-.98 2.4-1.59 0-.83-.44-1.61-2.67-2.14-2.48-.6-4.18-1.62-4.18-3.67 0-1.72 1.39-2.84 3.11-3.21V4h2.67v1.95c1.86.45 2.79 1.86 2.85 3.39H14.3c-.05-1.11-.64-1.87-2.22-1.87-1.5 0-2.4.68-2.4 1.64 0 .84.65 1.39 2.67 1.91s4.18 1.39 4.18 3.91c-.01 1.83-1.38 2.83-3.12 3.16z" />
</symbol>
<symbol id="icon-theme" viewBox="0 0 24 24">
<symbol id="icon-theme-auto" viewBox="0 0 24 24">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-54 -54 620 620"><!--! Font Awesome Pro 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M448 256c0-106-86-192-192-192V448c106 0 192-86 192-192zM0 256a256 256 0 1 1 512 0A256 256 0 1 1 0 256z"/></svg>
</symbol>
<symbol id="icon-theme-light" viewBox="0 0 24 24">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-54 -54 620 620"><!--! Font Awesome Pro 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M361.5 1.2c5 2.1 8.6 6.6 9.6 11.9L391 121l107.9 19.8c5.3 1 9.8 4.6 11.9 9.6s1.5 10.7-1.6 15.2L446.9 256l62.3 90.3c3.1 4.5 3.7 10.2 1.6 15.2s-6.6 8.6-11.9 9.6L391 391 371.1 498.9c-1 5.3-4.6 9.8-9.6 11.9s-10.7 1.5-15.2-1.6L256 446.9l-90.3 62.3c-4.5 3.1-10.2 3.7-15.2 1.6s-8.6-6.6-9.6-11.9L121 391 13.1 371.1c-5.3-1-9.8-4.6-11.9-9.6s-1.5-10.7 1.6-15.2L65.1 256 2.8 165.7c-3.1-4.5-3.7-10.2-1.6-15.2s6.6-8.6 11.9-9.6L121 121 140.9 13.1c1-5.3 4.6-9.8 9.6-11.9s10.7-1.5 15.2 1.6L256 65.1 346.3 2.8c4.5-3.1 10.2-3.7 15.2-1.6zM160 256a96 96 0 1 1 192 0 96 96 0 1 1 -192 0zm224 0a128 128 0 1 0 -256 0 128 128 0 1 0 256 0z"/></svg>
</symbol>
<symbol id="icon-theme-dark" viewBox="0 0 24 24">
<rect fill="none" height="24" width="24"/><path d="M12,3c-4.97,0-9,4.03-9,9s4.03,9,9,9s9-4.03,9-9c0-0.46-0.04-0.92-0.1-1.36c-0.98,1.37-2.58,2.26-4.4,2.26 c-2.98,0-5.4-2.42-5.4-5.4c0-1.81,0.89-3.42,2.26-4.4C12.92,3.04,12.46,3,12,3L12,3z"/>
</symbol>
<symbol id="pair-device-icon" viewBox="0 0 640 512">

View file

@ -1,39 +1,78 @@
(function(){
// Select the button
const btnTheme = document.getElementById('theme');
// Check for dark mode preference at the OS level
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
// Get the user's theme preference from local storage, if it's available
const currentTheme = localStorage.getItem('theme');
// If the user's preference in localStorage is dark...
const prefersDarkTheme = window.matchMedia('(prefers-color-scheme: dark)').matches;
const prefersLightTheme = window.matchMedia('(prefers-color-scheme: light)').matches;
const $themeAuto = document.getElementById('theme-auto');
const $themeLight = document.getElementById('theme-light');
const $themeDark = document.getElementById('theme-dark');
let currentTheme = localStorage.getItem('theme');
if (currentTheme === 'dark') {
// ...let's toggle the .dark-theme class on the body
document.body.classList.toggle('dark-theme');
// Otherwise, if the user's preference in localStorage is light...
setModeToDark();
} else if (currentTheme === 'light') {
// ...let's toggle the .light-theme class on the body
document.body.classList.toggle('light-theme');
setModeToLight();
}
// Listen for a click on the button
btnTheme.addEventListener('click', function(e) {
e.preventDefault();
// If the user's OS setting is dark and matches our .dark-theme class...
let theme;
if (prefersDarkScheme.matches) {
// ...then toggle the light mode class
document.body.classList.toggle('light-theme');
// ...but use .dark-theme if the .light-theme class is already on the body,
theme = document.body.classList.contains('light-theme') ? 'light' : 'dark';
$themeAuto.addEventListener('click', _ => {
if (currentTheme) {
setModeToAuto();
} else {
// Otherwise, let's do the same thing, but for .dark-theme
document.body.classList.toggle('dark-theme');
theme = document.body.classList.contains('dark-theme') ? 'dark' : 'light';
setModeToDark();
}
});
$themeLight.addEventListener('click', _ => {
if (currentTheme !== 'light') {
setModeToLight();
} else {
setModeToAuto();
}
});
$themeDark.addEventListener('click', _ => {
if (currentTheme !== 'dark') {
setModeToDark();
} else {
setModeToLight();
}
// Finally, let's save the current preference to localStorage to keep using it
localStorage.setItem('theme', theme);
});
function setModeToDark() {
document.body.classList.remove('light-theme');
document.body.classList.add('dark-theme');
localStorage.setItem('theme', 'dark');
currentTheme = 'dark';
$themeAuto.classList.remove("selected");
$themeLight.classList.remove("selected");
$themeDark.classList.add("selected");
}
function setModeToLight() {
document.body.classList.remove('dark-theme');
document.body.classList.add('light-theme');
localStorage.setItem('theme', 'light');
currentTheme = 'light';
$themeAuto.classList.remove("selected");
$themeLight.classList.add("selected");
$themeDark.classList.remove("selected");
}
function setModeToAuto() {
document.body.classList.remove('dark-theme');
document.body.classList.remove('light-theme');
if (prefersDarkTheme) {
document.body.classList.add('dark-theme');
} else if (prefersLightTheme) {
document.body.classList.add('light-theme');
}
localStorage.removeItem('theme');
currentTheme = undefined;
$themeAuto.classList.add("selected");
$themeLight.classList.remove("selected");
$themeDark.classList.remove("selected");
}
})();

View file

@ -75,11 +75,74 @@ html {
}
header {
position: relative;
height: 56px;
align-items: center;
padding: 16px;
position: absolute;
align-items: baseline;
padding: 8px 16px;
box-sizing: border-box;
width: 100vw;
z-index: 2;
top: 0;
right: 0;
}
header > a,
header > div {
margin-left: 8px;
}
header > div {
display: flex;
flex-direction: column;
align-self: flex-start;
touch-action: manipulation;
}
header > div a {
height: 40px;
transition: all 300ms;
}
header > div > div {
display: flex;
flex-direction: column;
}
header > div:not(:hover) a:not(.selected) {
height: 0;
opacity: 0;
}
header > div:hover::before {
border-radius: 20px;
background: currentColor;
opacity: 0.1;
transition: opacity 300ms;
content: '';
position: absolute;
width: 40px;
top: 0;
bottom: 0;
margin-top: 8px;
margin-bottom: 8px;
}
header > div:hover a.selected::before {
opacity: 0.1;
}
@media (pointer: coarse) {
header > div:hover a.selected:hover::before {
opacity: 0.2;
}
header > div a:not(.selected) {
height: 0;
opacity: 0;
pointer-events: none;
}
header > div > div {
flex-direction: column-reverse;
}
}
[hidden] {
@ -192,15 +255,10 @@ x-noscript {
}
}
/* Main Header */
body>header a {
margin-left: 8px;
}
#center {
position: relative;
display: flex;
margin-top: 56px;
flex-direction: column-reverse;
flex-grow: 1;
--footer-height: 132px;
@ -974,8 +1032,8 @@ button::-moz-focus-inner {
#about x-background {
position: absolute;
top: calc(32px - 250px);
right: calc(32px - 250px);
top: calc(28px - 250px);
right: calc(36px - 250px);
width: 500px;
height: 500px;
border-radius: 50%;

View file

@ -44,11 +44,25 @@
<use xlink:href="#info-outline" />
</svg>
</a>
<a id="theme" class="icon-button" title="Switch Darkmode/Lightmode" >
<svg class="icon">
<use xlink:href="#icon-theme" />
</svg>
</a>
<div id="theme-wrapper">
<a id="theme-auto" class="icon-button selected" title="Adapt to System" >
<svg class="icon">
<use xlink:href="#icon-theme-auto" />
</svg>
</a>
<div>
<a id="theme-light" class="icon-button" title="Always Light" >
<svg class="icon">
<use xlink:href="#icon-theme-light" />
</svg>
</a>
<a id="theme-dark" class="icon-button" title="Always Dark" >
<svg class="icon">
<use xlink:href="#icon-theme-dark" />
</svg>
</a>
</div>
</div>
<a id="notification" class="icon-button" title="Enable Notifications" hidden>
<svg class="icon">
<use xlink:href="#notifications" />
@ -331,7 +345,13 @@
<path d="M0 0h24v24H0z" fill="none" />
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1.41 16.09V20h-2.67v-1.93c-1.71-.36-3.16-1.46-3.27-3.4h1.96c.1 1.05.82 1.87 2.65 1.87 1.96 0 2.4-.98 2.4-1.59 0-.83-.44-1.61-2.67-2.14-2.48-.6-4.18-1.62-4.18-3.67 0-1.72 1.39-2.84 3.11-3.21V4h2.67v1.95c1.86.45 2.79 1.86 2.85 3.39H14.3c-.05-1.11-.64-1.87-2.22-1.87-1.5 0-2.4.68-2.4 1.64 0 .84.65 1.39 2.67 1.91s4.18 1.39 4.18 3.91c-.01 1.83-1.38 2.83-3.12 3.16z" />
</symbol>
<symbol id="icon-theme" viewBox="0 0 24 24">
<symbol id="icon-theme-auto" viewBox="0 0 24 24">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-54 -54 620 620"><!--! Font Awesome Pro 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M448 256c0-106-86-192-192-192V448c106 0 192-86 192-192zM0 256a256 256 0 1 1 512 0A256 256 0 1 1 0 256z"/></svg>
</symbol>
<symbol id="icon-theme-light" viewBox="0 0 24 24">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-54 -54 620 620"><!--! Font Awesome Pro 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M361.5 1.2c5 2.1 8.6 6.6 9.6 11.9L391 121l107.9 19.8c5.3 1 9.8 4.6 11.9 9.6s1.5 10.7-1.6 15.2L446.9 256l62.3 90.3c3.1 4.5 3.7 10.2 1.6 15.2s-6.6 8.6-11.9 9.6L391 391 371.1 498.9c-1 5.3-4.6 9.8-9.6 11.9s-10.7 1.5-15.2-1.6L256 446.9l-90.3 62.3c-4.5 3.1-10.2 3.7-15.2 1.6s-8.6-6.6-9.6-11.9L121 391 13.1 371.1c-5.3-1-9.8-4.6-11.9-9.6s-1.5-10.7 1.6-15.2L65.1 256 2.8 165.7c-3.1-4.5-3.7-10.2-1.6-15.2s6.6-8.6 11.9-9.6L121 121 140.9 13.1c1-5.3 4.6-9.8 9.6-11.9s10.7-1.5 15.2 1.6L256 65.1 346.3 2.8c4.5-3.1 10.2-3.7 15.2-1.6zM160 256a96 96 0 1 1 192 0 96 96 0 1 1 -192 0zm224 0a128 128 0 1 0 -256 0 128 128 0 1 0 256 0z"/></svg>
</symbol>
<symbol id="icon-theme-dark" viewBox="0 0 24 24">
<rect fill="none" height="24" width="24"/><path d="M12,3c-4.97,0-9,4.03-9,9s4.03,9,9,9s9-4.03,9-9c0-0.46-0.04-0.92-0.1-1.36c-0.98,1.37-2.58,2.26-4.4,2.26 c-2.98,0-5.4-2.42-5.4-5.4c0-1.81,0.89-3.42,2.26-4.4C12.92,3.04,12.46,3,12,3L12,3z"/>
</symbol>
<symbol id="pair-device-icon" viewBox="0 0 640 512">

View file

@ -1,39 +1,78 @@
(function(){
// Select the button
const btnTheme = document.getElementById('theme');
// Check for dark mode preference at the OS level
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
// Get the user's theme preference from local storage, if it's available
const currentTheme = localStorage.getItem('theme');
// If the user's preference in localStorage is dark...
const prefersDarkTheme = window.matchMedia('(prefers-color-scheme: dark)').matches;
const prefersLightTheme = window.matchMedia('(prefers-color-scheme: light)').matches;
const $themeAuto = document.getElementById('theme-auto');
const $themeLight = document.getElementById('theme-light');
const $themeDark = document.getElementById('theme-dark');
let currentTheme = localStorage.getItem('theme');
if (currentTheme === 'dark') {
// ...let's toggle the .dark-theme class on the body
document.body.classList.toggle('dark-theme');
// Otherwise, if the user's preference in localStorage is light...
setModeToDark();
} else if (currentTheme === 'light') {
// ...let's toggle the .light-theme class on the body
document.body.classList.toggle('light-theme');
setModeToLight();
}
// Listen for a click on the button
btnTheme.addEventListener('click', function(e) {
e.preventDefault();
// If the user's OS setting is dark and matches our .dark-theme class...
let theme;
if (prefersDarkScheme.matches) {
// ...then toggle the light mode class
document.body.classList.toggle('light-theme');
// ...but use .dark-theme if the .light-theme class is already on the body,
theme = document.body.classList.contains('light-theme') ? 'light' : 'dark';
$themeAuto.addEventListener('click', _ => {
if (currentTheme) {
setModeToAuto();
} else {
// Otherwise, let's do the same thing, but for .dark-theme
document.body.classList.toggle('dark-theme');
theme = document.body.classList.contains('dark-theme') ? 'dark' : 'light';
setModeToDark();
}
});
$themeLight.addEventListener('click', _ => {
if (currentTheme !== 'light') {
setModeToLight();
} else {
setModeToAuto();
}
});
$themeDark.addEventListener('click', _ => {
if (currentTheme !== 'dark') {
setModeToDark();
} else {
setModeToLight();
}
// Finally, let's save the current preference to localStorage to keep using it
localStorage.setItem('theme', theme);
});
function setModeToDark() {
document.body.classList.remove('light-theme');
document.body.classList.add('dark-theme');
localStorage.setItem('theme', 'dark');
currentTheme = 'dark';
$themeAuto.classList.remove("selected");
$themeLight.classList.remove("selected");
$themeDark.classList.add("selected");
}
function setModeToLight() {
document.body.classList.remove('dark-theme');
document.body.classList.add('light-theme');
localStorage.setItem('theme', 'light');
currentTheme = 'light';
$themeAuto.classList.remove("selected");
$themeLight.classList.add("selected");
$themeDark.classList.remove("selected");
}
function setModeToAuto() {
document.body.classList.remove('dark-theme');
document.body.classList.remove('light-theme');
if (prefersDarkTheme) {
document.body.classList.add('dark-theme');
} else if (prefersLightTheme) {
document.body.classList.add('light-theme');
}
localStorage.removeItem('theme');
currentTheme = undefined;
$themeAuto.classList.add("selected");
$themeLight.classList.remove("selected");
$themeDark.classList.remove("selected");
}
})();

View file

@ -76,11 +76,74 @@ html {
}
header {
position: relative;
height: 56px;
align-items: center;
padding: 16px;
position: absolute;
align-items: baseline;
padding: 8px 16px;
box-sizing: border-box;
width: 100vw;
z-index: 2;
top: 0;
right: 0;
}
header > a,
header > div {
margin-left: 8px;
}
header > div {
display: flex;
flex-direction: column;
align-self: flex-start;
touch-action: manipulation;
}
header > div a {
height: 40px;
transition: all 300ms;
}
header > div > div {
display: flex;
flex-direction: column;
}
header > div:not(:hover) a:not(.selected) {
height: 0;
opacity: 0;
}
header > div:hover::before {
border-radius: 20px;
background: currentColor;
opacity: 0.1;
transition: opacity 300ms;
content: '';
position: absolute;
width: 40px;
top: 0;
bottom: 0;
margin-top: 8px;
margin-bottom: 8px;
}
header > div:hover a.selected::before {
opacity: 0.1;
}
@media (pointer: coarse) {
header > div:hover a.selected:hover::before {
opacity: 0.2;
}
header > div a:not(.selected) {
height: 0;
opacity: 0;
pointer-events: none;
}
header > div > div {
flex-direction: column-reverse;
}
}
[hidden] {
@ -193,15 +256,10 @@ x-noscript {
}
}
/* Main Header */
body>header a {
margin-left: 8px;
}
#center {
position: relative;
display: flex;
margin-top: 56px;
flex-direction: column-reverse;
flex-grow: 1;
--footer-height: 146px;