fix: mention input zoom behaviour

This commit is contained in:
gosangam
2026-06-10 19:21:32 +05:30
parent a46e29d998
commit 3903423d06
3 changed files with 66 additions and 63 deletions
@@ -318,7 +318,7 @@ export function ComposePost({
<DrawerHeader className="text-left border-b border-border pb-4"> <DrawerHeader className="text-left border-b border-border pb-4">
<DrawerTitle>Share to feed</DrawerTitle> <DrawerTitle>Share to feed</DrawerTitle>
</DrawerHeader> </DrawerHeader>
<div className="p-4 pb-8">{body}</div> <div className="max-h-[75vh] overflow-y-auto p-4 pb-8">{body}</div>
</DrawerContent> </DrawerContent>
</Drawer> </Drawer>
); );
@@ -331,7 +331,7 @@ export function ComposePost({
className="fixed inset-0 z-100 bg-foreground/40 backdrop-blur-sm" className="fixed inset-0 z-100 bg-foreground/40 backdrop-blur-sm"
onClick={onClose} onClick={onClose}
/> />
<div className="fixed left-1/2 top-1/2 z-101 w-full max-w-md -translate-x-1/2 -translate-y-1/2 rounded-2xl border border-border bg-card shadow-xl"> <div className="fixed left-1/2 top-1/2 z-101 flex max-h-[85vh] w-full max-w-md -translate-x-1/2 -translate-y-1/2 flex-col overflow-hidden rounded-2xl border border-border bg-card shadow-xl">
<div className="flex items-center justify-between px-5 py-4 border-b border-border"> <div className="flex items-center justify-between px-5 py-4 border-b border-border">
<h2 className="font-[family-name:var(--font-title)] font-semibold text-lg"> <h2 className="font-[family-name:var(--font-title)] font-semibold text-lg">
Share to feed Share to feed
@@ -344,7 +344,7 @@ export function ComposePost({
<X className="size-4" /> <X className="size-4" />
</button> </button>
</div> </div>
<div className="p-5">{body}</div> <div className="overflow-y-auto p-5">{body}</div>
</div> </div>
</>, </>,
document.body, document.body,
+56 -56
View File
@@ -1,16 +1,24 @@
"use client"; "use client";
/** /**
* MentionAutocomplete — a search input with a suggestions popover for tagging * MentionAutocomplete — a search field for tagging users. Results render
* users. Suggestions come from the backend prioritised (following → followers * INLINE (in normal flow) directly below the input, not as a floating
* → others), 20 per page, and the popover infinite-scrolls (≈5 rows visible). * popover. This is deliberate: a floating popover inside a drawer/modal
* Already-selected users are filtered out. Calls onSelect with the chosen * overflows the sheet and its outside-click listener fights the drawer's own
* user; the caller manages the selected set. * dismiss. Inline results:
* • never resize/overflow the drawer (the container scrolls them),
* • close on blur (no document listener → outside clicks don't close the
* drawer),
* • only appear once the user types (no suggestions on bare focus).
*
* Suggestions come from the backend prioritised (following → followers →
* others), 20 per page, infinite-scrolling. Already-selected users are
* filtered out. Selecting a user adds it (via onSelect) and closes the list.
*/ */
import { useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
import Image from "next/image"; import Image from "next/image";
import { Loader2, Search } from "lucide-react"; import { Loader2 } from "lucide-react";
import { posts, type UserSummary } from "@/lib/social"; import { posts, type UserSummary } from "@/lib/social";
const PAGE = 20; const PAGE = 20;
@@ -30,22 +38,25 @@ export function MentionAutocomplete({
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [items, setItems] = useState<UserSummary[]>([]); const [items, setItems] = useState<UserSummary[]>([]);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [hasMore, setHasMore] = useState(true); const [hasMore, setHasMore] = useState(false);
const reqRef = useRef(0); const reqRef = useRef(0);
const busyRef = useRef(false); const busyRef = useRef(false);
const rootRef = useRef<HTMLDivElement>(null); const blurTimer = useRef<number | null>(null);
// (Re)load page 0 on open / query change (debounced — timer callback, so const query = q.trim();
// setState isn't synchronous-in-effect). const showResults = open && query !== "";
// (Re)load page 0 when the query changes — debounced, only when non-empty.
// No fetch on bare focus, so suggestions appear "while typing" only.
useEffect(() => { useEffect(() => {
if (!open) return; if (query === "") return;
const t = window.setTimeout(() => { const t = window.setTimeout(() => {
const token = ++reqRef.current; const token = ++reqRef.current;
busyRef.current = true; busyRef.current = true;
setLoading(true); setLoading(true);
posts posts
.mentionSuggest(q, { limit: PAGE, offset: 0 }) .mentionSuggest(query, { limit: PAGE, offset: 0 })
.then((res) => { .then((res) => {
if (reqRef.current !== token) return; if (reqRef.current !== token) return;
setItems(res); setItems(res);
@@ -65,7 +76,7 @@ export function MentionAutocomplete({
}); });
}, 200); }, 200);
return () => window.clearTimeout(t); return () => window.clearTimeout(t);
}, [q, open]); }, [query]);
function loadMore() { function loadMore() {
if (busyRef.current || !hasMore) return; if (busyRef.current || !hasMore) return;
@@ -73,7 +84,7 @@ export function MentionAutocomplete({
busyRef.current = true; busyRef.current = true;
setLoading(true); setLoading(true);
posts posts
.mentionSuggest(q, { limit: PAGE, offset: items.length }) .mentionSuggest(query, { limit: PAGE, offset: items.length })
.then((res) => { .then((res) => {
if (reqRef.current !== token) return; if (reqRef.current !== token) return;
setItems((prev) => [...prev, ...res]); setItems((prev) => [...prev, ...res]);
@@ -88,50 +99,33 @@ export function MentionAutocomplete({
}); });
} }
// Close on outside click / Escape.
useEffect(() => {
if (!open) return;
function onDown(e: PointerEvent) {
if (rootRef.current && !rootRef.current.contains(e.target as Node)) {
setOpen(false);
}
}
function onKey(e: KeyboardEvent) {
if (e.key === "Escape") setOpen(false);
}
document.addEventListener("pointerdown", onDown);
document.addEventListener("keydown", onKey);
return () => {
document.removeEventListener("pointerdown", onDown);
document.removeEventListener("keydown", onKey);
};
}, [open]);
const excluded = new Set(excludeIds); const excluded = new Set(excludeIds);
const visible = items.filter((u) => !u.id || !excluded.has(u.id)); const visible = items.filter((u) => !u.id || !excluded.has(u.id));
return ( return (
<div ref={rootRef} className="relative"> <div>
<div className="relative"> <input
<Search className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" /> type="text"
<input value={q}
type="text" autoFocus={autoFocus}
value={q} onFocus={() => setOpen(true)}
autoFocus={autoFocus} onChange={(e) => {
onFocus={() => setOpen(true)} setQ(e.target.value);
onChange={(e) => { setOpen(true);
setQ(e.target.value); }}
setOpen(true); onBlur={() => {
}} // Defer so a result tap (which fires onMouseDown→onClick) isn't
placeholder={placeholder} // pre-empted by the list closing. Plain blur (tapping elsewhere)
autoComplete="off" // just closes the inline list — the drawer is unaffected.
// text-base (16px) on mobile prevents iOS Safari from auto-zooming blurTimer.current = window.setTimeout(() => setOpen(false), 120);
// on focus (it zooms any input < 16px); compact 14px on desktop. }}
className="w-full rounded-lg border border-border bg-background py-2 pl-9 pr-3 text-base sm:text-sm text-foreground outline-none placeholder:text-muted-foreground focus-visible:ring-2 focus-visible:ring-ring" placeholder={placeholder}
/> autoComplete="off"
</div> // 16px on mobile so iOS Safari doesn't auto-zoom on focus.
className="w-full rounded-lg border border-border bg-background px-3 py-2 text-base sm:text-sm text-foreground outline-none placeholder:text-muted-foreground focus-visible:ring-2 focus-visible:ring-ring"
/>
{open ? ( {showResults ? (
<div <div
onScroll={(e) => { onScroll={(e) => {
const el = e.currentTarget; const el = e.currentTarget;
@@ -139,21 +133,27 @@ export function MentionAutocomplete({
loadMore(); loadMore();
} }
}} }}
className="absolute z-50 mt-1 max-h-60 w-full overflow-y-auto overscroll-contain rounded-lg border border-border bg-card shadow-lg" className="mt-2 max-h-56 overflow-y-auto overscroll-contain rounded-lg border border-border bg-card"
> >
{visible.length === 0 && !loading ? ( {visible.length === 0 && !loading ? (
<p className="px-3 py-3 text-sm text-muted-foreground"> <p className="px-3 py-3 text-sm text-muted-foreground">
{q.trim() ? "No people found." : "No suggestions."} No people found.
</p> </p>
) : ( ) : (
visible.map((u) => ( visible.map((u) => (
<button <button
key={u.id ?? u.handle} key={u.id ?? u.handle}
type="button" type="button"
onMouseDown={(e) => e.preventDefault()} // keep input focused // Keep focus + cancel the pending blur-close so the click
// registers before the list would otherwise close.
onMouseDown={(e) => {
e.preventDefault();
if (blurTimer.current) window.clearTimeout(blurTimer.current);
}}
onClick={() => { onClick={() => {
onSelect(u); onSelect(u);
setQ(""); setQ("");
setOpen(false);
}} }}
className="flex w-full items-center gap-2.5 px-3 py-2 text-left transition-colors hover:bg-muted/60" className="flex w-full items-center gap-2.5 px-3 py-2 text-left transition-colors hover:bg-muted/60"
> >
+7 -4
View File
@@ -113,8 +113,14 @@ export function MentionsDrawer({
> >
<DrawerHeader className="border-b border-border text-left"> <DrawerHeader className="border-b border-border text-left">
<DrawerTitle className="text-base">Mentions</DrawerTitle> <DrawerTitle className="text-base">Mentions</DrawerTitle>
</DrawerHeader>
<div className="flex-1 min-h-0 overflow-y-auto">
{/* Author add-field lives inside the scroll area so its inline
results scroll with the sheet instead of overflowing a fixed
header. */}
{isAuthor ? ( {isAuthor ? (
<div className="mt-2"> <div className="p-4 pb-2">
<MentionAutocomplete <MentionAutocomplete
excludeIds={list.map((m) => m.id).filter((id): id is string => !!id)} excludeIds={list.map((m) => m.id).filter((id): id is string => !!id)}
onSelect={add} onSelect={add}
@@ -122,9 +128,6 @@ export function MentionsDrawer({
/> />
</div> </div>
) : null} ) : null}
</DrawerHeader>
<div className="flex-1 min-h-0 overflow-y-auto">
{status === "loading" ? ( {status === "loading" ? (
<Message>Loading…</Message> <Message>Loading…</Message>
) : status === "error" ? ( ) : status === "error" ? (