2020-11-12 13:48:08 -05:00
|
|
|
function toggleDarkMode() { // jshint ignore:line
|
2020-11-12 13:36:15 -05:00
|
|
|
var toggler = document.getElementById( 'dark-mode-toggler' );
|
2020-11-02 14:45:07 -05:00
|
|
|
|
|
|
|
if ( 'false' === toggler.getAttribute( 'aria-pressed' ) ) {
|
|
|
|
toggler.setAttribute( 'aria-pressed', 'true' );
|
2020-11-12 13:36:15 -05:00
|
|
|
document.documentElement.classList.add( 'is-dark-theme' );
|
|
|
|
document.body.classList.add( 'is-dark-theme' );
|
2020-11-02 14:45:07 -05:00
|
|
|
window.localStorage.setItem( 'twentytwentyoneDarkMode', 'yes' );
|
|
|
|
} else {
|
|
|
|
toggler.setAttribute( 'aria-pressed', 'false' );
|
2020-11-12 13:36:15 -05:00
|
|
|
document.documentElement.classList.remove( 'is-dark-theme' );
|
|
|
|
document.body.classList.remove( 'is-dark-theme' );
|
2020-11-02 14:45:07 -05:00
|
|
|
window.localStorage.setItem( 'twentytwentyoneDarkMode', 'no' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 14:00:08 -05:00
|
|
|
function twentytwentyoneIsDarkMode() {
|
|
|
|
var isDarkMode = window.matchMedia( '(prefers-color-scheme: dark)' ).matches;
|
2020-11-02 14:45:07 -05:00
|
|
|
|
|
|
|
if ( 'yes' === window.localStorage.getItem( 'twentytwentyoneDarkMode' ) ) {
|
|
|
|
isDarkMode = true;
|
|
|
|
} else if ( 'no' === window.localStorage.getItem( 'twentytwentyoneDarkMode' ) ) {
|
|
|
|
isDarkMode = false;
|
|
|
|
}
|
|
|
|
|
2020-11-17 14:00:08 -05:00
|
|
|
return isDarkMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
function darkModeInitialLoad() {
|
|
|
|
var toggler = document.getElementById( 'dark-mode-toggler' ),
|
|
|
|
isDarkMode = twentytwentyoneIsDarkMode();
|
2020-11-02 14:45:07 -05:00
|
|
|
|
|
|
|
if ( isDarkMode ) {
|
2020-11-12 13:36:15 -05:00
|
|
|
document.documentElement.classList.add( 'is-dark-theme' );
|
|
|
|
document.body.classList.add( 'is-dark-theme' );
|
2020-11-02 14:45:07 -05:00
|
|
|
} else {
|
2020-11-12 13:36:15 -05:00
|
|
|
document.documentElement.classList.remove( 'is-dark-theme' );
|
|
|
|
document.body.classList.remove( 'is-dark-theme' );
|
2020-11-02 14:45:07 -05:00
|
|
|
}
|
2020-11-17 14:00:08 -05:00
|
|
|
|
|
|
|
if ( toggler && isDarkMode ) {
|
|
|
|
toggler.setAttribute( 'aria-pressed', 'true' );
|
|
|
|
}
|
2020-11-02 14:45:07 -05:00
|
|
|
}
|
|
|
|
|
2020-11-12 13:36:15 -05:00
|
|
|
function darkModeRepositionTogglerOnScroll() {
|
2021-02-09 09:56:04 -05:00
|
|
|
|
|
|
|
var toggler = document.getElementById( 'dark-mode-toggler' ),
|
|
|
|
prevScroll = window.scrollY || document.documentElement.scrollTop,
|
2020-11-12 13:36:15 -05:00
|
|
|
currentScroll,
|
|
|
|
|
|
|
|
checkScroll = function() {
|
|
|
|
currentScroll = window.scrollY || document.documentElement.scrollTop;
|
|
|
|
if (
|
|
|
|
currentScroll + ( window.innerHeight * 1.5 ) > document.body.clientHeight ||
|
|
|
|
currentScroll < prevScroll
|
|
|
|
) {
|
2021-02-09 09:56:04 -05:00
|
|
|
toggler.classList.remove( 'hide' );
|
2020-11-12 13:36:15 -05:00
|
|
|
} else if ( currentScroll > prevScroll && 250 < currentScroll ) {
|
2021-02-09 09:56:04 -05:00
|
|
|
toggler.classList.add( 'hide' );
|
2020-11-12 13:36:15 -05:00
|
|
|
}
|
|
|
|
prevScroll = currentScroll;
|
|
|
|
};
|
2021-02-09 09:56:04 -05:00
|
|
|
|
|
|
|
if ( toggler ) {
|
|
|
|
window.addEventListener( 'scroll', checkScroll );
|
|
|
|
}
|
2020-11-12 13:36:15 -05:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:45:07 -05:00
|
|
|
darkModeInitialLoad();
|
2020-11-12 13:36:15 -05:00
|
|
|
darkModeRepositionTogglerOnScroll();
|