Close menu when click outside

This commit is contained in:
Saoud Rizwan
2024-09-16 18:21:16 -04:00
parent f13850e739
commit 5974679aaf
2 changed files with 31 additions and 13 deletions

View File

@@ -40,7 +40,6 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
const [showContextMenu, setShowContextMenu] = useState(false) const [showContextMenu, setShowContextMenu] = useState(false)
const [cursorPosition, setCursorPosition] = useState(0) const [cursorPosition, setCursorPosition] = useState(0)
const [searchQuery, setSearchQuery] = useState("") const [searchQuery, setSearchQuery] = useState("")
const containerRef = useRef<HTMLDivElement>(null)
const textAreaRef = useRef<HTMLTextAreaElement | null>(null) const textAreaRef = useRef<HTMLTextAreaElement | null>(null)
const [isMouseDownOnMenu, setIsMouseDownOnMenu] = useState(false) const [isMouseDownOnMenu, setIsMouseDownOnMenu] = useState(false)
const highlightLayerRef = useRef<HTMLDivElement>(null) const highlightLayerRef = useRef<HTMLDivElement>(null)
@@ -48,6 +47,27 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
const [selectedType, setSelectedType] = useState<string | null>(null) const [selectedType, setSelectedType] = useState<string | null>(null)
const [justDeletedSpaceAfterMention, setJustDeletedSpaceAfterMention] = useState(false) const [justDeletedSpaceAfterMention, setJustDeletedSpaceAfterMention] = useState(false)
const [intendedCursorPosition, setIntendedCursorPosition] = useState<number | null>(null) const [intendedCursorPosition, setIntendedCursorPosition] = useState<number | null>(null)
const contextMenuContainerRef = useRef<HTMLDivElement>(null)
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (
contextMenuContainerRef.current &&
!contextMenuContainerRef.current.contains(event.target as Node)
) {
setShowContextMenu(false)
}
}
if (showContextMenu) {
document.addEventListener("mousedown", handleClickOutside)
}
return () => {
document.removeEventListener("mousedown", handleClickOutside)
}
}, [showContextMenu, setShowContextMenu])
const handleMentionSelect = useCallback( const handleMentionSelect = useCallback(
(type: string, value: string) => { (type: string, value: string) => {
if (value === "File" || value === "Folder") { if (value === "File" || value === "Folder") {
@@ -297,7 +317,6 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
return ( return (
<div <div
ref={containerRef}
style={{ style={{
padding: "10px 15px", padding: "10px 15px",
opacity: textAreaDisabled ? 0.5 : 1, opacity: textAreaDisabled ? 0.5 : 1,
@@ -305,8 +324,8 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
display: "flex", display: "flex",
}}> }}>
{showContextMenu && ( {showContextMenu && (
<div ref={contextMenuContainerRef}>
<ContextMenu <ContextMenu
containerWidth={containerRef.current?.clientWidth || 0}
onSelect={handleMentionSelect} onSelect={handleMentionSelect}
searchQuery={searchQuery} searchQuery={searchQuery}
onMouseDown={handleMenuMouseDown} onMouseDown={handleMenuMouseDown}
@@ -314,6 +333,7 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
setSelectedIndex={setSelectedMenuIndex} setSelectedIndex={setSelectedMenuIndex}
selectedType={selectedType} selectedType={selectedType}
/> />
</div>
)} )}
{!isTextAreaFocused && ( {!isTextAreaFocused && (
<div <div

View File

@@ -2,7 +2,6 @@ import React, { useEffect, useState, useRef } from "react"
import { getContextMenuOptions } from "../utils/mention-context" import { getContextMenuOptions } from "../utils/mention-context"
interface ContextMenuProps { interface ContextMenuProps {
containerWidth: number
onSelect: (type: string, value: string) => void onSelect: (type: string, value: string) => void
searchQuery: string searchQuery: string
onMouseDown: () => void onMouseDown: () => void
@@ -12,7 +11,6 @@ interface ContextMenuProps {
} }
const ContextMenu: React.FC<ContextMenuProps> = ({ const ContextMenu: React.FC<ContextMenuProps> = ({
containerWidth,
onSelect, onSelect,
searchQuery, searchQuery,
onMouseDown, onMouseDown,