mirror of
https://github.com/adityatelange/hugo-PaperMod.git
synced 2025-10-27 14:42:30 +01:00
Use data-theme attribute for theme switching and set color-scheme (#1808)
- Update theme toggle and initialization scripts to set data-theme attribute - Replace CSS selectors using .dark class with [data-theme="dark"] and [data-theme="light"] attribute selectors - Set CSS color-scheme variable alongside theme colors for better native control appearance - Removes redundant styles --------- Co-authored-by: Aditya Telange <21258296+adityatelange@users.noreply.github.com>
This commit is contained in:
@@ -41,12 +41,11 @@ button#theme-toggle {
|
|||||||
margin: auto 4px;
|
margin: auto 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
body.dark #moon {
|
[data-theme="dark"] #moon {
|
||||||
vertical-align: middle;
|
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
body:not(.dark) #sun {
|
[data-theme="light"] #sun {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -268,7 +268,7 @@
|
|||||||
padding: 0.4em;
|
padding: 0.4em;
|
||||||
}
|
}
|
||||||
|
|
||||||
.dark .toc {
|
[data-theme="dark"] .toc {
|
||||||
background: var(--entry);
|
background: var(--entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,9 +15,10 @@
|
|||||||
--code-block-bg: rgb(28, 29, 33);
|
--code-block-bg: rgb(28, 29, 33);
|
||||||
--code-bg: rgb(245, 245, 245);
|
--code-bg: rgb(245, 245, 245);
|
||||||
--border: rgb(238, 238, 238);
|
--border: rgb(238, 238, 238);
|
||||||
|
color-scheme: light;
|
||||||
}
|
}
|
||||||
|
|
||||||
.dark {
|
:root[data-theme="dark"] {
|
||||||
--theme: rgb(29, 30, 32);
|
--theme: rgb(29, 30, 32);
|
||||||
--entry: rgb(46, 46, 51);
|
--entry: rgb(46, 46, 51);
|
||||||
--primary: rgb(218, 218, 219);
|
--primary: rgb(218, 218, 219);
|
||||||
@@ -27,12 +28,13 @@
|
|||||||
--code-block-bg: rgb(46, 46, 51);
|
--code-block-bg: rgb(46, 46, 51);
|
||||||
--code-bg: rgb(55, 56, 62);
|
--code-bg: rgb(55, 56, 62);
|
||||||
--border: rgb(51, 51, 51);
|
--border: rgb(51, 51, 51);
|
||||||
|
color-scheme: dark;
|
||||||
}
|
}
|
||||||
|
|
||||||
.list {
|
.list {
|
||||||
background: var(--code-bg);
|
background: var(--code-bg);
|
||||||
}
|
}
|
||||||
|
|
||||||
.dark.list {
|
[data-theme="dark"] .list {
|
||||||
background: var(--theme);
|
background: var(--theme);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,17 +3,13 @@
|
|||||||
background: 0 0;
|
background: 0 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.list:not(.dark)::-webkit-scrollbar-track {
|
|
||||||
background: var(--code-bg);
|
|
||||||
}
|
|
||||||
|
|
||||||
::-webkit-scrollbar-thumb {
|
::-webkit-scrollbar-thumb {
|
||||||
background: var(--tertiary);
|
background: var(--tertiary);
|
||||||
border: 5px solid var(--theme);
|
border: 5px solid var(--theme);
|
||||||
border-radius: var(--radius);
|
border-radius: var(--radius);
|
||||||
}
|
}
|
||||||
|
|
||||||
.list:not(.dark)::-webkit-scrollbar-thumb {
|
[data-theme="light"] .list::-webkit-scrollbar-thumb {
|
||||||
border: 5px solid var(--code-bg);
|
border: 5px solid var(--code-bg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -81,11 +81,12 @@
|
|||||||
{{- if (not site.Params.disableThemeToggle) }}
|
{{- if (not site.Params.disableThemeToggle) }}
|
||||||
<script>
|
<script>
|
||||||
document.getElementById("theme-toggle").addEventListener("click", () => {
|
document.getElementById("theme-toggle").addEventListener("click", () => {
|
||||||
if (document.body.className.includes("dark")) {
|
const html = document.querySelector("html");
|
||||||
document.body.classList.remove('dark');
|
if (html.dataset.theme === "dark") {
|
||||||
|
html.dataset.theme = 'light';
|
||||||
localStorage.setItem("pref-theme", 'light');
|
localStorage.setItem("pref-theme", 'light');
|
||||||
} else {
|
} else {
|
||||||
document.body.classList.add('dark');
|
html.dataset.theme = 'dark';
|
||||||
localStorage.setItem("pref-theme", 'dark');
|
localStorage.setItem("pref-theme", 'dark');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -127,18 +127,21 @@
|
|||||||
--code-block-bg: rgb(46, 46, 51);
|
--code-block-bg: rgb(46, 46, 51);
|
||||||
--code-bg: rgb(55, 56, 62);
|
--code-bg: rgb(55, 56, 62);
|
||||||
--border: rgb(51, 51, 51);
|
--border: rgb(51, 51, 51);
|
||||||
|
color-scheme: dark;
|
||||||
}
|
}
|
||||||
|
|
||||||
.list {
|
.list {
|
||||||
background: var(--theme);
|
background: var(--theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
.list:not(.dark)::-webkit-scrollbar-track {
|
.toc {
|
||||||
background: 0 0;
|
background: var(--entry);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.list:not(.dark)::-webkit-scrollbar-thumb {
|
@media (prefers-color-scheme: light) {
|
||||||
border-color: var(--theme);
|
.list::-webkit-scrollbar-thumb {
|
||||||
|
border-color: var(--code-bg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
{{- if (eq site.Params.defaultTheme "light") }}
|
{{- if (eq site.Params.defaultTheme "light") }}
|
||||||
<script>
|
<script>
|
||||||
if (localStorage.getItem("pref-theme") === "dark") {
|
if (localStorage.getItem("pref-theme") === "dark") {
|
||||||
document.body.classList.add('dark');
|
document.querySelector("html").dataset.theme = 'dark';
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
{{- else if (eq site.Params.defaultTheme "dark") }}
|
{{- else if (eq site.Params.defaultTheme "dark") }}
|
||||||
<script>
|
<script>
|
||||||
if (localStorage.getItem("pref-theme") === "light") {
|
if (localStorage.getItem("pref-theme") === "light") {
|
||||||
document.body.classList.remove('dark')
|
document.querySelector("html").dataset.theme = 'light';
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
@@ -20,11 +20,13 @@
|
|||||||
{{- /* theme is auto */}}
|
{{- /* theme is auto */}}
|
||||||
<script>
|
<script>
|
||||||
if (localStorage.getItem("pref-theme") === "dark") {
|
if (localStorage.getItem("pref-theme") === "dark") {
|
||||||
document.body.classList.add('dark');
|
document.querySelector("html").dataset.theme = 'dark';
|
||||||
} else if (localStorage.getItem("pref-theme") === "light") {
|
} else if (localStorage.getItem("pref-theme") === "light") {
|
||||||
document.body.classList.remove('dark')
|
document.querySelector("html").dataset.theme = 'light';
|
||||||
} else if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
} else if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||||
document.body.classList.add('dark');
|
document.querySelector("html").dataset.theme = 'dark';
|
||||||
|
} else {
|
||||||
|
document.querySelector("html").dataset.theme = 'light';
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
@@ -33,7 +35,9 @@
|
|||||||
{{- else if (and (ne site.Params.defaultTheme "light") (ne site.Params.defaultTheme "dark"))}}
|
{{- else if (and (ne site.Params.defaultTheme "light") (ne site.Params.defaultTheme "dark"))}}
|
||||||
<script>
|
<script>
|
||||||
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||||
document.body.classList.add('dark');
|
document.querySelector("html").dataset.theme = 'dark';
|
||||||
|
} else {
|
||||||
|
document.querySelector("html").dataset.theme = 'light';
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user