Skip to content

Commit dc9ac9a

Browse files
authored
consider scrolling for hover function (#4731)
this pr sets a flag to help indicate whether the page is being scrolled so that the toggles don’t expand while scrolling. this pr also modifies the `handleMouseEnter` function to check this flag and prevents the toggle from expanding if the page is being scrolled. - adds a new state variable to track if the page is scrolling. - add event listeners for scroll events to set this variable. - modifies the handleMouseEnter function to check the scrolling state. So: - `isScrolling` tracks if user is scrolling - `handleScroll` function to call on scrolling events and disables toggle if `isScrolling` is true - `handleMouseEnter` checks for `isScrolling`
2 parents 923de83 + 74bafe9 commit dc9ac9a

File tree

1 file changed

+36
-20
lines changed
  • website/src/components/detailsToggle

1 file changed

+36
-20
lines changed

website/src/components/detailsToggle/index.js

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,49 @@ import styles from './styles.module.css';
33

44
function detailsToggle({ children, alt_header = null }) {
55
const [isOn, setOn] = useState(false);
6-
const [hoverActive, setHoverActive] = useState(true);
6+
const [isScrolling, setIsScrolling] = useState(false); // New state to track scrolling
77
const [hoverTimeout, setHoverTimeout] = useState(null);
88

99
const handleToggleClick = () => {
10-
setHoverActive(true); // Disable hover when clicked
1110
setOn(current => !current); // Toggle the current state
12-
};
13-
14-
const handleMouseEnter = () => {
15-
if (isOn) return; // Ignore hover if already open
16-
setHoverActive(true); // Enable hover
17-
const timeout = setTimeout(() => {
18-
if (hoverActive) setOn(true);
19-
}, 500);
20-
setHoverTimeout(timeout);
21-
};
22-
23-
const handleMouseLeave = () => {
24-
if (!isOn) {
11+
};
12+
13+
const handleMouseEnter = () => {
14+
if (isOn || isScrolling) return; // Ignore hover if already open or if scrolling
15+
const timeout = setTimeout(() => {
16+
if (!isScrolling) setOn(true);
17+
}, 700); //
18+
setHoverTimeout(timeout);
19+
};
20+
21+
const handleMouseLeave = () => {
22+
if (!isOn) {
2523
clearTimeout(hoverTimeout);
2624
setOn(false);
27-
}
28-
};
25+
}
26+
};
27+
28+
const handleScroll = () => {
29+
setIsScrolling(true);
30+
clearTimeout(hoverTimeout);
31+
setOn(false);
32+
33+
// Reset scrolling state after a delay
34+
setTimeout(() => {
35+
setIsScrolling(false);
36+
}, 800);
37+
};
38+
39+
useEffect(() => {
40+
window.addEventListener('scroll', handleScroll);
41+
return () => {
42+
window.removeEventListener('scroll', handleScroll);
43+
};
44+
}, []);
2945

30-
useEffect(() => {
31-
return () => clearTimeout(hoverTimeout);
32-
}, [hoverTimeout]);
46+
useEffect(() => {
47+
return () => clearTimeout(hoverTimeout);
48+
}, [hoverTimeout]);
3349

3450
return (
3551
<div className='detailsToggle'>

0 commit comments

Comments
 (0)