Skip to content

Refactor rating selection logic using .closest() for better UX and maintainability#263

Open
qingning7 wants to merge 1 commit intobradtraversy:masterfrom
qingning7:patch-2
Open

Refactor rating selection logic using .closest() for better UX and maintainability#263
qingning7 wants to merge 1 commit intobradtraversy:masterfrom
qingning7:patch-2

Conversation

@qingning7
Copy link

Problem: The previous event listener logic relied on strict DOM structure patterns (using parentNode and nextElementSibling). This caused the feedback selection to be unreliable—clicks on certain padding areas or specific child elements often failed to trigger the active state, leading to a "laggy" or "unresponsive" user experience.

Solution: Refactored the click event handler using the Element.closest() method.

Key Changes:

  • Improved Hitbox: By using e.target.closest('.rating'), the entire area of the rating card becomes clickable, regardless of whether the user clicks the icon, the text, or the container.
  • Simplified Logic: Removed complex if-else nesting and positional sibling selectors, making the code much cleaner and less prone to breaking if the HTML structure changes in the future.
  • Better Performance: Reduced redundant DOM traversal and conditional checks.

Testing: Verified that the 'active' class toggles correctly on all screen sizes and that the selectedRating value is captured accurately regardless of where the user clicks within the rating element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant