From 86915096c7830d356250424c802518cead2bbbce Mon Sep 17 00:00:00 2001 From: Yehonatan Daniv Date: Tue, 18 Feb 2025 14:48:03 +0200 Subject: [PATCH] Fix false positive webkit bug check on chrome (#18) * Fixed check for WebKit bug with synthetic pointer events giving false-positive on Chrome * 0.4.8 --- dist/index.cjs | 39 ++++++++++++++++++++++--------------- docs/demo/index.js | 39 ++++++++++++++++++++++--------------- docs/reference/index.html | 4 ++-- package.json | 2 +- src/utilities.js | 41 +++++++++++++++++++++++---------------- 5 files changed, 73 insertions(+), 52 deletions(-) diff --git a/dist/index.cjs b/dist/index.cjs index 78010d8..b23ad6c 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -95,29 +95,36 @@ function testPointerOffsetDprBug () { function testPointerOffsetScrollBug () { return new Promise((resolve) => { - function scrollHandler () { - if (window.scrollY > 0) { - window.removeEventListener('scroll', scrollHandler); - - let fixRequired = false; - - document.body.addEventListener('pointerdown', (e) => { - fixRequired = e.clientY === e.pageY; - }, { once: true }); + const startY = window.scrollY; + let fixRequired = false; + let currentY; + + function sampleOffsetY () { + document.body.addEventListener('pointerdown', (e) => { + if (currentY === undefined) { + currentY = e.offsetY; + } else { + // when dispatching on BODY in different scroll it gives the same offsetY value only on WebKit + fixRequired = e.offsetY === currentY; + } + }, { once: true }); - const event = new PointerEvent('pointerdown', { - clientX: 10 - }); + const event = new PointerEvent('pointerdown', { + clientY: 500 + }); - document.body.dispatchEvent(event); + document.body.dispatchEvent(event); + } + function scrollHandler () { + if (window.scrollY !== startY) { + window.removeEventListener('scroll', scrollHandler); + sampleOffsetY(); resolve(fixRequired); } } - if (window.scrollY > 0) { - scrollHandler(); - } + sampleOffsetY(); window.addEventListener('scroll', scrollHandler); }); diff --git a/docs/demo/index.js b/docs/demo/index.js index ea17b6d..367c025 100644 --- a/docs/demo/index.js +++ b/docs/demo/index.js @@ -93,29 +93,36 @@ function testPointerOffsetDprBug () { function testPointerOffsetScrollBug () { return new Promise((resolve) => { - function scrollHandler () { - if (window.scrollY > 0) { - window.removeEventListener('scroll', scrollHandler); - - let fixRequired = false; - - document.body.addEventListener('pointerdown', (e) => { - fixRequired = e.clientY === e.pageY; - }, { once: true }); + const startY = window.scrollY; + let fixRequired = false; + let currentY; + + function sampleOffsetY () { + document.body.addEventListener('pointerdown', (e) => { + if (currentY === undefined) { + currentY = e.offsetY; + } else { + // when dispatching on BODY in different scroll it gives the same offsetY value only on WebKit + fixRequired = e.offsetY === currentY; + } + }, { once: true }); - const event = new PointerEvent('pointerdown', { - clientX: 10 - }); + const event = new PointerEvent('pointerdown', { + clientY: 500 + }); - document.body.dispatchEvent(event); + document.body.dispatchEvent(event); + } + function scrollHandler () { + if (window.scrollY !== startY) { + window.removeEventListener('scroll', scrollHandler); + sampleOffsetY(); resolve(fixRequired); } } - if (window.scrollY > 0) { - scrollHandler(); - } + sampleOffsetY(); window.addEventListener('scroll', scrollHandler); }); diff --git a/docs/reference/index.html b/docs/reference/index.html index f904f26..ea9d1d4 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -2,7 +2,7 @@ - kuliso 0.4.6 | Documentation + kuliso 0.4.7 | Documentation @@ -15,7 +15,7 @@

kuliso

-
0.4.6
+
0.4.7
{ - function scrollHandler () { - if (window.scrollY > 0) { - window.removeEventListener('scroll', scrollHandler); - - let fixRequired = false; - - document.body.addEventListener('pointerdown', (e) => { - fixRequired = e.clientY === e.pageY; - }, { once: true }); - - const event = new PointerEvent('pointerdown', { - clientX: 10 - }); + const startY = window.scrollY; + let fixRequired = false; + let currentY; + + function sampleOffsetY () { + document.body.addEventListener('pointerdown', (e) => { + if (currentY === undefined) { + currentY = e.offsetY; + } else { + // when dispatching on BODY in different scroll it gives the same offsetY value only on WebKit + fixRequired = e.offsetY === currentY; + } + }, { once: true }); + + const event = new PointerEvent('pointerdown', { + clientY: 500 + }); - document.body.dispatchEvent(event); + document.body.dispatchEvent(event); + } + function scrollHandler () { + if (window.scrollY !== startY) { + window.removeEventListener('scroll', scrollHandler); + sampleOffsetY(); resolve(fixRequired); } } - if (window.scrollY > 0) { - scrollHandler(); - } + sampleOffsetY(); window.addEventListener('scroll', scrollHandler); });