2024-06-27 05:33:38 +10:00
|
|
|
import { useEffect, useRef, useState, useCallback } from 'react';
|
|
|
|
|
|
|
|
import { useLocation } from 'react-router-dom';
|
|
|
|
|
|
|
|
import Overlay from 'react-overlays/Overlay';
|
|
|
|
import type {
|
|
|
|
OffsetValue,
|
|
|
|
UsePopperOptions,
|
|
|
|
} from 'react-overlays/esm/usePopper';
|
|
|
|
|
|
|
|
import { useTimeout } from 'mastodon/../hooks/useTimeout';
|
|
|
|
import { HoverCardAccount } from 'mastodon/components/hover_card_account';
|
|
|
|
|
|
|
|
const offset = [-12, 4] as OffsetValue;
|
2024-07-02 01:52:01 +10:00
|
|
|
const enterDelay = 750;
|
|
|
|
const leaveDelay = 150;
|
2024-06-27 05:33:38 +10:00
|
|
|
const popperConfig = { strategy: 'fixed' } as UsePopperOptions;
|
|
|
|
|
|
|
|
const isHoverCardAnchor = (element: HTMLElement) =>
|
|
|
|
element.matches('[data-hover-card-account]');
|
|
|
|
|
|
|
|
export const HoverCardController: React.FC = () => {
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const [accountId, setAccountId] = useState<string | undefined>();
|
|
|
|
const [anchor, setAnchor] = useState<HTMLElement | null>(null);
|
2024-07-02 01:52:01 +10:00
|
|
|
const cardRef = useRef<HTMLDivElement | null>(null);
|
2024-06-27 05:33:38 +10:00
|
|
|
const [setLeaveTimeout, cancelLeaveTimeout] = useTimeout();
|
2024-07-02 01:52:01 +10:00
|
|
|
const [setEnterTimeout, cancelEnterTimeout, delayEnterTimeout] = useTimeout();
|
|
|
|
const [setScrollTimeout] = useTimeout();
|
2024-06-27 05:33:38 +10:00
|
|
|
const location = useLocation();
|
|
|
|
|
2024-07-02 01:52:01 +10:00
|
|
|
const handleClose = useCallback(() => {
|
|
|
|
cancelEnterTimeout();
|
|
|
|
cancelLeaveTimeout();
|
|
|
|
setOpen(false);
|
|
|
|
setAnchor(null);
|
|
|
|
}, [cancelEnterTimeout, cancelLeaveTimeout, setOpen, setAnchor]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
handleClose();
|
|
|
|
}, [handleClose, location]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let isScrolling = false;
|
|
|
|
let currentAnchor: HTMLElement | null = null;
|
2024-07-08 02:14:15 +10:00
|
|
|
let currentTitle: string | null = null;
|
2024-07-02 01:52:01 +10:00
|
|
|
|
|
|
|
const open = (target: HTMLElement) => {
|
|
|
|
target.setAttribute('aria-describedby', 'hover-card');
|
|
|
|
setOpen(true);
|
|
|
|
setAnchor(target);
|
|
|
|
setAccountId(target.getAttribute('data-hover-card-account') ?? undefined);
|
|
|
|
};
|
|
|
|
|
|
|
|
const close = () => {
|
|
|
|
currentAnchor?.removeAttribute('aria-describedby');
|
|
|
|
currentAnchor = null;
|
|
|
|
setOpen(false);
|
|
|
|
setAnchor(null);
|
|
|
|
setAccountId(undefined);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleMouseEnter = (e: MouseEvent) => {
|
2024-06-27 05:33:38 +10:00
|
|
|
const { target } = e;
|
|
|
|
|
2024-07-02 01:52:01 +10:00
|
|
|
// We've exited the window
|
|
|
|
if (!(target instanceof HTMLElement)) {
|
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've entered an anchor
|
|
|
|
if (!isScrolling && isHoverCardAnchor(target)) {
|
2024-06-27 05:33:38 +10:00
|
|
|
cancelLeaveTimeout();
|
|
|
|
|
2024-07-02 01:52:01 +10:00
|
|
|
currentAnchor?.removeAttribute('aria-describedby');
|
|
|
|
currentAnchor = target;
|
|
|
|
|
2024-07-08 02:14:15 +10:00
|
|
|
currentTitle = target.getAttribute('title');
|
|
|
|
target.removeAttribute('title');
|
|
|
|
|
2024-06-27 05:33:38 +10:00
|
|
|
setEnterTimeout(() => {
|
2024-07-02 01:52:01 +10:00
|
|
|
open(target);
|
2024-06-27 05:33:38 +10:00
|
|
|
}, enterDelay);
|
|
|
|
}
|
|
|
|
|
2024-07-02 01:52:01 +10:00
|
|
|
// We've entered the hover card
|
|
|
|
if (
|
|
|
|
!isScrolling &&
|
|
|
|
(target === currentAnchor || target === cardRef.current)
|
|
|
|
) {
|
2024-06-27 05:33:38 +10:00
|
|
|
cancelLeaveTimeout();
|
|
|
|
}
|
2024-07-02 01:52:01 +10:00
|
|
|
};
|
2024-06-27 05:33:38 +10:00
|
|
|
|
2024-07-02 01:52:01 +10:00
|
|
|
const handleMouseLeave = (e: MouseEvent) => {
|
2024-07-08 02:14:15 +10:00
|
|
|
const { target } = e;
|
|
|
|
|
2024-07-02 01:52:01 +10:00
|
|
|
if (!currentAnchor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-08 02:14:15 +10:00
|
|
|
if (
|
|
|
|
currentTitle &&
|
|
|
|
target instanceof HTMLElement &&
|
|
|
|
target === currentAnchor
|
|
|
|
)
|
|
|
|
target.setAttribute('title', currentTitle);
|
|
|
|
|
|
|
|
if (target === currentAnchor || target === cardRef.current) {
|
2024-06-27 05:33:38 +10:00
|
|
|
cancelEnterTimeout();
|
|
|
|
|
|
|
|
setLeaveTimeout(() => {
|
2024-07-02 01:52:01 +10:00
|
|
|
close();
|
2024-06-27 05:33:38 +10:00
|
|
|
}, leaveDelay);
|
|
|
|
}
|
2024-07-02 01:52:01 +10:00
|
|
|
};
|
2024-06-27 05:33:38 +10:00
|
|
|
|
2024-07-02 01:52:01 +10:00
|
|
|
const handleScrollEnd = () => {
|
|
|
|
isScrolling = false;
|
|
|
|
};
|
2024-06-27 05:33:38 +10:00
|
|
|
|
2024-07-02 01:52:01 +10:00
|
|
|
const handleScroll = () => {
|
|
|
|
isScrolling = true;
|
|
|
|
cancelEnterTimeout();
|
|
|
|
setScrollTimeout(handleScrollEnd, 100);
|
|
|
|
};
|
2024-06-27 05:33:38 +10:00
|
|
|
|
2024-07-02 01:52:01 +10:00
|
|
|
const handleMouseMove = () => {
|
|
|
|
delayEnterTimeout(enterDelay);
|
|
|
|
};
|
|
|
|
|
|
|
|
document.body.addEventListener('mouseenter', handleMouseEnter, {
|
|
|
|
passive: true,
|
|
|
|
capture: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
document.body.addEventListener('mousemove', handleMouseMove, {
|
|
|
|
passive: true,
|
|
|
|
capture: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
document.body.addEventListener('mouseleave', handleMouseLeave, {
|
2024-06-27 05:33:38 +10:00
|
|
|
passive: true,
|
|
|
|
capture: true,
|
|
|
|
});
|
2024-07-02 01:52:01 +10:00
|
|
|
|
|
|
|
document.addEventListener('scroll', handleScroll, {
|
2024-06-27 05:33:38 +10:00
|
|
|
passive: true,
|
|
|
|
capture: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
2024-07-02 01:52:01 +10:00
|
|
|
document.body.removeEventListener('mouseenter', handleMouseEnter);
|
|
|
|
document.body.removeEventListener('mousemove', handleMouseMove);
|
|
|
|
document.body.removeEventListener('mouseleave', handleMouseLeave);
|
|
|
|
document.removeEventListener('scroll', handleScroll);
|
2024-06-27 05:33:38 +10:00
|
|
|
};
|
2024-07-02 01:52:01 +10:00
|
|
|
}, [
|
|
|
|
setEnterTimeout,
|
|
|
|
setLeaveTimeout,
|
|
|
|
setScrollTimeout,
|
|
|
|
cancelEnterTimeout,
|
|
|
|
cancelLeaveTimeout,
|
|
|
|
delayEnterTimeout,
|
|
|
|
setOpen,
|
|
|
|
setAccountId,
|
|
|
|
setAnchor,
|
|
|
|
]);
|
2024-06-27 05:33:38 +10:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Overlay
|
|
|
|
rootClose
|
|
|
|
onHide={handleClose}
|
|
|
|
show={open}
|
|
|
|
target={anchor}
|
|
|
|
placement='bottom-start'
|
|
|
|
flip
|
|
|
|
offset={offset}
|
|
|
|
popperConfig={popperConfig}
|
|
|
|
>
|
|
|
|
{({ props }) => (
|
|
|
|
<div {...props} className='hover-card-controller'>
|
|
|
|
<HoverCardAccount accountId={accountId} ref={cardRef} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Overlay>
|
|
|
|
);
|
|
|
|
};
|