Skip to content

Commit

Permalink
perf: cache sidebar and using JavaScript to locate current active menu (
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang authored Jun 4, 2024
1 parent 32c400a commit 1ce46a2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions assets/hb/modules/docs/js/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import "js/bootstrap/src/collapse";
import "./nav";
import "./nav-toggle";
27 changes: 27 additions & 0 deletions assets/hb/modules/docs/js/nav.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(() => {
const activeParent = (child: HTMLElement) => {
const parent = child.parentElement?.closest('.hb-docs-nav-links-group') as HTMLElement
if (parent == null) {
return
}

parent.querySelector('.collapse')?.classList.add('show')
const heading = parent.querySelector('.hb-docs-nav-heading') as HTMLElement
heading.classList.add('active')
heading.querySelector('.hb-docs-nav-item-toggle')?.setAttribute('aria-expanded', 'true')
console.log(parent.closest('.hb-docs-nav-links-group'))
activeParent(parent)
}

const active = (link: HTMLElement) => {
link.classList.add('active')
activeParent(link)
}

const link = document.querySelector(`.hb-docs-nav-links a[href="${location.pathname}"]`) as HTMLElement
if (link == null) {
return
}

active(link)
})()
2 changes: 1 addition & 1 deletion layouts/docs/list.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{- define "main" }}
<div class="hb-docs">
<div class="hb-docs-sidebar" tabindex="-1" data-bs-hide="focusout">
{{ partial "hb/modules/docs/nav" . }}
{{ partialCached "hb/modules/docs/nav" . .FirstSection }}
</div>
<div class="hb-docs-main col-12 col-xxl-10">
{{ partial "hugopress/functions/render-hooks" (dict "Page" . "Name" "hb-docs-main-begin") }}
Expand Down
2 changes: 1 addition & 1 deletion layouts/docs/single.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{- define "main" }}
<div class="hb-docs">
<div class="hb-docs-sidebar" tabindex="-1" data-bs-hide="focusout">
{{ partial "hb/modules/docs/nav" . }}
{{ partialCached "hb/modules/docs/nav" . .FirstSection }}
</div>
<div class="hb-docs-main">
{{ partial "hugopress/functions/render-hooks" (dict "Page" . "Name" "hb-docs-main-begin") }}
Expand Down
12 changes: 4 additions & 8 deletions layouts/partials/hb/modules/docs/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ <h5 class="offcanvas-title" id="hb-docs-nav-label">
{{- $border := default true site.Params.hb.docs.navs_border }}
{{- range .Tree }}
{{- $node := . }}
{{- $active := eq $page.RelPermalink .Page.RelPermalink }}
{{- if $node.Page.IsSection }}
{{- if partial "hb/modules/docs/functions/is-section-page" (dict "Page" $page "Section" $node.Page) }}
{{- $active = true }}
{{- end }}
<li class="hb-docs-nav-links-group">
{{- $collapseId := "" }}
{{- with $node.Page.File }}
Expand All @@ -41,7 +37,7 @@ <h5 class="offcanvas-title" id="hb-docs-nav-label">
{{- $collapseId = printf "collapse-%d" now.UnixNano }}
{{- end }}
<div
class="hb-docs-nav-heading d-flex align-items-center justify-content-between rounded{{ cond $active ` active` `` }}">
class="hb-docs-nav-heading d-flex align-items-center justify-content-between rounded">
<a
class="d-flex flex-grow-1 align-items-center"
href="{{ $node.Page.RelPermalink }}">
Expand All @@ -53,7 +49,7 @@ <h5 class="offcanvas-title" id="hb-docs-nav-label">
type="button"
data-bs-toggle="collapse"
data-bs-target="#{{ $collapseId }}"
aria-expanded="{{ cond $active `true` `false` }}"
aria-expanded="false"
aria-controls="{{ $collapseId }}">
{{ partial "icons/icon" (dict
"vendor" "bootstrap"
Expand All @@ -67,7 +63,7 @@ <h5 class="offcanvas-title" id="hb-docs-nav-label">
</div>
{{- with .Children }}
<ul
class="collapse list-unstyled fw-normal small {{ cond $border `ms-1 ps-1 border-start` `ms-2` }}{{ cond $active ` show` `` }}"
class="collapse list-unstyled fw-normal small {{ cond $border `ms-1 ps-1 border-start` `ms-2` }}"
id="{{ $collapseId }}">
{{- template "walk-docs-tree" (dict "Tree" . "Page" $page) }}
{{- with $node.Page.Params.nav_menus }}
Expand All @@ -79,7 +75,7 @@ <h5 class="offcanvas-title" id="hb-docs-nav-label">
{{- else }}
<li class="hb-docs-nav-links-span-all">
<a
class="hb-docs-nav-link d-flex align-items-center rounded{{ cond $active ` active` `` }}"
class="hb-docs-nav-link d-flex align-items-center rounded"
{{ with .Page.Params.redirect }}
target="_blank" href="{{ . | absURL }}"
{{ else }}
Expand Down

0 comments on commit 1ce46a2

Please sign in to comment.