1 import {onSelect} from '../services/dom.ts';
2 import {KeyboardNavigationHandler} from '../services/keyboard-navigation.ts';
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;
37 const containerRect = this.container.getBoundingClientRect();
39 // If enabled, Move to body to prevent being trapped within scrollable sections
41 this.body.appendChild(this.menu);
42 this.menu.style.position = 'fixed';
43 this.menu.style.width = `${menuOriginalRect.width}px`;
44 this.menu.style.left = `${menuOriginalRect.left}px`;
46 heightOffset = (window.innerHeight - menuOriginalRect.top - toggleHeight / 2);
48 heightOffset = menuOriginalRect.top;
52 // Adjust menu to display upwards if near the bottom of the screen
54 this.menu.style.top = 'initial';
55 this.menu.style.bottom = `${heightOffset}px`;
56 const maxHeight = (window.innerHeight - 40) - (window.innerHeight - containerRect.bottom);
57 this.menu.style.maxHeight = `${Math.floor(maxHeight)}px`;
59 this.menu.style.top = `${heightOffset}px`;
60 this.menu.style.bottom = 'initial';
61 const maxHeight = (window.innerHeight - 40) - containerRect.top;
62 this.menu.style.maxHeight = `${Math.floor(maxHeight)}px`;
65 // Set listener to hide on mouse leave or window click
66 this.menu.addEventListener('mouseleave', this.hide);
67 window.addEventListener('click', clickEvent => {
68 if (!this.menu.contains(clickEvent.target)) {
73 // Focus on first input if existing
74 const input = this.menu.querySelector('input');
75 if (input !== null) input.focus();
79 const showEvent = new Event('show');
80 this.container.dispatchEvent(showEvent);
83 event.stopPropagation();
88 for (const dropdown of window.$components.get('dropdown')) {
94 this.menu.style.display = 'none';
95 this.menu.classList.remove('anim', 'menuIn');
96 this.toggle.setAttribute('aria-expanded', 'false');
97 this.menu.style.top = '';
98 this.menu.style.bottom = '';
99 this.menu.style.maxHeight = '';
102 this.menu.style.position = '';
103 this.menu.style[this.direction] = '';
104 this.menu.style.width = '';
105 this.menu.style.left = '';
106 this.container.appendChild(this.menu);
109 this.showing = false;
113 const keyboardNavHandler = new KeyboardNavigationHandler(this.container, event => {
116 if (!this.bubbleEscapes) {
117 event.stopPropagation();
120 if (event.target.nodeName === 'INPUT') {
121 event.preventDefault();
122 event.stopPropagation();
128 keyboardNavHandler.shareHandlingToEl(this.menu);
131 // Hide menu on option click
132 this.container.addEventListener('click', event => {
133 const possibleChildren = Array.from(this.menu.querySelectorAll('a'));
134 if (possibleChildren.includes(event.target)) {
139 onSelect(this.toggle, event => {
140 event.stopPropagation();
141 event.preventDefault();
143 if (event instanceof KeyboardEvent) {
144 keyboardNavHandler.focusNext();