Giter Club home page Giter Club logo

metismenujs's Introduction

Collapsible menu plugin with Vanilla-JS

npm version install size npm bundle size npm downloads Packagist

Table of Contents

Browser Support

This plugin does not support any version of IE browser.

Chrome Firefox Safari Opera Edge IE
Latest ✔ Latest ✔ Latest ✔ Latest ✔ Latest ✔

Install

Package Managers

Using npm:

npm install metismenujs

Using yarn:

yarn add metismenujs

Using pnpm:

pnpm add metismenujs

Once the package is installed, you can import the library using import or require approach:

// recommended approach
import { MetisMenu } from 'metismenujs';
// You can also use the default export
import MetisMenu from 'metismenujs';

If you use require for importing, only default export is available:

const MetisMenu = require('metismenujs');

Note CommonJS usage In order to gain the TypeScript typings (for intellisense / autocomplete) while using CommonJS imports with require(), add the following to your tsconfig.json:

{
  "compilerOptions": {
    "moduleResolution": "NodeNext"
  }
}

CDN

ES6 UMD browser module

Using jsDelivr CDN:

<script src="https://cdn.jsdelivr.net/npm/metismenujs"></script>

Using unpkg CDN:

<script src="https://unpkg.com/metismenujs"></script>

ES6 ES module

Using jsDelivr CDN:

<script type="module">
  import { MetisMenu } from 'https://cdn.jsdelivr.net/npm/metismenujs/dist/metismenujs.esm.min.js';
</script>

Using unpkg CDN:

<script type="module">
  import { MetisMenu } from 'https://unpkg.com/metismenujs/dist/metismenujs.esm.min.js';
</script>

Install with composer

composer require onokumus/metismenujs:dev-master

Download

download

Ready to use files are located in the dist directory.

Usage

  1. Include metismenujs StyleSheet

    Using CDN

    jsDelivr :

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/metismenujs/dist/metismenujs.min.css">

    unpkg :

    <link rel="stylesheet" href="https://unpkg.com/metismenujs/dist/metismenujs.min.css">

    Using Vite, Astro etc.

    import 'metismenujs/style';

    or sass source file

    import 'metismenujs/sass';
  2. Add class metismenu to unordered list

<ul class="metismenu" id="menu">

</ul>
  1. Make expand/collapse controls accessible

Be sure to add aria-expanded to the element a. This attribute explicitly defines the current state of the collapsible element to screen readers and similar assistive technologies. If the collapsible element is closed by default, it should have a value of aria-expanded="false". If you've set the collapsible element's parent li element to be open by default using the active class, set aria-expanded="true" on the control instead. The plugin will automatically toggle this attribute based on whether or not the collapsible element has been opened or closed.

<ul class="metismenu" id="menu">
  <li class="mm-active">
    <a href="#" aria-expanded="true">Menu 1</a>
    <ul>
    ...
    </ul>
  </li>
  <li>
    <a href="#" aria-expanded="false">Menu 2</a>
    <ul>
    ...
    </ul>
  </li>
  ...
  </ul>
  1. Arrow Options

add has-arrow class to a element

<ul class="metismenu" id="menu">
<li class="mm-active">
  <a class="has-arrow" href="#" aria-expanded="true">Menu 1</a>
  <ul>
  ...
  </ul>
</li>
<li>
  <a class="has-arrow" href="#" aria-expanded="false">Menu 2</a>
  <ul>
  ...
  </ul>
</li>
...
</ul>
  1. Call the plugin:
  new MetisMenu("#menu");
  // or
  MetisMenu.attach('#menu');

Stopping list opening on certain elements

Setting aria-disabled="true" in the <a> element as shown will stop metisMenu opening the menu for that particular list. This can be changed dynamically and will be obeyed correctly:

<a href="#" aria-expanded="false" aria-disabled="true">List 1</a>

Options

Option Type Default Description Example
toggle Boolean true For auto collapse support. new MetisMenu("#menu", { toggle: false });
triggerElement css selector a new MetisMenu("#menu", { triggerElement: '.nav-link' });
parentTrigger css selector li new MetisMenu("#menu", { parentTrigger: '.nav-item' });
subMenu css selector ul new MetisMenu("#menu", { subMenu: '.nav.flex-column' });

API

dispose

For stop and destroy metisMenu.

 const mm = new MetisMenu("#menu");
 mm.dispose();

update

Re-init metisMenu.

const mm = new MetisMenu("#menu");
mm.dispose();
// ajax ...
mm.update();

Events

Event Type Description
show.metisMenu This event fires immediately when the show instance method is called.
shown.metisMenu This event is fired when a collapse ul element has been made visible to the user (will wait for CSS transitions to complete).
hide.metisMenu This event is fired immediately when the hide method has been called.
hidden.metisMenu This event is fired when a collapse ul element has been hidden from the user (will wait for CSS transitions to complete).

CSS custom properties (variables)

Property Default Description
--mm-transition-timing-function ease This property sets how intermediate values are calculated for CSS properties being affected by a transition effect.
--mm-trantisition-duration 0.35s This property sets the length of time a transition animation should take to complete.

Migrating to v1.0.3 from v1.4.0

  • Update metisMenu.js & metisMenu.css files
  • Change active class to mm-active

Examples

https://github.com/metismenu/examples

Demo

https://onokumus.github.io/metismenujs

Contains a simple HTML file to demonstrate metisMenu plugin.

About

Related projects

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Author

Osman Nuri Okumus

License

Copyright © 2023, Osman Nuri Okumuş. Released under the MIT License.

metismenujs's People

Contributors

dependabot[bot] avatar onokumus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

metismenujs's Issues

Elements in a-tag

Describe the bug
If there are other elements within the a-tag (e.g. <a href="#">Text <i class="fa fa-archive"></i></a>, the submenu will not show/hide if the users clicks on the element within the a-tag.

To Reproduce
Create a menu structure like this, where the a-tag contains another element:

<ul class="metismenu" id="menu">
    <li>
        <a href="#" class="has-arrow" aria-expanded="false">
            <i class="fa fa-archive"></i>
            Text
        </a>
        <ul>
             <li><a href="#">Link</a>
        </ul>
    </li>
</ul>

and intialize the menu: new MetisMenu('#menu');

Now click on the icon (i-tag) and the active-class won't be added to the li-tag.

Expected behavior
The expected behaviour is that the submenu shows or hides depending on the state.

Desktop (please complete the following information):

  • OS: Windows 10 1803
  • Browser Chrome
  • Version 68

Additional context
Stacktrace

Uncaught TypeError: Cannot read property 'parentNode' of null
    at e.t.toggle (index.js:97)
    at e.t.clickEvent (index.js:93)

Code available here: https://jsfiddle.net/aq9Laaew/125078/

MetisMenu root menu items without sub menu items not working

how to activate homepage items.

<li>
      <NavLink to="/home"> Home Page </NavLink>
</li>
<li>
    <a className="has-arrow" href="#" aria-expanded="false">Master </a>
    <ul className="nav nav-second-level">
    <li>
          <NavLink to="/master/one"> One </NavLink>
   </li>
   <li>
          <NavLink to="/master/two">Two</NavLink>
   </li>
   </ul>
</li>

'prefers-reduced-motion' and transition-duration: 0s

Describe the bug
Appears to be an issue if the following is part of the CSS and reduced motion is enabled on the OS...

@media (prefers-reduced-motion: reduce) { 
   *, ::before, ::after { 
     transition-duration: 0s !important; 
   } 
 }

The first event on the menu works fine but after that, nothing, the menu completely freezes up with no errors. Strangely works fine if transition-duration: 0.000000001s, just not with transition-duration: 0s

Expected behavior
Menu works fine with no animations

Desktop (please complete the following information):

  • OS: Windows 10]
  • Browser CHrome / Firefox (latest)
  • Latest

Submenu is not closing when user clicks outside of its container in horizontal menus.

Describe the bug
Submenu cannot be hidden when user clicks outside of its container

To Reproduce
1- Go to http://onokumus.com/metismenujs/mm-horizontal.html
2- Click any menu with submenu
3- Slick outside of submenu

Expected behavior
It should close the submenu open

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):
Any browser

Smartphone (please complete the following information):
Any device

Additional context
No additional context

aria-expanded must be used on elements with the role "button"

Describe the bug
In the examples <a> is used with aria-expaned but without role="button".

To Reproduce
See documenation / web page.

Expected behavior

For proper a11y the aria-expanded attribute must be used on real buttons or elements with their role set to button.
Either add role="button" in the examples or us <button> instead of <a>.

Note: a button must be able to be triggerd by hitting the space key.

Menu Dropdowns Not Functioning Properly in Angular 17 SSR Project with 'metismenujs' Library

Describe the bug:
I am encountering an issue while using "metismenujs" version 1.4.0 in my Angular 17 SSR (Server-Side Rendering) project, which utilizes Node.js version 18.13.0. The problem arises when attempting to re-initialize the menu using the provided code snippets.

Error 1:
When attempting to re-initialize the menu with the following code:
this.menu = new MetisMenu(this.sideMenu.nativeElement);

I receive the following error message during Angular SSR CLI execution:
ERROR TypeError: this.element.querySelectorAll is not a function or its return value is not iterable
at _MetisMenu.init

Error 2:
Similarly, when trying to re-initialize the menu with the code snippet:
this.menu = new MetisMenu("#menu");
this.menu.update();

I encounter the error:
ERROR ReferenceError: document is not defined
at new _MetisMenu

Symptoms:
When commenting out the menu re-initialization code and only leaving the activation function (this._activateMenuDropdown()), Angular CLI does not display any errors. However, the menu functionality on the web browser is not working properly.

For better clarity, please refer to the attached snapshots.

To Reproduce:
Install "metismenujs" version 1.4.0 in an Angular 17 SSR project.
Attempt to re-initialize the menu using either of the provided code snippets.

Expected behavior
The menu should be successfully re-initialized without encountering errors.
The menu dropdowns should be activated and functional without any issues.

Actual Behavior:
While no errors are shown in the Angular CLI, the menu dropdowns do not function properly in the web browser.

Screenshots

Error 1:
image
image

Error 2:
image
image

Symptoms:
image
image
image

Angular Version
image

Additional context:
Angular version: 17
"metismenujs" version: 1.4.0
Node.js version: 18.13.0
Please let me know if further information is required to address this issue.

1st level inactive

I added metismenu (metismenujs - v1.2.1 vertical - because it doesnt need jquery) to cmsimple-cms and have it functioning.
But just the anchors of Menulevel 1 without dropdowns function. At Menulevel 1 with dropdowns only the sublinks function - similar to bootstrap 4 (where the toplevel is inactive as soon as it has a dropdown). The click just opens the dropdown....
If i open the dead anchor in a new browser-tab it functions. The anchors of the deeper menulevels function. Smartmenus doesnt have the problem (hover)

A CSS triangle for horizontal submenus pointing to the parent link like github menus

Is your feature request related to a problem? Please describe.
It is easier for users to see how the submenu is connected its parent link.

Describe the solution you'd like
A CSS triangle like in the github menus makes it visually easy to point to a submenu's parent link
Describe alternatives you've considered
There are no alternatives
Additional context
screenshot from 2018-10-09 09-33-23

API to collapse all active ULs

Is your feature request related to a problem? Please describe.
Simply put, I am trying to create a button that will close all active ULs

Describe the solution you'd like

const mm = new MetisMenu("#menu");
mm.closeall();

Additional context
Forgive me if this is already possible and the issue is really my lack of JS knowledge 😄

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.