1 import {onSelect} from "../services/dom";
2 import {Component} from "./component";
6 * Provides some simple logic to create simple dropdown menus.
8 export class Dropdown extends Component {
11 this.container = this.$el;
12 this.menu = this.$refs.menu;
13 this.toggle = this.$refs.toggle;
14 this.moveMenu = this.$opts.moveMenu;
15 this.bubbleEscapes = this.$opts.bubbleEscapes === 'true';
17 this.direction = (document.dir === 'rtl') ? 'right' : 'left';
18 this.body = document.body;
20 this.setupListeners();
21 this.hide = this.hide.bind(this);
27 this.menu.style.display = 'block';
28 this.menu.classList.add('anim', 'menuIn');
29 this.toggle.setAttribute('aria-expanded', 'true');
31 const menuOriginalRect = this.menu.getBoundingClientRect();
33 const toggleHeight = this.toggle.getBoundingClientRect().height;
34 const dropUpwards = menuOriginalRect.bottom > window.innerHeight;
36 // If enabled, Move to body to prevent being trapped within scrollable sections
38 this.body.appendChild(this.menu);
39 this.menu.style.position = 'fixed';
40 this.menu.style.width = `${menuOriginalRect.width}px`;
41 this.menu.style.left = `${menuOriginalRect.left}px`;
42 heightOffset = dropUpwards ? (window.innerHeight - menuOriginalRect.top - toggleHeight / 2) : menuOriginalRect.top;
45 // Adjust menu to display upwards if near the bottom of the screen
47 this.menu.style.top = 'initial';
48 this.menu.style.bottom = `${heightOffset}px`;
50 this.menu.style.top = `${heightOffset}px`;
51 this.menu.style.bottom = 'initial';
54 // Set listener to hide on mouse leave or window click
55 this.menu.addEventListener('mouseleave', this.hide.bind(this));
56 window.addEventListener('click', event => {
57 if (!this.menu.contains(event.target)) {
62 // Focus on first input if existing
63 const input = this.menu.querySelector('input');
64 if (input !== null) input.focus();
68 const showEvent = new Event('show');
69 this.container.dispatchEvent(showEvent);
72 event.stopPropagation();
77 for (let dropdown of window.$components.get('dropdown')) {
83 this.menu.style.display = 'none';
84 this.menu.classList.remove('anim', 'menuIn');
85 this.toggle.setAttribute('aria-expanded', 'false');
86 this.menu.style.top = '';
87 this.menu.style.bottom = '';
90 this.menu.style.position = '';
91 this.menu.style[this.direction] = '';
92 this.menu.style.width = '';
93 this.menu.style.left = '';
94 this.container.appendChild(this.menu);
101 return Array.from(this.menu.querySelectorAll('[tabindex]:not([tabindex="-1"]),[href],button,input:not([type=hidden])'));
105 const focusable = this.getFocusable();
106 const currentIndex = focusable.indexOf(document.activeElement);
107 let newIndex = currentIndex + 1;
108 if (newIndex >= focusable.length) {
112 focusable[newIndex].focus();
116 const focusable = this.getFocusable();
117 const currentIndex = focusable.indexOf(document.activeElement);
118 let newIndex = currentIndex - 1;
120 newIndex = focusable.length - 1;
123 focusable[newIndex].focus();
127 // Hide menu on option click
128 this.container.addEventListener('click', event => {
129 const possibleChildren = Array.from(this.menu.querySelectorAll('a'));
130 if (possibleChildren.includes(event.target)) {
135 onSelect(this.toggle, event => {
136 event.stopPropagation();
138 if (event instanceof KeyboardEvent) {
143 // Keyboard navigation
144 const keyboardNavigation = event => {
145 if (event.key === 'ArrowDown' || event.key === 'ArrowRight') {
147 event.preventDefault();
148 } else if (event.key === 'ArrowUp' || event.key === 'ArrowLeft') {
149 this.focusPrevious();
150 event.preventDefault();
151 } else if (event.key === 'Escape') {
154 if (!this.bubbleEscapes) {
155 event.stopPropagation();
159 this.container.addEventListener('keydown', keyboardNavigation);
161 this.menu.addEventListener('keydown', keyboardNavigation);
164 // Hide menu on enter press or escape
165 this.menu.addEventListener('keydown ', event => {
166 if (event.key === 'Enter') {
167 event.preventDefault();
168 event.stopPropagation();