8000 Matchers and voters · zimmo-be/node-menu Wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Matchers and voters

Mathieu Maes edited this page May 15, 2018 · 1 revision

The Matcher service will help you identify which menu items should have the Current or Ancestor state. Ancestor means one of its children is Current or an Ancestor. For example, on the /team/sales page:

  - Home
  - Our Team (ancestor)
    - Sales (current)
  - Contact
  - Jobs
    - Apply now

The Matcher will make use of a collection of so-called voters. These are functions that decide if a given item is marked as current or not.

The Core package currently includes 1 voter calls UriVoter that makes the decision based on the given URL.

const { Matcher, UriVoter } = require("@node-menu/core");

const uriVoter = new UriVoter("/team/sales");
const matcher = new Matcher([uriVoter]);

console.log(matcher.isCurrent(salesMenu); // true
console.log(matcher.isCurrent(ourTeamMenu); // false
console.log(matcher.isAncestor(ourTeamMenu); // true

Custom voters

You can add your own voters by simply defining a function that will be called with a given MenuItem:

const selectedItemVoter = (item) => item === ourTeamMenu;

const matcher = new Matcher([selectedItemVoter]);

console.log(matcher.isCurrent(ourTeamMenu); // true
Clone this wiki locally
0