Skip to content

fix(web-components/scroll-locker): remove iOS-specific behaviors #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/sad-webs-fall.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@tapsioss/web-components": patch
---

Remove iOS-specific behaviors xD

190 changes: 3 additions & 187 deletions packages/web-components/src/utils/controllers/ScrollLocker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { runBeforeRepaint } from "../event-loop-execution.ts";
import isSsr from "../is-ssr.ts";

type LockOptions = {
reserveScrollBarGap?: boolean;
allowTouchMove?: (element: HTMLElement) => boolean;
Expand All @@ -14,15 +11,8 @@ type Lock = {
type HandleScrollEvent = TouchEvent;

class ScrollLocker {
private _hasPassiveEvents = false;
private _isIosDevice = false;

private _locks: Array<Lock> = [];

private _documentListenerAdded: boolean = false;

private _initialClientY: number = -1;

private _cachedBodySettings: {
overflow: string;
positioning: {
Expand All @@ -40,36 +30,6 @@ class ScrollLocker {

constructor() {
this._preventDefault = this._preventDefault.bind(this);

if (!isSsr()) {
let hasPassiveEvents = false;

const passiveTestOptions = {
get passive() {
hasPassiveEvents = true;
return undefined;
},
};

/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
window.addEventListener("test-passive", null, passiveTestOptions);
// @ts-ignore
window.removeEventListener("test-passive", null);
/* eslint-enable @typescript-eslint/ban-ts-comment */

this._isIosDevice =
typeof window !== "undefined" &&
!!window.navigator &&
((!!window.navigator.platform &&
(/iP(ad|hone|od)/.test(window.navigator.platform) ||
(window.navigator.platform === "MacIntel" &&
window.navigator.maxTouchPoints > 1))) ||
(!!window.navigator.userAgent &&
/iP(ad|hone|od)/.test(window.navigator.userAgent)));

this._hasPassiveEvents = hasPassiveEvents;
}
}

/**
Expand Down Expand Up @@ -135,92 +95,6 @@ class ScrollLocker {
this._cachedBodySettings.overflow = "";
}

private _setPositionFixed() {
window.requestAnimationFrame(() => {
if (!this._cachedBodySettings.positioning) {
this._cachedBodySettings.positioning = {
position: document.body.style.position,
top: document.body.style.top,
left: document.body.style.left,
right: document.body.style.right,
};

// Update the dom inside an animation frame
const { scrollY, scrollX, innerHeight } = window;

document.body.style.position = "fixed";
document.body.style.top = `${-scrollY}px`;
document.body.style.left = `${-scrollX}px`;
document.body.style.right = `0`;

runBeforeRepaint(() => {
// Attempt to check if the bottom bar appeared due to the position change
const bottomBarHeight = innerHeight - window.innerHeight;

if (bottomBarHeight && scrollY >= innerHeight) {
// Move the content further up so that the bottom bar doesn't hide it
document.body.style.top = `${-(scrollY + bottomBarHeight)}px`;
}
});
}
});
}

private _restorePositionSetting() {
// Convert the position from "px" to Int
const y = -parseInt(document.body.style.top, 10);
const x = -parseInt(document.body.style.left, 10);

const {
left = "",
position = "",
top = "",
right = "",
} = this._cachedBodySettings.positioning ?? {};

// Restore styles
document.body.style.position = position;
document.body.style.top = top;
document.body.style.left = left;
document.body.style.right = right;

// Restore scroll
window.scrollTo(x, y);

this._cachedBodySettings.positioning = null;
}

// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#Problems_and_solutions
private _isTargetElementTotallyScrolled(targetElement: HTMLElement) {
if (!targetElement) return false;

return (
targetElement.scrollHeight - targetElement.scrollTop <=
targetElement.clientHeight
);
}

private _handleScroll(event: HandleScrollEvent, targetElement: HTMLElement) {
const touchY = event.targetTouches[0]?.clientY ?? 0;
const clientY = touchY - this._initialClientY;

if (this._allowTouchMove(event.target as HTMLElement)) return false;

if (targetElement && targetElement.scrollTop === 0 && clientY > 0) {
// Element is at the top of its scroll.
return this._preventDefault(event);
}

if (this._isTargetElementTotallyScrolled(targetElement) && clientY < 0) {
// Element is at the bottom of its scroll.
return this._preventDefault(event);
}

event.stopPropagation();

return true;
}

public lock(targetElement: HTMLElement, options?: LockOptions): void {
const alreadyLocked = this._locks.some(
lock => lock.targetElement === targetElement,
Expand All @@ -234,57 +108,11 @@ class ScrollLocker {
};

this._locks.push(lock);

if (this._isIosDevice) this._setPositionFixed();
else this._setOverflowHidden(options);

if (this._isIosDevice) {
targetElement.ontouchstart = (event: HandleScrollEvent) => {
if (event.targetTouches.length === 1) {
// Detect single touch.
this._initialClientY = event.targetTouches[0]!.clientY;
}
};

targetElement.ontouchmove = (event: HandleScrollEvent) => {
if (event.targetTouches.length === 1) {
// Detect single touch.
this._handleScroll(event, targetElement);
}
};

if (!this._documentListenerAdded) {
document.addEventListener(
"touchmove",
this._preventDefault,
this._hasPassiveEvents ? { passive: false } : undefined,
);

this._documentListenerAdded = true;
}
}
this._setOverflowHidden(options);
}

public clearLocks(): void {
if (this._isIosDevice) {
// Clear all locks ontouchstart/ontouchmove handlers, and the references.
this._locks.forEach((lock: Lock) => {
lock.targetElement.ontouchstart = null;
lock.targetElement.ontouchmove = null;
});

if (this._documentListenerAdded) {
document.removeEventListener("touchmove", this._preventDefault);

this._documentListenerAdded = false;
}

// Reset initial clientY.
this._initialClientY = -1;
}

if (this._isIosDevice) this._restorePositionSetting();
else this._restoreOverflowSetting();
this._restoreOverflowSetting();

this._locks = [];
}
Expand All @@ -294,19 +122,7 @@ class ScrollLocker {
lock => lock.targetElement !== targetElement,
);

if (this._isIosDevice) {
targetElement.ontouchstart = null;
targetElement.ontouchmove = null;

if (this._documentListenerAdded && this._locks.length === 0) {
document.removeEventListener("touchmove", this._preventDefault);

this._documentListenerAdded = false;
}
}

if (this._isIosDevice) this._restorePositionSetting();
else this._restoreOverflowSetting();
this._restoreOverflowSetting();
}
}

Expand Down