-
Notifications
You must be signed in to change notification settings - Fork 60
/
dropdown.tsx
249 lines (213 loc) · 5.4 KB
/
dropdown.tsx
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* COPYRIGHT (c) Siemens AG 2018-2022 ALL RIGHTS RESERVED.
*/
import {
createPopper,
Instance as PopperInstance,
Placement,
PositioningStrategy,
} from '@popperjs/core';
import {
Component,
Element,
Event,
EventEmitter,
h,
Host,
Listen,
Method,
Prop,
Watch,
} from '@stencil/core';
@Component({
tag: 'cw-dropdown',
styleUrl: 'dropdown.scss',
shadow: true,
})
export class Dropdown {
@Element() hostElement!: HTMLCwDropdownElement;
/**
* Show dropdown
*/
@Prop({ mutable: true, reflect: true }) show = false;
/**
* Define an element that triggers the dropdown.
* A trigger can either be a string that will be interprated as id attribute or a DOM element.
*/
@Prop() trigger: string | HTMLElement;
/**
* Define an anchor element
*/
@Prop() anchor: string | HTMLElement;
/**
* Close behavior
*/
@Prop() closeBehavior: 'inside' | 'outside' | 'both' | boolean = 'both';
/**
* Placement of the dropdown
*/
@Prop() placement: Placement = 'bottom-end';
/**
* Position strategy
*/
@Prop() positioningStrategy: PositioningStrategy = 'fixed';
/**
* Adjust dropdown width to the parent width
* @deprecated - property has a typo and will get removed in the next major version. Use `adjustDropdownWidthToReferenceWidth` instead.
*/
@Prop() adjustDropdownWidthToReferenceWith = false;
/**
* Adjust dropdown width to the parent width
*/
@Prop() adjustDropdownWidthToReferenceWidth = false;
/**
* An optional header shown at the top of the dropdown
*/
@Prop() header?: string;
/**
* Fire event after visibility of dropdown has changed
*/
@Event() showChanged: EventEmitter<boolean>;
private popperInstance: PopperInstance;
private triggerElement?: Element;
private anchorElement?: Element;
private dropdownRef: HTMLElement;
private openBind: any;
constructor() {
this.openBind = this.open.bind(this);
}
get dropdownItems() {
return Array.from(this.hostElement.querySelectorAll('cw-dropdown-item'));
}
private resolveElement(prop: string | HTMLElement) {
if (typeof prop === 'string') {
return document.querySelector('#' + prop);
}
return prop;
}
async componentDidLoad() {
if (this.trigger) {
this.registerListener(this.trigger);
}
}
private registerListener(element: string | HTMLElement) {
this.triggerElement = this.resolveElement(element);
this.triggerElement.addEventListener('click', this.openBind);
}
private unregisterListener(element: string | HTMLElement) {
const trigger = this.resolveElement(element);
trigger.removeEventListener('click', this.openBind);
}
componentDidRender() {
this.popperInstance?.update();
}
@Watch('show')
async changedShow(newShow: boolean) {
if (newShow) {
this.anchorElement = this.anchor
? this.resolveElement(this.anchor)
: this.resolveElement(this.trigger);
if (this.anchorElement) {
this.popperInstance?.destroy();
this.popperInstance = createPopper(
this.anchorElement,
this.dropdownRef,
{
placement: this.placement,
strategy: this.positioningStrategy,
onFirstUpdate: ({ elements }) => {
if (
this.adjustDropdownWidthToReferenceWith ||
this.adjustDropdownWidthToReferenceWidth
) {
const { popper, reference } = elements;
const width = reference.getBoundingClientRect().width;
popper.style.width = `${width}px`;
}
},
}
);
}
}
}
@Watch('trigger')
changedTrigger(
newTriggerValue: string | HTMLElement,
oldTriggerValue: string | HTMLElement
) {
if (newTriggerValue) {
this.registerListener(newTriggerValue);
}
if (oldTriggerValue) {
this.unregisterListener(oldTriggerValue);
}
}
@Listen('click', {
target: 'window',
})
clickOutside(event: Event) {
const target = event.target as HTMLElement;
if (
this.show === false ||
this.closeBehavior === false ||
this.anchorElement === target ||
this.triggerElement === target
) {
return;
}
switch (this.closeBehavior) {
case 'outside':
if (!this.dropdownRef.contains(target)) {
this.close();
}
break;
case 'inside':
if (this.dropdownRef.contains(target)) {
this.close();
}
break;
default:
this.close();
}
}
private open(event?: Event) {
event?.preventDefault();
event?.stopPropagation();
this.show = !this.show;
this.showChanged.emit(this.show);
}
private close() {
this.show = false;
this.showChanged.emit(this.show);
}
disconnectedCallback() {
this.popperInstance?.destroy();
}
/**
* Update position of dropdown
*/
@Method()
async updatePosition() {
await this.popperInstance?.update();
}
render() {
return (
<Host
ref={(ref) => (this.dropdownRef = ref)}
class={{
'dropdown-menu': true,
show: this.show,
}}
style={{
margin: '0',
minWidth: '0px',
}}
>
<div style={{ display: 'contents' }}>
{this.header ? <div class="dropdown-header">{this.header}</div> : ''}
<slot></slot>
</div>
</Host>
);
}
}