verbb / navigation

A Craft CMS plugin to create navigation menus for your site.
Other
90 stars 23 forks source link

Add the attribute current to the node #361

Closed Megafry closed 1 year ago

Megafry commented 1 year ago

What are you trying to do?

To Know if a node is the current one, I need to first make a query with help of getActiveNode.

{% set currentNodeId = craft.navigation.getActiveNode().id | default %}
...
class: [
  subNode.active ? 'is-active' : null,
  subNode.id == currentNodeId ? 'is-current' : null
]

What's your proposed solution?

Add a new attribute current to all nodes identical to active.

Additional context

Did I miss something in the documentation?

engram-design commented 1 year ago

Is there any reason why node.active is not suitable for your needs? I’m not sure what difference an active and current node is-

Megafry commented 1 year ago

Hi Josh Thanks for your replay, the status current should indicate the page that the user is currently on. Highlighting the current page differently is often necessary, especially with mega menus. To set the right aria-current state would be greatly simplified. aria-current - MDN Web Docs

For the following URL page-a/page-a2

best regards

engram-design commented 1 year ago

Right, I suppose I see what you mean. In the case of a nested structure, we will mark parent nodes as active if a child is. Commonly for visual purposes to show an active state (your in Page A + Page A2). good call on aria-current, i actually wasn't aware of this.

You can use node.getActive(false) to not include children-active checks, which will effectively tell you if you're on that current node.

I'll also add a node.getCurrent() which adds the aria-current attribute to the render() call.

To get this early, run composer require verbb/navigation:"dev-craft-4 as 2.0.19".

engram-design commented 1 year ago

Added in 2.0.20