forked from tiny-pilot/tinypilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton-dropdown.html
More file actions
128 lines (113 loc) · 3.6 KB
/
button-dropdown.html
File metadata and controls
128 lines (113 loc) · 3.6 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
<template id="dropdown-button-template">
<style>
@import "css/style.css";
:host {
--offset-top: 1rem;
--offset-right: 1rem;
--dropdown-width: 8rem;
position: relative;
display: inline-block;
}
#dropdown-items {
display: none;
position: absolute;
min-width: var(--dropdown-width);
right: var(--offset-right);
padding: 0;
margin: calc(-1 * var(--offset-top)) 0 0 0;
z-index: var(--z-index-overlay);
background-color: var(--brand-metallic-light);
box-shadow: 0 0.2em 0.3rem rgba(0, 0, 0, 0.3);
text-align: right;
border-radius: var(--border-radius);
}
slot[name="item"]::slotted(li) {
display: block;
position: relative;
list-style: none;
padding: 0.3em 0.6em;
color: white;
cursor: pointer;
}
slot[name="item"]::slotted(li:hover) {
background-color: var(--brand-metallic-bright);
}
slot[name="item"]::slotted(li.disabled) {
opacity: 0.5;
pointer-events: none;
/* Since pointer events are disabled, the cursor can’t be modified freely.
So it’s not possible to set the `not-allowed` cursor, for example. */
cursor: default;
}
</style>
<slot name="button" id="dropdown-button"></slot>
<ul id="dropdown-items">
<slot name="item"></slot>
</ul>
</template>
<script type="module">
(function () {
const template = document.querySelector("#dropdown-button-template");
customElements.define(
"dropdown-button",
class extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: "open" }).appendChild(
template.content.cloneNode(true)
);
this._handleCloseOnOutsideClick =
this._handleCloseOnOutsideClick.bind(this);
this._elements = {
dropdownButton: this.shadowRoot.getElementById("dropdown-button"),
dropdownItems: this.shadowRoot.getElementById("dropdown-items"),
};
this._elements.dropdownButton.addEventListener("click", () =>
this._toggleDropdown()
);
}
disconnectedCallback() {
document.removeEventListener(
"click",
this._handleCloseOnOutsideClick
);
}
_toggleDropdown() {
if (this._isDropdownShowing()) {
this._closeDropdown();
} else {
this._showDropdown();
}
}
_isDropdownShowing() {
return this._elements.dropdownItems.style.display === "block";
}
_showDropdown() {
this._elements.dropdownItems.style.display = "block";
// `addEventListener` will only register a function once, so it’s safe
// to call this method multiple times.
document.addEventListener("click", this._handleCloseOnOutsideClick);
}
_closeDropdown() {
this._elements.dropdownItems.style.display = "none";
document.removeEventListener(
"click",
this._handleCloseOnOutsideClick
);
}
_handleCloseOnOutsideClick(evt) {
const target = evt.composedPath()[0];
// Skip if the user has clicked on the main button of this component.
// In this case, the event handler of the button will already handle
// the click event.
if (
this.contains(target) &&
(target.slot === "button" || target.parentElement.slot === "button")
) {
return;
}
this._closeDropdown();
}
}
);
})();
</script>