Skip to content

Commit bc36f03

Browse files
authored
FIX: prevents scroll-lock to crash the app (#36246)
In rare cases, especially on mobile, we could end up in a situation where `null` is given to `unlockScroll`, we were given a default arg to the function through: `unlockScroll(element = document.scrollingElement)` but this is working only if the given element is `undefined` and not `null` resulting sometimes in the following error: ```js TypeError: null is not an object (evaluating 'element.classList') ``` Following this error, the whole UI would become unresponsive. Here is a simple example to demonstrate the buggy behavior here: ```js function greet(name = "World") { console.log(`Hello, ${name}!`); } greet(); // Hello, World! ✓ (default applied) greet(undefined); // Hello, World! ✓ (default applied) greet(null); // Hello, null! ✗ (default NOT applied!) ```
1 parent 9a4d5d9 commit bc36f03

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

frontend/discourse/app/lib/scroll-lock.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
lock scroll of an element using overflow:hidden
33
preserve gutter with scroll detection
44
*/
5-
function lockScroll(element = document.scrollingElement) {
5+
function lockScroll(element) {
6+
element ??= document.scrollingElement;
7+
if (!element) {
8+
return;
9+
}
10+
611
let scrollGap = 0;
712

813
//Add scroll gap if using default scrolling element
@@ -15,7 +20,12 @@ function lockScroll(element = document.scrollingElement) {
1520
element.classList.add("scroll-lock");
1621
}
1722

18-
function unlockScroll(element = document.scrollingElement) {
23+
function unlockScroll(element) {
24+
element ??= document.scrollingElement;
25+
if (!element) {
26+
return;
27+
}
28+
1929
element.classList.remove("scroll-lock");
2030
element.style.setProperty("--scroll-gap", null);
2131
}

0 commit comments

Comments
 (0)