-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkwc-drop-down.js
177 lines (160 loc) · 4.51 KB
/
kwc-drop-down.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
`kwc-drop-down` is a simple tooltip like drop-down box
Example:
<kwc-drop-down>
<ul>
<li>One</li>
<li>Two</li>
</ul>
</kwc-drop-down>
<kwc-drop-down caret-positoin="right" align="right">
<img src="/my-img.png"/>
</kwc-drop-down>
Custom property | Description | Default
----------------|-------------|----------
`--kwc-drop-down` | Mixin applied to the drop-down | `{}`
`--kwc-drop-down-background-color` | Background color of the dropdown | `white`
`--kwc-drop-down-caret-padding` | Distance of the caret from the border | `10px`
@group Kano Elements
@demo ./demo/kwc-drop-down.html
*/
/*
FIXME(polymer-modulizer): the above comments were extracted
from HTML and may be out of place here. Review them and
then delete this comment!
*/
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
class KWCDropDown extends PolymerElement {
static get template() {
return html`
<style>
:host {
cursor: auto;
z-index: 1;
position: absolute;
display: none;
}
.drop-down {
position: absolute;
left: 0px;
display: inline-block;
filter: drop-shadow(0 -1px 0 rgba(0,0,0,0.08))
drop-shadow(0 1px 0 rgba(0,0,0,0.08))
drop-shadow(1px 0px 0 rgba(0,0,0,0.08))
drop-shadow(-1px 0px 0 rgba(0,0,0,0.08));
}
.content {
box-shadow: 0px 4px 4px rgba(0,0,0,0.10);
background-color: var(--kwc-drop-down-background-color, white);
border-radius: 6px;
overflow: hidden;
@apply --kwc-drop-down;
}
.drop-down.right {
left: auto;
right: 0px;
}
b.caret {
display: block;
margin: 0 var(--kwc-drop-down-caret-padding, 10px);
padding: 0;
width: 0;
height: 0;
border-top: 0;
border-left: 8px solid transparent;
border-right: 8px solid transparent;
border-bottom: 8px solid var(--kwc-drop-down-background-color, white);
}
b.caret.right {
margin: 0 auto;
margin-right: var(--kwc-drop-down-caret-padding, 10px);
}
b.caret.center {
margin: 0 auto;
}
</style>
<div class\$="drop-down [[align]]" id="drop">
<b class\$="caret [[caretPosition]]" id="caret"></b>
<div class="content">
<slot></slot>
</div>
</div>
`;
}
static get is() {
return 'kwc-drop-down';
}
static get properties() {
return {
/**
* Position of the caret (left, right or center).
*
* @type {String}
*/
caretPosition: {
type: String,
value: 'left'
},
/**
* Alignment of the drop-down relative to the top-left corner of the
* element in DOM (left, right or center).
*
* @type {String}
*/
align: {
type: String,
value: 'left'
},
opened: {
type: Boolean,
notify: true
}
};
}
constructor() {
super();
this.opened = false;
this._onClick = this._onClick.bind(this);
}
/**
* Hides the dropdown
*/
close() {
this.opened = false;
this.style.display = 'none';
window.removeEventListener('click', this._onClick);
}
/**
* Displays the dropdown
*/
open(e) {
if (!this.opened) {
this.opened = true;
this.style.display = 'block';
setTimeout(() => {
window.addEventListener('click', this._onClick);
}, 0);
}
}
/**
* Toggles between opened and closed
*/
toggle() {
if (!this.opened) {
this.open();
} else {
this.close();
}
}
_alignChanged(alignment) {
this.toggleClass(alignment, true, this.$.drop);
}
_onClick (e) {
const path = e.path || e.composedPath();
if (this.opened && path.indexOf(this) === -1) {
this.close();
}
}
}
customElements.define(KWCDropDown.is, KWCDropDown);