|
| 1 | +import React, { useRef, useCallback, useEffect } from "react" |
| 2 | +import { mergeRefs } from "use-callback-ref" |
| 3 | + |
| 4 | +import { cn } from "@/lib/utils" |
| 5 | +import { Command, CommandList, CommandGroup, CommandItem, CommandEmpty } from "@/components/ui" |
| 6 | + |
| 7 | +import type { Mentionable } from "./types" |
| 8 | +import { useMention } from "./useMention" |
| 9 | +import { getCursorOffset, getCursorPos, replaceMention } from "./contentEditable" |
| 10 | + |
| 11 | +interface MentionProps extends React.ComponentProps<"div"> { |
| 12 | + suggestions: Mentionable[] |
| 13 | + onMention?: (value: Mentionable) => void |
| 14 | +} |
| 15 | + |
| 16 | +export const Mention = React.forwardRef<HTMLDivElement, MentionProps>( |
| 17 | + ({ suggestions, onMention, className, ...props }, ref) => { |
| 18 | + const contentEditableRef = useRef<HTMLDivElement | null>(null) |
| 19 | + const combinedRef = mergeRefs([contentEditableRef, ref]) |
| 20 | + const menuRef = useRef<HTMLDivElement | null>(null) |
| 21 | + |
| 22 | + const { |
| 23 | + triggerPos, |
| 24 | + setTriggerPos, |
| 25 | + selectedSuggestion, |
| 26 | + setSelectedSuggestion, |
| 27 | + openMenu, |
| 28 | + closeMenu, |
| 29 | + isOpen, |
| 30 | + isOpenRef, |
| 31 | + } = useMention(suggestions) |
| 32 | + |
| 33 | + const onInput = useCallback( |
| 34 | + (event: React.FormEvent<HTMLDivElement>) => { |
| 35 | + const content = event.currentTarget.textContent || "" |
| 36 | + |
| 37 | + if (!content) { |
| 38 | + closeMenu() |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + const selection = window.getSelection() |
| 43 | + |
| 44 | + if (!selection?.rangeCount) { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + const range = selection.getRangeAt(0) |
| 49 | + const div = contentEditableRef.current |
| 50 | + const offset = getCursorOffset(div, range.endContainer, range.endOffset) |
| 51 | + const text = content.slice(0, offset) |
| 52 | + const char = text[offset - 1] |
| 53 | + |
| 54 | + if (char === "@") { |
| 55 | + const coords = getCursorPos(div, offset - 1) |
| 56 | + |
| 57 | + if (coords) { |
| 58 | + setTriggerPos(coords) |
| 59 | + openMenu() |
| 60 | + return |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (isOpenRef.current) { |
| 65 | + if (/\s/.test(char) || char === "\n") { |
| 66 | + closeMenu() |
| 67 | + } else { |
| 68 | + // const atIndex = text.lastIndexOf("@") |
| 69 | + // const query = text.slice(atIndex + 1) |
| 70 | + // handleSearch(query) |
| 71 | + } |
| 72 | + } |
| 73 | + }, |
| 74 | + [setTriggerPos, openMenu, closeMenu, isOpenRef], |
| 75 | + ) |
| 76 | + |
| 77 | + const onKeyDown = (event: React.KeyboardEvent) => { |
| 78 | + if (!isOpen) { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + // Prevent default behavior for all these keys to avoid unwanted scrolling. |
| 83 | + if (["Escape", "ArrowDown", "ArrowUp", "Enter", "Tab"].includes(event.key)) { |
| 84 | + event.preventDefault() |
| 85 | + } |
| 86 | + |
| 87 | + switch (event.key) { |
| 88 | + case "Escape": |
| 89 | + closeMenu() |
| 90 | + setSelectedSuggestion(undefined) |
| 91 | + break |
| 92 | + case "ArrowDown": |
| 93 | + setSelectedSuggestion((current) => { |
| 94 | + console.log(`ArrowDown, current = ${current}`) |
| 95 | + if (!current) { |
| 96 | + return suggestions[0]?.name |
| 97 | + } |
| 98 | + const currentIndex = suggestions.findIndex((suggestion) => suggestion.name === current) |
| 99 | + console.log(`ArrowDown, currentIndex = ${currentIndex}`) |
| 100 | + const nextIndex = Math.min(currentIndex + 1, suggestions.length - 1) |
| 101 | + return suggestions[nextIndex]?.name |
| 102 | + }) |
| 103 | + break |
| 104 | + case "ArrowUp": |
| 105 | + setSelectedSuggestion((current) => { |
| 106 | + if (!current) { |
| 107 | + return suggestions[suggestions.length - 1]?.name |
| 108 | + } |
| 109 | + const currentIndex = suggestions.findIndex((suggestion) => suggestion.name === current) |
| 110 | + const prevIndex = Math.max(currentIndex - 1, 0) |
| 111 | + return suggestions[prevIndex]?.name |
| 112 | + }) |
| 113 | + break |
| 114 | + case "Enter": |
| 115 | + case "Tab": |
| 116 | + if (selectedSuggestion) { |
| 117 | + onMentionSelect(selectedSuggestion) |
| 118 | + } |
| 119 | + break |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + const onMentionSelect = (name: string) => { |
| 124 | + replaceMention(contentEditableRef.current, name) |
| 125 | + const mentionable = suggestions.find((suggestion) => suggestion.name === name) |
| 126 | + |
| 127 | + if (mentionable) { |
| 128 | + onMention?.(mentionable) |
| 129 | + } |
| 130 | + |
| 131 | + closeMenu() |
| 132 | + } |
| 133 | + |
| 134 | + const onClickOutside = useCallback( |
| 135 | + (event: MouseEvent) => { |
| 136 | + if (!isOpen) { |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + const target = event.target as Node |
| 141 | + const contentEditable = contentEditableRef.current |
| 142 | + const menu = menuRef.current |
| 143 | + |
| 144 | + if (contentEditable && menu && !contentEditable.contains(target) && !menu.contains(target)) { |
| 145 | + closeMenu() |
| 146 | + } |
| 147 | + }, |
| 148 | + [isOpen, closeMenu], |
| 149 | + ) |
| 150 | + |
| 151 | + useEffect(() => { |
| 152 | + document.addEventListener("mousedown", onClickOutside) |
| 153 | + return () => document.removeEventListener("mousedown", onClickOutside) |
| 154 | + }, [onClickOutside]) |
| 155 | + |
| 156 | + const top = triggerPos ? triggerPos.top : 0 |
| 157 | + const left = triggerPos ? triggerPos.left : 0 |
| 158 | + |
| 159 | + return ( |
| 160 | + <div className="relative"> |
| 161 | + <div |
| 162 | + ref={combinedRef} |
| 163 | + contentEditable |
| 164 | + onInput={onInput} |
| 165 | + onKeyDown={onKeyDown} |
| 166 | + className={cn( |
| 167 | + "w-[300px] h-[100px] rounded-xs border border-gray-300 bg-gray-200 px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-1 disabled:cursor-not-allowed disabled:opacity-50 break-words whitespace-pre-wrap overflow-y-auto", |
| 168 | + className, |
| 169 | + )} |
| 170 | + {...props} |
| 171 | + /> |
| 172 | + {isOpen && triggerPos && ( |
| 173 | + <div ref={menuRef} className="absolute w-[200px]" style={{ top: `${top}px`, left: `${left}px` }}> |
| 174 | + <Command value={selectedSuggestion} onValueChange={setSelectedSuggestion}> |
| 175 | + <CommandList> |
| 176 | + <CommandGroup> |
| 177 | + {suggestions.length > 0 ? ( |
| 178 | + suggestions.map((suggestion) => ( |
| 179 | + <CommandItem |
| 180 | + key={suggestion.id} |
| 181 | + tabIndex={0} |
| 182 | + onSelect={() => onMentionSelect(suggestion.name)}> |
| 183 | + {suggestion.name} |
| 184 | + </CommandItem> |
| 185 | + )) |
| 186 | + ) : ( |
| 187 | + <CommandEmpty>No suggestions found</CommandEmpty> |
| 188 | + )} |
| 189 | + </CommandGroup> |
| 190 | + </CommandList> |
| 191 | + </Command> |
| 192 | + </div> |
| 193 | + )} |
| 194 | + </div> |
| 195 | + ) |
| 196 | + }, |
| 197 | +) |
| 198 | + |
| 199 | +Mention.displayName = "Mention" |
0 commit comments