10up / component-library

A library of barebones front-end components built with WordPress and accessibility in mind.
https://baseline.10up.com/
MIT License
59 stars 12 forks source link

adding new navigation #49

Open saltnpixels opened 3 years ago

saltnpixels commented 3 years ago

Description of the Change

See #45 I have remade the navigation based on issues found and features I felt were missing.

Major Changes

How to use

Similar to the original you would create a navigation instance, only this time you have more options.

Second parameter breakpoint changes: The second parameter doesn't need to take a media query. It can now take a media query or a css unit or a boolean (20px, 60em, --var, true, false). Below this unit means mobile, above it means desktop. This controls when the menu is vertical or horizontal. When vertical, hover events will be ignored. CSS is also applied a bit differently for vertical menus. It can also be set to true/false which means the menu will be considered vertical or horizontal at all times. True means mobile, which means always vertical. This comes in handy, for example, on a menu placed in a sidebar.

A menu toggle button is now optional. you can pass navButtonElement of false for no button.

Options

Helpful Properties

You can use some class properties to help you when creating some callbacks.

Example

let nav = new CreateNavigation('#primary-nav', {
   breakpoint: '--nav-breakpoint',
   navButtonElement: '.menu-toggle',
   onSubMenuOpen: (menuItem, submenu)=>{
      if(nav.isMobile()){
         $(submenu).slideDown();
      }
   },
 onSubMenuClose: (menuItem, submenu)=>{
       if(nav.isMobile()){
          $(submenu).slideUp();
       }
    }
})

The Above example will make a menu that slides the submenus up and down on mobile when the menu is vertical. We will assume a css variable of --nav-breakpoint exists. If this variable changes at any time, the js will update. Below is html markup that matches the above js.

Html/PHP Markup Example

<button class="menu-toggle" aria-label="Toggle Main Menu" aria-controls="nav-holder">
   Menu Button
</button>

<div id="nav-holder">
  <nav id="primary-nav" class="horizontal-menu js-menu" role="navigation" aria-label="Main Menu">
      <?php wp_nav_menu( array(
          'theme_location' => 'top-menu',
          'menu_id'        => 'top-menu',
          'container'      => '',
                   'fallback_cb'    => '__return_false',  

      ) ); ?>
   </nav>
</div>

Note that the aria-controls of the button do not need to point to a nav element. They can open/close anything. This allows for opening a div that potentially holds many menus etc.. All other aria attributes will be added for you.

I also added a class of horizontal-menu to the nav. This creates a horizontal menu that will go vertical at the breakpoint you specified. This class is not needed, as js will put it there. It is however recommended to add it because if javascript has been disabled it will still work.

CSS Changes

NOTE: There is no css for how to hide/show the menu. When the menu button is clicked, aria attributes are applied, but not styled. It is up to the developer to decide how the menu appears. (flies in from side, opacity change etc...)

Alternate Designs

I tried to find the most common scenarios found on most sites I have made or seen and what is expected from a menu.

Benefits

Save 10+ hours making a menu :)

Possible Drawbacks

I don't see any...yet.

Verification Process

Tested on a WordPress site.

Checklist:

saltnpixels commented 3 years ago

If it has to be backwards compatible, I can do that... The biggest changes are the breakpoint accepts a simple unit e.g: 768px instead of a whole query like (min-width...) And the menu button is optional which means if someone does not add it as an option, it is considered not there and the button will be ignored.