From 0b1a0a0f12adc6bebc308ca58d128ae3788d370f Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Sun, 4 Aug 2024 15:56:02 -0500 Subject: [PATCH] Auto-scroll content if focused and covered by toolbar --- src/js/a11y.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/js/a11y.js b/src/js/a11y.js index 78988c1..388f4d3 100644 --- a/src/js/a11y.js +++ b/src/js/a11y.js @@ -105,4 +105,24 @@ function eraseCookie(name) { return false; }); + var focusable = document.querySelectorAll('input,a,select,textarea,button'); + focusable.forEach((el) => { + el.addEventListener( 'focus', function() { + var bounds = el.getBoundingClientRect(); + var toolbar = document.querySelector( '.a11y-toolbar' ).getBoundingClientRect(); + console.log( { bounds, toolbar } ); + + var overlap = !( + bounds.top > toolbar.bottom || + bounds.right < toolbar.left || + bounds.bottom < toolbar.top || + bounds.left > toolbar.right + ); + if ( overlap ) { + var diff = bounds.bottom - toolbar.top; + window.scrollBy( 0, diff ); + } + }); + }); + } )( jQuery );