1 import {onSelect} from "../services/dom";
2 import {KeyboardNavigationHandler} from "../services/keyboard-navigation";
6 * Provides some simple logic to create simple dropdown menus.
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`;
44 heightOffset = dropUpwards ? (window.innerHeight - menuOriginalRect.top - toggleHeight / 2) : menuOriginalRect.top;
47 // Adjust menu to display upwards if near the bottom of the screen
49 this.menu.style.top = 'initial';
50 this.menu.style.bottom = `${heightOffset}px`;
52 this.menu.style.top = `${heightOffset}px`;
53 this.menu.style.bottom = 'initial';
56 // Set listener to hide on mouse leave or window click
57 this.menu.addEventListener('mouseleave', this.hide);
58 window.addEventListener('click', event => {
59 if (!this.menu.contains(event.target)) {
64 // Focus on first input if existing
65 const input = this.menu.querySelector('input');
66 if (input !== null) input.focus();
70 const showEvent = new Event('show');
71 this.container.dispatchEvent(showEvent);
74 event.stopPropagation();
79 for (let dropdown of window.components.dropdown) {
85 this.menu.style.display = 'none';
86 this.menu.classList.remove('anim', 'menuIn');
87 this.toggle.setAttribute('aria-expanded', 'false');
88 this.menu.style.top = '';
89 this.menu.style.bottom = '';
92 this.menu.style.position = '';
93 this.menu.style[this.direction] = '';
94 this.menu.style.width = '';
95 this.menu.style.left = '';
96 this.container.appendChild(this.menu);
103 const keyboardNavHandler = new KeyboardNavigationHandler(this.container, (event) => {
106 if (!this.bubbleEscapes) {
107 event.stopPropagation();
110 if (event.target.nodeName === 'INPUT') {
111 event.preventDefault();
112 event.stopPropagation();
118 keyboardNavHandler.shareHandlingToEl(this.menu);
121 // Hide menu on option click
122 this.container.addEventListener('click', event => {
123 const possibleChildren = Array.from(this.menu.querySelectorAll('a'));
124 if (possibleChildren.includes(event.target)) {
129 onSelect(this.toggle, event => {
130 event.stopPropagation();
132 if (event instanceof KeyboardEvent) {
133 keyboardNavHandler.focusNext();
140 export default DropDown;