Skip to content

Commit ac41c13

Browse files
authored
fix: resolve news image zoom flickering on hover (#526)
1 parent 17fe0f3 commit ac41c13

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/scenes/News/image-zoom.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ const Overlay = styled.div<{ visible: boolean }>`
2626
opacity: ${({ visible }) => (visible ? 1 : 0)};
2727
pointer-events: ${({ visible }) => (visible ? "auto" : "none")};
2828
transition: opacity 0.2s ease-in-out;
29+
cursor: pointer;
2930
`
3031

3132
const Wrapper = styled.div`
3233
z-index: 1001;
34+
cursor: pointer;
3335
3436
img {
3537
border: 1px solid ${({ theme }) => theme.color.offWhite};
@@ -72,9 +74,12 @@ export const ImageZoom = () => {
7274

7375
return (
7476
<Root ref={rootRef} visible={imageToZoom !== undefined}>
75-
<Overlay visible={imageToZoom !== undefined} />
77+
<Overlay
78+
visible={imageToZoom !== undefined}
79+
onClick={() => dispatch(actions.console.setImageToZoom(undefined))}
80+
/>
7681
{imageToZoom && (
77-
<Wrapper>
82+
<Wrapper onClick={() => dispatch(actions.console.setImageToZoom(undefined))}>
7883
<Thumbnail
7984
{...imageToZoom}
8085
containerWidth={rootWidth ? rootWidth * 0.9 : 460}

src/scenes/News/index.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
PrimaryToggleButton,
77
} from "../../components"
88
import styled from "styled-components"
9-
import React, { useEffect, useState, useContext } from "react"
9+
import React, { useEffect, useState, useContext, useRef } from "react"
1010
import { QuestContext } from "../../providers"
1111
import { NewsItem } from "../../utils"
1212
import { useDispatch, useSelector } from "react-redux"
@@ -93,8 +93,11 @@ const News = () => {
9393
// This boolean is to animate the bell icon and display a bullet indicator
9494
const [hasUnreadNews, setHasUnreadNews] = useState(false)
9595
const activeSidebar = useSelector(selectors.console.getActiveSidebar)
96+
const imageToZoom = useSelector(selectors.console.getImageToZoom)
9697

97-
let hoverTimeout: ReturnType<typeof setTimeout>
98+
const hoverTimeoutRef = useRef<ReturnType<typeof setTimeout>>()
99+
const imageToZoomRef = useRef(imageToZoom)
100+
imageToZoomRef.current = imageToZoom
98101

99102
const getEnterpriseNews = async () => {
100103
setIsLoading(true)
@@ -248,7 +251,7 @@ const News = () => {
248251
? {
249252
onMouseOver: () => {
250253
if (newsItem.thumbnail) {
251-
hoverTimeout = setTimeout(() => {
254+
hoverTimeoutRef.current = setTimeout(() => {
252255
if (newsItem && newsItem.thumbnail) {
253256
dispatch(
254257
actions.console.setImageToZoom({
@@ -268,12 +271,15 @@ const News = () => {
268271
}
269272
},
270273
onMouseOut: () => {
271-
clearTimeout(hoverTimeout)
272-
setTimeout(() => {
273-
dispatch(
274-
actions.console.setImageToZoom(undefined),
275-
)
276-
}, 250)
274+
clearTimeout(hoverTimeoutRef.current)
275+
// Only dismiss if zoom isn't visible (overlay intercepts mouse when visible)
276+
if (!imageToZoomRef.current) {
277+
setTimeout(() => {
278+
dispatch(
279+
actions.console.setImageToZoom(undefined),
280+
)
281+
}, 250)
282+
}
277283
},
278284
}
279285
: {})}

0 commit comments

Comments
 (0)