Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nested menus #781

Merged
merged 9 commits into from
Jul 10, 2020
6 changes: 4 additions & 2 deletions assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ $(document)
.on('click', '.dropdown-item', (e) => {
const dropdown = e.target.parentElement;
const button = $(dropdown.parentElement).$('.dropdown-toggle');
button.text(e.target.innerText);
button.attr('data-value', e.target.dataset.value);
if (!button.$nodes[0].classList.contains('nav-link')) {
button.text(e.target.innerText);
button.attr('data-value', e.target.dataset.value);
}
$(dropdown).removeClass('show');
$(dropdown.parentElement).removeClass('show');
})
Expand Down
2 changes: 1 addition & 1 deletion assets/scripts/syna-main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cypress/integration/navbar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ describe("Navbar", () => {
cy.get('#nav-1 .search-container input').focus().type('nav');
cy.get('#nav-1 .search-results-container').should("be.visible");
cy.get('#nav-2 .search-results-container').should("be.hidden");
cy.get('#nav-1').click();
cy.get('body').click();
cy.get('#nav-1 .search-results-container').should("be.hidden");

cy.get('#nav-2 .search-container input').focus().type('nav');
cy.get('#nav-2 .search-results-container').should("be.visible");
cy.get('#nav-1 .search-results-container').should("be.hidden");
cy.get('#nav-2').click();
cy.get('body').click();
cy.get('#nav-2 .search-results-container').should("be.hidden");
});
});
30 changes: 30 additions & 0 deletions exampleSite/config-dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ lastmod = ["lastmod", ":git", "date"]
name = "Docs"
weight = 30

[[menu.main]]
url = "/dev"
name = "Dev Section"
weight = 35
identifier = "dev"

[[menu.main]]
url = "/dev"
name = "Dev Section"
weight = 10
parent = "dev"

[[menu.main]]
url = "/dev/colors"
name = "Colors"
weight = 20
parent = "dev"

[[menu.main]]
url = "/dev/blog"
name = "Blog"
weight = 30
parent = "dev"

[[menu.main]]
url = "/dev/events"
name = "Events"
weight = 40
parent = "dev"

[[menu.main]]
url = "/about"
name = "About"
Expand Down
24 changes: 24 additions & 0 deletions exampleSite/content/fragments/nav/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ Use one instance of this fragment per page. Running more might lead to unexpecte

- .Site.Menus.main

**Note:** Menus displayed in the nav fragment can be nested, in which case the nested menus are displayed in a dropdown. Please see "[nesting](https://gohugo.io/content-management/menus/#nesting)" section of Menus documentation in Hugo documentation.

```
# config.toml

[[menu.main]]
url = "/about"
name = "About"
weight = 10
identifier = "about"

[[menu.main]]
url = "/about/team"
name = "Our team"
weight = 10
parent = "about"

[[menu.main]]
url = "/about/office"
name = "Our office"
weight = 20
parent = "about"
```

### Variables

#### search
Expand Down
27 changes: 22 additions & 5 deletions layouts/partials/fragments/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,33 @@
{{- end }}
{{- if .Site.Menus.main -}}
{{- range sort .Site.Menus.main }}
{{- $isMenuCurrent := or (eq $.root.Permalink .URL) (eq (printf "%s/" $.root.Permalink) .URL) (eq $.root.Permalink (printf "%s/" .URL)) -}}
<li class="nav-item">
<a href="{{ .URL | relLangURL }}"
{{- $isMenuCurrent := or ($.root.HasMenuCurrent "main" .) (eq $.root.Permalink .URL) (eq (printf "%s/" $.root.Permalink) .URL) (eq $.root.Permalink (printf "%s/" .URL)) -}}
<li class="nav-item {{ if .HasChildren }}dropdown{{ end }}">
<a
{{- if hasPrefix .URL "#" }}
class="nav-link anchor"
{{- else }}
class="nav-link {{- if $isMenuCurrent }} active default-active {{- end -}}"
{{- end }}>
class="nav-link {{- if $isMenuCurrent }} active default-active {{- end -}} {{ if .HasChildren }} dropdown-toggle{{ end }}"
{{- end }}
{{if .HasChildren}}
id="navbarDropdown"
role="button"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
href="#"
{{ else }}
href="{{ .URL | relLangURL }}"
{{ end }}>
{{ .Name }}
</a>
{{ if .HasChildren }}
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
{{ range .Children }}
<a class="dropdown-item {{ if $.root.IsMenuCurrent "main" . }}active{{ end }}" href="{{ .URL }}">{{ .Name }}</a>
{{ end }}
</div>
{{ end }}
</li>
{{- end -}}
{{- end }}
Expand Down