<?php
/**
** sets tab focus to top of .facetwp-template after a refresh
** .facetwp-template can be changed to whatever element you prefer
**/
add_action('facetwp_scripts', function () {
?>
<script>
(function($) {
document.addEventListener('focus', (e) => {
if ( 'undefined' != typeof e.target.classList && true === e.target.classList.contains( 'facetwp-page' ) ) {
FWP.active_facet = $(e.target.closest('.facetwp-facet')); // emulates setting active_facet for pager when using keyboard enter to select
} else {
FWP.active_facet = null;
}
}, true);
FWP.hooks.addAction('facetwp/loaded', function() {
try {
if ( FWP.loaded && null !== FWP.active_facet && 'pager' == FWP.active_facet.attr('data-type') ) {
let target = document.querySelector('.facetwp-template');
if (!target.hasAttribute('tabindex')) {
target.setAttribute('tabindex', '-1');
}
target.focus();
}
}
catch(e) {
// do nothing
}
}, 101 );
})(fUtil);
</script>
<?php
}, 100);