mirror of
https://github.com/MarginaliaSearch/MarginaliaSearch.git
synced 2025-02-24 05:18:58 +00:00
Add a theme select to the header that lets users toggle their theme independent of their OS theme
This commit is contained in:
parent
d4f4d751c0
commit
e2f68d9ccf
BIN
code/libraries/array/cpp/resources/libcpp.so
Executable file
BIN
code/libraries/array/cpp/resources/libcpp.so
Executable file
Binary file not shown.
@ -1,4 +1,6 @@
|
|||||||
:root {
|
:root {
|
||||||
|
color-scheme: light;
|
||||||
|
|
||||||
--clr-bg-page: hsl(60, 42%, 95%); // $nicotine-light
|
--clr-bg-page: hsl(60, 42%, 95%); // $nicotine-light
|
||||||
|
|
||||||
--clr-bg-ui: hsl(0, 0%, 100%);
|
--clr-bg-ui: hsl(0, 0%, 100%);
|
||||||
@ -26,8 +28,11 @@
|
|||||||
--font-family-heading: serif; // $heading-fonts
|
--font-family-heading: serif; // $heading-fonts
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
|
||||||
:root {
|
:root[data-theme='dark'] {
|
||||||
|
|
||||||
|
color-scheme: dark;
|
||||||
|
|
||||||
--clr-bg-page: hsl(0, 0%, 6%);
|
--clr-bg-page: hsl(0, 0%, 6%);
|
||||||
|
|
||||||
--clr-bg-ui: hsl(0, 0%, 18%);
|
--clr-bg-ui: hsl(0, 0%, 18%);
|
||||||
@ -50,16 +55,12 @@
|
|||||||
--clr-link-visited: #ffadff;
|
--clr-link-visited: #ffadff;
|
||||||
--clr-heading-link-visited: var(--clr-link-visited);
|
--clr-heading-link-visited: var(--clr-link-visited);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
* {
|
* {
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
|
|
||||||
html {
|
|
||||||
color-scheme: light dark;
|
|
||||||
}
|
|
||||||
|
|
||||||
a {
|
a {
|
||||||
color: var(--clr-link);
|
color: var(--clr-link);
|
||||||
}
|
}
|
||||||
@ -199,6 +200,8 @@ header {
|
|||||||
color: var(--clr-text-ui);
|
color: var(--clr-text-ui);
|
||||||
box-shadow: 0 0 0.5ch var(--clr-shadow);
|
box-shadow: 0 0 0.5ch var(--clr-shadow);
|
||||||
margin-bottom: 1ch;
|
margin-bottom: 1ch;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
|
||||||
nav {
|
nav {
|
||||||
a {
|
a {
|
||||||
@ -228,6 +231,11 @@ header {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#theme {
|
||||||
|
margin-left: auto;
|
||||||
|
margin-right: .5ch;
|
||||||
|
}
|
||||||
|
|
||||||
#complaint {
|
#complaint {
|
||||||
@extend .dialog;
|
@extend .dialog;
|
||||||
max-width: 60ch;
|
max-width: 60ch;
|
||||||
@ -333,13 +341,11 @@ section.cards {
|
|||||||
line-height: 1.6;
|
line-height: 1.6;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
[data-theme='dark'] & {
|
||||||
& {
|
|
||||||
border: 1px solid var(--clr-border);
|
border: 1px solid var(--clr-border);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
.positions {
|
.positions {
|
||||||
box-shadow: 0 0 2px var(--clr-shadow);
|
box-shadow: 0 0 2px var(--clr-shadow);
|
||||||
|
@ -0,0 +1,57 @@
|
|||||||
|
function getTheme() {
|
||||||
|
const theme = window.localStorage.getItem('theme');
|
||||||
|
|
||||||
|
// if a valid theme is set in localStorage, return it
|
||||||
|
if (theme === 'dark' || theme === 'light') {
|
||||||
|
return { value: theme, system: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
// if matchMedia is supported and OS theme is dark
|
||||||
|
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||||
|
return { value: 'dark', system: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { value: 'light', system: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
function setTheme(value) {
|
||||||
|
if (value === 'dark' || value === 'light') {
|
||||||
|
window.localStorage.setItem('theme', value);
|
||||||
|
} else {
|
||||||
|
window.localStorage.removeItem('theme');
|
||||||
|
}
|
||||||
|
|
||||||
|
const theme = getTheme();
|
||||||
|
|
||||||
|
document.documentElement.setAttribute('data-theme', theme.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeTheme() {
|
||||||
|
const themeSelect = document.getElementById('theme-select');
|
||||||
|
|
||||||
|
const theme = getTheme();
|
||||||
|
|
||||||
|
document.documentElement.setAttribute('data-theme', theme.value);
|
||||||
|
|
||||||
|
// system is selected by default in the themeSwitcher so ignore it here
|
||||||
|
if (!theme.system) {
|
||||||
|
themeSelect.value = theme.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
themeSelect.addEventListener('change', e => {
|
||||||
|
setTheme(e.target.value);
|
||||||
|
});
|
||||||
|
|
||||||
|
const mql = window.matchMedia('(prefers-color-scheme: dark)');
|
||||||
|
|
||||||
|
// if someone changes their theme at the OS level we need to update
|
||||||
|
// their theme immediately if they're using their OS theme
|
||||||
|
mql.addEventListener('change', e => {
|
||||||
|
if (themeSelect.value !== 'system') return;
|
||||||
|
|
||||||
|
if (e.matches) setTheme('dark');
|
||||||
|
else setTheme('light');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
initializeTheme();
|
@ -7,4 +7,15 @@
|
|||||||
<a href="https://memex.marginalia.nu/projects/edge/supporting.gmi">Donate</a>
|
<a href="https://memex.marginalia.nu/projects/edge/supporting.gmi">Donate</a>
|
||||||
<a class="extra" href="https://search.marginalia.nu/explore/random">Random</a>
|
<a class="extra" href="https://search.marginalia.nu/explore/random">Random</a>
|
||||||
</nav>
|
</nav>
|
||||||
|
<div id="theme">
|
||||||
|
<label for="theme-select" class="screenreader-only">Color Theme</label>
|
||||||
|
<select id="theme-select">
|
||||||
|
<option value="system" selected>System</option>
|
||||||
|
<option value="light">Light</option>
|
||||||
|
<option value="dark">Dark</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
|
<!-- load this ASAP to avoid color theme flicker -->
|
||||||
|
<script src="/theme.js"></script>
|
Loading…
Reference in New Issue
Block a user