1 import {findClosestScrollContainer, 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 containerBounds = findClosestScrollContainer(this.menu).getBoundingClientRect();
37 const dropUpwards = menuOriginalRect.bottom > containerBounds.bottom;
38 const containerRect = this.container.getBoundingClientRect();
40 // If enabled, Move to body to prevent being trapped within scrollable sections
42 this.body.appendChild(this.menu);
43 this.menu.style.position = 'fixed';
44 this.menu.style.width = `${menuOriginalRect.width}px`;
45 this.menu.style.left = `${menuOriginalRect.left}px`;
47 heightOffset = (window.innerHeight - menuOriginalRect.top - toggleHeight / 2);
49 heightOffset = menuOriginalRect.top;
53 // Adjust menu to display upwards if near the bottom of the screen
55 this.menu.style.top = 'initial';
56 this.menu.style.bottom = `${heightOffset}px`;
57 const maxHeight = (window.innerHeight - 40) - (window.innerHeight - containerRect.bottom);
58 this.menu.style.maxHeight = `${Math.floor(maxHeight)}px`;
60 this.menu.style.top = `${heightOffset}px`;
61 this.menu.style.bottom = 'initial';
62 const maxHeight = (window.innerHeight - 40) - containerRect.top;
63 this.menu.style.maxHeight = `${Math.floor(maxHeight)}px`;
66 // Set listener to hide on mouse leave or window click
67 this.menu.addEventListener('mouseleave', this.hide);
68 window.addEventListener('click', clickEvent => {
69 if (!this.menu.contains(clickEvent.target)) {
74 // Focus on first input if existing
75 const input = this.menu.querySelector('input');
76 if (input !== null) input.focus();
80 const showEvent = new Event('show');
81 this.container.dispatchEvent(showEvent);
84 event.stopPropagation();
89 for (const dropdown of window.$components.get('dropdown')) {
95 this.menu.style.display = 'none';
96 this.menu.classList.remove('anim', 'menuIn');
97 this.toggle.setAttribute('aria-expanded', 'false');
98 this.menu.style.top = '';
99 this.menu.style.bottom = '';
100 this.menu.style.maxHeight = '';
103 this.menu.style.position = '';
104 this.menu.style[this.direction] = '';
105 this.menu.style.width = '';
106 this.menu.style.left = '';
107 this.container.appendChild(this.menu);
110 this.showing = false;
114 const keyboardNavHandler = new KeyboardNavigationHandler(this.container, event => {
117 if (!this.bubbleEscapes) {
118 event.stopPropagation();
121 if (event.target.nodeName === 'INPUT') {
122 event.preventDefault();
123 event.stopPropagation();
129 keyboardNavHandler.shareHandlingToEl(this.menu);
132 // Hide menu on option click
133 this.container.addEventListener('click', event => {
134 const possibleChildren = Array.from(this.menu.querySelectorAll('a'));
135 if (possibleChildren.includes(event.target)) {
140 onSelect(this.toggle, event => {
141 event.stopPropagation();
142 event.preventDefault();
144 if (event instanceof KeyboardEvent) {
145 keyboardNavHandler.focusNext();