Skip to content

Commit 1c09422

Browse files
authored
feat: update Headroom implementation to support recent Hugo versions (#2950)
It has been reported that recent Hugo versions broke user's configuration of Headroom JS via the `header.on_scroll` option. With this PR, Headroom JS configuration is working again. Fixes #2935
1 parent 92cc5a2 commit 1c09422

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

modules/wowchemy/layouts/partials/site_js.html

+35-4
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,43 @@
126126
<script src="{{ $js_algolia_search.RelPermalink }}" type="module"></script>
127127
{{ end }}
128128

129-
{{/* Page Data */}}
130-
{{ $default_headroom := not (.IsHome | or (eq .Type "book")) }}
131-
{{ $use_headroom := cond (isset $.Params "header.on_scroll") (eq $.Params.header.on_scroll "disappear") (default $default_headroom) }}
129+
{{/* Headroom */}}
130+
{{/*
131+
Headroom JS enables hiding the header on scrolling down and showing it on scrolling up.
132+
This is especially valuable on smaller displays such as mobile devices.
133+
This option can be set in the following ways:
134+
- globally, in the site's 'params.yaml':
135+
```
136+
header:
137+
on_scroll: "disappear" | "sticky"
138+
```
139+
- per page, in the page front matter:
140+
```
141+
header:
142+
on_scroll: "disappear" | "sticky"
143+
```
144+
When the header is set to disappear, the Headroom JS is loaded.
145+
146+
By default, the header is sticky in the homepage and in the book layout,
147+
and it disappears when scrolling in all other pages.
148+
*/}}
149+
150+
{{/* By default don't use Headroom in the homepage and book layout */}}
151+
{{ $use_headroom := not (.IsHome | or (eq .Type "book")) }}
152+
153+
{{/* Check for the global site parameter `header.on_scroll` */}}
154+
{{ if (isset site.Params.header "on_scroll") }}
155+
{{ $use_headroom = eq site.Params.header.on_scroll "disappear" }}
156+
{{ end }}
157+
158+
{{/* Check for the page parameter `header.on_scroll` */}}
159+
{{ if (isset $.Params.header "on_scroll") }}
160+
{{ $use_headroom = eq $.Params.header.on_scroll "disappear" }}
161+
{{ end }}
162+
163+
{{/* Output `use_headroom` variable for Headroom initialization in Wowchemy JS */}}
132164
{{ printf "<script id=\"page-data\" type=\"application/json\">%s</script>" (dict "use_headroom" $use_headroom | jsonify) | safeHTML}}
133165

134-
{{/* Headroom */}}
135166
{{ if $use_headroom }}
136167
{{ $headroom := resources.Get "js/wowchemy-headroom.js" | js.Build (dict "format" "esm" "minify" true) }}
137168
{{- if hugo.IsProduction -}}

0 commit comments

Comments
 (0)