UNPKG

react-emoji-popup-lister

Version:

React emoji search & select by colon syntax, with keyboard navigation

191 lines (183 loc) 9.99 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; import React from "react"; import { useState, useEffect, useRef } from "react"; import { gemoji } from "gemoji"; import FuzzySearch from "fuzzy-search"; import EmojiInput from "./EmojiInput"; import EmojiList from "./EmojiList"; var fuzzysearch = new FuzzySearch(gemoji, ["names"], { sort: true }); var themeDefault = "auto"; var EmojiPopup = function (_a) { var input = _a.input, userPropInputText = _a.value, setUserPropInputText = _a.setValue, _b = _a.theme, theme = _b === void 0 ? themeDefault : _b, _c = _a.strict, strict = _c === void 0 ? true : _c, _d = _a.listMax, listMax = _d === void 0 ? 6 : _d, _e = _a.maxWidth, maxWidth = _e === void 0 ? "400px" : _e, _f = _a.maxHeight, maxHeight = _f === void 0 ? "250px" : _f, _g = _a.footer, footer = _g === void 0 ? true : _g, placeholder = _a.placeholder, ariaLabel = _a.ariaLabel, props = __rest(_a, ["input", "value", "setValue", "theme", "strict", "listMax", "maxWidth", "maxHeight", "footer", "placeholder", "ariaLabel"]); var _h = __read(useState(theme), 2), themeMode = _h[0], setThemeMode = _h[1]; var _j = __read(useState(false), 2), active = _j[0], setActive = _j[1]; var _k = __read(useState(""), 2), value = _k[0], setValue = _k[1]; var _l = __read(useState({ list: [], search: "", select: "", }), 2), emoji = _l[0], setEmoji = _l[1]; var _m = __read(useState(false), 2), mouseNav = _m[0], setMouseNav = _m[1]; var wrapperRef = useRef(null); var emojiContainerRef = useRef(null); // TODO: Make sure this doesn't have any race conditions var _o = __read(useState(-1), 2), selStart = _o[0], setSelStart = _o[1]; var _p = __read(useState(0), 2), elIndex = _p[0], setElIndex = _p[1]; // Reset index, and add display transition class useEffect(function () { if (!active) setElIndex(0); if (emojiContainerRef === null || emojiContainerRef === void 0 ? void 0 : emojiContainerRef.current) { if (active) { emojiContainerRef.current.classList.add("active"); } else { emojiContainerRef.current.classList.remove("active"); } } }, [active]); // Theme change styling useEffect(function () { var _a, _b, _c; if (wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current) { (_a = wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current) === null || _a === void 0 ? void 0 : _a.classList.remove("themelight"); (_b = wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current) === null || _b === void 0 ? void 0 : _b.classList.remove("themedark"); if (themeMode === "dark" || themeMode === "light") { (_c = wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current) === null || _c === void 0 ? void 0 : _c.classList.add("theme".concat(themeMode)); } } }, [themeMode]); // Theme change events and state useEffect(function () { if (theme === "auto") { window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", function (e) { return setThemeMode(e.matches ? "dark" : "light"); }); setThemeMode(window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"); } else { setThemeMode(theme); window.matchMedia("(prefers-color-scheme: dark)").removeEventListener("change", function () { }); } }, [theme]); // Sync user prop state with internal state useEffect(function () { var _a; setUserPropInputText === null || setUserPropInputText === void 0 ? void 0 : setUserPropInputText(value); if (wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current) { var inp = (_a = wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current) === null || _a === void 0 ? void 0 : _a.children[0]; if (inp) { inp.selectionStart = selStart; inp.selectionEnd = selStart; // Focus input when user clicks list item if (value) inp.focus(); } } }, [value]); // Display fuzzy-matched emojis useEffect(function () { list.update(emoji.search); }, [emoji.search]); // Replace emoji string match with emoji, and reset states useEffect(function () { if (emoji.select) { // console.log([...emoji.select]); // Calculate selection start, to fix going to the end of input if (wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current) { var delta = selStart - value.indexOf(emoji.search); setSelStart(selStart - delta + emoji.select.length); setValue(value.replace("".concat(emoji.search), emoji.select)); setActive(false); setEmoji(__assign(__assign({}, emoji), { search: "", select: "" })); } } }, [emoji.select]); // List display updates helpers useEffect(function () { list.update(); }, [elIndex]); var list = { index: function (i) { setElIndex(i); }, next: function () { list.index((elIndex + 1) % list.checkMax()); }, prev: function () { list.index(elIndex - 1 < 0 ? list.checkMax() - 1 : elIndex - 1); }, select: function (override) { if (override) { setEmoji(__assign(__assign({}, emoji), { select: override })); } else if (emoji.list.length && emoji.list[elIndex]) { setEmoji(__assign(__assign({}, emoji), { select: emoji.list[elIndex].emoji })); } }, checkMax: function () { return listMax < emoji.list.length ? listMax : emoji.list.length; }, update: function (str) { var list = emoji.list.slice(); if (str && str.length) { str = str.replaceAll(":", ""); var search = fuzzysearch.search(str).slice(0, listMax); list = search.length ? search : []; } if (elIndex >= list.length) setElIndex(0); list = list.map(function (s, i) { s.active = i === elIndex ? true : false; return s; }); setEmoji(__assign(__assign({}, emoji), { list: list })); }, }; return (<> <div ref={wrapperRef} className="wrapper"> <EmojiInput input={input} value={value} setValue={setValue} active={active} setActive={setActive} list={list} emoji={emoji} setEmoji={setEmoji} selStart={selStart} setSelStart={setSelStart} setMouseNav={setMouseNav} placeholder={placeholder} ariaLabel={ariaLabel} strict={strict} {...props}></EmojiInput> <div className="container" aria-label={active ? "Emoji lister popup" : ""} ref={emojiContainerRef}> <EmojiList active={active} list={list} setElIndex={setElIndex} mouseNav={mouseNav} setMouseNav={setMouseNav} emoji={emoji} maxWidth={maxWidth} maxHeight={maxHeight} footer={footer}></EmojiList> </div> {props.children ? __assign({}, props.children) : null} </div> <style>{"\n\t\t\t\t.wrapper {\n\t\t\t\t\tposition: relative;\n\t\t\t\t}\n\t\t\t\t.themedark.wrapper {\n\t\t\t\t\tcolor: #ddd;\n\t\t\t\t}\n\t\t\t\t.themelight.wrapper {\n\t\t\t\t\tcolor: #222;\n\t\t\t\t}\n\t\t\t\t.input,\n\t\t\t\t.wrapper > input,\n\t\t\t\t.wrapper > textarea {\n\t\t\t\t\twidth: 100%;\n\t\t\t\t\tbox-sizing: border-box;\n\t\t\t\t\tfont-size: inherit;\n\t\t\t\t\tfont-family: inherit;\n\t\t\t\t}\n\t\t\t\t.container {\n\t\t\t\t\tposition: absolute;\n\t\t\t\t\tz-index: 20;\n\t\t\t\t\tbottom: -7px;\n\t\t\t\t\tborder-radius: 8px;\n\t\t\t\t\toverflow: hidden;\n\t\t\t\t\ttransition: all 0.1s;\n\t\t\t\t\topacity: 0;\n\t\t\t\t\ttransform: translateY(96%);\n\t\t\t\t}\n\t\t\t\t.container.active {\n\t\t\t\t\topacity: 1;\n\t\t\t\t\ttransform: translateY(100%);\n\t\t\t\t}\n\t\t\t\t.themedark .container {\n\t\t\t\t\tbackground: rgba(0,0,0,0.9);\n\t\t\t\t\tbox-shadow: 0 4px 18px rgba(0,0,0,0.6);\n\t\t\t\t\t/* border: 2px solid rgba(255,255,255,0.14); */\n\t\t\t\t}\n\t\t\t\t.themelight .container {\n\t\t\t\t\tbackground: rgba(255, 255, 255, 1);\n\t\t\t\t\tbox-shadow: 0 4px 18px rgba(0,0,0,0.3);\n\t\t\t\t\t/* border: 2px solid rgba(0, 0, 0, 0.14); */\n\t\t\t\t}\n\t\t\t"}</style> </>); }; export default EmojiPopup; //# sourceMappingURL=EmojiPopup.jsx.map