[Glitch] Prevent hover card from showing on touch devices
Port de4ee8565c to glitch-soc
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
@@ -23,6 +23,7 @@ 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();
|
||||||
@@ -62,6 +63,12 @@ export const HoverCardController: React.FC = () => {
|
|||||||
setAccountId(undefined);
|
setAccountId(undefined);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleTouchStart = () => {
|
||||||
|
// Keeping track of touch events to prevent the
|
||||||
|
// hover card from being displayed on touch devices
|
||||||
|
isUsingTouchRef.current = true;
|
||||||
|
};
|
||||||
|
|
||||||
const handleMouseEnter = (e: MouseEvent) => {
|
const handleMouseEnter = (e: MouseEvent) => {
|
||||||
const { target } = e;
|
const { target } = e;
|
||||||
|
|
||||||
@@ -71,6 +78,11 @@ export const HoverCardController: React.FC = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bail out if a touch is active
|
||||||
|
if (isUsingTouchRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// We've entered an anchor
|
// We've entered an anchor
|
||||||
if (!isScrolling && isHoverCardAnchor(target)) {
|
if (!isScrolling && isHoverCardAnchor(target)) {
|
||||||
cancelLeaveTimeout();
|
cancelLeaveTimeout();
|
||||||
@@ -129,9 +141,16 @@ export const HoverCardController: React.FC = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleMouseMove = () => {
|
const handleMouseMove = () => {
|
||||||
|
if (isUsingTouchRef.current) {
|
||||||
|
isUsingTouchRef.current = false;
|
||||||
|
}
|
||||||
delayEnterTimeout(enterDelay);
|
delayEnterTimeout(enterDelay);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
document.body.addEventListener('touchstart', handleTouchStart, {
|
||||||
|
passive: true,
|
||||||
|
});
|
||||||
|
|
||||||
document.body.addEventListener('mouseenter', handleMouseEnter, {
|
document.body.addEventListener('mouseenter', handleMouseEnter, {
|
||||||
passive: true,
|
passive: true,
|
||||||
capture: true,
|
capture: true,
|
||||||
@@ -153,6 +172,7 @@ export const HoverCardController: React.FC = () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
document.body.removeEventListener('touchstart', handleTouchStart);
|
||||||
document.body.removeEventListener('mouseenter', handleMouseEnter);
|
document.body.removeEventListener('mouseenter', handleMouseEnter);
|
||||||
document.body.removeEventListener('mousemove', handleMouseMove);
|
document.body.removeEventListener('mousemove', handleMouseMove);
|
||||||
document.body.removeEventListener('mouseleave', handleMouseLeave);
|
document.body.removeEventListener('mouseleave', handleMouseLeave);
|
||||||
|
|||||||
Reference in New Issue
Block a user