Skip to content

Commit

Permalink
Use new data-wp-on-window and data-wp-on-document.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixarntz committed Mar 8, 2024
1 parent 50adcfa commit 44e4977
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
37 changes: 13 additions & 24 deletions src/wp-content/themes/twentytwentyone/assets/js/interactivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function checkClass( element, className ) {
const { state, actions } = store( 'twentytwentyone', {
state: {
isPrimaryMenuOpen: false,
windowWidth: 0,
prevScroll: 0,
isDarkMode: false,
isDarkModeTogglerHidden: false,
Expand Down Expand Up @@ -159,14 +158,6 @@ const { state, actions } = store( 'twentytwentyone', {
} );
},

updateWindowWidthOnResize: () => {
// The following may be needed here since we can't use `data-wp-on--resize`?
const refreshWidth = () => {
state.windowWidth = window.innerWidth;
}
window.onresize = refreshWidth;
},

initDarkMode: () => {
let isDarkMode = window.matchMedia( '(prefers-color-scheme: dark)' ).matches;

Expand All @@ -177,21 +168,6 @@ const { state, actions } = store( 'twentytwentyone', {
}

state.isDarkMode = isDarkMode;

// The following may be needed here since we can't use `data-wp-on--scroll`?
const checkScroll = () => {
const currentScroll = window.scrollY || document.documentElement.scrollTop;
if (
currentScroll + ( window.innerHeight * 1.5 ) > document.body.clientHeight ||
currentScroll < state.prevScroll
) {
state.isDarkModeTogglerHidden = false;
} else if ( currentScroll > state.prevScroll && 250 < currentScroll ) {
state.isDarkModeTogglerHidden = true;
}
state.prevScroll = currentScroll;
}
window.addEventListener( 'scroll', checkScroll );
},

refreshHtmlElementDarkMode: () => {
Expand All @@ -202,5 +178,18 @@ const { state, actions } = store( 'twentytwentyone', {
document.documentElement.classList.remove( 'is-dark-theme' );
}
},

refreshDarkModeToggler: () => {
const currentScroll = window.scrollY || document.documentElement.scrollTop;
if (
currentScroll + ( window.innerHeight * 1.5 ) > document.body.clientHeight ||
currentScroll < state.prevScroll
) {
state.isDarkModeTogglerHidden = false;
} else if ( currentScroll > state.prevScroll && 250 < currentScroll ) {
state.isDarkModeTogglerHidden = true;
}
state.prevScroll = currentScroll;
},
},
} );
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,10 @@ public function the_html( $attrs = array() ) {

// Extra attributes depending on whether or not the Interactivity API is being used.
if ( function_exists( 'wp_register_script_module' ) ) {
$defaults['data-wp-on--click'] = 'actions.toggleDarkMode';
$defaults['data-wp-bind--aria-pressed'] = 'state.isDarkMode';
$defaults['data-wp-class--hide'] = 'state.isDarkModeTogglerHidden';
$defaults['data-wp-on--click'] = 'actions.toggleDarkMode';
$defaults['data-wp-bind--aria-pressed'] = 'state.isDarkMode';
$defaults['data-wp-class--hide'] = 'state.isDarkModeTogglerHidden';
$defaults['data-wp-on-document--scroll'] = 'callbacks.refreshDarkModeToggler';
} else {
$defaults['onClick'] = 'toggleDarkMode()';
}
Expand Down
4 changes: 2 additions & 2 deletions src/wp-content/themes/twentytwentyone/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
data-wp-class--primary-navigation-open="state.isPrimaryMenuOpen"
data-wp-class--lock-scrolling="state.isPrimaryMenuOpen"
data-wp-class--is-dark-theme="state.isDarkMode"
data-wp-init--iframes="callbacks.updateWindowWidthOnResize"
data-wp-watch--iframes="callbacks.makeIframesResponsive"
data-wp-init--iframes="callbacks.makeIframesResponsive"
data-wp-on-window--resize="callbacks.makeIframesResponsive"
data-wp-init--darkmode="callbacks.initDarkMode"
data-wp-watch--darkmode="callbacks.refreshHtmlElementDarkMode"
>
Expand Down

0 comments on commit 44e4977

Please sign in to comment.