PaperModX/layouts/partials/header.html

142 lines
5.8 KiB
HTML
Raw Normal View History

2022-05-27 15:40:21 +01:00
<script data-no-instant>
function switchTheme(theme) {
switch (theme) {
case 'light':
document.body.classList.remove('dark');
break;
case 'dark':
document.body.classList.add('dark');
break;
// auto
default:
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.body.classList.add('dark');
2022-05-27 15:40:21 +01:00
}
}
}
2022-05-27 15:40:21 +01:00
function isDarkTheme() {
return document.body.className.includes("dark");
}
2022-05-27 15:40:21 +01:00
function getPrefTheme() {
return localStorage.getItem("pref-theme");
}
function setPrefTheme(theme) {
switchTheme(theme)
localStorage.setItem("pref-theme", theme);
}
const toggleThemeCallbacks = {}
toggleThemeCallbacks['main'] = (isDark) => {
// console.log('window toggle-theme 1')
2022-05-27 15:40:21 +01:00
if (isDark) {
setPrefTheme('light');
} else {
setPrefTheme('dark');
}
}
2022-05-27 15:40:21 +01:00
// listen to set-theme event,
// because window is never changed by InstantClick,
// we add the listener to window to ensure the event is always received
window.addEventListener('toggle-theme', function() {
// console.log('window toggle-theme')
2022-05-27 15:40:21 +01:00
const isDark = isDarkTheme()
for (const key in toggleThemeCallbacks) {
toggleThemeCallbacks[key](isDark)
}
2022-05-27 15:40:21 +01:00
});
</script>
<script>
2022-05-27 15:40:21 +01:00
// load theme, as early as possible
(function() {
const defaultTheme = '{{ site.Params.defaultTheme | default "light" }}';
const prefTheme = getPrefTheme();
const theme = prefTheme ? prefTheme : defaultTheme;
2022-05-27 15:40:21 +01:00
switchTheme(theme);
})();
</script>
2021-03-30 13:37:36 +01:00
2020-07-21 11:06:30 +01:00
<header class="header">
<nav class="nav">
<div class="logo">
{{- $label_text := (site.Params.logo.text | default site.Title) }}
{{- if site.Title }}
2022-05-04 18:25:37 +01:00
<a href="{{ "" | absURL }}" accesskey="h" title="{{ $label_text }} (Alt + H)">
{{- if site.Params.logo.icon }}
{{- $img := resources.Get site.Params.logo.icon }}
{{- if $img }}
{{- $processableFormats := (slice "jpg" "jpeg" "png" "tif" "bmp" "gif") -}}
{{- if hugo.IsExtended -}}
{{- $processableFormats = $processableFormats | append "webp" -}}
{{- end -}}
{{- $prod := (hugo.IsProduction | or (eq site.Params.env "production")) }}
{{- if and (in $processableFormats $img.MediaType.SubType) (eq $prod true)}}
{{- if site.Params.logo.iconHeight }}
{{- $img = $img.Resize (printf "x%d" site.Params.logo.iconHeight) }}
{{ else }}
{{- $img = $img.Resize "x30" }}
{{- end }}
{{- end }}
<img src="{{ $img.Permalink }}" alt="logo" aria-label="logo"
height="{{- site.Params.logo.iconHeight | default "30" -}}">
{{- else }}
<img src="{{- site.Params.logo.icon | absURL -}}" alt="logo" aria-label="logo"
height="{{- site.Params.logo.iconHeight | default "30" -}}">
{{- end -}}
{{- end -}}
{{- $label_text -}}
</a>
{{- end }}
<span class="logo-switches">
{{- if (not site.Params.disableThemeToggle) }}
<button id="theme-toggle" accesskey="t" title="(Alt + T)">
<svg id="moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
stroke-linejoin="round">
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path>
</svg>
<svg id="sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
stroke-linejoin="round">
<circle cx="12" cy="12" r="5"></circle>
<line x1="12" y1="1" x2="12" y2="3"></line>
<line x1="12" y1="21" x2="12" y2="23"></line>
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
<line x1="1" y1="12" x2="3" y2="12"></line>
<line x1="21" y1="12" x2="23" y2="12"></line>
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
</svg>
</button>
2021-03-30 13:37:36 +01:00
{{- end }}
</span>
</div>
{{- $currentPage := . }}
<ul id="menu">
{{- range site.Menus.main }}
2022-05-29 14:50:06 +01:00
{{- $menu_item_url := (cond (strings.HasSuffix .URL "/") .URL (printf "%s/" .URL)) | absURL }}
{{- $page_url:= $currentPage.Permalink }}
{{- $is_search := eq (site.GetPage .KeyName).Layout `search` }}
2020-07-21 11:06:30 +01:00
<li>
2022-05-29 14:50:06 +01:00
<a href="{{ $menu_item_url }}" title="{{ .Title | default .Name }} {{- cond $is_search (" (Alt + /)" | safeHTMLAttr) ("" | safeHTMLAttr ) }}"
2022-04-12 16:24:51 +01:00
{{- if $is_search -}}data-no-instant{{- end -}}
{{- cond $is_search (" accesskey=/" | safeHTMLAttr) ("" | safeHTMLAttr ) }}
2022-04-14 09:27:34 +01:00
{{- if eq $menu_item_url $page_url }} class="active" {{- end }}
{{- if .Params.External }} target="_blank" {{- end }}
>
{{- .Pre }}
{{- .Name -}}
{{ .Post -}}
2022-04-14 09:27:34 +01:00
{{- if .Params.External }}<span class="external-link">{{ safeHTML (index $.Site.Data.svg "external-link") }}</span>{{- end }}
</a>
2020-07-21 11:06:30 +01:00
</li>
2021-03-30 13:37:36 +01:00
{{- end }}
2020-07-21 11:06:30 +01:00
</ul>
</nav>
</header>