Skip to content

Commit 1e6bfc4

Browse files
committed
try fix person expansion vs scroll on mobile by separating events
1 parent 12aa25f commit 1e6bfc4

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/components/Team.tsx

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from "react";
1+
import { useState, useRef } from "react";
22

33
import { Person, PersonSchema, gracefulParse } from "../types";
44

@@ -7,28 +7,46 @@ import teamJSON from "../content/text/team.json";
77
const PersonComponent = ({ personData }: { personData: Person }) => {
88
const { name, role, desc, outLinks, imagePath } = personData;
99
const [isHovered, setIsHovered] = useState(false);
10+
const touchStartPos = useRef<number | null>(null);
1011

12+
// Pixels
13+
const scrollThreshold = 10;
1114
const w = 150;
1215
const h = 1.5 * w;
1316

1417
const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
15-
const handleExpand = (state: boolean, click: boolean) => {
16-
if (isMobile && click) {
17-
setIsHovered(!state);
18-
return;
19-
} else if (isMobile) {
18+
const handleExpand = (state: boolean) => {
19+
if (isMobile) {
2020
return;
2121
}
2222

2323
setIsHovered(state);
2424
};
2525

26+
const handleTouchStart = (e: React.TouchEvent) => {
27+
touchStartPos.current = e.touches[0].clientY;
28+
};
29+
30+
const handleTouchEnd = (e: React.TouchEvent) => {
31+
if (touchStartPos.current === null) return;
32+
33+
if (!isMobile) return;
34+
35+
const touchEndPos = e.changedTouches[0].clientY;
36+
const distance = Math.abs(touchEndPos - touchStartPos.current);
37+
if (distance < scrollThreshold) {
38+
setIsHovered((prev) => !prev);
39+
}
40+
touchStartPos.current = null;
41+
};
42+
2643
return (
2744
<div
2845
className="outlined-content person-card"
29-
onPointerEnter={() => handleExpand(true, false)}
30-
onPointerLeave={() => handleExpand(false, false)}
31-
onPointerDownCapture={() => handleExpand(isHovered, true)}
46+
onPointerEnter={() => handleExpand(true)}
47+
onPointerLeave={() => handleExpand(false)}
48+
onTouchStart={(e) => handleTouchStart(e)}
49+
onTouchEnd={(e) => handleTouchEnd(e)}
3250
>
3351
<div>
3452
<img

0 commit comments

Comments
 (0)