mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 00:09:24 +08:00
The composer surface is a container query. Firefox 129 treats that as the containing block for position:fixed, so the resources dialog's viewport top is applied under the composer and the panel drops below the window. Portal the overlay to document.body, keep the existing flip-up and max-height clamp, and count the portaled panel as inside for outside clicks.
40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
import { type RefObject, useEffect, useRef } from "react";
|
|
|
|
/**
|
|
* Run *onOutside* when a mousedown lands outside *ref*, while *enabled*.
|
|
*
|
|
* Four composer dropdowns each carried this effect verbatim, each with the
|
|
* same lint suppression for leaving its close handler out of the deps. The
|
|
* handler is held in a ref here instead, so the listener is attached exactly
|
|
* once per open — which is what those suppressions were preserving.
|
|
*
|
|
* *also* is an extra node that counts as inside, for a menu portaled to
|
|
* document.body so it is no longer a descendant of *ref*.
|
|
*/
|
|
export function useOutsideClick(
|
|
ref: RefObject<HTMLElement | null>,
|
|
enabled: boolean,
|
|
onOutside: () => void,
|
|
also?: RefObject<HTMLElement | null>,
|
|
): void {
|
|
const handlerRef = useRef(onOutside);
|
|
const alsoRef = useRef(also);
|
|
useEffect(() => {
|
|
handlerRef.current = onOutside;
|
|
alsoRef.current = also;
|
|
});
|
|
useEffect(() => {
|
|
if (!enabled) return;
|
|
const handler = (event: MouseEvent) => {
|
|
const target = event.target as Node;
|
|
if (ref.current?.contains(target)) return;
|
|
if (alsoRef.current?.current?.contains(target)) return;
|
|
// No anchor yet: nothing to treat as inside, so stay quiet.
|
|
if (!ref.current && !alsoRef.current?.current) return;
|
|
handlerRef.current();
|
|
};
|
|
document.addEventListener("mousedown", handler);
|
|
return () => document.removeEventListener("mousedown", handler);
|
|
}, [enabled, ref]);
|
|
}
|