Giter VIP home page Giter VIP logo

Comments (12)

frnco avatar frnco commented on May 18, 2024 3

This actually fixes it for single-level menus:

<div class="navbar-collapse collapse" id="navigation">
  <ul class="nav navbar-nav navbar-right">
    {{ $url := .URL }}
    {{ range .Site.Menus.main }}
      <li class="{{ if eq $url .URL }}active{{ end }} {{ if .HasChildren }}dropdown{{ end }}">
        {{ if .HasChildren }}

from hugo-universal-theme.

aaronlauterer avatar aaronlauterer commented on May 18, 2024 2

While working on my personal theme I came across a similar issue and found this issue.
I took the code snippet from @frnco and modified it to be more universal.
It might be useful for you guys :)

My solution looks as follows:

    {{- $firstUrlElement := print "/" (index (split .URL "/") 1) "/" -}}
    {{- range .Site.Menus.main -}}
        <li><a class="{{if eq ($firstUrlElement|lower) (.URL|lower) }}active{{end}}" title="{{ .Name }}" href="{{.URL}}">{{ .Name }}</a></li>
    {{ end }}

This will return true on a first level of the menu even if the page currently visited is a subpage of that page as long as it has the same url until the first /.

from hugo-universal-theme.

jaclyntan avatar jaclyntan commented on May 18, 2024 1

Thanks @aaronlauterer I'm pretty new to Hugo and it's template structure; I honestly thought I'd have to hardcode the menu manually as Hugo's documentation on it is very confusing.

I added on to your snippet to include an active class for the homepage too (and moved the class to the li so it's more flexible):

    {{- range .Site.Menus.main -}}
        <li class="{{if or ($.IsMenuCurrent "main" .) (eq ($firstUrlElement|lower) (.URL|lower)) }}active{{end}}" title="{{ .Name }}"><a href="{{.URL}}">{{ .Name }}</a></li>
    {{ end }}

So now I can create all the top level .md's I want without dodgy hacks! yay.

from hugo-universal-theme.

GeorgeWL avatar GeorgeWL commented on May 18, 2024
		<li {{ if .IsHome }}class="active"{{ end }}><a href="{{ $baseUrl }}/">Home</a></li>
			{{ $url := .URL }}
			{{ with .Site.Menus.main }}
				{{ range $menu := . }}
					<li {{ if eq $url .URL }}class="active"{{ end }}><a href="{{ .URL }}">{{ .Name }</a>
                                        </li>
				{{ end }}
			{{ end }}
				{{ if not .Site.Params.noCategoryLink }}
					<li class="dropdown">
						<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Categories<span class="caret"></span></a>
						<ul class="dropdown-menu" role="menu">
						{{ range $name, $taxonomy := .Site.Taxonomies.categories }}
							<li><a href="{{ $baseUrl }}/categories/{{ $name | urlize }}">{{ title $name }}</a></li>
						{{ end }}
						</ul>
					</li>
				{{ end }}

Would this snippet work with ours?

I'm having a brainfart on the logic of it tbh.

from hugo-universal-theme.

adrianmo avatar adrianmo commented on May 18, 2024

@GeorgeWL I'm working on this in PR #87

from hugo-universal-theme.

GeorgeWL avatar GeorgeWL commented on May 18, 2024

Anyone that's not already involved, can you give us ideas on making this work fully?

from hugo-universal-theme.

GeorgeWL avatar GeorgeWL commented on May 18, 2024

@frnco This works in both use cases without issue?

Can you be nice and make a pull request?

from hugo-universal-theme.

frnco avatar frnco commented on May 18, 2024

@GeorgeWL I can but it only works for first-level pages, i.e., blog item wouldn't be highlighted when on article page.

It's a good starting point but needs some work, I'll improve on it and send a PR as soon as I have some spare time.

from hugo-universal-theme.

GeorgeWL avatar GeorgeWL commented on May 18, 2024

We really could do with a way that works equally well for both tbh.

We just need to figure out the logic chain.

from hugo-universal-theme.

salim-b avatar salim-b commented on May 18, 2024

The commit by @jpvicari is nice! But it still lacks proper sub-level highlighting support for certain corner cases. Namely for /categories/* and /tags/* pages the "blog" menu item (posts or whatever one names it) should be highlighted, too.

UPDATE:

The following is the best I could come up with (not a Hugo expert):

<li class="dropdown{{ if eq $current.RelPermalink .URL | or (eq $current.Type $topLevel) | or (and (eq .Name "[NAME_OF_YOUR_BLOG_MENU_ENTRY]") (in (slice "taxonomy" "taxonomyTerm") $current.Kind)) }} active{{ end }}">

Also note my comment about the deprecated use of Page's .URL

UPDATE 2:

I think I came up with a reasonable solution by making use of the menu identifer key. I will submit a corresponding pull request as soon as I have time.

from hugo-universal-theme.

salim-b avatar salim-b commented on May 18, 2024

I've just submitted a PR to improve the navigation highlighting: #211

from hugo-universal-theme.

ryanfox1985 avatar ryanfox1985 commented on May 18, 2024

I closed because is resolved on the latest theme version.

from hugo-universal-theme.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.