generated from halo-dev/plugin-starter
-
Notifications
You must be signed in to change notification settings - Fork 18
/
emoji-button.ts
162 lines (134 loc) · 4.1 KB
/
emoji-button.ts
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
import './icons/icon-loading';
import './icons/icon-emoji';
import { LitElement, css, html } from 'lit';
import { Ref, createRef, ref } from 'lit/directives/ref.js';
import { state } from 'lit/decorators.js';
import { Picker } from 'emoji-mart';
import baseStyles from './styles/base';
import { consume } from '@lit/context';
import { emojiDataUrlContext } from './context';
import varStyles from './styles/var';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
import zh from '@emoji-mart/data/i18n/zh.json';
export class EmojiButton extends LitElement {
@state()
emojiPickerVisible = false;
@state()
emojiLoading = false;
@state()
emojiPicker: Picker | null = null;
@consume({ context: emojiDataUrlContext })
@state()
emojiDataUrl = '';
emojiPickerWrapperRef: Ref<HTMLDivElement> = createRef<HTMLDivElement>();
constructor() {
super();
this.emojiPickerVisible = false;
this.handleClickOutside = this.handleClickOutside.bind(this);
}
override connectedCallback() {
super.connectedCallback();
document.addEventListener('click', this.handleClickOutside, true);
}
override disconnectedCallback() {
document.removeEventListener('click', this.handleClickOutside, true);
super.disconnectedCallback();
}
handleClickOutside(event: Event) {
if (this.emojiPickerVisible && !event.composedPath().includes(this)) {
this.emojiPickerVisible = false;
}
}
async handleOpenEmojiPicker() {
if (this.emojiPickerVisible) {
this.emojiPickerVisible = false;
return;
}
if (this.emojiPickerWrapperRef.value?.children.length) {
this.emojiPickerVisible = true;
return;
}
this.emojiLoading = true;
const response = await fetch(this.emojiDataUrl);
const data = await response.json();
const emojiPicker = new Picker({
data,
onEmojiSelect: ({ native }: { native: string }) => {
this.dispatchEvent(new CustomEvent('emoji-select', { detail: { native } }));
},
i18n: zh,
});
// TODO: fix this ts error
this.emojiPickerWrapperRef.value?.appendChild(emojiPicker as unknown as Node);
this.emojiPickerVisible = true;
this.emojiLoading = false;
}
override render() {
return html`<button class="emoji-button" type="button">
${this.emojiLoading
? html`<icon-loading></icon-loading>`
: html`<icon-emoji @click=${this.handleOpenEmojiPicker}></icon-emoji>`}
<div
class="form__emoji-panel"
style="display: ${this.emojiPickerVisible ? 'block' : 'none'}"
${ref(this.emojiPickerWrapperRef)}
></div>
</button>`;
}
static override styles = [
varStyles,
baseStyles,
css`
:host {
display: inline-flex;
}
em-emoji-picker {
--rgb-color: var(--component-emoji-picker-rgb-color);
--rgb-accent: var(--component-emoji-picker-rgb-accent);
--rgb-background: var(--component-emoji-picker-rgb-background);
--rgb-input: var(--component-emoji-picker-rgb-input);
--color-border: var(--component-emoji-picker-color-border);
--color-border-over: var(--component-emoji-picker-color-border-over);
}
.emoji-button {
color: var(--base-color);
display: inline-flex;
position: relative;
}
.emoji-button:hover icon-emoji {
opacity: 0.8;
}
.form__emoji-panel {
position: absolute;
top: 2em;
right: 0;
box-shadow: 0 0.5em 1em rgba(0, 0, 0, 0.1);
border-radius: 0.875em;
overflow: hidden;
animation: fadeInUp 0.3s both;
}
@media (max-width: 640px) {
.form__emoji-panel {
right: -7.8em;
}
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translate3d(0, 5%, 0);
}
to {
opacity: 1;
transform: translate3d(0, 0, 0);
}
}
`,
];
}
customElements.get('emoji-button') || customElements.define('emoji-button', EmojiButton);
declare global {
interface HTMLElementTagNameMap {
'emoji-button': EmojiButton;
}
}