Skip to content

Commit

Permalink
Adds autoScrollBack behavior to Mobile Web Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
sidferreira committed Nov 23, 2021
1 parent c95c2dd commit e04c6a7
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"react-web-config": "^1.0.0",
"rn-fetch-blob": "^0.12.0",
"save": "^2.4.0",
"smoothscroll-polyfill": "^0.4.4",
"underscore": "^1.13.1",
"urbanairship-react-native": "^11.0.2"
},
Expand Down
1 change: 1 addition & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import OnyxProvider from './components/OnyxProvider';
import HTMLEngineProvider from './components/HTMLEngineProvider';
import ComposeProviders from './components/ComposeProviders';
import SafeArea from './components/SafeArea';
import './libs/autoScrollback';

LogBox.ignoreLogs([
// Basically it means that if the app goes in the background and back to foreground on Android,
Expand Down
Empty file.
88 changes: 88 additions & 0 deletions src/libs/autoScrollback/index.web.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import smoothscroll from 'smoothscroll-polyfill';

import getBrowser from '../getBrowser';

const init = () => {
if (getBrowser() === 'safari') {
const userAgent = navigator.userAgent.toLowerCase();

if (userAgent.indexOf('iphone')) {
let top = 0;
let innerHeightTooSmall = 0;

if (userAgent.indexOf('iphone os 15_') > 0) { // iOS 15
if (window.outerHeight === 812) { // iPhone Pro
top = 280;
innerHeightTooSmall = 350;
} else if (window.outerHeight === 896) {
if (window.devicePixelRatio === 3) { // iPhone Pro Max
top = 290;
innerHeightTooSmall = 425;
} else { // iPhone
top = 245;
innerHeightTooSmall = 470;
}
}
} else if (userAgent.indexOf('iphone os 1') > 0) { // iOS Before 15
if (window.outerHeight === 812) { // iPhone Pro
top = 280;
innerHeightTooSmall = 350;
} else if (window.outerHeight === 896) {
if (window.devicePixelRatio === 3) { // iPhone Pro Max
top = 290;
innerHeightTooSmall = 425;
} else { // iPhone
top = 300;
innerHeightTooSmall = 420;
}
}
}

if (!top || !innerHeightTooSmall) {
return;
}

// const innerHeightTooSmall = 470;
let isTouching = false;
let timeout;
smoothscroll.polyfill();

const scrollBack = () => {
window.requestAnimationFrame(() => {
window.scrollTo({
top,
behavior: 'smooth',
});
});
};

const clearTimeoutIfNeeded = () => {
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}
};

const scheduleScrollback = () => {
clearTimeoutIfNeeded();

if (!isTouching && innerHeightTooSmall > window.innerHeight) {
timeout = setTimeout(scrollBack, 34);
}
};

document.addEventListener('touchstart', () => {
isTouching = true;
});
document.addEventListener('touchend', () => {
isTouching = false;
scheduleScrollback();
});
document.addEventListener('scroll', () => {
scheduleScrollback();
});
}
}
};

init();

0 comments on commit e04c6a7

Please sign in to comment.