-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathesn-popover.tsx
84 lines (70 loc) · 2.07 KB
/
esn-popover.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
import { Component, ComponentInterface, Host, Element, State, h, Method } from '@stencil/core';
import { createPopper } from '@popperjs/core/lib/popper-lite';
import flip from '@popperjs/core/lib/modifiers/flip';
import preventOverflow from '@popperjs/core/lib/modifiers/preventOverflow';
@Component({
tag: 'esn-popover',
styleUrl: 'esn-popover.scss',
shadow: true
})
export class EsnPopover implements ComponentInterface {
private clickOutsideListener: EventListener;
private popoverContainerEl: HTMLDivElement;
@Element() private hostEl: HTMLElement;
@State() active: boolean;
@State() fadingState: string;
componentDidLoad() {
createPopper(this.hostEl, this.popoverContainerEl, {
strategy: 'fixed',
modifiers: [flip, preventOverflow, {
name: 'preventOverflow',
options: {
padding: 8
}
}]
});
this.popoverContainerEl.addEventListener('animationstart', function (e: AnimationEvent) {
if (e.animationName === 'fade-in') {
(e.target as HTMLElement).classList.add('did-fade-in');
}
});
this.popoverContainerEl.addEventListener('animationend', function (e) {
if (e.animationName === 'fade-out') {
(e.target as HTMLElement).classList.remove('did-fade-in');
}
});
this.clickOutsideListener = (e: MouseEvent) => {
const isClickInside = this.hostEl.contains(e.target as HTMLElement);
if (!isClickInside) {
this.active = false;
}
};
document.addEventListener('click', this.clickOutsideListener);
}
disconnectedCallback() {
document.removeEventListener('click', this.clickOutsideListener);
}
/**
* A method to toggle the popover on/off.
*/
@Method()
async toggleShowState() {
this.active = !this.active;
}
render() {
return (
<Host>
<div
class={{
'esn-popover': true,
active: this.active,
inactive: !this.active
}}
ref={el => (this.popoverContainerEl = el)}
>
<slot />
</div>
</Host>
);
}
}