Skip to content

Commit

Permalink
MWPW-135595 Marketo - Remove focus scroll on desktop (#1172)
Browse files Browse the repository at this point in the history
  • Loading branch information
meganthecoder authored Aug 29, 2023
1 parent d5811d5 commit abd1cfc
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions libs/blocks/marketo/marketo.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,17 @@ export const formSuccess = (form) => {

const readyForm = (form) => {
const formEl = form.getFormElem().get(0);
const isDesktop = matchMedia('(min-width: 900px)');

formEl.addEventListener('focus', ({ target }) => {
/* c8 ignore next 9 */
const hasError = formEl.classList.contains('show-warnings');
const firstInvalidField = formEl.querySelector('.mktoRequired[aria-invalid=true]');
if (!['text', 'email', 'tel', 'textarea'].includes(target.type)
|| (isDesktop.matches && !(hasError && target === firstInvalidField))) return;

formEl.addEventListener('focus', (e) => {
/* c8 ignore next 5 */
if (!['text', 'email', 'tel', 'textarea'].includes(e.target.type)) return;
const pageTop = document.querySelector('header')?.offsetHeight ?? 0;
const targetPosition = e.target?.getBoundingClientRect().top ?? 0;
const targetPosition = target?.getBoundingClientRect().top ?? 0;
const offsetPosition = targetPosition + window.pageYOffset - pageTop - window.innerHeight / 2;
window.scrollTo(0, offsetPosition);
}, true);
Expand Down

0 comments on commit abd1cfc

Please sign in to comment.