Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Opens the emoji picker relative to the input cursor's location (#3698)

Pedro Ladeirafdc42a9d4659
diff --git a/apps/web/ui/shared/emoji-picker.tsx b/apps/web/ui/shared/emoji-picker.tsx
index 0f57b153dd..2b91a9e288 100644
--- a/apps/web/ui/shared/emoji-picker.tsx
+++ b/apps/web/ui/shared/emoji-picker.tsx
@@ -2,10 +2,10 @@ import { Button, Popover } from "@dub/ui";
import { FaceSmile } from "@dub/ui/icons";
import { EmojiPicker as EmojiPickerBase } from "frimousse";
import {
- type KeyboardEvent as ReactKeyboardEvent,
PropsWithChildren,
useRef,
useState,
+ type KeyboardEvent as ReactKeyboardEvent,
} from "react";
type EmojiPickerProps = PropsWithChildren<{
@@ -13,6 +13,12 @@ type EmojiPickerProps = PropsWithChildren<{
openPopover?: boolean;
setOpenPopover?: (open: boolean) => void;
onKeyboardDismissFocusEditor?: () => void;
+ anchorRect?: {
+ top: number;
+ bottom: number;
+ left: number;
+ right: number;
+ } | null;
}>;
export function EmojiPicker({
@@ -21,6 +27,7 @@ export function EmojiPicker({
openPopover: controlledOpen,
setOpenPopover: controlledSetOpen,
onKeyboardDismissFocusEditor,
+ anchorRect,
}: EmojiPickerProps) {
const [internalOpen, setInternalOpen] = useState(false);
const isControlled =
@@ -29,6 +36,19 @@ export function EmojiPicker({
const setOpenPopover = isControlled ? controlledSetOpen : setInternalOpen;
const keyboardDismissRef = useRef(false);
+ const anchorEl = anchorRect ? (
+ <div
+ style={{
+ position: "fixed",
+ top: anchorRect.top,
+ left: anchorRect.left,
+ width: Math.max(anchorRect.right - anchorRect.left, 1),
+ height: Math.max(anchorRect.bottom - anchorRect.top, 1),
+ pointerEvents: "none",
+ }}
+ />
+ ) : undefined;
+
const handleBackspaceClose = (e: ReactKeyboardEvent) => {
if (e.key !== "Backspace") return;
const target = e.target;
@@ -47,7 +67,8 @@ export function EmojiPicker({
setOpenPopover={setOpenPopover}
side="top"
align="start"
- sideOffset={38}
+ sideOffset={anchorRect ? 6 : 38}
+ anchor={anchorEl}
onEscapeKeyDown={() => {
keyboardDismissRef.current = true;
}}
diff --git a/apps/web/ui/shared/message-input.tsx b/apps/web/ui/shared/message-input.tsx
index 11d196b2a2..6d8977de33 100644
--- a/apps/web/ui/shared/message-input.tsx
+++ b/apps/web/ui/shared/message-input.tsx
@@ -40,10 +40,17 @@ export function MessageInput({
const richTextRef = useRef<{ setContent: (content: any) => void }>(null);
const [typedMessage, setTypedMessage] = useState(defaultValue || "");
const [emojiPickerOpen, setEmojiPickerOpenState] = useState(false);
+ const [cursorRect, setCursorRect] = useState<{
+ top: number;
+ bottom: number;
+ left: number;
+ right: number;
+ } | null>(null);
const stripColonOnEmojiPickRef = useRef(false);
const setEmojiPickerOpen = useCallback((open: boolean) => {
stripColonOnEmojiPickRef.current = false;
+ if (!open) setCursorRect(null);
setEmojiPickerOpenState(open);
}, []);
@@ -99,6 +106,8 @@ export function MessageInput({
),
);
if (atBlockStart || afterWhitespace || afterHardBreak) {
+ const coords = view.coordsAtPos(view.state.selection.from);
+ setCursorRect(coords);
setTimeout(() => {
stripColonOnEmojiPickRef.current = true;
setEmojiPickerOpenState(true);
@@ -119,6 +128,7 @@ export function MessageInput({
emojiPickerOpen={emojiPickerOpen}
setEmojiPickerOpen={setEmojiPickerOpen}
stripColonOnEmojiPickRef={stripColonOnEmojiPickRef}
+ cursorRect={cursorRect}
/>
<div className="flex items-center justify-between gap-2">
{onCancel && (
@@ -266,10 +276,17 @@ function MessageInputToolbar({
emojiPickerOpen,
setEmojiPickerOpen,
stripColonOnEmojiPickRef,
+ cursorRect,
}: {
emojiPickerOpen: boolean;
setEmojiPickerOpen: (open: boolean) => void;
stripColonOnEmojiPickRef: { current: boolean };
+ cursorRect: {
+ top: number;
+ bottom: number;
+ left: number;
+ right: number;
+ } | null;
}) {
const { editor } = useRichTextContext();
@@ -280,6 +297,7 @@ function MessageInputToolbar({
openPopover={emojiPickerOpen}
setOpenPopover={setEmojiPickerOpen}
onKeyboardDismissFocusEditor={() => editor?.commands.focus()}
+ anchorRect={cursorRect}
onSelect={(emoji) => {
if (!editor) return;
const stripColon = stripColonOnEmojiPickRef.current;
diff --git a/packages/ui/src/popover.tsx b/packages/ui/src/popover.tsx
index 6596602b29..3194624e02 100644
--- a/packages/ui/src/popover.tsx
+++ b/packages/ui/src/popover.tsx
@@ -3,6 +3,7 @@
import { cn } from "@dub/utils";
import * as PopoverPrimitive from "@radix-ui/react-popover";
import { PropsWithChildren, ReactNode, WheelEventHandler } from "react";
+import { createPortal } from "react-dom";
import { Drawer } from "vaul";
import { useMediaQuery } from "./hooks";
@@ -22,6 +23,7 @@ export type PopoverProps = PropsWithChildren<{
onEscapeKeyDown?: (event: KeyboardEvent) => void;
onWheel?: WheelEventHandler;
sideOffset?: number;
+ anchor?: ReactNode;
}>;
export function Popover({
@@ -41,6 +43,7 @@ export function Popover({
onEscapeKeyDown,
onWheel,
sideOffset = 8,
+ anchor,
}: PopoverProps) {
const { isMobile } = useMediaQuery();
@@ -80,6 +83,12 @@ export function Popover({
return (
<PopoverPrimitive.Root open={openPopover} onOpenChange={setOpenPopover}>
+ {anchor &&
+ typeof document !== "undefined" &&
+ createPortal(
+ <PopoverPrimitive.Anchor asChild>{anchor}</PopoverPrimitive.Anchor>,
+ document.body,
+ )}
<PopoverPrimitive.Trigger className="sm:inline-flex" asChild>
{children}
</PopoverPrimitive.Trigger>