-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathEmojiPicker.tsx
More file actions
448 lines (401 loc) · 18.4 KB
/
EmojiPicker.tsx
File metadata and controls
448 lines (401 loc) · 18.4 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
Copyright 2024 New Vector Ltd.
Copyright 2020 The Matrix.org Foundation C.I.C.
Copyright 2019 Tulir Asokan <tulir@maunium.net>
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import React, { type Dispatch } from "react";
import { DATA_BY_CATEGORY, getEmojiFromUnicode, type Emoji as IEmoji } from "@matrix-org/emojibase-bindings";
import { clamp } from "@element-hq/web-shared-components";
import classNames from "classnames";
import { _t } from "../../../languageHandler";
import * as recent from "../../../emojipicker/recent";
import AutoHideScrollbar from "../../structures/AutoHideScrollbar";
import Header from "./Header";
import Search from "./Search";
import Preview from "./Preview";
import QuickReactions from "./QuickReactions";
import Category, { type CategoryKey, type ICategory } from "./Category";
import { filterBoolean } from "../../../utils/arrays";
import {
type IAction as RovingAction,
type IState as RovingState,
RovingTabIndexProvider,
RovingStateActionType,
} from "../../../accessibility/RovingTabIndex";
import { Key } from "../../../Keyboard";
import { type ButtonEvent } from "../elements/AccessibleButton";
export const CATEGORY_HEADER_HEIGHT = 20;
export const EMOJI_HEIGHT = 35;
export const EMOJIS_PER_ROW = 8;
const ZERO_WIDTH_JOINER = "\u200D";
interface IProps {
selectedEmojis?: Set<string>;
onChoose(unicode: string): boolean;
onFinished(): void;
isEmojiDisabled?: (unicode: string) => boolean;
}
interface IState {
filter: string;
previewEmoji?: IEmoji;
scrollTop: number;
// initial estimation of height, dialog is hardcoded to 450px height.
// should be enough to never have blank rows of emojis as
// 3 rows of overflow are also rendered. The actual value is updated on scroll.
viewportHeight: number;
// Track if user has interacted with arrow keys or search
showHighlight: boolean;
}
class EmojiPicker extends React.Component<IProps, IState> {
private readonly recentlyUsed: IEmoji[];
private readonly memoizedDataByCategory: Record<CategoryKey, IEmoji[]>;
private readonly categories: ICategory[];
private scrollRef = React.createRef<AutoHideScrollbar<"div">>();
public constructor(props: IProps) {
super(props);
this.state = {
filter: "",
scrollTop: 0,
viewportHeight: 280,
showHighlight: false,
};
// Convert recent emoji characters to emoji data, removing unknowns and duplicates
this.recentlyUsed = Array.from(new Set(filterBoolean(recent.get().map(getEmojiFromUnicode))));
this.memoizedDataByCategory = {
recent: this.recentlyUsed,
...DATA_BY_CATEGORY,
};
const hasRecentlyUsed = this.recentlyUsed.length > 0;
const categoryConfig: Pick<ICategory, "id" | "name" | "emoji">[] = [
{ id: "recent", name: _t("emoji|category_frequently_used"), emoji: "🕒" },
{ id: "people", name: _t("emoji|category_smileys_people"), emoji: "😀" },
{ id: "nature", name: _t("emoji|category_animals_nature"), emoji: "🐕" },
{ id: "foods", name: _t("emoji|category_food_drink"), emoji: "🍎" },
{ id: "activity", name: _t("emoji|category_activities"), emoji: "⚽️" },
{ id: "places", name: _t("emoji|category_travel_places"), emoji: "🚗" },
{ id: "objects", name: _t("emoji|category_objects"), emoji: "💡" },
{ id: "symbols", name: _t("emoji|category_symbols"), emoji: "⁉️" },
{ id: "flags", name: _t("emoji|category_flags"), emoji: "🏁" },
];
this.categories = categoryConfig.map((config) => {
let isEnabled = true;
let isVisible = false;
let firstVisible = false;
if (config.id === "recent") {
isEnabled = hasRecentlyUsed;
isVisible = hasRecentlyUsed;
firstVisible = hasRecentlyUsed;
} else if (config.id === "people") {
isVisible = true;
firstVisible = !hasRecentlyUsed;
}
return {
...config,
enabled: isEnabled,
visible: isVisible,
firstVisible: firstVisible,
ref: React.createRef(),
};
});
}
private onScroll = (): void => {
const body = this.scrollRef.current?.containerRef.current;
if (!body) return;
this.setState({
scrollTop: body.scrollTop,
viewportHeight: body.clientHeight,
});
this.updateVisibility();
};
// Given a roving emoji button returns the role=row element containing it
private getRow(rovingNode?: Element): Element | undefined {
return this.getGridcell(rovingNode)?.parentElement ?? undefined;
}
// Given a roving emoji button returns the role=gridcell element containing it
private getGridcell(rovingNode?: Element): Element | undefined {
return rovingNode?.parentElement ?? undefined;
}
// Given a role=gridcell node returns the roving emoji button contained within
private getRovingNode(gridcellNode?: Element): Element | undefined {
return gridcellNode?.children[0];
}
private keyboardNavigation(ev: React.KeyboardEvent, state: RovingState, dispatch: Dispatch<RovingAction>): void {
const rowElement = this.getRow(state.activeNode);
const gridcellNode = this.getGridcell(state.activeNode);
if (!rowElement || !gridcellNode || !state.activeNode) return;
// Index of element within row container
const columnIndex = Array.from(rowElement.children).indexOf(gridcellNode);
// Index of element within the list of roving nodes
const refIndex = state.nodes.indexOf(state.activeNode);
let focusNode: HTMLElement | undefined;
let newRowElement: Element | undefined;
switch (ev.key) {
case Key.ARROW_LEFT:
focusNode = state.nodes[refIndex - 1];
newRowElement = this.getRow(focusNode);
break;
case Key.ARROW_RIGHT:
focusNode = state.nodes[refIndex + 1];
newRowElement = this.getRow(focusNode);
break;
case Key.ARROW_UP:
case Key.ARROW_DOWN: {
// For up/down we find the prev/next parent by inspecting the refs either side of our row
const node =
ev.key === Key.ARROW_UP
? state.nodes[refIndex - columnIndex - 1]
: state.nodes[refIndex - columnIndex + EMOJIS_PER_ROW];
newRowElement = this.getRow(node);
if (newRowElement) {
const newColumnIndex = clamp(columnIndex, 0, newRowElement.children.length - 1);
const newTarget = this.getRovingNode(newRowElement?.children[newColumnIndex]);
focusNode = state.nodes.find((r) => r === newTarget);
}
break;
}
}
if (focusNode) {
// Only move actual DOM focus if an emoji already has focus
// If the input has focus, keep using aria-activedescendant for virtual focus
if (document.activeElement !== document.querySelector(".mx_EmojiPicker_search input")) {
focusNode?.focus();
}
dispatch({
type: RovingStateActionType.SetFocus,
payload: { node: focusNode },
});
if (rowElement !== newRowElement) {
focusNode?.scrollIntoView({
behavior: "auto",
block: "center",
inline: "center",
});
}
}
ev.preventDefault();
ev.stopPropagation();
}
private onKeyDown = (ev: React.KeyboardEvent, state: RovingState, dispatch: Dispatch<RovingAction>): void => {
if (state.activeNode && [Key.ARROW_DOWN, Key.ARROW_RIGHT, Key.ARROW_LEFT, Key.ARROW_UP].includes(ev.key)) {
// If highlight is not shown yet, show it and reset to first emoji
if (!this.state.showHighlight) {
this.setState({ showHighlight: true });
// Reset to first emoji when showing highlight for the first time (or after it was hidden)
if (state.nodes.length > 0) {
dispatch({
type: RovingStateActionType.SetFocus,
payload: { node: state.nodes[0] },
});
}
ev.preventDefault();
ev.stopPropagation();
return;
}
this.keyboardNavigation(ev, state, dispatch);
}
};
private updateVisibility = (): void => {
const body = this.scrollRef.current?.containerRef.current;
if (!body) return;
const rect = body.getBoundingClientRect();
let firstVisibleFound = false;
for (const cat of this.categories) {
const elem = body.querySelector(`[data-category-id="${cat.id}"]`);
if (!elem) {
cat.visible = false;
cat.ref.current?.classList.remove("mx_EmojiPicker_anchor_visible");
continue;
}
const elemRect = elem.getBoundingClientRect();
const y = elemRect.y - rect.y;
const yEnd = elemRect.y + elemRect.height - rect.y;
cat.visible = y < rect.height && yEnd > 0;
if (cat.visible && !firstVisibleFound) {
firstVisibleFound = true;
cat.firstVisible = true;
} else {
cat.firstVisible = false;
}
// We update this here instead of through React to avoid re-render on scroll.
if (!cat.ref.current) continue;
if (cat.visible) {
cat.ref.current.classList.add("mx_EmojiPicker_anchor_visible");
cat.ref.current.setAttribute("aria-selected", "true");
} else {
cat.ref.current.classList.remove("mx_EmojiPicker_anchor_visible");
cat.ref.current.setAttribute("aria-selected", "false");
}
if (cat.firstVisible) {
cat.ref.current.setAttribute("tabindex", "0");
} else {
cat.ref.current.setAttribute("tabindex", "-1");
}
}
};
private scrollToCategory = (category: string): void => {
this.scrollRef.current?.containerRef.current
?.querySelector(`[data-category-id="${category}"]`)
?.scrollIntoView();
};
private onChangeFilter = (filter: string): void => {
const lcFilter = filter.toLowerCase().trim(); // filter is case insensitive
// User has typed a query, show highlight
// If filter is cleared, hide highlight again
if (lcFilter && !this.state.showHighlight) {
this.setState({ showHighlight: true });
} else if (!lcFilter && this.state.showHighlight) {
this.setState({ showHighlight: false });
}
for (const cat of this.categories) {
let emojis: IEmoji[];
// If the new filter string includes the old filter string, we don't have to re-filter the whole dataset.
if (lcFilter.includes(this.state.filter)) {
emojis = this.memoizedDataByCategory[cat.id];
} else {
emojis = cat.id === "recent" ? this.recentlyUsed : DATA_BY_CATEGORY[cat.id];
}
if (lcFilter !== "") {
emojis = emojis.filter((emoji) => this.emojiMatchesFilter(emoji, lcFilter));
// Copy the array to not clobber the original unfiltered sorting
emojis = [...emojis].sort((a, b) => {
const indexA = a.shortcodes[0].indexOf(lcFilter);
const indexB = b.shortcodes[0].indexOf(lcFilter);
// Prioritize emojis containing the filter in its shortcode
if (indexA == -1 || indexB == -1) {
return indexB - indexA;
}
// If both emojis start with the filter
// put the shorter emoji first
if (indexA == 0 && indexB == 0) {
return a.shortcodes[0].length - b.shortcodes[0].length;
}
// Prioritize emojis starting with the filter
return indexA - indexB;
});
}
this.memoizedDataByCategory[cat.id] = emojis;
cat.enabled = emojis.length > 0;
// The setState below doesn't re-render the header and we already have the refs for updateVisibility, so...
if (cat.ref.current) {
cat.ref.current.disabled = !cat.enabled;
}
}
this.setState({ filter });
// Header underlines need to be updated, but updating requires knowing
// where the categories are, so we wait for a tick.
window.setTimeout(this.updateVisibility, 0);
};
private emojiMatchesFilter = (emoji: IEmoji, filter: string): boolean => {
// If the query is an emoji containing a variation then strip it to provide more useful matches
if (filter.includes(ZERO_WIDTH_JOINER)) {
filter = filter.split(ZERO_WIDTH_JOINER, 2)[0];
}
return (
emoji.label.toLowerCase().includes(filter) ||
(Array.isArray(emoji.emoticon)
? emoji.emoticon.some((x) => x.includes(filter))
: emoji.emoticon?.includes(filter)) ||
emoji.shortcodes.some((x) => x.toLowerCase().includes(filter)) ||
emoji.unicode.split(ZERO_WIDTH_JOINER).includes(filter)
);
};
private onEnterFilter = (): void => {
// Only select emoji if highlight is shown
if (!this.state.showHighlight) return;
const btn = this.scrollRef.current?.containerRef.current?.querySelector<HTMLButtonElement>(
'.mx_EmojiPicker_item_wrapper [tabindex="0"]',
);
btn?.click();
this.props.onFinished();
};
private onHoverEmoji = (emoji: IEmoji): void => {
this.setState({
previewEmoji: emoji,
});
};
private onHoverEmojiEnd = (): void => {
this.setState({
previewEmoji: undefined,
});
};
private onClickEmoji = (ev: ButtonEvent, emoji: IEmoji): void => {
if (this.props.onChoose(emoji.unicode) !== false) {
recent.add(emoji.unicode);
}
if ((ev as React.KeyboardEvent).key === Key.ENTER) {
this.props.onFinished();
}
};
private static categoryHeightForEmojiCount(count: number): number {
if (count === 0) {
return 0;
}
return CATEGORY_HEADER_HEIGHT + Math.ceil(count / EMOJIS_PER_ROW) * EMOJI_HEIGHT;
}
public render(): React.ReactNode {
return (
<RovingTabIndexProvider onKeyDown={this.onKeyDown}>
{({ onKeyDownHandler }) => {
let heightBefore = 0;
return (
<section
className="mx_EmojiPicker"
data-testid="mx_EmojiPicker"
onKeyDown={onKeyDownHandler}
aria-label={_t("a11y|emoji_picker")}
>
<Header categories={this.categories} onAnchorClick={this.scrollToCategory} />
<Search
query={this.state.filter}
onChange={this.onChangeFilter}
onEnter={this.onEnterFilter}
onKeyDown={onKeyDownHandler}
/>
<AutoHideScrollbar
id="mx_EmojiPicker_body"
className={classNames("mx_EmojiPicker_body", {
mx_EmojiPicker_body_showHighlight: this.state.showHighlight,
})}
ref={this.scrollRef}
onScroll={this.onScroll}
>
{this.categories.map((category) => {
const emojis = this.memoizedDataByCategory[category.id];
const categoryElement = (
<Category
key={category.id}
id={category.id}
name={category.name}
heightBefore={heightBefore}
viewportHeight={this.state.viewportHeight}
scrollTop={this.state.scrollTop}
emojis={emojis}
onClick={this.onClickEmoji}
onMouseEnter={this.onHoverEmoji}
onMouseLeave={this.onHoverEmojiEnd}
isEmojiDisabled={this.props.isEmojiDisabled}
selectedEmojis={this.props.selectedEmojis}
/>
);
const height = EmojiPicker.categoryHeightForEmojiCount(emojis.length);
heightBefore += height;
return categoryElement;
})}
</AutoHideScrollbar>
{this.state.previewEmoji ? (
<Preview emoji={this.state.previewEmoji} />
) : (
<QuickReactions
onClick={this.onClickEmoji}
selectedEmojis={this.props.selectedEmojis}
/>
)}
</section>
);
}}
</RovingTabIndexProvider>
);
}
}
export default EmojiPicker;