Adding 'active' classes to parent nodes in a navigation list. #2612
Unanswered
aaronstezycki
asked this question in
Q&A
Replies: 2 comments 4 replies
-
|
Support for it is very new and it isn‘t yet supported in Firefox, but you could instead use the .has-subnav:has(.is-active) {
/* apply active styles ... */
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
Trying to use a similar approach to #1580 by setting active if the page.url is similar to the Using @pdehaan contains filter to match against the eleventyConfig.addFilter("contains", (haystack=[], needle="") => {
if (haystack != false)
return haystack.some(hay => needle.includes(hay));
});but keep getting a 'needle.includes is not a function' template error. {#- Pull development pages into a nav collection. -#}
{%- set devComponents = collections.all | eleventyNavigation("Elements") -%}
{% macro renderNavListItem(entry) -%}
<li{% if entry.url == page.url %} class="is-active"{% endif %}>
<a>
{{ 'Active: ' if entry.url == page.url or ['key'] | contains(page.url) else "Not Active:" }}
{{ entry.title }}
</a>
{%- if entry.children.length -%}
<ul>
{%- for child in entry.children %}
{{ renderNavListItem(child) }}
{% endfor -%}
</ul>
{%- endif -%}
</li>
{%- endmacro %}
<ul>
{%- for entry in devComponents %}
{{ renderNavListItem(entry) }}
{%- endfor -%}
</ul> |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all, I'm currently racking my brain with this one.
I'm using eleventyNavigation plugin and trying to create a 'bring your own html' navigation menu. I've started with the basic example given in the docs:
What I would like to generate is if you are within a section that has a parent is ...
So effectively, I want the entire section sub tree to have an active class on them throughout the 'journey'. i.e the Work
<ul>is active as well as the active e-commerce<li>*if i'm on the e-commerce page.I've tried using combinations of querying if the page.url has the entry.parentKey within it, but varying degrees of success. Ive also tried querying the current pages tags against the entry.parentKey, and still no dice.
I can't really use a shortcode to transform it, as I've seen others do it that way, the code would end up very verbose as I'm using Alpine.js to add extra functionality to the menu.
Beta Was this translation helpful? Give feedback.
All reactions