Skip to content

Commit 69cb289

Browse files
authored
Merge pull request #3 from cybertec-postgresql/theme-v2
feat: enhance theme customization
2 parents 0a05892 + d778b2b commit 69cb289

8 files changed

Lines changed: 38 additions & 38 deletions

File tree

config/_default/hugo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ tag = "tags"
77

88
[params]
99
geekdocCollapseAllSections = true
10+
geekdocCustomLayout = true

layouts/partials/site-header.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@
5151
<title>{{ i18n "button_toggle_dark" }}</title>
5252
<use xlink:href="#gdoc_brightness_light"></use>
5353
</svg>
54-
<svg class="gdoc-icon gdoc_brightness_auto">
55-
<title>{{ i18n "button_toggle_dark" }}</title>
56-
<use xlink:href="#gdoc_brightness_auto"></use>
57-
</svg>
5854
</span>
5955

6056
{{ if not (default false .Root.Site.Params.geekdocCustomLayout) }}

src/js/colorTheme.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
import Storage from "store2"
2-
import { TOGGLE_COLOR_THEMES, THEME, COLOR_THEME_AUTO } from "./config.js"
2+
import {
3+
TOGGLE_COLOR_THEMES,
4+
THEME,
5+
COLOR_THEME_AUTO,
6+
COLOR_THEME_LIGHT,
7+
COLOR_THEME_DARK,
8+
} from "./config.js"
39
;(() => {
410
applyTheme()
511
})()
612

13+
function resolveStoredColorTheme(stored) {
14+
if (TOGGLE_COLOR_THEMES.includes(stored)) {
15+
return stored
16+
}
17+
if (
18+
stored === COLOR_THEME_AUTO ||
19+
stored === undefined ||
20+
stored === null ||
21+
stored === ""
22+
) {
23+
return window.matchMedia("(prefers-color-scheme: dark)").matches
24+
? COLOR_THEME_DARK
25+
: COLOR_THEME_LIGHT
26+
}
27+
return COLOR_THEME_LIGHT
28+
}
29+
730
document.addEventListener("DOMContentLoaded", () => {
831
const colorThemeToggle = document.getElementById("gdoc-color-theme")
932

1033
function toggleColorTheme() {
1134
let lstore = Storage.namespace(THEME)
12-
let currentColorTheme = lstore.get("color-theme") || COLOR_THEME_AUTO
35+
let currentColorTheme = resolveStoredColorTheme(lstore.get("color-theme"))
1336
let nextColorTheme = toggle(TOGGLE_COLOR_THEMES, currentColorTheme)
1437

1538
lstore.set("color-theme", TOGGLE_COLOR_THEMES[nextColorTheme])
@@ -33,18 +56,16 @@ function applyTheme(init = true) {
3356

3457
let lstore = Storage.namespace(THEME)
3558
let html = document.documentElement
36-
let currentColorTheme = TOGGLE_COLOR_THEMES.includes(lstore.get("color-theme"))
37-
? lstore.get("color-theme")
38-
: COLOR_THEME_AUTO
59+
let raw = lstore.get("color-theme")
60+
let currentColorTheme = resolveStoredColorTheme(raw)
3961

40-
html.setAttribute("class", "color-toggle-" + currentColorTheme)
41-
42-
if (currentColorTheme === COLOR_THEME_AUTO) {
43-
html.removeAttribute("color-theme")
44-
} else {
45-
html.setAttribute("color-theme", currentColorTheme)
62+
if (raw !== currentColorTheme) {
63+
lstore.set("color-theme", currentColorTheme)
4664
}
4765

66+
html.setAttribute("class", "color-toggle-" + currentColorTheme)
67+
html.setAttribute("color-theme", currentColorTheme)
68+
4869
if (!init) {
4970
// Reload required to re-initialize e.g. Mermaid with the new theme
5071
// and re-parse the Mermaid code blocks.

src/js/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const COLOR_THEME_DARK = "dark"
22
export const COLOR_THEME_LIGHT = "light"
3+
/** @deprecated Legacy localStorage value; migrated to light or dark on read. */
34
export const COLOR_THEME_AUTO = "auto"
45
export const THEME = "hugo-geekdoc"
5-
export const TOGGLE_COLOR_THEMES = [COLOR_THEME_AUTO, COLOR_THEME_DARK, COLOR_THEME_LIGHT]
6+
export const TOGGLE_COLOR_THEMES = [COLOR_THEME_LIGHT, COLOR_THEME_DARK]

src/sass/_base.scss

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ html {
5656
.gdoc_brightness_light {
5757
display: inline-block;
5858
}
59-
.gdoc_brightness_auto,
59+
6060
.gdoc_brightness_dark {
6161
display: none;
6262
}
@@ -68,24 +68,10 @@ html {
6868
.gdoc_brightness_dark {
6969
display: inline-block;
7070
}
71-
.gdoc_brightness_auto,
72-
.gdoc_brightness_light {
73-
display: none;
74-
}
75-
}
76-
}
7771

78-
&.color-toggle-auto {
79-
#gdoc-color-theme {
8072
.gdoc_brightness_light {
8173
display: none;
8274
}
83-
.gdoc_brightness_dark {
84-
display: none;
85-
}
86-
.gdoc_brightness_auto {
87-
display: inline-block;
88-
}
8975
}
9076
}
9177
}

src/sass/_custom-header.scss

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,6 @@ body.gdoc-layout--custom {
147147
color: var(--cx-icon-brightness-dark);
148148
}
149149

150-
#gdoc-color-theme .gdoc_brightness_auto {
151-
color: var(--cx-icon-brightness-auto);
152-
}
153-
154150
.gdoc-support__button a {
155151
display: inline-flex;
156152
align-items: center;

src/sass/_custom-shell.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,13 @@ body.gdoc-layout--custom {
7373

7474
.cx-page-bg-art {
7575
position: fixed;
76-
top: -27%;
76+
top: -37%;
7777
right: -20%;
7878
width: min(50vw, 650px);
7979
max-width: 650px;
8080
pointer-events: none;
8181
z-index: 0;
82+
opacity: 0.5;
8283
fill: var(--header-background);
8384
}
8485

src/sass/_custom-tokens.scss

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
/* Theme toggle icon colors (light theme) */
3131
--cx-icon-brightness-light: #eab308;
3232
--cx-icon-brightness-dark: #0ea5e9;
33-
--cx-icon-brightness-auto: #6366f1;
3433
}
3534

3635
/* Dark theme overrides for custom tokens */
@@ -57,5 +56,4 @@
5756
/* Theme toggle icon colors (dark theme) */
5857
--cx-icon-brightness-light: #facc15;
5958
--cx-icon-brightness-dark: #38bdf8;
60-
--cx-icon-brightness-auto: #a5b4fc;
6159
}

0 commit comments

Comments
 (0)