From bec6a311775f10057708fd12f21a8cddc2fe825c Mon Sep 17 00:00:00 2001 From: Antamansid Date: Thu, 10 Oct 2024 15:37:08 +0300 Subject: [PATCH] feat(utils): trackpad detector improvements --- src/utils/functions/index.ts | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/utils/functions/index.ts b/src/utils/functions/index.ts index a1d369a..bbecd8b 100644 --- a/src/utils/functions/index.ts +++ b/src/utils/functions/index.ts @@ -153,15 +153,34 @@ export function isWindows() { return navigator.appVersion.indexOf("Win") !== -1; } -export function isTrackpadWheelEvent(e: WheelEvent) { - // deltaY in the mouse scroll even, usually is a float number. - // It's a bit of a hack, but it's the easiest way to detect a trackpad. - if (e.deltaY && !Number.isInteger(e.deltaY)) { - return false; +function isTrackpadDetector() { + let isTrackpadDetected = false; + let cleanStateTimer = setTimeout(() => {}, 0); + + return (e: WheelEvent) => { + // deltaX in the trackpad scroll usually is not zero. + if (e.deltaX) { + isTrackpadDetected = true; + clearTimeout(cleanStateTimer); + cleanStateTimer = setTimeout(() => { + isTrackpadDetected = false; + }, 1000 * 60); + + return true; + } + + // deltaY in the mouse scroll event, usually is a float number. + // It's a bit of a hack, but it's the easiest way to detect a mouse. + if (e.deltaY && !Number.isInteger(e.deltaY)) { + return false; + } + + return isTrackpadDetected; } - return true; } +export const isTrackpadWheelEvent = isTrackpadDetector(); + export function computeCssVariable(name: string) { if (!name.startsWith("var(")) return name;