-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdropdown.js
More file actions
153 lines (134 loc) · 5.56 KB
/
Copy pathdropdown.js
File metadata and controls
153 lines (134 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { chevron } from './icons.js';
class Dropdown extends HTMLElement {
constructor() {
let visibile = false;
super();
setTimeout(() => {
this.setAttributes();
this.setItems();
this.setLabel();
this.setIcon();
this.setItemsWidth();
// this.setStyleSheet();
this.dropdownOpenController();
this.dropdownAlignmentController();
});
}
// setStyleSheet() {
// const linkElement = document.createElement('link');
// linkElement.setAttribute('rel', 'stylesheet');
// linkElement.setAttribute('href', 'dropdown.css');
// document.head.appendChild(linkElement);
// }
setAttributes() {
this.setAttribute('data-its-dropdown', '');
this.setItemAttributes('data-its-dropdown-item', this.children);
}
setItemAttributes(attributeName, childElements) {
for (let item = 0; item < childElements.length; item++) {
childElements[item].setAttribute(attributeName, '');
}
}
setLabel() {
if (this.attributes.label) {
const titleElement = document.createElement('p');
const textElement = document.createTextNode(this.attributes.label.value);
titleElement.appendChild(textElement);
titleElement.setAttribute('data-its-dropdown-label', '');
this.insertBefore(titleElement, this.firstElementChild);
}
}
setItems() {
const itemElements = this.querySelectorAll('[data-its-dropdown-item]');
const containerElement = document.createElement('div');
containerElement.setAttribute('data-its-dropdown-items', '');
for (let itemIndex = 0; itemIndex < itemElements.length; itemIndex++) {
containerElement.appendChild(itemElements[itemIndex]);
}
this.appendChild(containerElement);
}
setIcon() {
const titleElement = this.querySelector('[data-its-dropdown-label]');
if (titleElement) {
titleElement.appendChild(chevron());
}
}
setItemsWidth() {
const labelDimensions = this.querySelector('[data-its-dropdown-label]').getBoundingClientRect();
const itemsDimensions = this.querySelector('[data-its-dropdown-items]').getBoundingClientRect();
if (labelDimensions.width > itemsDimensions.width) {
this.querySelector('[data-its-dropdown-items]').style.minWidth = `${labelDimensions.width}px`;
}
}
showItems() {
this.querySelector('[data-its-dropdown-items]').classList.add('active');
}
hideItems() {
this.querySelector('[data-its-dropdown-items]').classList.remove('active');
}
rotateIconOpen() {
this.querySelector('[data-its-dropdown-icon]').style.transform = 'rotate(180deg)';
}
rotateIconClose() {
this.querySelector('[data-its-dropdown-icon]').style.transform = 'rotate(0deg)';
}
dropdownOpenController() {
this.querySelector('[data-its-dropdown-label]').addEventListener('click', () => {
if (this.visibile) {
this.visibile = false;
this.hideItems();
this.rotateIconClose();
} else {
this.visibile = true;
this.showItems();
this.rotateIconOpen();
startOutsideClickListener();
startEscListener();
}
});
const startOutsideClickListener = () => {
document.addEventListener('click', clickEvent => {
if (!this.contains(clickEvent.target)) {
this.visibile = false;
this.hideItems();
this.rotateIconClose();
stopListeners();
}
});
}
const startEscListener = () => {
document.addEventListener('keyup', keyUpEvent => {
if (keyUpEvent.key.toLowerCase() === 'escape') {
this.visibile = false;
this.hideItems();
this.rotateIconClose();
stopListeners();
}
});
}
const stopListeners = () => {
document.removeEventListener('click', startOutsideClickListener);
document.removeEventListener('keyup', startEscListener);
}
}
dropdownAlignmentController() {
const setItemsAlignment = () => {
const labelDimensions = this.querySelector('[data-its-dropdown-label]').getBoundingClientRect();
const itemsDimensions = this.querySelector('[data-its-dropdown-items]').getBoundingClientRect();
// Calculate minimum space needed before items go off screen
if (labelDimensions.left < (itemsDimensions.width - labelDimensions.width)) {
// If there is not enough space for items align them left against label
this.querySelector('[data-its-dropdown-items]').style.left = `${labelDimensions.left}px`;
} else {
// If there is enough space align them right against label by setting label.left - label.width
this.querySelector('[data-its-dropdown-items]').style.left = `${(labelDimensions.left + labelDimensions.width) - itemsDimensions.width}px`;
}
}
const updateItemsAlignmentOnResize = window.addEventListener('resize', () => {
setItemsAlignment();
});
// const updateItemsAlignmentOn
setItemsAlignment();
}
}
window.customElements.define('its-dropdown', Dropdown);