Twenty Twenty-One: Prevent Dark Mode related JavaScript error.

This prevents an `Uncaught TypeError` in the block editor when scrolling caused by the absence of a `#dark-mode-toggler` element.

Props ocean90, mukesh27, justinahinon.
Fixes #52473.

git-svn-id: https://develop.svn.wordpress.org/trunk@50269 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonathan Desrosiers
2021-02-09 14:54:48 +00:00
parent 2833031cce
commit 73c1b28ec7
@@ -44,7 +44,9 @@ function darkModeInitialLoad() {
}
function darkModeRepositionTogglerOnScroll() {
var prevScroll = window.scrollY || document.documentElement.scrollTop,
var toggler = document.getElementById( 'dark-mode-toggler' ),
prevScroll = window.scrollY || document.documentElement.scrollTop,
currentScroll,
checkScroll = function() {
@@ -53,13 +55,16 @@ function darkModeRepositionTogglerOnScroll() {
currentScroll + ( window.innerHeight * 1.5 ) > document.body.clientHeight ||
currentScroll < prevScroll
) {
document.getElementById( 'dark-mode-toggler' ).classList.remove( 'hide' );
toggler.classList.remove( 'hide' );
} else if ( currentScroll > prevScroll && 250 < currentScroll ) {
document.getElementById( 'dark-mode-toggler' ).classList.add( 'hide' );
toggler.classList.add( 'hide' );
}
prevScroll = currentScroll;
};
window.addEventListener( 'scroll', checkScroll );
if ( toggler ) {
window.addEventListener( 'scroll', checkScroll );
}
}
darkModeInitialLoad();