Skip to content

Commit

Permalink
feat(utils): trackpad detector improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Antamansid committed Oct 10, 2024
1 parent 0d53e16 commit bec6a31
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/utils/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit bec6a31

Please sign in to comment.