2.0.0 • Published 6 years ago
@unplugstudio/droppy v2.0.0
Droppy
Simple, accessible, nested dropdow menus. Inspired by Bootstrap dropdowns.
Supports TAB navigation, Space/Enter to toggle the dropdowns, Esc to close. All positioning is done via CSS, so it can easily be converted to accordions / drawers for mobile users.
No dependencies. Less than 4 KB minified, less than 2 KB gzipped.
Open demo.html for a complete demo.
Installation
You can include Droppy directly in your document:
<script src="lib/droppy.min.js">
<script>
droppy.Droppy(...)
droppy.init(...)
<script>You can also install from git and consume as a ES6 module:
npm install git+https://gitlab.com/jerivas/droppy.gitimport Droppy, { init } from 'droppy'Required markup
<button data-toggle="dropdown" aria-controls="dropdown-1">
Open dropdown
</button>
<ul id="dropdown-1">
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
</ul>
<script>
// Single element
var btn = document.querySelector('[data-toggle="dropdown"]')
var myDroppy = new droppy.Droppy(btn)
// OR: All elements with [data-toggle="dropdown"] in one go
droppy.init()
</script>- Add
data-toggle="dropdown"to thebutton - Add an id to the
<ul>and pass it toaria-controlson the button - Initialize Droppy on the
button - When the dropdown is toggled, the class
openwill be added to the dropdown (you can pass a custom class toinitandDroppyas the second argument) - Also the
aria-expandedattribute will be toggled on the button - Optional: Attach event listeners to the document and listen for:
show.droppyshown.droppyhide.droppyhidden.droppyOn each event you'll have access to the dropdown asevent.targetand the button asevent.detail.relatedTarget
2.0.0
6 years ago