Code Monkey home page Code Monkey logo

aria-dropdown's Introduction

ARIA DROPDOWN

About

HTML, CSS and JS UI-components for user-friendly and accessible drop-downs: WAI ARIA 1.1 compliant. Go to demo page or check on npm.

  • Developed following BEM methodology
  • User-friendly and accessible
  • Supports nested drop-downs (>= v2.0.0)
  • Supports mouse toggling (>= 2.0.0)
  • Only 5KB JS (minified)
  • JS plugin runs in strict mode
  • Compatible with UMD

Dependencies

jQuery

Developed and tested with jQuery 3.2.1

Cross-browser tests

  • Tested on Google Chrome 57 / macOS Sierra 10.
  • Tested on Google Chrome 60 / Windows 10.

Options

Name Default Type Description
btnClass dropdown__btn string Class used to select dropdown's buttons.
menuClass dropdown__menu string Class used to select dropdown's collapsible region
dropdownExpandedClass dropdown_expanded string Class added to dropdown when expanded
btnExpandedClass dropdown__btn_expanded string Class added to dropdown's button when dropdown is expanded.
menuExpandedClass dropdown__collapse_expanded string Class added to dropdown's collapsible region when expanded.
slideSpeed 300 int (>= 0) Slide-up and slide-down animation duration.
easing swing string The easing function to use for the dropdown animation. Applies only for jQuery transitions, if cssTransitions is set to true, this option will not have any effect on the transition. Accepted values are swing and linear. For more easing functions a jQuery easing plugin is needed.
collapseOnOutsideClick true bool Collapse dropdown, when user clicks on any region of the page which is not part of a dropdown.
collapseOnMenuClick false bool Collapse dropdown even when user clicks inside of it. Useful for non-navigational dropdown, like a dropdown in a toolbar, which should collapse automatically after an option has been selected from user.
expandOnlyOne true bool Automatically collapse dropdown if another dropdown is expanded by user
cssTransitions false bool Use css transitions to expand/collapse drop-downs instead of jQuery slide animation. Read section 'Using CSS transitions' for more informations
expandZIndex 10 int Z-index of dropdown during expand animation and while expanded.
collapseZIndex 1 int Z-index set to dropdown just before collapsing.
mouse false bool Toggle dropdown on mousenter and mouseleave (dropdown remains operable with keyboard)
dynamicBtnLabel false bool or string (selector) Define 'dynamic' labels for the dropdown button. Set the text for the expanded and collapsed status of a dropdown by adding the data-ariadropdown-expandlabel and data-ariadropdown-collapselabel attributes to the button and setting this option to true (or pass a selector - like > span - if you want to inject the text in a specific child element of the button).

Installation

Download the package from GitHub and get the compiled files from the dist folder.

The plugin is also available on npm:

npm install t-aria-dropdown

Usage

  1. Include the JS script dropdown.js - or the minified production script aria-dropdown.min.js - in the head or the body of your HTML file.
  2. Include the CSS file dropdown.css in the head of your HTML file, or include the SCSS files in your project.

HTML

Use following HTML markup to implement a dropdown:

<div class="dropdown">
  <button type="button" class="dropdown__btn">Dropdown</button>
  <div class="dropdown__menu">
    <ul>
      <li>Link 1</li>
      <li>Link 2</li>
      <li>Link 3</li>
      <li>Link 4</li>
      <li>Link 5</li>
    </ul>
    <button type="button" class="dropdown__dismiss-btn">Close dropdown</button>
  </div>
</div>

By default the menu is aligned on the left side with the button. Use modifier classes .dropdown__menu_center or .dropdown__menu_right to change menu alignment.

JS: Initialise

Initialise the plugin as follows:

$('.dropdown').ariaDropdown({
  option1: value1,
  option2: value2
});

Methods:

Methods can be called on an initialised dropdown using following syntax:

$('#my-dropdown').ariaDropdown('methodName');

The plugin supports following methods: slideDown, slideUp, toggle and destroy.

slideDown

The method slideDown expands a dropdown, if currently collapsed

$('#my-dropdown').ariaDropdown('slideDown');

slideUp

The method slideUp collapses a dropdown, if currently expanded.

$('#my-dropdown').ariaDropdown('slideUp');

toggle

Toggle expands or collapses a dropdown based on the current state of the dropdown.

$('#my-dropdown').ariaDropdown('toggle');

destroy

Destroy destroys a dropdown by removing the attributes, classes and event listeners added by the plugin. Also the jQuery data object is removed from the jQuery element's object.

$('#my-dropdown').ariaDropdown('destroy');

Custom events

The plugin triggers following events:

  • ariaDropdown.initialised after a dropdown is initialised
  • ariaDropdown.slideDown when a dropdown is expanded
  • ariaDropdown.slideUp when a dropdown is collapsed
  • ariaDropdown.destroyed when a dropdown is destroyed

The events are triggered on window and return the dropdown's data object as arguments.

//listen for ariaDropdowns.slideDown
$(window).on('ariaDropdown.slideDown', function(event, dropdown){
  console.log('The dropdown ' + dropdown.element + ' was expanded');
});

Using CSS transitions

By default the plugin is configured to use the jQuery methods slideDown() and slideUp() to expand/collapse drop-downs. Setting the option cssTransitions to 'true' will disable the JS animations. This makes possible to implement the transitions directly with css. In fact, the plugin toggles the classes passed along with the options dropdownExpandedClass, btnExpandedClass and menuExpandedClass, when a dropdown is toggled.

LICENSE

This project is licensed under the terms of the MIT license.

See LICENSE.md for detailed informations.

aria-dropdown's People

Contributors

davidetriso avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

aria-dropdown's Issues

Dropdown menu briefly shown on page load

Describe the bug
When the page hasn't fully loaded, the dropdown menu can be visible.

To Reproduce
Steps to reproduce the behavior:

  1. Load the demo page - https://davidetriso.github.io/aria-dropdown/
  2. You can view the menu for a split second or two.

Expected behavior
The dropdown menu should not be visible unless it has focus.

Additional context
I have the following CSS fix in a testing environment:

.dropdown__menu {
  /* hide offscreen for accessibility */
  position: absolute;
  left:-10000px;
  top: auto;
  width: 1px;
  height: 1px;
  overflow: hidden;
}
/* show menu when expanded */
.dropdown__menu_expanded {
  left: auto;
  width: auto;
  height: auto;
  overflow: auto;
}

The "moving content offscreen" technique is from the WebAIM article - "Invisible Content Just for Screen Reader Users":
https://webaim.org/techniques/css/invisiblecontent/

I'm not sure if this is the proper technique for menus. CSS Tricks has the following article on dropdown menus:
https://css-tricks.com/solved-with-css-dropdown-menus/

They are using simply visibility: hidden and display: none. I don't necessarily think "display: none" is the best approach there. Would welcome your thoughts.

Also worth noting from the comments section of the CSSTricks article are the following attempts at addressing accessible dropdown menus:

Add method to prevent dropdown from opening on click

Could be useful to manage dropdown state with mouse enter and mouse out on desktop.
If mousernter event is detected, block click event until mouseout occurs. By doing this it will be possible to prevent dropdown from collapsing on click for users with mouse. At the same time, the click functionality is preserved for users without mouse, and the dropdown will also be operable on touch devices.

Check behaviour of parent and nested dropdown

Currently nested dropdown works, but clicking inside the parent dropdown will not collapse any nested dropdown, even if collapseOnOutsideClick is set to true, because the parent elements prevents the default event. If a dropdown has mixed content (dropdowns and other controls), an expanded dropdown will not automatically collapse when other controls at the same nesting level are operated.
With the third level of nested dropdowns the plugin actually breaks with some specific options, and trying to expand the dropdown will instead lead to collapsing the parent dropdown.

iOS window click is not triggered

iOS window click is not triggered, need to test on IE mobile and android

collapseOnOutsideClick has no effect on mobile iOS because no click event is fired when window is clicked.

Needs to implement touch events

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.