WordPress/wp-content/themes/twentytwentyone/assets/js/dark-mode-toggler.js
desrosj e0669ddaae Bundled Themes: Sync Twenty Twenty-One with the latest changes from GitHub.
For a full list of changes since [49330], see 5759e96...e7d5991.

Props poena, luminuu, ryelle, kjellr, aristath, justinahinon, felipeelia, joostdevalk.
See #51526.
Built from https://develop.svn.wordpress.org/trunk@49478


git-svn-id: http://core.svn.wordpress.org/trunk@49237 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-11-02 19:45:07 +00:00

43 lines
1.2 KiB
JavaScript

function toggleDarkMode() { // jshint ignore:line
var toggler = document.getElementById( 'dark-mode-toggler' ),
html = document.querySelector( 'html' );
if ( 'false' === toggler.getAttribute( 'aria-pressed' ) ) {
toggler.setAttribute( 'aria-pressed', 'true' );
html.classList.add( 'is-dark-mode' );
window.localStorage.setItem( 'twentytwentyoneDarkMode', 'yes' );
} else {
toggler.setAttribute( 'aria-pressed', 'false' );
html.classList.remove( 'is-dark-mode' );
window.localStorage.setItem( 'twentytwentyoneDarkMode', 'no' );
}
}
function darkModeInitialLoad() {
var toggler = document.getElementById( 'dark-mode-toggler' ),
isDarkMode = window.matchMedia( '(prefers-color-scheme: dark)' ).matches,
html;
if ( 'yes' === window.localStorage.getItem( 'twentytwentyoneDarkMode' ) ) {
isDarkMode = true;
} else if ( 'no' === window.localStorage.getItem( 'twentytwentyoneDarkMode' ) ) {
isDarkMode = false;
}
if ( ! toggler ) {
return;
}
if ( isDarkMode ) {
toggler.setAttribute( 'aria-pressed', 'true' );
}
html = document.querySelector( 'html' );
if ( isDarkMode ) {
html.classList.add( 'is-dark-mode' );
} else {
html.classList.remove( 'is-dark-mode' );
}
}
darkModeInitialLoad();