From abd1cfc8ca06c9fde3d61aec1ab51d31dfa270aa Mon Sep 17 00:00:00 2001 From: Megan Thomas Date: Tue, 29 Aug 2023 15:06:39 -0700 Subject: [PATCH] MWPW-135595 Marketo - Remove focus scroll on desktop (#1172) --- libs/blocks/marketo/marketo.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/libs/blocks/marketo/marketo.js b/libs/blocks/marketo/marketo.js index fd00a80aa5..f5d5602d73 100644 --- a/libs/blocks/marketo/marketo.js +++ b/libs/blocks/marketo/marketo.js @@ -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);