Debian Codegraph @

web/src/components/EmojiPicker.jsx

raw
1 import * as React from "react";
2 import { useRef, useState } from "react";
3 import { Typography, Box, TextField, ClickAwayListener, Fade, InputAdornment, styled, IconButton, Popper } from "@mui/material";
4 import Close from "@mui/icons-material/Close";
5 import { useTranslation } from "react-i18next";
6 import { splitNoEmpty } from "../app/utils";
7 import { rawEmojis } from "../app/emojis";
8
9 // Create emoji list by category and create a search base (string with all search words)
10 //
11 // This also filters emojis that are not supported by Desktop Chrome.
12 // This is a hack, but on Ubuntu 18.04, with Chrome 99, only Emoji <= 11 are supported.
13
14 const emojisByCategory = {};
15 const isDesktopChrome = /Chrome/.test(navigator.userAgent) && !/Mobile/.test(navigator.userAgent);
16 const maxSupportedVersionForDesktopChrome = 11;
17 rawEmojis.forEach((emoji) => {
18 if (!emojisByCategory[emoji.category]) {
19 emojisByCategory[emoji.category] = [];
20 }
21 try {
22 const unicodeVersion = parseFloat(emoji.unicode_version);
23 const supportedEmoji = unicodeVersion <= maxSupportedVersionForDesktopChrome || !isDesktopChrome;
24 if (supportedEmoji) {
25 const searchBase = `${emoji.description.toLowerCase()} ${emoji.aliases.join(" ")} ${emoji.tags.join(" ")}`;
26 const emojiWithSearchBase = { ...emoji, searchBase };
27 emojisByCategory[emoji.category].push(emojiWithSearchBase);
28 }
29 } catch (e) {
30 // Nothing. Ignore.
31 }
32 });
33
34 const EmojiPicker = (props) => {
35 const { t } = useTranslation();
36 const open = Boolean(props.anchorEl);
37 const [search, setSearch] = useState("");
38 const searchRef = useRef(null);
39 const searchFields = splitNoEmpty(search.toLowerCase(), " ");
40
41 const handleSearchClear = () => {
42 setSearch("");
43 searchRef.current?.focus();
44 };
45
46 return (
47 <Popper open={open} anchorEl={props.anchorEl} placement="bottom-start" sx={{ zIndex: 10005 }} transition>
48 {({ TransitionProps }) => (
49 <ClickAwayListener onClickAway={props.onClose}>
50 <Fade {...TransitionProps} timeout={350}>
51 <Box
52 sx={{
53 boxShadow: 3,
54 padding: 2,
55 paddingRight: 0,
56 paddingBottom: 1,
57 width: "380px",
58 maxHeight: "300px",
59 backgroundColor: "background.paper",
60 overflowY: "scroll",
61 }}
62 >
63 <TextField
64 inputRef={searchRef}
65 margin="dense"
66 size="small"
67 placeholder={t("emoji_picker_search_placeholder")}
68 value={search}
69 onChange={(ev) => setSearch(ev.target.value)}
70 type="text"
71 variant="standard"
72 fullWidth
73 sx={{ marginTop: 0, marginBottom: "12px", paddingRight: 2 }}
74 slotProps={{
75 htmlInput: {
76 role: "searchbox",
77 "aria-label": t("emoji_picker_search_placeholder"),
78 },
79 input: {
80 endAdornment: (
81 <InputAdornment position="end" sx={{ display: search ? "" : "none" }}>
82 <IconButton size="small" onClick={handleSearchClear} edge="end" aria-label={t("emoji_picker_search_clear")}>
83 <Close />
84 </IconButton>
85 </InputAdornment>
86 ),
87 },
88 }}
89 />
90 <Box
91 sx={{
92 display: "flex",
93 flexWrap: "wrap",
94 paddingRight: 0,
95 marginTop: 1,
96 }}
97 >
98 {Object.keys(emojisByCategory).map((category) => (
99 <Category
100 key={category}
101 title={category}
102 emojis={emojisByCategory[category]}
103 search={searchFields}
104 onPick={props.onEmojiPick}
105 />
106 ))}
107 </Box>
108 </Box>
109 </Fade>
110 </ClickAwayListener>
111 )}
112 </Popper>
113 );
114 };
115
116 const Category = (props) => {
117 const showTitle = props.search.length === 0;
118 return (
119 <>
120 {showTitle && (
121 <Typography variant="body1" sx={{ width: "100%", marginBottom: 1 }}>
122 {props.title}
123 </Typography>
124 )}
125 {props.emojis.map((emoji) => (
126 <Emoji key={emoji.aliases[0]} emoji={emoji} search={props.search} onClick={() => props.onPick(emoji.aliases[0])} />
127 ))}
128 </>
129 );
130 };
131
132 const emojiMatches = (emoji, words) => words.length === 0 || words.some((word) => emoji.searchBase.includes(word));
133
134 const Emoji = (props) => {
135 const { emoji } = props;
136 const matches = emojiMatches(emoji, props.search);
137 const title = `${emoji.description} (${emoji.aliases[0]})`;
138 return (
139 <EmojiDiv onClick={props.onClick} title={title} aria-label={title} style={{ display: matches ? "" : "none" }}>
140 {props.emoji.emoji}
141 </EmojiDiv>
142 );
143 };
144
145 const EmojiDiv = styled("div")({
146 fontSize: "30px",
147 width: "30px",
148 height: "30px",
149 marginTop: "8px",
150 marginBottom: "8px",
151 marginRight: "8px",
152 lineHeight: "30px",
153 cursor: "pointer",
154 opacity: 0.85,
155 "&:hover": {
156 opacity: 1,
157 },
158 });
159
160 export default EmojiPicker;