fix(web): portal autocomplete overlays
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
|
import { createPortal } from "react-dom";
|
||||||
import { useCallback, useEffect, useRef, useState } from "react";
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
import { trpc } from "~/lib/trpc/client.js";
|
import { trpc } from "~/lib/trpc/client.js";
|
||||||
|
import { useAnchoredOverlay } from "~/hooks/useAnchoredOverlay.js";
|
||||||
|
|
||||||
interface CommentInputProps {
|
interface CommentInputProps {
|
||||||
entityType: "estimate";
|
entityType: "estimate";
|
||||||
@@ -55,6 +57,13 @@ export function CommentInput({
|
|||||||
);
|
);
|
||||||
}).slice(0, 8)
|
}).slice(0, 8)
|
||||||
: [];
|
: [];
|
||||||
|
const mentionOpen = mentionQuery !== null && filteredUsers.length > 0;
|
||||||
|
const { panelRef, position } = useAnchoredOverlay<HTMLTextAreaElement>({
|
||||||
|
open: mentionOpen,
|
||||||
|
onClose: () => setMentionQuery(null),
|
||||||
|
align: "start",
|
||||||
|
triggerRef: textareaRef,
|
||||||
|
});
|
||||||
|
|
||||||
// Reset mention index when filtered list changes
|
// Reset mention index when filtered list changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -177,33 +186,43 @@ export function CommentInput({
|
|||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Mention autocomplete dropdown */}
|
{/* Mention autocomplete dropdown */}
|
||||||
{mentionQuery !== null && filteredUsers.length > 0 && (
|
{mentionOpen && typeof document !== "undefined"
|
||||||
<div className="absolute left-0 bottom-full mb-1 z-50 w-72 rounded-xl border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 shadow-lg overflow-hidden">
|
? createPortal(
|
||||||
{filteredUsers.map((user, idx) => (
|
<div
|
||||||
<button
|
ref={panelRef}
|
||||||
key={user.id}
|
className="fixed z-[9998] w-72 overflow-hidden rounded-xl border border-gray-200 bg-white shadow-lg dark:border-gray-600 dark:bg-gray-800"
|
||||||
type="button"
|
style={{
|
||||||
onMouseDown={(e) => {
|
top: position.top,
|
||||||
e.preventDefault(); // prevent textarea blur
|
left: position.left,
|
||||||
insertMention(user);
|
|
||||||
}}
|
}}
|
||||||
className={`flex w-full items-center gap-3 px-3 py-2 text-left text-sm transition-colors ${
|
|
||||||
idx === mentionIndex
|
|
||||||
? "bg-brand-50 dark:bg-sky-900/40 text-brand-700 dark:text-sky-200"
|
|
||||||
: "text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700"
|
|
||||||
}`}
|
|
||||||
>
|
>
|
||||||
<span className="flex h-7 w-7 shrink-0 items-center justify-center rounded-full bg-brand-100 dark:bg-sky-800 text-xs font-semibold text-brand-700 dark:text-sky-200">
|
{filteredUsers.map((user, idx) => (
|
||||||
{(user.name ?? user.email).charAt(0).toUpperCase()}
|
<button
|
||||||
</span>
|
key={user.id}
|
||||||
<span className="truncate">
|
type="button"
|
||||||
<span className="font-medium">{user.name ?? "—"}</span>
|
onMouseDown={(e) => {
|
||||||
<span className="ml-1 text-gray-400 text-xs">{user.email}</span>
|
e.preventDefault();
|
||||||
</span>
|
insertMention(user);
|
||||||
</button>
|
}}
|
||||||
))}
|
className={`flex w-full items-center gap-3 px-3 py-2 text-left text-sm transition-colors ${
|
||||||
</div>
|
idx === mentionIndex
|
||||||
)}
|
? "bg-brand-50 dark:bg-sky-900/40 text-brand-700 dark:text-sky-200"
|
||||||
|
: "text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<span className="flex h-7 w-7 shrink-0 items-center justify-center rounded-full bg-brand-100 text-xs font-semibold text-brand-700 dark:bg-sky-800 dark:text-sky-200">
|
||||||
|
{(user.name ?? user.email).charAt(0).toUpperCase()}
|
||||||
|
</span>
|
||||||
|
<span className="truncate">
|
||||||
|
<span className="font-medium">{user.name ?? "—"}</span>
|
||||||
|
<span className="ml-1 text-xs text-gray-400">{user.email}</span>
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>,
|
||||||
|
document.body,
|
||||||
|
)
|
||||||
|
: null}
|
||||||
|
|
||||||
<div className="mt-2 flex items-center justify-between">
|
<div className="mt-2 flex items-center justify-between">
|
||||||
<span className="text-xs text-gray-400">
|
<span className="text-xs text-gray-400">
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { createPortal } from "react-dom";
|
||||||
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
|
import { useAnchoredOverlay } from "~/hooks/useAnchoredOverlay.js";
|
||||||
|
|
||||||
export interface AutocompleteOption {
|
export interface AutocompleteOption {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -36,6 +38,17 @@ export function AutocompleteInput({ options, value, onChange, placeholder = "Sea
|
|||||||
const [activeIdx, setActiveIdx] = useState(0);
|
const [activeIdx, setActiveIdx] = useState(0);
|
||||||
const containerRef = useRef<HTMLDivElement>(null);
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
const closeDropdown = useCallback(() => {
|
||||||
|
setOpen(false);
|
||||||
|
setInput(selected?.label ?? "");
|
||||||
|
}, [selected?.label]);
|
||||||
|
const { panelRef, position } = useAnchoredOverlay<HTMLDivElement>({
|
||||||
|
open,
|
||||||
|
onClose: closeDropdown,
|
||||||
|
align: "start",
|
||||||
|
matchTriggerWidth: true,
|
||||||
|
triggerRef: containerRef,
|
||||||
|
});
|
||||||
|
|
||||||
// Keep input text in sync when selected value changes externally
|
// Keep input text in sync when selected value changes externally
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -80,19 +93,6 @@ export function AutocompleteInput({ options, value, onChange, placeholder = "Sea
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close on outside click
|
|
||||||
useEffect(() => {
|
|
||||||
if (!open) return;
|
|
||||||
function handleClick(e: MouseEvent) {
|
|
||||||
if (containerRef.current && !containerRef.current.contains(e.target as Node)) {
|
|
||||||
setOpen(false);
|
|
||||||
setInput(selected?.label ?? "");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
document.addEventListener("mousedown", handleClick);
|
|
||||||
return () => document.removeEventListener("mousedown", handleClick);
|
|
||||||
}, [open, selected?.label]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={containerRef} className={`relative ${className ?? ""}`}>
|
<div ref={containerRef} className={`relative ${className ?? ""}`}>
|
||||||
<div className="relative">
|
<div className="relative">
|
||||||
@@ -124,29 +124,40 @@ export function AutocompleteInput({ options, value, onChange, placeholder = "Sea
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{open && filtered.length > 0 && (
|
{open && filtered.length > 0 && typeof document !== "undefined"
|
||||||
<ul
|
? createPortal(
|
||||||
className="absolute z-50 left-0 right-0 mt-1 bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-lg shadow-lg overflow-hidden max-h-56 overflow-y-auto"
|
<div
|
||||||
onMouseDown={(e) => e.preventDefault()}
|
ref={panelRef}
|
||||||
>
|
className="fixed z-[9998] max-h-56 overflow-y-auto rounded-lg border border-gray-200 bg-white shadow-lg dark:border-gray-700 dark:bg-gray-800"
|
||||||
{filtered.map((opt, idx) => (
|
style={{
|
||||||
<li key={opt.id}>
|
top: position.top,
|
||||||
<button
|
left: position.left,
|
||||||
type="button"
|
width: position.minWidth,
|
||||||
onMouseDown={(e) => { e.preventDefault(); select(opt); }}
|
}}
|
||||||
className={`w-full text-left px-3 py-2 text-sm flex items-baseline gap-2 transition-colors ${
|
onMouseDown={(e) => e.preventDefault()}
|
||||||
idx === activeIdx
|
>
|
||||||
? "bg-brand-50 dark:bg-brand-900/30 text-brand-700 dark:text-brand-300"
|
<ul>
|
||||||
: "text-gray-700 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-700"
|
{filtered.map((opt, idx) => (
|
||||||
}`}
|
<li key={opt.id}>
|
||||||
>
|
<button
|
||||||
<span className="truncate">{opt.label}</span>
|
type="button"
|
||||||
{opt.sub && <span className="text-xs text-gray-400 dark:text-gray-500 shrink-0">{opt.sub}</span>}
|
onMouseDown={(e) => { e.preventDefault(); select(opt); }}
|
||||||
</button>
|
className={`flex w-full items-baseline gap-2 px-3 py-2 text-left text-sm transition-colors ${
|
||||||
</li>
|
idx === activeIdx
|
||||||
))}
|
? "bg-brand-50 text-brand-700 dark:bg-brand-900/30 dark:text-brand-300"
|
||||||
</ul>
|
: "text-gray-700 hover:bg-gray-50 dark:text-gray-200 dark:hover:bg-gray-700"
|
||||||
)}
|
}`}
|
||||||
|
>
|
||||||
|
<span className="truncate">{opt.label}</span>
|
||||||
|
{opt.sub && <span className="shrink-0 text-xs text-gray-400 dark:text-gray-500">{opt.sub}</span>}
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>,
|
||||||
|
document.body,
|
||||||
|
)
|
||||||
|
: null}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user