1
0
Fork 0
hugo-paper/layouts/partials/header.html
2023-06-07 03:39:39 +08:00

99 lines
3.5 KiB
HTML

<header class="mx-auto flex h-[5rem] max-w-3xl px-8 lg:justify-center">
<div class="relative z-50 mr-auto flex items-center">
<a
class="-translate-x-[1px] -translate-y-[1px] text-2xl font-semibold"
href="{{ `/` | absURL }}"
>{{ site.Title }}</a
>
<div
class="btn-dark text-[0] ml-4 h-6 w-6 shrink-0 cursor-pointer {{ if site.Params.monoDarkIcon }}[background:url(./theme.svg)_left_center/cover_no-repeat] dark:invert{{ else }}[background:url(./theme.png)_left_center/_auto_theme('spacing.6')_no-repeat] [transition:_background-position_0.4s_steps(5)]{{ end }} dark:[background-position:right]"
role="button"
aria-label="Dark"
></div>
</div>
<div
class="btn-menu relative z-50 -mr-8 flex h-[5rem] w-[5rem] shrink-0 cursor-pointer flex-col items-center justify-center gap-2.5 lg:hidden"
role="button"
aria-label="Menu"
></div>
{{ $bg_color := $.Scratch.Get "bg_color" }}
<script>
// base
const htmlClass = document.documentElement.classList;
setTimeout(() => {
htmlClass.remove('not-ready');
}, 10);
// mobile menu
const btnMenu = document.querySelector('.btn-menu');
btnMenu.addEventListener('click', () => {
htmlClass.toggle('open');
});
// dark theme
const metaTheme = document.querySelector('meta[name="theme-color"]');
const lightBg = `{{ $.Scratch.Get "bg_color" }}`.replace(/"/g, '');
const setDark = (isDark) => {
metaTheme.setAttribute('content', isDark ? '#000' : lightBg);
htmlClass[isDark ? 'add' : 'remove']('dark');
localStorage.setItem('dark', isDark);
};
// init
const darkScheme = window.matchMedia('(prefers-color-scheme: dark)');
if (htmlClass.contains('dark')) {
setDark(true);
} else {
const darkVal = localStorage.getItem('dark');
setDark(darkVal ? darkVal === 'true' : darkScheme.matches);
}
// listen system
darkScheme.addEventListener('change', (event) => {
setDark(event.matches);
});
// manual switch
const btnDark = document.querySelector('.btn-dark');
btnDark.addEventListener('click', () => {
setDark(localStorage.getItem('dark') !== 'true');
});
</script>
<div
class="nav-wrapper fixed inset-x-0 top-full z-40 flex h-full select-none flex-col justify-center pb-16 duration-200 dark:bg-black lg:static lg:h-auto lg:flex-row lg:!bg-transparent lg:pb-0 lg:transition-none"
>
{{ $url := .RelPermalink }}<!---->
{{ with site.Menus.main }}
<nav class="lg:ml-12 lg:flex lg:flex-row lg:items-center lg:space-x-6">
{{ range . }}
<a
class="block text-center text-2xl leading-[5rem] lg:text-base lg:font-normal"
href="{{ .URL }}"
>{{ .Name }}</a
>
{{ end }}
</nav>
{{ end }}<!---->
{{ with $.Scratch.Get "social_list" }}
<nav
class="mt-12 flex justify-center space-x-10 dark:invert lg:ml-12 lg:mt-0 lg:items-center lg:space-x-6"
>
{{ range . }}<!---->
<a
class="h-8 w-8 text-[0] [background:var(--url)_center_center/cover_no-repeat] lg:h-6 lg:w-6"
style="--url: url(./{{ . }}.svg)"
href="{{ if eq . `rss` }}{{ `index.xml` | absURL }}{{ else if eq . `mastodon` }}{{ index site.Params . }}{{ else }}https://{{ . }}.com/{{ if eq . `linkedin` }}in/{{ end }}{{ index site.Params . }}{{ end }}"
target="_blank"
rel="{{ if eq . `rss` }}alternate{{ else }}me{{ end }}"
>
{{ . }}
</a>
{{ end }}<!---->
</nav>
{{ end }}<!---->
</div>
</header>