refactor: simplify highlightElement cleanup logic

This commit is contained in:
neerajadhav 2025-07-08 18:00:35 +05:30
parent b3ab6fdd59
commit 30677df3d3

View File

@ -96,12 +96,11 @@ export function highlightElement(
color: string = "var(--highlight, #ffeb3b40)", color: string = "var(--highlight, #ffeb3b40)",
) { ) {
// Clear any existing highlight on this element // Clear any existing highlight on this element
activeHighlights.forEach((highlight) => { const existingHighlight = Array.from(activeHighlights).find(h => h.element === el)
if (highlight.element === el) { if (existingHighlight) {
clearTimeout(highlight.timeoutId) clearTimeout(existingHighlight.timeoutId)
activeHighlights.delete(highlight) activeHighlights.delete(existingHighlight)
} }
})
// Store original styles // Store original styles
const originalBackground = el.style.backgroundColor const originalBackground = el.style.backgroundColor
@ -112,27 +111,21 @@ export function highlightElement(
el.style.backgroundColor = color el.style.backgroundColor = color
// Set up cleanup // Set up cleanup
const timeoutId = window.setTimeout(() => { const highlight = {
el.style.backgroundColor = originalBackground
// Remove transition after background fades back
setTimeout(() => {
el.style.transition = originalTransition
// Remove from active highlights
activeHighlights.forEach((highlight) => {
if (highlight.element === el) {
activeHighlights.delete(highlight)
}
})
}, 300)
}, duration)
// Track this highlight
activeHighlights.add({
element: el, element: el,
originalBackground, originalBackground,
originalTransition, originalTransition,
timeoutId, timeoutId: 0,
}) }
highlight.timeoutId = window.setTimeout(() => {
el.style.backgroundColor = originalBackground
el.style.transition = originalTransition
activeHighlights.delete(highlight)
}, duration)
// Track this highlight
activeHighlights.add(highlight)
} }
/** /**