Skip to content
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

fix: sidebar scrolling on ios devices #1161

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 39 additions & 7 deletions v1/lib/core/nav/SideNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class SideNav extends React.Component {
dangerouslySetInnerHTML={{
__html: `
document.addEventListener('DOMContentLoaded', function() {
createToggler('#navToggler', '#docsNav', 'docsSliderActive');
createToggler('#tocToggler', 'body', 'tocActive');
createModalToggler('#navToggler', 'docsSliderActive');
createModalToggler('#tocToggler', 'tocActive');

const headings = document.querySelector('.toc-headings');
headings && headings.addEventListener('click', function(event) {
Expand All @@ -144,19 +144,51 @@ class SideNav extends React.Component {
}
}, false);

function createToggler(togglerSelector, targetSelector, className) {
function createModalToggler(togglerSelector, className) {
var toggler = document.querySelector(togglerSelector);
var target = document.querySelector(targetSelector);

if (!toggler) {
return;
}

toggler.onclick = function(event) {
toggler.onclick = isIOS() ? getIOSToggleHandler(className) : getToggleHandler(className);
}

function isIOS() {
var iOSDevices = ['iPad Simulator', 'iPhone Simulator', 'iPod Simulator', 'iPad', 'iPhone', 'iPod'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can only comment from code perspective, but this part can definitely be refactored with something like

iosDevices.indexOf(navigator.platform)

instead of the whole pop and while loop.

This also doesn't completely fix #877. #877 is related to glitchy scrolling on Mac Safari too. I don't have a Mac to test it out, unfortunately

cc @yangshun @JoelMarcey


if (!!navigator.platform) {
while (iOSDevices.length) {
if (navigator.platform === iOSDevices.pop()) {
return true;
}
}
}

return false;
}

function getToggleHandler(className) {
return function(event) {
event.preventDefault();
document.body.classList.toggle(className);
}
}

function getIOSToggleHandler(className) {
var savedScrollY;

target.classList.toggle(className);
};
return function(event) {
event.preventDefault();
var isToggledOnNow = document.body.classList.contains(className);
if (isToggledOnNow) {
document.body.classList.remove(className, 'ios')
window.scrollTo(0, savedScrollY);
} else {
savedScrollY = window.scrollY;
document.body.classList.add(className, 'ios')
}
}
}
});
`,
Expand Down
16 changes: 15 additions & 1 deletion v1/lib/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,21 @@ input::placeholder {
}
}

.docsSliderActive.docsNavContainer {
.tocActive, .docsSliderActive {
overflow: hidden;
}

.tocActive.ios, .docsSliderActive.ios {
position: fixed;
}

.tocActive.ios .mainContainer,
.docsSliderActive.ios .mainContainer {
transform: translate3d(0,0,0);
-webkit-transform: translate3d(0,0,0);
}

.docsSliderActive .docsNavContainer {
box-sizing: border-box;
height: 100%;
-webkit-overflow-scrolling: touch;
Expand Down