[Glitch] Prevent hover card from showing unintentionally

Port 316290ba9d to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
diondiondion
2026-03-11 08:42:36 +01:00
committed by Claire
parent 3b517fc8c5
commit 04e390654e

View File

@@ -14,6 +14,10 @@ import { useTimeout } from 'flavours/glitch/hooks/useTimeout';
const offset = [-12, 4] as OffsetValue; const offset = [-12, 4] as OffsetValue;
const enterDelay = 750; const enterDelay = 750;
const leaveDelay = 150; const leaveDelay = 150;
// Only open the card if the mouse was moved within this time,
// to avoid triggering the card without intentional mouse movement
// (e.g. when content changed underneath the mouse cursor)
const activeMovementThreshold = 150;
const popperConfig = { strategy: 'fixed' } as UsePopperOptions; const popperConfig = { strategy: 'fixed' } as UsePopperOptions;
const isHoverCardAnchor = (element: HTMLElement) => const isHoverCardAnchor = (element: HTMLElement) =>
@@ -23,10 +27,10 @@ export const HoverCardController: React.FC = () => {
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [accountId, setAccountId] = useState<string | undefined>(); const [accountId, setAccountId] = useState<string | undefined>();
const [anchor, setAnchor] = useState<HTMLElement | null>(null); const [anchor, setAnchor] = useState<HTMLElement | null>(null);
const isUsingTouchRef = useRef(false);
const cardRef = useRef<HTMLDivElement | null>(null); const cardRef = useRef<HTMLDivElement | null>(null);
const [setLeaveTimeout, cancelLeaveTimeout] = useTimeout(); const [setLeaveTimeout, cancelLeaveTimeout] = useTimeout();
const [setEnterTimeout, cancelEnterTimeout, delayEnterTimeout] = useTimeout(); const [setEnterTimeout, cancelEnterTimeout, delayEnterTimeout] = useTimeout();
const [setMoveTimeout, cancelMoveTimeout] = useTimeout();
const [setScrollTimeout] = useTimeout(); const [setScrollTimeout] = useTimeout();
const location = useLocation(); const location = useLocation();
@@ -43,6 +47,8 @@ export const HoverCardController: React.FC = () => {
useEffect(() => { useEffect(() => {
let isScrolling = false; let isScrolling = false;
let isUsingTouch = false;
let isActiveMouseMovement = false;
let currentAnchor: HTMLElement | null = null; let currentAnchor: HTMLElement | null = null;
let currentTitle: string | null = null; let currentTitle: string | null = null;
@@ -64,7 +70,7 @@ export const HoverCardController: React.FC = () => {
const handleTouchStart = () => { const handleTouchStart = () => {
// Keeping track of touch events to prevent the // Keeping track of touch events to prevent the
// hover card from being displayed on touch devices // hover card from being displayed on touch devices
isUsingTouchRef.current = true; isUsingTouch = true;
}; };
const handleMouseEnter = (e: MouseEvent) => { const handleMouseEnter = (e: MouseEvent) => {
@@ -76,13 +82,14 @@ export const HoverCardController: React.FC = () => {
return; return;
} }
// Bail out if a touch is active // Bail out if we're scrolling, a touch is active,
if (isUsingTouchRef.current) { // or if there was no active mouse movement
if (isScrolling || !isActiveMouseMovement || isUsingTouch) {
return; return;
} }
// We've entered an anchor // We've entered an anchor
if (!isScrolling && isHoverCardAnchor(target)) { if (isHoverCardAnchor(target)) {
cancelLeaveTimeout(); cancelLeaveTimeout();
currentAnchor?.removeAttribute('aria-describedby'); currentAnchor?.removeAttribute('aria-describedby');
@@ -97,10 +104,7 @@ export const HoverCardController: React.FC = () => {
} }
// We've entered the hover card // We've entered the hover card
if ( if (target === currentAnchor || target === cardRef.current) {
!isScrolling &&
(target === currentAnchor || target === cardRef.current)
) {
cancelLeaveTimeout(); cancelLeaveTimeout();
} }
}; };
@@ -139,10 +143,17 @@ export const HoverCardController: React.FC = () => {
}; };
const handleMouseMove = () => { const handleMouseMove = () => {
if (isUsingTouchRef.current) { if (isUsingTouch) {
isUsingTouchRef.current = false; isUsingTouch = false;
} }
delayEnterTimeout(enterDelay); delayEnterTimeout(enterDelay);
cancelMoveTimeout();
isActiveMouseMovement = true;
setMoveTimeout(() => {
isActiveMouseMovement = false;
}, activeMovementThreshold);
}; };
document.body.addEventListener('touchstart', handleTouchStart, { document.body.addEventListener('touchstart', handleTouchStart, {
@@ -186,6 +197,8 @@ export const HoverCardController: React.FC = () => {
setOpen, setOpen,
setAccountId, setAccountId,
setAnchor, setAnchor,
setMoveTimeout,
cancelMoveTimeout,
]); ]);
return ( return (