Skip to content

Commit 988953b

Browse files
committed
Add support for dropdown menu.
1 parent 9a174c3 commit 988953b

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-0
lines changed

assets/css/extended/header.css

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Dropdown menu */
2+
.dropdown {
3+
display: inline-block;
4+
}
5+
6+
.dropdown-content {
7+
display: none;
8+
position: absolute;
9+
background-color: var(--tertiary);
10+
box-shadow: 0px 8px 16px 0px var(--border);
11+
border-radius: 8px;
12+
line-height: 1.3rem;
13+
z-index: 1;
14+
}
15+
16+
.dropdown:hover .dropdown-content {
17+
display: block;
18+
}
19+
20+
.dropdown-content a {
21+
padding: 10px 18px 10px 14px;
22+
text-decoration: none;
23+
display: block;
24+
& i {
25+
margin-right: 3px;
26+
}
27+
}
28+
29+
.dropdown-content a:hover {
30+
background-color: var(--entry);
31+
border-radius: 4px;
32+
}
33+
34+
@media screen and (max-width: 680px) {
35+
.dropdown {
36+
display: inline;
37+
}
38+
.dropdown:hover .dropdown-content {
39+
display: inline;
40+
z-index: 1;
41+
margin-top: -2em;
42+
margin-left: 3em;
43+
}
44+
.dropdown-content a:hover {
45+
background-color: transparent;
46+
}
47+
}

layouts/partials/header.html

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
{{- /* theme-toggle is enabled */}}
2+
{{- if (not site.Params.disableThemeToggle) }}
3+
{{- /* theme is light */}}
4+
{{- if (eq site.Params.defaultTheme "light") }}
5+
<script>
6+
if (localStorage.getItem("pref-theme") === "dark") {
7+
document.body.classList.add('dark');
8+
}
9+
10+
</script>
11+
{{- /* theme is dark */}}
12+
{{- else if (eq site.Params.defaultTheme "dark") }}
13+
<script>
14+
if (localStorage.getItem("pref-theme") === "light") {
15+
document.body.classList.remove('dark')
16+
}
17+
18+
</script>
19+
{{- else }}
20+
{{- /* theme is auto */}}
21+
<script>
22+
if (localStorage.getItem("pref-theme") === "dark") {
23+
document.body.classList.add('dark');
24+
} else if (localStorage.getItem("pref-theme") === "light") {
25+
document.body.classList.remove('dark')
26+
} else if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
27+
document.body.classList.add('dark');
28+
}
29+
30+
</script>
31+
{{- end }}
32+
{{- /* theme-toggle is disabled and theme is auto */}}
33+
{{- else if (and (ne site.Params.defaultTheme "light") (ne site.Params.defaultTheme "dark"))}}
34+
<script>
35+
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
36+
document.body.classList.add('dark');
37+
}
38+
39+
</script>
40+
{{- end }}
41+
42+
<header class="header">
43+
<nav class="nav">
44+
<div class="logo">
45+
{{- $label_text := (site.Params.label.text | default site.Title) }}
46+
{{- if site.Title }}
47+
<a href="{{ "" | absLangURL }}" accesskey="h" title="{{ $label_text }} (Alt + H)">
48+
{{- if site.Params.label.icon }}
49+
{{- $img := resources.Get site.Params.label.icon }}
50+
{{- if $img }}
51+
{{- $processableFormats := (slice "jpg" "jpeg" "png" "tif" "bmp" "gif") -}}
52+
{{- if hugo.IsExtended -}}
53+
{{- $processableFormats = $processableFormats | append "webp" -}}
54+
{{- end -}}
55+
{{- $prod := (hugo.IsProduction | or (eq site.Params.env "production")) }}
56+
{{- if and (in $processableFormats $img.MediaType.SubType) (eq $prod true)}}
57+
{{- if site.Params.label.iconHeight }}
58+
{{- $img = $img.Resize (printf "x%d" site.Params.label.iconHeight) }}
59+
{{ else }}
60+
{{- $img = $img.Resize "x30" }}
61+
{{- end }}
62+
{{- end }}
63+
<img src="{{ $img.Permalink }}" alt="" aria-label="logo"
64+
height="{{- site.Params.label.iconHeight | default "30" -}}">
65+
{{- else }}
66+
<img src="{{- site.Params.label.icon | absURL -}}" alt="" aria-label="logo"
67+
height="{{- site.Params.label.iconHeight | default "30" -}}">
68+
{{- end -}}
69+
{{- else if hasPrefix site.Params.label.iconSVG "<svg" }}
70+
{{ site.Params.label.iconSVG | safeHTML }}
71+
{{- end -}}
72+
{{- $label_text -}}
73+
</a>
74+
{{- end }}
75+
<div class="logo-switches">
76+
{{- if (not site.Params.disableThemeToggle) }}
77+
<button id="theme-toggle" accesskey="t" title="(Alt + T)">
78+
<svg id="moon" xmlns="http://www.w3.org/2000/svg" width="24" height="18" viewBox="0 0 24 24"
79+
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
80+
stroke-linejoin="round">
81+
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path>
82+
</svg>
83+
<svg id="sun" xmlns="http://www.w3.org/2000/svg" width="24" height="18" viewBox="0 0 24 24"
84+
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
85+
stroke-linejoin="round">
86+
<circle cx="12" cy="12" r="5"></circle>
87+
<line x1="12" y1="1" x2="12" y2="3"></line>
88+
<line x1="12" y1="21" x2="12" y2="23"></line>
89+
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
90+
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
91+
<line x1="1" y1="12" x2="3" y2="12"></line>
92+
<line x1="21" y1="12" x2="23" y2="12"></line>
93+
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
94+
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
95+
</svg>
96+
</button>
97+
{{- end }}
98+
99+
{{- $lang := .Lang}}
100+
{{- $separator := or $label_text (not site.Params.disableThemeToggle)}}
101+
{{- with site.Home.Translations }}
102+
<ul class="lang-switch">
103+
{{- if $separator }}<li>|</li>{{ end }}
104+
{{- range . -}}
105+
{{- if ne $lang .Lang }}
106+
<li>
107+
<a href="{{- .Permalink -}}" title="{{ .Language.Params.languageAltTitle | default (.Language.LanguageName | emojify) | default (.Lang | title) }}"
108+
aria-label="{{ .Language.LanguageName | default (.Lang | title) }}">
109+
{{- if (and site.Params.displayFullLangName (.Language.LanguageName)) }}
110+
{{- .Language.LanguageName | emojify -}}
111+
{{- else }}
112+
{{- .Lang | title -}}
113+
{{- end -}}
114+
</a>
115+
</li>
116+
{{- end -}}
117+
{{- end}}
118+
</ul>
119+
{{- end }}
120+
</div>
121+
</div>
122+
{{- $currentPage := . }}
123+
<ul id="menu">
124+
{{- range site.Menus.main }}
125+
{{- $menu_item_url := (cond (strings.HasSuffix .URL "/") .URL (printf "%s/" .URL) ) | absLangURL }}
126+
{{- $page_url:= $currentPage.Permalink | absLangURL }}
127+
{{- $is_search := eq (site.GetPage .KeyName).Layout `search` }}
128+
{{ if .HasChildren}}
129+
<li class="dropdown">
130+
<a href="{{ .URL | absLangURL }}" title="{{ .Title | default .Name }} {{- cond $is_search (" (Alt + /)" | safeHTMLAttr) ("" | safeHTMLAttr ) }}"
131+
{{- cond $is_search (" accesskey=/" | safeHTMLAttr) ("" | safeHTMLAttr ) }}>
132+
<span {{- if eq $menu_item_url $page_url }} class="active" {{- end }}>
133+
{{- .Pre | markdownify}}
134+
{{- .Name -}}
135+
{{ .Post | markdownify -}}
136+
</span>
137+
</a>
138+
<div class="menu-more-content dropdown-content">
139+
{{- range .Children -}}
140+
{{- $menu_item_url := (cond (strings.HasSuffix .URL "/") .URL (printf "%s/" .URL) ) | absLangURL }}
141+
{{- $page_url:= $currentPage.Permalink | absLangURL }}
142+
{{- $is_search := eq (site.GetPage .KeyName).Layout `search` }}
143+
<a href="{{ .URL | absLangURL }}" title="{{ .Title | default .Name }} {{- cond $is_search (" (Alt + /)" | safeHTMLAttr) ("" | safeHTMLAttr ) }}"
144+
{{- cond $is_search (" accesskey=/" | safeHTMLAttr) ("" | safeHTMLAttr ) }}>
145+
<span {{- if eq $menu_item_url $page_url }} class="active" {{- end }}>
146+
{{ .Pre | markdownify }}
147+
{{ .Name }}
148+
{{ .Post | markdownify }}
149+
</span>
150+
</a>
151+
{{- end -}}
152+
</div>
153+
</li>
154+
{{ else }}
155+
<li>
156+
<a href="{{ .URL | absLangURL }}" title="{{ .Title | default .Name }} {{- cond $is_search (" (Alt + /)" | safeHTMLAttr) ("" | safeHTMLAttr ) }}"
157+
{{- cond $is_search (" accesskey=/" | safeHTMLAttr) ("" | safeHTMLAttr ) }}>
158+
<span {{- if eq $menu_item_url $page_url }} class="active" {{- end }}>
159+
{{- .Pre | markdownify }}
160+
{{- .Name -}}
161+
{{ .Post | markdownify -}}
162+
</span>
163+
{{- if (findRE "://" .URL) }}&nbsp;
164+
<svg fill="none" shape-rendering="geometricPrecision" stroke="currentColor" stroke-linecap="round"
165+
stroke-linejoin="round" stroke-width="2.5" viewBox="0 0 24 24" height="12" width="12">
166+
<path d="M18 13v6a2 2 0 01-2 2H5a2 2 0 01-2-2V8a2 2 0 012-2h6"></path>
167+
<path d="M15 3h6v6"></path>
168+
<path d="M10 14L21 3"></path>
169+
</svg>
170+
{{- end }}
171+
</a>
172+
</li>
173+
{{- end }}
174+
{{- end -}}
175+
</ul>
176+
</nav>
177+
</header>

0 commit comments

Comments
 (0)