Skip to content

fix: process all intersection observer event entries - #5668

Merged
bwajtr merged 4 commits into
mainfrom
5667-tooltip-not-displayed-after-items-are-sorted-in-$repeat-directive-chrome-edge-only
Mar 17, 2023
Merged

bwajtr merged 4 commits into
mainfrom
5667-tooltip-not-displayed-after-items-are-sorted-in-$repeat-directive-chrome-edge-only

Conversation

@bwajtr

@bwajtr bwajtr commented Mar 14, 2023 •

Copy link
Copy Markdown
Contributor

Description

vaadin-tooltip uses an IntersectionObserver to watch for the target full visibility. If the user scrolls the page in a way that the target of the tooltip is only partially visible (or not visible at all), the tooltip overlay - if previously shown - is automatically hidden too. This helps to prevent a situation when we would show a tooltip for an element which is not visible.

Issue #5667 reported buggy behaviour of this mechanism when rendering tooltips using lit $repeat directive. However, further investigation showed that it's a more general problem, and the buggy behaviour is observed when you are simply moving elements from place to place (which is what $repeat internally does) - but it's buggy only in Chromium based browsers.

Even though all involved elements always stay in the viewport Chromium-based browsers trigger IntersectionObserver with a pair of IntersectionObserverEntry instances when you move one of the elements. This does not happen every time, but only in specific situations - e.g. moving the element before some other element in the DOM. Surprisingly, it does not happen when moving in the other direction. Firefox and Safari do not produce any IntersectionObserver events whatsoever.

As I said, Chromium produces two instances of IntersectionObserverEntry and supplies it to the observer callback at once. From the content of the entries, it seems like the first entry is for an act of removing the element from the viewport (with isIntersecting==false) and the second entry is for placing it back to the viewport (with isIntersecting==true). I've reported this behaviour to Chromium bug database, because I don't think it's right - it should be clarified the least.

The problem in our code was that the Observer callback was processing always only the first entry from the IntersectionObserverEntries list. The code probably assumed, that because we are always observing only a single target, we will always get a single entry in the callback. In the case of buggy behavior, this is not true. Our code processed only the entry where isIntersecting==false, making it think that the target element was removed from the viewport and never returned.

I think the reasonable fix here is to always process all the entries which are passed to the IntersectionObserver callback. That'll force the tooltip to process also the second entry in the list, where isIntersecting is again set to true.

Fixes #5667

Type of change

  • Bugfix
  • Feature

Checklist

  • I have read the contribution guide: https://vaadin.com/docs/latest/contributing/overview
  • I have added a description following the guideline.
  • The issue is created in the corresponding repository and I have referenced it.
  • I have added tests to ensure my change is effective and works as intended.
  • New and existing tests are passing locally with my change.
  • I have performed self-review and corrected misspellings.

@bwajtr bwajtr changed the title fix: ignore false intersection events in Chrome fix: process all intersection observer event entries Mar 16, 2023
@bwajtr
bwajtr requested review from vursen and web-padawan March 16, 2023 15:27
@bwajtr
bwajtr marked this pull request as ready for review March 16, 2023 15:29
Comment thread packages/tooltip/test/tooltip.test.js Resolved
Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
@sonarqubecloud

Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@bwajtr
bwajtr merged commit bc227bd into main Mar 17, 2023
@bwajtr
bwajtr deleted the 5667-tooltip-not-displayed-after-items-are-sorted-in-$repeat-directive-chrome-edge-only branch March 17, 2023 08:43
bwajtr added a commit that referenced this pull request Mar 17, 2023
Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
bwajtr added a commit that referenced this pull request Mar 20, 2023
Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
@vaadin-bot

Copy link
Copy Markdown
Collaborator

This ticket/PR has been released with Vaadin 24.1.0.alpha1 and is also targeting the upcoming stable 24.1.0 version.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tooltip not displayed after items are sorted in $repeat directive (Chrome, Edge only)

3 participants