davidtheclark / no-scroll

Disable scrolling on an element that would otherwise scroll

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Only allow turning off no-scroll if it is currently turned on

danoc opened this issue · comments

Hello!

It's fairly easy to misuse no-scroll and call .off before it has been turned on. When misused, this causes the page to scroll to the top since scrollTop will be undefined:

no-scroll/index.js

Lines 37 to 46 in fcf54b6

function off() {
if (typeof document === 'undefined') return;
var doc = document.documentElement;
doc.style.width = '';
doc.style.position = '';
doc.style.top = '';
doc.style.overflow = '';
window.scroll(0, scrollTop);
isOn = false;
}

Would it make sense to return immediately on L38 when isOn is false? Alternatively, don't run window.scroll if scrollTop is undefined? (Happy to submit a PR.)

Thanks, @davidtheclark! Your modal libraries are 💯.

Would it make sense to returning immediately on L38 when isOn is false?

This sounds good to me 👍I'll cut a quick release with a fix.

Published in 2.1.1.