1 import {onSelect} from '../services/dom';
2 import {KeyboardNavigationHandler} from '../services/keyboard-navigation';
3 import {Component} from './component';
7 * Provides some simple logic to create simple dropdown menus.
9 export class Dropdown extends Component {
12 this.container = this.$el;
13 this.menu = this.$refs.menu;
14 this.toggle = this.$refs.toggle;
15 this.moveMenu = this.$opts.moveMenu;
16 this.bubbleEscapes = this.$opts.bubbleEscapes === 'true';
18 this.direction = (document.dir === 'rtl') ? 'right' : 'left';
19 this.body = document.body;
22 this.hide = this.hide.bind(this);
23 this.setupListeners();
29 this.menu.style.display = 'block';
30 this.menu.classList.add('anim', 'menuIn');
31 this.toggle.setAttribute('aria-expanded', 'true');
33 const menuOriginalRect = this.menu.getBoundingClientRect();
35 const toggleHeight = this.toggle.getBoundingClientRect().height;
36 const dropUpwards = menuOriginalRect.bottom > window.innerHeight;
38 // If enabled, Move to body to prevent being trapped within scrollable sections
40 this.body.appendChild(this.menu);
41 this.menu.style.position = 'fixed';
42 this.menu.style.width = `${menuOriginalRect.width}px`;
43 this.menu.style.left = `${menuOriginalRect.left}px`;
45 heightOffset = (window.innerHeight - menuOriginalRect.top - toggleHeight / 2);
47 heightOffset = menuOriginalRect.top;
51 // Adjust menu to display upwards if near the bottom of the screen
53 this.menu.style.top = 'initial';
54 this.menu.style.bottom = `${heightOffset}px`;
56 this.menu.style.top = `${heightOffset}px`;
57 this.menu.style.bottom = 'initial';
60 // Set listener to hide on mouse leave or window click
61 this.menu.addEventListener('mouseleave', this.hide);
62 window.addEventListener('click', clickEvent => {
63 if (!this.menu.contains(clickEvent.target)) {
68 // Focus on first input if existing
69 const input = this.menu.querySelector('input');
70 if (input !== null) input.focus();
74 const showEvent = new Event('show');
75 this.container.dispatchEvent(showEvent);
78 event.stopPropagation();
83 for (const dropdown of window.$components.get('dropdown')) {
89 this.menu.style.display = 'none';
90 this.menu.classList.remove('anim', 'menuIn');
91 this.toggle.setAttribute('aria-expanded', 'false');
92 this.menu.style.top = '';
93 this.menu.style.bottom = '';
96 this.menu.style.position = '';
97 this.menu.style[this.direction] = '';
98 this.menu.style.width = '';
99 this.menu.style.left = '';
100 this.container.appendChild(this.menu);
103 this.showing = false;
107 const keyboardNavHandler = new KeyboardNavigationHandler(this.container, event => {
110 if (!this.bubbleEscapes) {
111 event.stopPropagation();
114 if (event.target.nodeName === 'INPUT') {
115 event.preventDefault();
116 event.stopPropagation();
122 keyboardNavHandler.shareHandlingToEl(this.menu);
125 // Hide menu on option click
126 this.container.addEventListener('click', event => {
127 const possibleChildren = Array.from(this.menu.querySelectorAll('a'));
128 if (possibleChildren.includes(event.target)) {
133 onSelect(this.toggle, event => {
134 event.stopPropagation();
136 if (event instanceof KeyboardEvent) {
137 keyboardNavHandler.focusNext();