SmartMenus

repository·master·Indexed 20 days ago

https://github.com/vadikom/smartmenus

An advanced, mobile-first, responsive, and accessible jQuery plugin for creating list-based website menus. Version 1.2.1 supports AMD and CommonJS module systems and includes addons for Bootstrap, Bootstrap 4, and keyboard navigation.

Tokens
5.8K
Snippets
22
Records
26
Agent score
70%

What's inside smartmenus

  1. Use SmartMenus addons with CommonJS (npm)

    master

    Addons are available as separate npm packages. You can install and require them alongside jquery and smartmenus.

    Available Addon Packages:

    • Bootstrap 4 Addon: smartmenus-bootstrap-4
    • Bootstrap Addon: smartmenus-bootstrap
    • Keyboard Addon: smartmenus-keyboard

    Example implementation using npm and Browserify:

    1. Define your dependencies in package.json:
    {
      "name": "myapp-using-smartmenus",
      "version": "1.0.0",
      "license": "MIT",
      "dependencies": {
        "jquery": ">=2.1.3",
        "smartmenus": ">=1.1.0",
        "smartmenus-keyboard": ">=0.4.0"
      },
      "devDependencies": {
        "browserify": ">=9.0.3"
      }
    }
    1. Import the modules in your entry file (e.g., entry.js):
    var jQuery = require('jquery');
    require('smartmenus');
    require('smartmenus-keyboard');
    
    jQuery(function() {
      jQuery('#main-menu').smartmenus();
    });
    1. Bundle the files using Browserify:
    browserify entry.js > bundle.js
  2. Install SmartMenus

    master

    You can install SmartMenus using several methods:

    • npm: npm install smartmenus
    • Bower: bower install smartmenus
    • Manual: Download the latest release from the official website.
    • Git: Clone the repository directly: git clone https://github.com/vadikom/smartmenus.git.
    npm install smartmenus
  3. Use SmartMenus addons as AMD modules

    master

    To use addons from the addons directory as AMD modules (e.g., with RequireJS), you must ensure your SmartMenus jQuery plugin module is named smartmenus.

    Example RequireJS configuration:

    requirejs.config({
      "paths": {
        'smartmenus': 'jquery.smartmenus.min'
      }
      // ...
    });
  4. Understand collapsibleBehavior modes

    master

    When using mobile/collapsible styles, the collapsibleBehavior option determines how parent items behave when tapped:

    • 'default': The first tap on a parent item expands the sub-menu; the second tap loads the parent's link.
    • 'toggle': The entire parent item acts as a toggle button, expanding or collapsing the sub-menu on each tap.
    • 'link': The parent item acts as a regular link (first tap loads the link); the sub-menu can only be expanded via the sub-indicator (+/-) button.
    • 'accordion': Like 'default', but expanding a menu automatically resets (closes) any other visible sub-menus from deeper levels or different branches.
    • 'accordion-toggle': Like 'toggle', but expanding a menu automatically resets any other visible sub-menus.
    • 'accordion-link': Like 'link', but expanding a menu automatically resets any other visible sub-menus.
  5. Integrate SmartMenus with Bootstrap

    master

    The SmartMenus Bootstrap addon automates the integration between SmartMenus and Bootstrap's navbar components. It handles class synchronization (e.g., adding .open to Bootstrap menus and .active to parent items), manages collapsible behavior detection, and resolves keyboard navigation conflicts common in Bootstrap 3.3.5+.

    To use the addon, ensure jQuery, SmartMenus, and the Bootstrap addon are loaded. The addon automatically initializes all ul.navbar-nav elements that do not have the data-sm-skip attribute.

    <!-- Example HTML structure for automatic initialization -->
    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <ul class="navbar-nav">
          <li class="dropdown">
            <a href="#" class="dropdown-toggle">Menu</a>
            <ul class="dropdown-menu">
              <li><a href="#">Link</a></li>
            </ul>
          </li>
        </ul>
      </div>
    </nav>
  6. Install SmartMenus via CommonJS (npm)

    master

    SmartMenus supports CommonJS module loading. You can require the plugin and pass the jQuery dependency to the factory function.

    Note: Ensure jQuery is installed and available in your environment.

    const $ = require('jquery');
    const $.SmartMenus = require('smartmenus')( $ );
  7. Integrate SmartMenus with Bootstrap 4

    master

    The Bootstrap 4 addon automatically initializes SmartMenus on all ul.navbar-nav elements that do not have the data-sm-skip attribute. It synchronizes Bootstrap 4 classes (like .show and .active) with SmartMenus states and handles conflicts between Bootstrap's dropdown logic and SmartMenus' keyboard navigation.

    To prevent the addon from automatically initializing a specific navbar, add the data-sm-skip attribute to the <ul> element.

    <ul class="navbar-nav" data-sm-skip>
      <!-- This navbar will NOT be automatically initialized by the Bootstrap 4 addon -->
    </ul>
  8. Initialize SmartMenus via jQuery

    master

    To initialize SmartMenus on a <ul> element, use the .smartmenus() method. You can pass an options object directly to the method, or define configuration via the data-sm-options attribute on the root <ul> element. If using data-sm-options, the value must be a valid JSON object. Note that function-based options (like showFunction or hideFunction) cannot be provided via the HTML attribute and must be passed through the JavaScript options object.

    // Via JavaScript options
    $('ul').smartmenus({
      subIndicators: true,
      showDuration: 200
    });
    
    // Via HTML attribute (must be valid JSON)
    // <ul data-sm-options='{"subIndicators": true}'>...</ul>
  9. Install SmartMenus via AMD

    master

    SmartMenus supports AMD (Asynchronous Module Definition). You can define the module by providing jquery as a dependency.

    // Using RequireJS or similar AMD loader
    define(['jquery'], function($) {
        // SmartMenus is initialized via the factory pattern
        // provided by the library
    });
  10. Add keyboard navigation support to SmartMenus

    master

    The SmartMenus Keyboard Addon enables users to navigate menus using arrow keys (Left, Up, Right, Down). It automatically hooks into the keydown event for elements matching ul.sm or ul.navbar-nav:not([data-sm-skip]).

    Key behaviors include:

    • Arrow Keys: Navigates between menu items, handles sub-menu entry/exit, and respects menu direction (RTL support via .sm-rtl class).
    • Automatic Integration: Once the addon is loaded, it attaches itself to the document to listen for keyboard events on valid SmartMenus containers.
    • Exclusion: You can prevent a menu from receiving keyboard navigation by adding the data-sm-skip attribute to the <ul> element.
  11. Customize collapsible behavior for Bootstrap navbars

    master

    By default, the addon detects when a navbar becomes collapsible based on viewport width and CSS layout. You can modify this behavior using the data-sm-skip-collapsible-behavior attribute on the ul.navbar-nav element:

    1. Standard Behavior: The addon adds .sm-collapsible to the navbar and applies .navbar-toggle .sub-arrow classes to carets to make them look like Bootstrap buttons.
    2. Skip Behavior: If data-sm-skip-collapsible-behavior is present, the addon sets collapsibleBehavior to 'toggle'. This uses the entire item area as a submenu toggle and prevents the customization of carets.
    <ul class="navbar-nav" data-sm-skip-collapsible-behavior>
      <!-- Uses 'toggle' behavior and preserves default caret appearance -->
    </ul>