1 import {onSelect} from "../services/dom";
5 * Provides some simple logic to create simple dropdown menus.
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 if (this.direction === 'right') {
41 this.menu.style.right = `${(menuOriginalRect.right - menuOriginalRect.width)}px`;
43 this.menu.style.left = `${menuOriginalRect.left}px`;
45 this.menu.style.width = `${menuOriginalRect.width}px`;
46 heightOffset = dropUpwards ? (window.innerHeight - menuOriginalRect.top - toggleHeight / 2) : menuOriginalRect.top;
49 // Adjust menu to display upwards if near the bottom of the screen
51 this.menu.style.top = 'initial';
52 this.menu.style.bottom = `${heightOffset}px`;
54 this.menu.style.top = `${heightOffset}px`;
55 this.menu.style.bottom = 'initial';
58 // Set listener to hide on mouse leave or window click
59 this.menu.addEventListener('mouseleave', this.hide.bind(this));
60 window.addEventListener('click', event => {
61 if (!this.menu.contains(event.target)) {
66 // Focus on first input if existing
67 const input = this.menu.querySelector('input');
68 if (input !== null) input.focus();
72 const showEvent = new Event('show');
73 this.container.dispatchEvent(showEvent);
76 event.stopPropagation();
81 for (let dropdown of window.components.dropdown) {
87 this.menu.style.display = 'none';
88 this.menu.classList.remove('anim', 'menuIn');
89 this.toggle.setAttribute('aria-expanded', 'false');
90 this.menu.style.top = '';
91 this.menu.style.bottom = '';
94 this.menu.style.position = '';
95 this.menu.style[this.direction] = '';
96 this.menu.style.width = '';
97 this.container.appendChild(this.menu);
100 this.showing = false;
104 return Array.from(this.menu.querySelectorAll('[tabindex]:not([tabindex="-1"]),[href],button,input:not([type=hidden])'));
108 const focusable = this.getFocusable();
109 const currentIndex = focusable.indexOf(document.activeElement);
110 let newIndex = currentIndex + 1;
111 if (newIndex >= focusable.length) {
115 focusable[newIndex].focus();
119 const focusable = this.getFocusable();
120 const currentIndex = focusable.indexOf(document.activeElement);
121 let newIndex = currentIndex - 1;
123 newIndex = focusable.length - 1;
126 focusable[newIndex].focus();
130 // Hide menu on option click
131 this.container.addEventListener('click', event => {
132 const possibleChildren = Array.from(this.menu.querySelectorAll('a'));
133 if (possibleChildren.includes(event.target)) {
138 onSelect(this.toggle, event => {
139 event.stopPropagation();
141 if (event instanceof KeyboardEvent) {
146 // Keyboard navigation
147 const keyboardNavigation = event => {
148 if (event.key === 'ArrowDown' || event.key === 'ArrowRight') {
150 event.preventDefault();
151 } else if (event.key === 'ArrowUp' || event.key === 'ArrowLeft') {
152 this.focusPrevious();
153 event.preventDefault();
154 } else if (event.key === 'Escape') {
157 if (!this.bubbleEscapes) {
158 event.stopPropagation();
162 this.container.addEventListener('keydown', keyboardNavigation);
164 this.menu.addEventListener('keydown', keyboardNavigation);
167 // Hide menu on enter press or escape
168 this.menu.addEventListener('keydown ', event => {
169 if (event.key === 'Enter') {
170 event.preventDefault();
171 event.stopPropagation();
179 export default DropDown;